8 |
use Template; |
use Template; |
9 |
use List::Util qw/first/; |
use List::Util qw/first/; |
10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
use URI::Escape qw/uri_escape_utf8/; |
use Encode; |
12 |
|
|
13 |
=head1 NAME |
=head1 NAME |
14 |
|
|
16 |
|
|
17 |
=head1 VERSION |
=head1 VERSION |
18 |
|
|
19 |
Version 0.03 |
Version 0.07 |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
our $VERSION = '0.03'; |
our $VERSION = '0.07'; |
24 |
|
|
25 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
26 |
|
|
45 |
|
|
46 |
sub new { |
sub new { |
47 |
my $class = shift; |
my $class = shift; |
48 |
my $self = {@_}; |
my $self = {@_}; |
49 |
bless($self, $class); |
bless($self, $class); |
50 |
|
|
51 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
52 |
|
|
53 |
# create Template toolkit instance |
# create Template toolkit instance |
54 |
$self->{'tt'} = Template->new( |
$self->{'tt'} = Template->new( |
55 |
INCLUDE_PATH => $self->{'include_path'}, |
INCLUDE_PATH => $self->{'include_path'}, |
56 |
FILTERS => $self->{'filter'}, |
#FILTERS => $self->{'filters'}, |
57 |
EVAL_PERL => 1, |
EVAL_PERL => 1, |
58 |
); |
); |
59 |
|
|
60 |
$log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'}); |
$log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'}); |
61 |
|
|
62 |
$log->debug("filters defined: ",Dumper($self->{'filter'})); |
$log->debug("filters defined: ",Dumper($self->{'filters'})); |
63 |
|
|
64 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
65 |
} |
} |
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: |
It also has follwing template toolikit filter routies defined: |
145 |
|
|
146 |
filter to return links to search, usage in TT: |
filter to return links to search, usage in TT: |
147 |
|
|
148 |
[% search('FieldToDisplay','FieldToSearch','optional delimiter') %] |
[% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %] |
149 |
|
|
150 |
=cut |
=cut |
151 |
|
|
157 |
|
|
158 |
return sub { |
return sub { |
159 |
|
|
160 |
my ($display,$search,$delimiter) = @_; |
my ($display,$search,$delimiter,$template) = @_; |
161 |
|
|
162 |
# default delimiter |
# default delimiter |
163 |
$delimiter ||= '¶<br/>', |
$delimiter ||= '¶<br/>', |
169 |
my $item = $data->{'data'}->{$display} || return; |
my $item = $data->{'data'}->{$display} || return; |
170 |
|
|
171 |
return unless($item->{'display'}); |
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'}); |
if (! $item->{'search'}) { |
173 |
|
warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; |
174 |
|
return; |
175 |
|
} |
176 |
|
|
177 |
my @warn; |
my @warn; |
178 |
foreach my $type (qw/display search/) { |
foreach my $type (qw/display search/) { |
201 |
$s = $item->{'search'}->[0]; |
$s = $item->{'search'}->[0]; |
202 |
} |
} |
203 |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
204 |
$s = uri_escape_utf8( $s ); |
$s = __quotemeta( $s ); |
205 |
|
|
206 |
my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display"; |
my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display"; |
207 |
|
|
208 |
push @html, <<__JS_LINK_SEARCH__ |
my $template_arg = ''; |
209 |
<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> |
$template_arg = qq{,'$template'} if ($template); |
210 |
__JS_LINK_SEARCH__ |
|
211 |
|
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>}; |
212 |
} |
} |
213 |
|
|
214 |
return join($delimiter, @html); |
return join($delimiter, @html); |
228 |
|
|
229 |
$args->{'search'} = tt_filter_search($args); |
$args->{'search'} = tt_filter_search($args); |
230 |
|
|
231 |
|
=head3 load_rec |
232 |
|
|
233 |
|
Used mostly for onClick events like this: |
234 |
|
|
235 |
|
<a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a> |
236 |
|
|
237 |
|
It will automatically do sanity checking and create correct JavaScript code. |
238 |
|
|
239 |
|
=cut |
240 |
|
|
241 |
|
$args->{'load_rec'} = sub { |
242 |
|
my @errors; |
243 |
|
|
244 |
|
my $record_uri = shift or push @errors, "record_uri missing"; |
245 |
|
my $template = shift or push @errors, "template missing"; |
246 |
|
|
247 |
|
if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) { |
248 |
|
push @errors, "invalid format of record_uri: $record_uri"; |
249 |
|
} |
250 |
|
|
251 |
|
if (@errors) { |
252 |
|
return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;"; |
253 |
|
} else { |
254 |
|
return "load_rec('$record_uri','$template'); return false;"; |
255 |
|
} |
256 |
|
}; |
257 |
|
|
258 |
|
=head3 load_template |
259 |
|
|
260 |
|
Used to re-submit search request and load results in different template |
261 |
|
|
262 |
|
<a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a> |
263 |
|
|
264 |
|
=cut |
265 |
|
|
266 |
|
$args->{'load_template'} = sub { |
267 |
|
my $template = shift or return "Logger.error('load_template missing template name!'); return false;"; |
268 |
|
return "load_template($template); return false;"; |
269 |
|
}; |
270 |
|
|
271 |
|
if ($self->{filters}) { |
272 |
|
$args->{f} = $self->{filters}; |
273 |
|
$log->debug("using f.filters"); |
274 |
|
} |
275 |
|
|
276 |
my $out; |
my $out; |
277 |
|
|
278 |
$self->{'tt'}->process( |
$self->{'tt'}->process( |
319 |
} |
} |
320 |
|
|
321 |
|
|
322 |
|
=head2 __quotemeta |
323 |
|
|
324 |
|
Helper to quote JavaScript-friendly characters |
325 |
|
|
326 |
|
=cut |
327 |
|
|
328 |
|
sub __quotemeta { |
329 |
|
local $_ = shift; |
330 |
|
$_ = decode('iso-8859-2', $_); |
331 |
|
|
332 |
|
s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); |
333 |
|
{ |
334 |
|
use bytes; |
335 |
|
s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; |
336 |
|
} |
337 |
|
|
338 |
|
s/\\x09/\\t/g; |
339 |
|
s/\\x0A/\\n/g; |
340 |
|
s/\\x0D/\\r/g; |
341 |
|
s/"/\\"/g; |
342 |
|
s/\\x5C/\\\\/g; |
343 |
|
|
344 |
|
return $_; |
345 |
|
} |
346 |
|
|
347 |
=head1 AUTHOR |
=head1 AUTHOR |
348 |
|
|
349 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |