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 ) = @_;
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,
163 $params_ret{values}{$param} = {
164 orig => $p_value_orig,
165 no_replace => $p_value_no_replace[0],
166 final => $p_value[0],
171 if ($USE_PAGED_MODE) {
172 $cgi->delete( "page" );
173 $cgi->delete( "number" );
182 my $page = $params->{values}{page}{final}
184 my $res_per_page = $params->{values}{number}{final}
185 || DEFAULT_RES_PER_PAGE;
187 return 1 if $res_per_page =~ /^all$/i;
188 return $res_per_page * ($page - 1) + 1;
194 my $page = $params->{values}{page}{final}
196 my $res_per_page = $params->{values}{number}{final}
197 || DEFAULT_RES_PER_PAGE;
199 return $page * $res_per_page;
203 my ($cgi, $params, $num_res) = @_;
206 my $page = $params->{values}{page}{final}
208 my $res_per_page = $params->{values}{number}{final}
209 || DEFAULT_RES_PER_PAGE;
210 my $numpages = ceil($num_res /
212 for (my $i = 1; $i <= $numpages; $i++) {
216 $index_line .= "<a href=\"".encode_entities($cgi->self_url).
217 "&page=$i&number=$res_per_page\">".
220 if ($i < $numpages) {
221 $index_line .= " | ";
228 my ($cgi, $params, $no_results ) = @_;
230 my $page = $params->{values}{page}{final}
233 my $res_per_page = $params->{values}{number}{final}
234 || DEFAULT_RES_PER_PAGE;
236 if ((($page-1)*$res_per_page + 1) > $no_results) {
240 return "<a href=\"".encode_entities($cgi->self_url).
241 "&page=$page&number=$res_per_page\">>></a>";
245 my ($cgi, $params ) = @_;
247 my $page = $params->{values}{page}{final}
254 my $res_per_page = $params->{values}{number}{final}
255 || DEFAULT_RES_PER_PAGE;
257 return "<a href=\"".encode_entities($cgi->self_url).
258 "&page=$page&number=$res_per_page\"><<</a>";
262 my ($cgi, $params, $res_per_page ) = @_;
265 if ($res_per_page =~ /^all$/i) {
268 $page = ceil(start( $params ) / $res_per_page);
271 return "<a href=\"".encode_entities($cgi->self_url).
272 "&page=$page&number=$res_per_page\">$res_per_page</a>";