/[webpac2]/Webpacus/lib/Webpacus/Model/WebPAC.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 /Webpacus/lib/Webpacus/Model/WebPAC.pm

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

revision 378 by dpavlin, Sat Jan 21 23:27:16 2006 UTC revision 379 by dpavlin, Sun Jan 22 00:44:32 2006 UTC
# Line 11  use WebPAC::Search::Estraier 0.05; Line 11  use WebPAC::Search::Estraier 0.05;
11  use File::Slurp;  use File::Slurp;
12  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
13  use Encode qw/encode decode from_to/;  use Encode qw/encode decode from_to/;
14    use Template;
15  use Data::Dumper;  use Data::Dumper;
16    
17  =head1 NAME  =head1 NAME
# Line 102  sub new { Line 103  sub new {
103    
104          $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");          $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");
105    
106          $self->{model_record} = $c->comp('Model::Record') or $log->error("can't find Model::Record");          # create Template toolkit instance
107            $self->{'tt'} = Template->new(
108                    INCLUDE_PATH => $template_path,
109                    FILTERS => {
110                            dump_html => sub {
111                                    return unless (@_);
112                                    my $out;
113                                    my $i = 1;
114                                    foreach my $v (@_) {
115                                            $out .= qq{<div id="dump_$i">} .
116                                                    Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) .
117                                                    qq{</div>};
118                                            $i++;
119                                    }
120                                    $out =~ s!<table[^>/]*>!<table class="dump">!gis if ($out);
121                                    return $out;
122                            }
123                    },
124                    EVAL_PERL => 1,
125            );
126    
127          return $self;          return $self;
128    
# Line 208  sub search { Line 228  sub search {
228    
229                  $t = time();                  $t = time();
230    
231                  my $html = '[no output]';                  my $html = $self->apply(
232                            template => $template_filename,
233                  if ($self->{model_record}) {                          data => $ds,
234                          $html = $self->{model_record}->apply(                          record_uri => "${database}/${prefix}/${id}",
235                                  template => $template_filename,                          config => $self->{databases}->{$database},
236                                  data => $ds,                  );
                                 record_uri => "${database}/${prefix}/${id}",  
                                 config => $self->{databases}->{$database},  
                         );  
                 } else {  
                         $log->warn("skipped apply");  
                 }  
237    
238                  $times->{out} += time() - $t;                  $times->{out} += time() - $t;
239    
# Line 279  sub record { Line 293  sub record {
293                  return;                  return;
294          }          }
295    
296          my $html = $self->{model_record}->apply(          my $html = $self->apply(
297                  template => $args->{template},                  template => $args->{template},
298                  data => $ds,                  data => $ds,
299                  record_uri => $args->{record_uri},                  record_uri => $args->{record_uri},
# Line 348  sub load_html { Line 362  sub load_html {
362          return decode($self->{webpac_encoding}, $content);          return decode($self->{webpac_encoding}, $content);
363  }  }
364    
365    
366    =head2 apply
367    
368    Create output from in-memory data structure using Template Toolkit template.
369    
370     my $text = $tt->apply(
371            template => 'text.tt',
372            data => $ds,
373            record_uri => 'database/prefix/mfn',
374     );
375    
376    It also has follwing template toolikit filter routies defined:
377    
378    =cut
379    
380    sub apply {
381            my $self = shift;
382    
383            my $args = {@_};
384    
385            my $log = $self->{log} || die "no log?";
386    
387            foreach my $a (qw/template data/) {
388                    $log->logconfess("need $a") unless ($args->{$a});
389            }
390    
391    =head3 tt_filter_type
392    
393    filter to return values of specified from $ds, usage from TT template is in form
394    C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this:
395    
396      [% d('Title') %]
397      [% d('Author',', ' %]
398    
399    =cut
400    
401            sub tt_filter_type {
402                    my ($data,$type) = @_;
403                    
404                    die "no data?" unless ($data);
405                    $type ||= 'display';
406    
407                    my $default_delimiter = {
408                            'display' => '&#182;<br/>',
409                            'index' => '\n',
410                    };
411    
412                    return sub {
413    
414                            my ($name,$join) = @_;
415    
416                            die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
417                            # Hm? Should we die here?
418                            return unless ($name);
419    
420                            my $item = $data->{'data'}->{$name} || return;
421    
422                            my $v = $item->{$type} || return;
423    
424                            if (ref($v) eq 'ARRAY') {
425                                    if ($#{$v} == 0) {
426                                            $v = $v->[0];
427                                    } else {
428                                            $join = $default_delimiter->{$type} unless defined($join);
429                                            $v = join($join, @{$v});
430                                    }
431                            } else {
432                                    warn("TT filter $type(): field $name values aren't ARRAY, ignoring");
433                            }
434    
435                            return $v;
436                    }
437            }
438    
439            $args->{'d'} = tt_filter_type($args, 'display');
440            $args->{'display'} = tt_filter_type($args, 'display');
441    
442    =head3 tt_filter_search
443    
444    filter to return links to search, usage in TT:
445    
446      [% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %]
447    
448    =cut
449    
450            sub tt_filter_search {
451    
452                    my ($data) = @_;
453    
454                    die "no data?" unless ($data);
455                    
456                    return sub {
457    
458                            my ($display,$search,$delimiter,$template) = @_;
459                            
460                            # default delimiter
461                            $delimiter ||= '&#182;<br/>',
462    
463                            die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
464                            # Hm? Should we die here?
465                            return unless ($display);
466    
467                            my $item = $data->{'data'}->{$display} || return;
468    
469                            return unless($item->{'display'});
470                            if (! $item->{'search'}) {
471                                    warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)";
472                                    return;
473                            }
474    
475                            my @warn;
476                            foreach my $type (qw/display search/) {
477                                    push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY');
478                            }
479    
480                            if (@warn) {
481                                    warn("TT filter search(): " . join(",", @warn) . ", skipping");
482                                    return;
483                            }
484                            my @html;
485    
486                            my $d_el = $#{ $item->{'display'} };
487                            my $s_el = $#{ $item->{'search'} };
488    
489                            # easy, both fields have same number of elements or there is just
490                            # one search and multiple display
491                            if ( $d_el == $s_el || $s_el == 0 ) {
492    
493                                    foreach my $i ( 0 .. $d_el ) {
494    
495                                            my $s;
496                                            if ($s_el > 0) {
497                                                    $s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search";
498                                            } else {
499                                                    $s = $item->{'search'}->[0];
500                                            }
501                                            #$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;
502                                            $s = __quotemeta( $s );
503    
504                                            my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display";
505    
506                                            my $template_arg = '';
507                                            $template_arg = qq{,'$template'} if ($template);
508    
509                                            push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>};
510                                    }
511    
512                                    return join($delimiter, @html);
513                            } else {
514                                    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>};
515                                    my $v = $item->{'display'};
516    
517                                    if ($#{$v} == 0) {
518                                            $html .= $v->[0];
519                                    } else {
520                                            $html .= join($delimiter, @{$v});
521                                    }
522                                    return $html;
523                            }
524                    }
525            }
526    
527            $args->{'search'} = tt_filter_search($args);
528    
529    =head3 load_rec
530    
531    Used mostly for onClick events like this:
532    
533      <a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a>
534    
535    It will automatically do sanity checking and create correct JavaScript code.
536    
537    =cut
538    
539            $args->{'load_rec'} = sub {
540                    my @errors;
541    
542                    my $record_uri = shift or push @errors, "record_uri missing";
543                    my $template = shift or push @errors, "template missing";
544    
545                    if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) {
546                            push @errors, "invalid format of record_uri: $record_uri";
547                    }
548    
549                    if (@errors) {
550                            return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;";
551                    } else {
552                            return "load_rec('$record_uri','$template'); return false;";
553                    }
554            };
555    
556    =head3 load_template
557    
558    Used to re-submit search request and load results in different template
559    
560      <a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a>
561    
562    =cut
563    
564            $args->{'load_template'} = sub {
565                    my $template = shift or return "Logger.error('load_template missing template name!'); return false;";
566                    return "load_template($template); return false;";
567            };
568    
569            my $out;
570    
571            $self->{'tt'}->process(
572                    $args->{'template'},
573                    $args,
574                    \$out
575            ) || $log->error( "apply can't process template: ", $self->{'tt'}->error() );
576    
577            return $out;
578    }
579    
580    
581    =head2 __quotemeta
582    
583    Helper to quote JavaScript-friendly characters
584    
585    =cut
586    
587    sub __quotemeta {
588            local $_ = shift;
589            $_ = decode('iso-8859-2', $_);
590    
591            s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) );
592            {
593                    use bytes;  
594                    s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge;
595            }
596    
597            s/\\x09/\\t/g;
598            s/\\x0A/\\n/g;
599            s/\\x0D/\\r/g;
600            s/"/\\"/g;
601            s/\\x5C/\\\\/g;
602    
603            return $_;
604    }
605    
606    
607    
608  =head1 AUTHOR  =head1 AUTHOR
609    
610  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>

Legend:
Removed from v.378  
changed lines
  Added in v.379

  ViewVC Help
Powered by ViewVC 1.1.26