WebPAC::Output::TT 0.02, added search TT filter (which should probably post
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 30 Nov 2005 23:15:28 +0000 (23:15 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 30 Nov 2005 23:15:28 +0000 (23:15 +0000)
data and not get it!)

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@199 07558da8-63fa-0310-ba24-9fe276d99e06

Makefile.PL
lib/WebPAC/Output/TT.pm

index 5328462..b61a01c 100644 (file)
@@ -23,6 +23,7 @@ WriteMakefile(
        'Time::HiRes' => 0,
        'File::Temp' => 0,
        'List::Util' => 0,
+       'URI::Escape' => 0,
     },
     dist                => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
     clean               => { FILES => 'WebPAC-* pod2html Makefile tags' },
index d9bd3a1..fa97769 100644 (file)
@@ -8,6 +8,7 @@ use base qw/WebPAC::Common/;
 use Template;
 use List::Util qw/first/;
 use Data::Dumper;
+use URI::Escape;
 
 =head1 NAME
 
@@ -15,11 +16,11 @@ WebPAC::Output::TT - use Template Toolkit to produce output
 
 =head1 VERSION
 
-Version 0.01
+Version 0.02
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 =head1 SYNOPSIS
 
@@ -90,7 +91,11 @@ sub apply {
 
 =head3 tt_filter_type
 
-filter to return values of specified from $ds
+filter to return values of specified from $ds, usage from TT template is in form
+C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this:
+
+  [% d('Title') %]
+  [% d('Author',', ' %]
 
 =cut
 
@@ -124,6 +129,8 @@ filter to return values of specified from $ds
                                        $join = $default_delimiter->{$type} unless defined($join);
                                        $v = join($join, @{$v});
                                }
+                       } else {
+                               warn("TT filter $type(): field $name values aren't ARRAY, ignoring");
                        }
 
                        return $v;
@@ -131,6 +138,89 @@ filter to return values of specified from $ds
        }
 
        $args->{'d'} = tt_filter_type($args, 'display');
+       $args->{'display'} = tt_filter_type($args, 'display');
+
+=head3 tt_filter_search
+
+filter to return links to search, usage in TT:
+
+  [% search('FieldToDisplay','FieldToSearch','optional delimiter') %]
+
+=cut
+
+       sub tt_filter_search {
+
+               my ($data) = @_;
+
+               die "no data?" unless ($data);
+               
+               return sub {
+
+                       my ($display,$search,$delimiter) = @_;
+                       
+                       # default delimiter
+                       $delimiter ||= '&#182;<br/>',
+
+                       die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
+                       # Hm? Should we die here?
+                       return unless ($display);
+
+                       my $item = $data->{'data'}->{$display} || return;
+
+                       return unless($item->{'display'});
+                       die "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)" unless($item->{'search'});
+
+                       my @warn;
+                       foreach my $type (qw/display search/) {
+                               push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY');
+                       }
+
+                       if (@warn) {
+                               warn("TT filter search(): " . join(",", @warn) . ", skipping");
+                               return;
+                       }
+                       my @html;
+
+                       my $d_el = $#{ $item->{'display'} };
+                       my $s_el = $#{ $item->{'search'} }; 
+
+                       # easy, both fields have same number of elements or there is just
+                       # one search and multiple display
+                       if ( $d_el == $s_el || $s_el == 0 ) {
+
+                               foreach my $i ( 0 .. $d_el ) {
+
+                                       my $s;
+                                       if ($s_el > 0) {
+                                               $s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search";
+                                       } else {
+                                               $s = $item->{'search'}->[0];
+                                       }
+                                       $s = uri_escape( $s );
+
+                                       my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display";
+
+                                       push @html, <<__JS_LINK_SEARCH__
+<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>
+__JS_LINK_SEARCH__
+                               }
+
+                               return join($delimiter, @html);
+                       } else {
+                               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>};
+                               my $v = $item->{'display'};
+
+                               if ($#{$v} == 0) {
+                                       $html .= $v->[0];
+                               } else {
+                                       $html .= join($delimiter, @{$v});
+                               }
+                               return $html;
+                       }
+               }
+       }
+
+       $args->{'search'} = tt_filter_search($args);
 
        my $out;