8 |
use File::Slurp; |
use File::Slurp; |
9 |
use YAML; |
use YAML; |
10 |
use Search::Estraier; |
use Search::Estraier; |
11 |
|
use Data::Page; |
12 |
|
use Data::Dump qw/dump/; |
13 |
|
|
14 |
|
my $range_around = 5; |
15 |
|
my $entries_per_page = 30; |
16 |
|
|
17 |
print header; |
print header; |
18 |
|
|
21 |
print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>|; |
print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>|; |
22 |
} |
} |
23 |
|
|
24 |
|
sub show_pager { |
25 |
|
my ($pager,$coderef) = @_; |
26 |
|
|
27 |
|
my @show_pages; |
28 |
|
my $after_current = 0; |
29 |
|
|
30 |
|
if ( $pager->current_page <= $range_around + 2 ) { |
31 |
|
@show_pages = ( $pager->first_page .. $pager->current_page ); |
32 |
|
$after_current = $range_around - $pager->current_page; |
33 |
|
} else { |
34 |
|
@show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page ); |
35 |
|
} |
36 |
|
|
37 |
|
if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) { |
38 |
|
push @show_pages, ( $pager->current_page + 1 .. $pager->last_page ); |
39 |
|
} else { |
40 |
|
push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page ); |
41 |
|
} |
42 |
|
|
43 |
|
# dump_yaml( 'show_pages', \@show_pages ); |
44 |
|
|
45 |
|
return '' unless $#show_pages; |
46 |
|
|
47 |
|
my ( $prev, $next ) = ( '<<', '>>' ); |
48 |
|
|
49 |
|
return |
50 |
|
$pager->previous_page ? $coderef->( $pager->previous_page, $prev ) : qq|<span class=skip>$prev</span>| |
51 |
|
, join( ' ', map { |
52 |
|
if ( $_ == $pager->current_page ) { |
53 |
|
qq|<span class=current_page>$_</span>|; |
54 |
|
} elsif ( $_ eq '' ) { |
55 |
|
qq|<span class=skip>...</span>|; |
56 |
|
} else { |
57 |
|
$coderef->( $_ ); |
58 |
|
} |
59 |
|
} @show_pages ) |
60 |
|
, $pager->next_page ? $coderef->( $pager->next_page, $next ) : qq|<span class=skip>$next</span>| |
61 |
|
; |
62 |
|
|
63 |
|
} |
64 |
|
|
65 |
my $path = $ENV{PATH_INFO} || 'ecas'; |
my $path = $ENV{PATH_INFO} || 'ecas'; |
66 |
my $dir = $0; |
my $dir = $0; |
67 |
$dir =~ s{/[^/]+.cgi}{}; |
$dir =~ s{/[^/]+.cgi}{}; |
93 |
), |
), |
94 |
h1( $db->{name} ), |
h1( $db->{name} ), |
95 |
qq|<div id=description>|, $db->{description}, qq|</div>|, |
qq|<div id=description>|, $db->{description}, qq|</div>|, |
96 |
start_form, |
start_form( -action => self_url( query => 0 ) ), |
97 |
radio_group( |
radio_group( |
98 |
-name => 'attr', |
-name => 'attr', |
99 |
-values => [ @attr ], |
-values => [ @attr ], |
100 |
# -linebreak => 0, |
# -linebreak => 0, |
101 |
), |
), |
102 |
textfield( -name => 'search' ), |
textfield( -name => 'search' ), |
103 |
submit |
submit, |
104 |
|
textfield( -name => 'entries_per_page', -default => $entries_per_page ), |
105 |
|
textfield( -name => 'current_page', -default => 1 ), |
106 |
; |
; |
107 |
|
|
108 |
print end_form; |
print end_form; |
116 |
croak_on_error => 1, |
croak_on_error => 1, |
117 |
); |
); |
118 |
|
|
119 |
|
param( 'entries_per_page', $entries_per_page ) unless param('entries_per_page'); # FIXME not needed? |
120 |
|
my $pager = Data::Page->new; |
121 |
|
$pager->total_entries( param('current_page') * param('entries_per_page') ); |
122 |
|
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); |
123 |
|
|
124 |
|
dump_yaml( 'pager', $pager ); |
125 |
|
|
126 |
my $cond = Search::Estraier::Condition->new; |
my $cond = Search::Estraier::Condition->new; |
127 |
$cond->set_phrase( $search ); |
$cond->set_phrase( $search ); |
128 |
|
$cond->set_skip( $pager->skipped ); |
129 |
|
$cond->set_max( $pager->entries_per_page ); |
130 |
my $nres = $node->search( $cond, 0 ); |
my $nres = $node->search( $cond, 0 ); |
131 |
|
$pager->total_entries( $nres->hits ); |
132 |
|
|
133 |
|
|
134 |
|
dump_yaml( 'cond', $cond ); |
135 |
|
|
136 |
if ( ! $nres ) { |
if ( ! $nres ) { |
137 |
my $no_results = "No results for search '%s'"; |
my $no_results = "No results for search '%s'"; |
138 |
printf qq|<div class="error">$no_results</div>|, $search; |
printf qq|<div class="error">$no_results</div>|, $search; |
139 |
} else { |
} else { |
|
my $results = "Got %d results for search '%s'"; |
|
|
printf qq|<div class="message">$results</div>|, $nres->hits, $search; |
|
140 |
|
|
141 |
print qq|<ol>|; |
my $results = "%d results for search '%s' showing results %d - %d on page %d"; |
142 |
|
printf qq|<div class="message">$results</div>|, $nres->hits, $search, $pager->first, $pager->last, $pager->current_page; |
143 |
|
|
144 |
|
my $pager_html = |
145 |
|
join(' ', show_pager( $pager, |
146 |
|
sub { |
147 |
|
my ($page,$label) = @_; |
148 |
|
param( 'current_page', $page ); |
149 |
|
my $url = self_url( -query => 1 ); |
150 |
|
$label = $page unless defined $label; |
151 |
|
qq|<a href="$url">$label</a>|; |
152 |
|
} |
153 |
|
)) |
154 |
|
; |
155 |
|
|
156 |
|
print qq|<div class="pager top">$pager_html</div>| if $pager_html; |
157 |
|
|
158 |
|
my $start = $pager->first; |
159 |
|
print qq|<ol start=$start>|; |
160 |
|
|
161 |
foreach my $i ( 1 .. $nres->doc_num ) { |
foreach my $i ( 1 .. $nres->doc_num ) { |
162 |
my $rdoc = $nres->get_doc( $i - 1 ); |
my $rdoc = $nres->get_doc( $i - 1 ); |
176 |
print qq|</li>\n|; |
print qq|</li>\n|; |
177 |
} |
} |
178 |
print qq|</ol>|; |
print qq|</ol>|; |
179 |
|
|
180 |
|
print qq|<div class="pager bottom">$pager_html</div>| if $pager_html; |
181 |
} |
} |
182 |
print qq|</div>|; |
print qq|</div>|; |
183 |
|
|
184 |
|
dump_yaml( 'pager', $pager ); |
185 |
|
|
186 |
} |
} |
187 |
|
|
188 |
dump_yaml( 'estraier', $estraier ); |
dump_yaml( 'estraier', $estraier ); |