/
component.tag
133 lines (114 loc) · 3.06 KB
/
component.tag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2002-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
UserTag component Order component
UserTag component addAttr
UserTag component NoReparse 1
UserTag component Version 1.10
UserTag component Routine <<EOR
sub {
my ($name, $opt) = @_;
my %ignore = (
qw/
component 1
comp_table 1
comp_field 1
comp_cache 1
reparse 1
interpolate 1
/
);
my @override = grep ! $ignore{$_}, keys %$opt;
my $control = $::Control->[$::Scratch->{control_index}];
for(grep $_ !~ /^comp(?:onent)?_?/, keys %$opt) {
$control->{$_} = $opt->{$_};
}
$name ||= $control->{component};
$name ||= $opt->{default};
if (! $name or $name eq 'none') {
# Increment control_index so empty component has no side effect
$::Scratch->{control_index}++;
return;
}
my $t = $opt->{comp_table} || $::Variable->{MV_COMPONENT_TABLE} || 'component';
my $ctab = $::Variable->{MV_COMPONENT_CACHE} || 'component_cache';
my $record;
my $db = database_exists_ref($t);
my $nocache;
if($db) {
if(my $when = $Vend::Session->{teleport}) {
$nocache = 1;
my $q = qq{
SELECT code from $t
WHERE base_code = '$name'
AND expiration_date < $when
AND show_date >= $when
ORDER BY show_date DESC
};
my $ary = $db->query($q);
if($ary and $ary->[0]) {
$name = $ary->[0][0];
}
}
$record = $db->row_hash($name);
}
$record ||= $opt;
my $body = $record->{comptext};
if(! length($body)) {
my $dir = $opt->{comp_dir}
|| $::Variable->{MV_COMPONENT_DIR}
|| 'templates/components';
$body = readfile("$dir/$name",undef,1);
}
# Increment control_index so empty component has no side effect
if (! length $body) {
$::Scratch->{control_index}++;
return;
}
my $cache_it;
my $cdb;
my $now;
my $crecord;
if (
! $nocache
and $record->{cache_interval}
and $cdb = database_exists_ref($ctab)
)
{
$cache_it = $name;
# Cache based not only on name, but control values specified
if($record->{cache_options}) {
my @opts = split /[\s,\0]+/, $record->{cache_options};
$cache_it .= '.';
$cache_it .= generate_key( join "\0", @{$control}{@opts});
}
$crecord = $cdb->row_hash($cache_it) || {};
$now = time;
my $exp = adjust_time($record->{cache_interval}, $crecord->{cache_time});
if ($exp > $now) {
# Increment control_index as not done below
$::Scratch->{control_index}++;
return $crecord->{compcache};
}
}
my $result = interpolate_html($body);
$::Scratch->{control_index}++;
if($cache_it) {
my $thing = {
compcache => $result,
cache_time => $now,
};
$cdb->set_slice($cache_it, $thing);
}
if($record->{output}) {
Vend::Interpolate::substitute_image(\$result)
unless $opt->{no_image_substitute};
$Tag->output_to($record->{output}, undef, $result);
return;
}
return $result;
}
EOR