/[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 70 by dpavlin, Sat Nov 19 23:48:24 2005 UTC revision 199 by dpavlin, Wed Nov 30 23:15:28 2005 UTC
# Line 8  use base qw/WebPAC::Common/; Line 8  use base qw/WebPAC::Common/;
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;
12    
13  =head1 NAME  =head1 NAME
14    
# Line 15  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.02
20    
21  =cut  =cut
22    
23  our $VERSION = '0.01';  our $VERSION = '0.02';
24    
25  =head1 SYNOPSIS  =head1 SYNOPSIS
26    
# Line 90  sub apply { Line 91  sub apply {
91    
92  =head3 tt_filter_type  =head3 tt_filter_type
93    
94  filter to return values of specified from $ds  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  =cut
101    
# Line 124  filter to return values of specified fro Line 129  filter to return values of specified fro
129                                          $join = $default_delimiter->{$type} unless defined($join);                                          $join = $default_delimiter->{$type} unless defined($join);
130                                          $v = join($join, @{$v});                                          $v = join($join, @{$v});
131                                  }                                  }
132                            } else {
133                                    warn("TT filter $type(): field $name values aren't ARRAY, ignoring");
134                          }                          }
135    
136                          return $v;                          return $v;
# Line 131  filter to return values of specified fro Line 138  filter to return values of specified fro
138          }          }
139    
140          $args->{'d'} = tt_filter_type($args, 'display');          $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 = 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    

Legend:
Removed from v.70  
changed lines
  Added in v.199

  ViewVC Help
Powered by ViewVC 1.1.26