r1792@llin: dpavlin | 2009-04-23 11:24:02 +0200
[webpac2] / vhost / webpac2.cgi
index 7b18776..4079f3c 100755 (executable)
@@ -13,16 +13,19 @@ use Data::Dump qw/dump/;
 
 my $range_around = 5;
 my $entries_per_page = 30;
+my $debug = param('debug');
 
-print header;
+print header(
+       -charset => 'utf-8',
+);
 
 sub dump_yaml {
        my $name = shift;
-       print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>|;
+       print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>| if $debug;
 }
 
-sub show_pages {
-       my ($pager,$coderef) = @_;
+sub show_pager {
+       my ($pager) = @_;
 
        my @show_pages;
        my $after_current = 0;
@@ -34,23 +37,41 @@ sub show_pages {
                @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
        }
 
-       if ( $pager->current_page + $range_around + 1 >= $pager->last_page ) {
+       if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
                push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
        } else {
                push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
        }
 
-       warn "## show_pages = ",dump( @show_pages );
+#      dump_yaml( 'show_pages', \@show_pages );
 
-       return join( ' ', map {
-               if ( $_ == $pager->current_page ) {
-                       qq|<b>$_</b>|;
-               } elsif ( $_ eq '' ) {
-                       qq|...|;
-               } else {
-                       $coderef->( $_ );
-               }
-       } @show_pages );
+       return '' unless $#show_pages;
+
+       my ( $prev, $next ) = ( '&lt;&lt;', '&gt;&gt;' );
+
+       sub li_a_href {
+               my ( $page, $label, $attr ) = @_;
+               param( 'current_page', $page );
+               my $url = self_url( -query => 1 );
+               $attr ||= '';
+               $label ||= $page;
+               qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
+       }
+
+       return
+                 $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
+               , ( map {
+                       if ( $_ eq $pager->current_page ) {
+                               qq|<li class=current_page>$_</li>|;
+                       } elsif ( $_ eq '' ) {
+                               qq|<li class=skip>...</li>|;
+                       } else {
+                               li_a_href( $_ );
+                       }
+               } @show_pages )
+               , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
+               ;
+               
 }
 
 my $path = $ENV{PATH_INFO} || 'ecas';
@@ -84,16 +105,24 @@ print
        ),
        h1( $db->{name} ),
        qq|<div id=description>|, $db->{description}, qq|</div>|,
-       start_form,
+       start_form( -action => self_url( query => 0 ) ),
                radio_group(
                        -name => 'attr',
                        -values => [ @attr ],
 #                      -linebreak => 0,
                ),
                textfield( -name => 'search' ),
+               popup_menu( -name => 'attr_operator', -values => [ '', 'STRBW', 'STREQ' ],
+                       -labels => {
+                               '' => 'Bilo koja riječ',
+                               'STRBW' => 'Početak',
+                               'STREQ' => 'Točan oblik',
+                       },
+               ),
                submit,
-               textfield( -name => 'entries_per_page', -default => $entries_per_page ),
-               textfield( -name => 'current_page', -default => 1 ),
+               hidden( -name => 'entries_per_page', -default => $entries_per_page ),
+               hidden( -name => 'current_page', -default => 1 ),
+               checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
 ;
 
 print  end_form;
@@ -108,38 +137,41 @@ if ( my $search = param('search') ) {
        );
 
        param( 'entries_per_page', $entries_per_page ) unless param('entries_per_page'); # FIXME not needed?
+       my $pager = Data::Page->new;
+       $pager->total_entries( param('current_page') * param('entries_per_page') );
+       $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
+
+       dump_yaml( 'pager', $pager );
 
-       my $cond = Search::Estraier::Condition->new;
+       my $cond = Search::Estraier::Condition->new( debug => $debug );
        $cond->set_phrase( $search );
-       $cond->set_skip( param('current_page') );
-       $cond->set_max( param('entries_per_page') );
+       $cond->set_skip( $pager->skipped );
+       $cond->set_max(  $pager->entries_per_page );
+
+       if ( my $op = param('attr_operator') ) {
+               $cond->add_attr( param('attr') . " $op " . param('search') );
+       }
+
        my $nres = $node->search( $cond, 0 );
+       $pager->total_entries( $nres->hits );
 
-       my $pager = Data::Page->new( $nres->hits, param('entries_per_page'), param('current_page') );
+       dump_yaml( 'cond', $cond );
+       dump_yaml( 'nres', $nres );
 
        if ( ! $nres ) {
                my $no_results = "No results for search '%s'";
-               printf qq|<div class="error">$no_results</div>|, $search;
+               printf qq|<div class="error">$no_results</div>\n\n|, $search;
        } else {
 
-               my $results = "Got %d results for search '%s'";
-               printf qq|<div class="message">$results</div>|, $nres->hits, $search;
-
-               print
-                       qq|<div class=pager>|,
-                       show_pages( $pager,
-                               sub {
-                                       my ($page) = @_;
-                                       param( 'current_page', $page );
-                                       my $url = self_url( -query => 1 );
-                                       qq|<a href="$url">$_</a>|;
-                               }
-                       ),
-                       qq|</div>|
-               ;
+               my $results = "%d results for search '%s' showing results %d - %d on page %d";
+               printf qq|<div class="message">$results</div>\n\n|, $nres->hits, $search, $pager->first, $pager->last, $pager->current_page;
+
+               my $pager_html = join("\n", show_pager( $pager ));
+
+               print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
 
                my $start = $pager->first;
-               print qq|<ol start=$start>|;
+               print qq|<ol start=$start>\n|;
 
                foreach my $i ( 1 .. $nres->doc_num ) {
                        my $rdoc = $nres->get_doc( $i - 1 );
@@ -158,7 +190,9 @@ if ( my $search = param('search') ) {
                        }
                        print qq|</li>\n|;
                }
-               print qq|</ol>|;
+               print qq|</ol>\n\n|;
+
+               print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
        }
        print qq|</div>|;