6 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common/; |
7 |
|
|
8 |
use Template; |
use Template; |
9 |
|
use List::Util qw/first/; |
10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
|
use URI::Escape qw/uri_escape_utf8/; |
12 |
|
|
13 |
=head1 NAME |
=head1 NAME |
14 |
|
|
16 |
|
|
17 |
=head1 VERSION |
=head1 VERSION |
18 |
|
|
19 |
Version 0.01 |
Version 0.03 |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
our $VERSION = '0.01'; |
our $VERSION = '0.03'; |
24 |
|
|
25 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
26 |
|
|
71 |
|
|
72 |
my $text = $tt->apply( |
my $text = $tt->apply( |
73 |
template => 'text.tt', |
template => 'text.tt', |
74 |
data => @ds |
data => $ds, |
75 |
|
record_uri => 'database/prefix/mfn', |
76 |
); |
); |
77 |
|
|
78 |
|
It also has follwing template toolikit filter routies defined: |
79 |
|
|
80 |
=cut |
=cut |
81 |
|
|
82 |
sub apply { |
sub apply { |
90 |
$log->logconfess("need $a") unless ($args->{$a}); |
$log->logconfess("need $a") unless ($args->{$a}); |
91 |
} |
} |
92 |
|
|
93 |
|
=head3 tt_filter_type |
94 |
|
|
95 |
|
filter to return values of specified from $ds, usage from TT template is in form |
96 |
|
C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this: |
97 |
|
|
98 |
|
[% d('Title') %] |
99 |
|
[% d('Author',', ' %] |
100 |
|
|
101 |
|
=cut |
102 |
|
|
103 |
|
sub tt_filter_type { |
104 |
|
my ($data,$type) = @_; |
105 |
|
|
106 |
|
die "no data?" unless ($data); |
107 |
|
$type ||= 'display'; |
108 |
|
|
109 |
|
my $default_delimiter = { |
110 |
|
'display' => '¶<br/>', |
111 |
|
'index' => '\n', |
112 |
|
}; |
113 |
|
|
114 |
|
return sub { |
115 |
|
|
116 |
|
my ($name,$join) = @_; |
117 |
|
|
118 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
119 |
|
# Hm? Should we die here? |
120 |
|
return unless ($name); |
121 |
|
|
122 |
|
my $item = $data->{'data'}->{$name} || return; |
123 |
|
|
124 |
|
my $v = $item->{$type} || return; |
125 |
|
|
126 |
|
if (ref($v) eq 'ARRAY') { |
127 |
|
if ($#{$v} == 0) { |
128 |
|
$v = $v->[0]; |
129 |
|
} else { |
130 |
|
$join = $default_delimiter->{$type} unless defined($join); |
131 |
|
$v = join($join, @{$v}); |
132 |
|
} |
133 |
|
} else { |
134 |
|
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
135 |
|
} |
136 |
|
|
137 |
|
return $v; |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
|
$args->{'d'} = tt_filter_type($args, 'display'); |
142 |
|
$args->{'display'} = tt_filter_type($args, 'display'); |
143 |
|
|
144 |
|
=head3 tt_filter_search |
145 |
|
|
146 |
|
filter to return links to search, usage in TT: |
147 |
|
|
148 |
|
[% search('FieldToDisplay','FieldToSearch','optional delimiter') %] |
149 |
|
|
150 |
|
=cut |
151 |
|
|
152 |
|
sub tt_filter_search { |
153 |
|
|
154 |
|
my ($data) = @_; |
155 |
|
|
156 |
|
die "no data?" unless ($data); |
157 |
|
|
158 |
|
return sub { |
159 |
|
|
160 |
|
my ($display,$search,$delimiter) = @_; |
161 |
|
|
162 |
|
# default delimiter |
163 |
|
$delimiter ||= '¶<br/>', |
164 |
|
|
165 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
166 |
|
# Hm? Should we die here? |
167 |
|
return unless ($display); |
168 |
|
|
169 |
|
my $item = $data->{'data'}->{$display} || return; |
170 |
|
|
171 |
|
return unless($item->{'display'}); |
172 |
|
die "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)" unless($item->{'search'}); |
173 |
|
|
174 |
|
my @warn; |
175 |
|
foreach my $type (qw/display search/) { |
176 |
|
push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY'); |
177 |
|
} |
178 |
|
|
179 |
|
if (@warn) { |
180 |
|
warn("TT filter search(): " . join(",", @warn) . ", skipping"); |
181 |
|
return; |
182 |
|
} |
183 |
|
my @html; |
184 |
|
|
185 |
|
my $d_el = $#{ $item->{'display'} }; |
186 |
|
my $s_el = $#{ $item->{'search'} }; |
187 |
|
|
188 |
|
# easy, both fields have same number of elements or there is just |
189 |
|
# one search and multiple display |
190 |
|
if ( $d_el == $s_el || $s_el == 0 ) { |
191 |
|
|
192 |
|
foreach my $i ( 0 .. $d_el ) { |
193 |
|
|
194 |
|
my $s; |
195 |
|
if ($s_el > 0) { |
196 |
|
$s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search"; |
197 |
|
} else { |
198 |
|
$s = $item->{'search'}->[0]; |
199 |
|
} |
200 |
|
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
201 |
|
$s = uri_escape_utf8( $s ); |
202 |
|
|
203 |
|
my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display"; |
204 |
|
|
205 |
|
push @html, <<__JS_LINK_SEARCH__ |
206 |
|
<a href="#" onclick="new Ajax.Updater( 'results', '/search/results?$search=$s', { asynchronous: 1,onLoading: function(request){show_searching();},onLoaded: function(request){hide_searching();} } ) ; return false">$d</a> |
207 |
|
__JS_LINK_SEARCH__ |
208 |
|
} |
209 |
|
|
210 |
|
return join($delimiter, @html); |
211 |
|
} else { |
212 |
|
my $html = qq{<div class="notice">WARNING: we should really support if there is $d_el display elements and $s_el search elements, but currently there is no nice way to do so, so we will just display values</div>}; |
213 |
|
my $v = $item->{'display'}; |
214 |
|
|
215 |
|
if ($#{$v} == 0) { |
216 |
|
$html .= $v->[0]; |
217 |
|
} else { |
218 |
|
$html .= join($delimiter, @{$v}); |
219 |
|
} |
220 |
|
return $html; |
221 |
|
} |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
|
$args->{'search'} = tt_filter_search($args); |
226 |
|
|
227 |
|
=head3 load_rec |
228 |
|
|
229 |
|
Used mostly for onClick events like this: |
230 |
|
|
231 |
|
<a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a> |
232 |
|
|
233 |
|
It will automatically do sanity checking and create correct JavaScript code. |
234 |
|
|
235 |
|
=cut |
236 |
|
|
237 |
|
$args->{'load_rec'} = sub { |
238 |
|
my @errors; |
239 |
|
|
240 |
|
my $record_uri = shift or push @errors, "record_uri missing"; |
241 |
|
my $template = shift or push @errors, "template missing"; |
242 |
|
|
243 |
|
if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) { |
244 |
|
push @errors, "invalid format of record_uri: $record_uri"; |
245 |
|
} |
246 |
|
|
247 |
|
if (@errors) { |
248 |
|
return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;"; |
249 |
|
} else { |
250 |
|
return "load_rec('$record_uri','$template'); return false;"; |
251 |
|
} |
252 |
|
}; |
253 |
|
|
254 |
|
=head3 load_template |
255 |
|
|
256 |
|
Used to re-submit search request and load results in different template |
257 |
|
|
258 |
|
<a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a> |
259 |
|
|
260 |
|
=cut |
261 |
|
|
262 |
|
$args->{'load_template'} = sub { |
263 |
|
my $template = shift or return "Logger.error('load_template missing template name!'); return false;"; |
264 |
|
return "load_template($template); return false;"; |
265 |
|
} |
266 |
|
|
267 |
my $out; |
my $out; |
268 |
|
|
269 |
$self->{'tt'}->process( |
$self->{'tt'}->process( |
283 |
$tt->to_file( |
$tt->to_file( |
284 |
file => 'out.txt', |
file => 'out.txt', |
285 |
template => 'text.tt', |
template => 'text.tt', |
286 |
data => @ds |
data => $ds |
287 |
); |
); |
288 |
|
|
289 |
=cut |
=cut |