/[webpac2]/trunk/lib/WebPAC/Output/TT.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Output/TT.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 21 by dpavlin, Sun Jul 17 22:28:11 2005 UTC revision 201 by dpavlin, Thu Dec 1 13:58:04 2005 UTC
# Line 6  use strict; Line 6  use strict;
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    
# Line 14  WebPAC::Output::TT - use Template Toolki Line 16  WebPAC::Output::TT - use Template Toolki
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    
# Line 69  Create output from in-memory data struct Line 71  Create output from in-memory data struct
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 {
# Line 85  sub apply { Line 89  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' => '&#182;<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 ||= '&#182;<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 =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;
200                                            $s = uri_escape_utf8( $s );
201    
202                                            my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display";
203    
204                                            push @html, <<__JS_LINK_SEARCH__
205    <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>
206    __JS_LINK_SEARCH__
207                                    }
208    
209                                    return join($delimiter, @html);
210                            } else {
211                                    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>};
212                                    my $v = $item->{'display'};
213    
214                                    if ($#{$v} == 0) {
215                                            $html .= $v->[0];
216                                    } else {
217                                            $html .= join($delimiter, @{$v});
218                                    }
219                                    return $html;
220                            }
221                    }
222            }
223    
224            $args->{'search'} = tt_filter_search($args);
225    
226          my $out;          my $out;
227    
228          $self->{'tt'}->process(          $self->{'tt'}->process(
# Line 104  to a file. Line 242  to a file.
242   $tt->to_file(   $tt->to_file(
243          file => 'out.txt',          file => 'out.txt',
244          template => 'text.tt',          template => 'text.tt',
245          data => @ds          data => $ds
246   );   );
247    
248  =cut  =cut

Legend:
Removed from v.21  
changed lines
  Added in v.201

  ViewVC Help
Powered by ViewVC 1.1.26