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; |
12 |
|
|
13 |
=head1 NAME |
=head1 NAME |
14 |
|
|
16 |
|
|
17 |
=head1 VERSION |
=head1 VERSION |
18 |
|
|
19 |
Version 0.01 |
Version 0.02 |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
our $VERSION = '0.01'; |
our $VERSION = '0.02'; |
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 |
); |
); |
76 |
|
|
77 |
|
It also has follwing template toolikit filter routies defined: |
78 |
|
|
79 |
=cut |
=cut |
80 |
|
|
81 |
sub apply { |
sub apply { |
89 |
$log->logconfess("need $a") unless ($args->{$a}); |
$log->logconfess("need $a") unless ($args->{$a}); |
90 |
} |
} |
91 |
|
|
92 |
|
=head3 tt_filter_type |
93 |
|
|
94 |
|
filter to return values of specified from $ds, usage from TT template is in form |
95 |
|
C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this: |
96 |
|
|
97 |
|
[% d('Title') %] |
98 |
|
[% d('Author',', ' %] |
99 |
|
|
100 |
|
=cut |
101 |
|
|
102 |
|
sub tt_filter_type { |
103 |
|
my ($data,$type) = @_; |
104 |
|
|
105 |
|
die "no data?" unless ($data); |
106 |
|
$type ||= 'display'; |
107 |
|
|
108 |
|
my $default_delimiter = { |
109 |
|
'display' => '¶<br/>', |
110 |
|
'index' => '\n', |
111 |
|
}; |
112 |
|
|
113 |
|
return sub { |
114 |
|
|
115 |
|
my ($name,$join) = @_; |
116 |
|
|
117 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
118 |
|
# Hm? Should we die here? |
119 |
|
return unless ($name); |
120 |
|
|
121 |
|
my $item = $data->{'data'}->{$name} || return; |
122 |
|
|
123 |
|
my $v = $item->{$type} || return; |
124 |
|
|
125 |
|
if (ref($v) eq 'ARRAY') { |
126 |
|
if ($#{$v} == 0) { |
127 |
|
$v = $v->[0]; |
128 |
|
} else { |
129 |
|
$join = $default_delimiter->{$type} unless defined($join); |
130 |
|
$v = join($join, @{$v}); |
131 |
|
} |
132 |
|
} else { |
133 |
|
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
134 |
|
} |
135 |
|
|
136 |
|
return $v; |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
|
$args->{'d'} = tt_filter_type($args, 'display'); |
141 |
|
$args->{'display'} = tt_filter_type($args, 'display'); |
142 |
|
|
143 |
|
=head3 tt_filter_search |
144 |
|
|
145 |
|
filter to return links to search, usage in TT: |
146 |
|
|
147 |
|
[% search('FieldToDisplay','FieldToSearch','optional delimiter') %] |
148 |
|
|
149 |
|
=cut |
150 |
|
|
151 |
|
sub tt_filter_search { |
152 |
|
|
153 |
|
my ($data) = @_; |
154 |
|
|
155 |
|
die "no data?" unless ($data); |
156 |
|
|
157 |
|
return sub { |
158 |
|
|
159 |
|
my ($display,$search,$delimiter) = @_; |
160 |
|
|
161 |
|
# default delimiter |
162 |
|
$delimiter ||= '¶<br/>', |
163 |
|
|
164 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
165 |
|
# Hm? Should we die here? |
166 |
|
return unless ($display); |
167 |
|
|
168 |
|
my $item = $data->{'data'}->{$display} || return; |
169 |
|
|
170 |
|
return unless($item->{'display'}); |
171 |
|
die "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)" unless($item->{'search'}); |
172 |
|
|
173 |
|
my @warn; |
174 |
|
foreach my $type (qw/display search/) { |
175 |
|
push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY'); |
176 |
|
} |
177 |
|
|
178 |
|
if (@warn) { |
179 |
|
warn("TT filter search(): " . join(",", @warn) . ", skipping"); |
180 |
|
return; |
181 |
|
} |
182 |
|
my @html; |
183 |
|
|
184 |
|
my $d_el = $#{ $item->{'display'} }; |
185 |
|
my $s_el = $#{ $item->{'search'} }; |
186 |
|
|
187 |
|
# easy, both fields have same number of elements or there is just |
188 |
|
# one search and multiple display |
189 |
|
if ( $d_el == $s_el || $s_el == 0 ) { |
190 |
|
|
191 |
|
foreach my $i ( 0 .. $d_el ) { |
192 |
|
|
193 |
|
my $s; |
194 |
|
if ($s_el > 0) { |
195 |
|
$s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search"; |
196 |
|
} else { |
197 |
|
$s = $item->{'search'}->[0]; |
198 |
|
} |
199 |
|
$s = uri_escape( $s ); |
200 |
|
|
201 |
|
my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display"; |
202 |
|
|
203 |
|
push @html, <<__JS_LINK_SEARCH__ |
204 |
|
<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> |
205 |
|
__JS_LINK_SEARCH__ |
206 |
|
} |
207 |
|
|
208 |
|
return join($delimiter, @html); |
209 |
|
} else { |
210 |
|
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>}; |
211 |
|
my $v = $item->{'display'}; |
212 |
|
|
213 |
|
if ($#{$v} == 0) { |
214 |
|
$html .= $v->[0]; |
215 |
|
} else { |
216 |
|
$html .= join($delimiter, @{$v}); |
217 |
|
} |
218 |
|
return $html; |
219 |
|
} |
220 |
|
} |
221 |
|
} |
222 |
|
|
223 |
|
$args->{'search'} = tt_filter_search($args); |
224 |
|
|
225 |
my $out; |
my $out; |
226 |
|
|
227 |
$self->{'tt'}->process( |
$self->{'tt'}->process( |
241 |
$tt->to_file( |
$tt->to_file( |
242 |
file => 'out.txt', |
file => 'out.txt', |
243 |
template => 'text.tt', |
template => 'text.tt', |
244 |
data => @ds |
data => $ds |
245 |
); |
); |
246 |
|
|
247 |
=cut |
=cut |