4 # Copyright (C) 2004-2006 Frank Lichtenheld <frank@lichtenheld.de>
6 # The code is based on the old search_packages.pl script that
9 # Copyright (C) 1998 James Treacy
10 # Copyright (C) 2000, 2001 Josip Rodin
11 # Copyright (C) 2001 Adam Heath
12 # Copyright (C) 2004 Martin Schulze
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 1 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 package Packages::Search;
46 use CGI qw( -oldstyle_urls );
53 our @ISA = qw( Exporter );
55 our @EXPORT_OK = qw( nextlink prevlink indexline
57 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
61 our $USE_PAGED_MODE = 1;
62 use constant DEFAULT_PAGE => 1;
63 use constant DEFAULT_RES_PER_PAGE => 50;
64 our %page_params = ( page => { default => DEFAULT_PAGE,
66 number => { default => DEFAULT_RES_PER_PAGE,
72 my ( $cgi, $params_def, $opts ) = @_;
74 my %params_ret = ( values => {}, errors => {} );
76 if ($USE_PAGED_MODE) {
77 print "DEBUG: Use PAGED_MODE<br>" if $debug;
78 %params = %$params_def;
79 foreach (keys %page_params) {
82 %params = ( %params, %page_params );
84 %params = %$params_def;
87 foreach my $param ( keys %params ) {
89 print "<hr><p>DEBUG: Param <strong>$param</strong><br>" if $debug;
91 my $p_value_orig = $cgi->param($param);
93 if (!defined($p_value_orig)
94 && defined $params_def->{$param}{alias}
95 && defined $cgi->param($params_def->{$param}{alias})) {
96 $p_value_orig = $cgi->param($params_def->{$param}{alias});
97 print "DEBUG: Used alias <strong>$params_def->{$param}{alias}</strong><br>"
101 my @p_value = ($p_value_orig);
103 print "DEBUG: Value (Orig) ".($p_value_orig||"")."<br>" if $debug;
105 if ($params_def->{$param}{array} && defined $p_value_orig) {
106 @p_value = split /$params_def->{$param}{array}/, $p_value_orig;
107 print "DEBUG: Value (Array Split) ".
108 join('##',@p_value)."<br>" if $debug;
111 if ($params_def->{$param}{match} && defined $p_value_orig) {
113 { $_ =~ m/$params_def->{$param}{match}/; $_ = $1 }
116 @p_value = grep { defined $_ } @p_value;
118 print "DEBUG: Value (Match) ".
119 join('##',@p_value)."<br>" if $debug;
122 if (defined $params{$param}{default}) {
123 @p_value = ($params{$param}{default});
126 $params_ret{errors}{$param} = "undef";
131 print "DEBUG: Value (Default) ".
132 join('##',@p_value)."<br>" if $debug;
133 my @p_value_no_replace = @p_value;
135 if ($params{$param}{replace} && @p_value) {
137 foreach my $pattern (keys %{$params{$param}{replace}}) {
138 foreach (@p_value_no_replace) {
139 if ($_ eq $pattern) {
140 my $replacement = $params{$param}{replace}{$_};
141 if (ref $replacement) {
142 push @p_value, @$replacement;
144 push @p_value, $replacement;
153 print "DEBUG: Value (Final) ".
154 join('##',@p_value)."<br>" if $debug;
156 if ($params_def->{$param}{array}) {
157 $params_ret{values}{$param} = {
158 orig => $p_value_orig,
159 no_replace => \@p_value_no_replace,
162 @{$params_def->{$param}{var}} = @p_value
163 if $params_def->{$param}{var};
165 $params_ret{values}{$param} = {
166 orig => $p_value_orig,
167 no_replace => $p_value_no_replace[0],
168 final => $p_value[0],
170 ${$params_def->{$param}{var}} = $p_value[0]
171 if $params_def->{$param}{var};
173 $opts->{$param} = $params_ret{values}{$param}{final} if $opts;
176 if ($USE_PAGED_MODE) {
177 $cgi->delete( "page" );
178 $cgi->delete( "number" );
187 my $page = $params->{values}{page}{final}
189 my $res_per_page = $params->{values}{number}{final}
190 || DEFAULT_RES_PER_PAGE;
192 return 1 if $res_per_page =~ /^all$/i;
193 return $res_per_page * ($page - 1) + 1;
199 my $page = $params->{values}{page}{final}
201 my $res_per_page = $params->{values}{number}{final}
202 || DEFAULT_RES_PER_PAGE;
204 return $page * $res_per_page;
208 my ($cgi, $params, $num_res) = @_;
211 my $page = $params->{values}{page}{final}
213 my $res_per_page = $params->{values}{number}{final}
214 || DEFAULT_RES_PER_PAGE;
215 my $numpages = ceil($num_res /
217 for (my $i = 1; $i <= $numpages; $i++) {
221 $index_line .= "<a href=\"".encode_entities($cgi->self_url).
222 "&page=$i&number=$res_per_page\">".
225 if ($i < $numpages) {
226 $index_line .= " | ";
233 my ($cgi, $params, $no_results ) = @_;
235 my $page = $params->{values}{page}{final}
238 my $res_per_page = $params->{values}{number}{final}
239 || DEFAULT_RES_PER_PAGE;
241 if ((($page-1)*$res_per_page + 1) > $no_results) {
245 return "<a href=\"".encode_entities($cgi->self_url).
246 "&page=$page&number=$res_per_page\">>></a>";
250 my ($cgi, $params ) = @_;
252 my $page = $params->{values}{page}{final}
259 my $res_per_page = $params->{values}{number}{final}
260 || DEFAULT_RES_PER_PAGE;
262 return "<a href=\"".encode_entities($cgi->self_url).
263 "&page=$page&number=$res_per_page\"><<</a>";
267 my ($cgi, $params, $res_per_page ) = @_;
270 if ($res_per_page =~ /^all$/i) {
273 $page = ceil(start( $params ) / $res_per_page);
276 return "<a href=\"".encode_entities($cgi->self_url).
277 "&page=$page&number=$res_per_page\">$res_per_page</a>";