Porting SimpleSearch return changes to all code calling it.
[koha.git] / serials / subscription-bib-search.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21
22 =head1 NAME
23
24 subscription-bib-search.pl
25
26 =head1 DESCRIPTION
27
28 this script search among all existing subscriptions.
29
30 =head1 PARAMETERS
31
32 =over 4
33
34 =item op
35 op use to know the operation to do on this template.
36  * do_search : to search the subscription.
37
38 Note that if op = do_search there are some others params specific to the search :
39     marclist,and_or,excluding,operator,value
40
41 =item startfrom
42 to multipage gestion.
43
44
45 =back
46
47 =cut
48
49
50 use strict;
51 require Exporter;
52 use CGI;
53 use C4::Koha;
54 use C4::Auth;
55 use C4::Context;
56 use C4::Output;
57 use C4::Search;
58 use C4::Biblio;
59
60 my $input=new CGI;
61 # my $type=$query->param('type');
62 my $op = $input->param('op');
63 my $dbh = C4::Context->dbh;
64
65 my $startfrom=$input->param('startfrom');
66 $startfrom=0 unless $startfrom;
67 my ($template, $loggedinuser, $cookie);
68 my $resultsperpage;
69
70 my $query = $input->param('q');
71 # don't run the search if no search term !
72 if ($op eq "do_search" && $query) {
73
74     # add the itemtype limit if applicable
75     my $itemtypelimit = $input->param('itemtypelimit');
76     $query .= " AND itype=$itemtypelimit" if $itemtypelimit;
77     
78     $resultsperpage= $input->param('resultsperpage');
79     $resultsperpage = 19 if(!defined $resultsperpage);
80
81     my ($error, $marcrecords, $total_hits) = SimpleSearch($query);
82     my $total = scalar @$marcrecords;
83
84     if (defined $error) {
85         $template->param(query_error => $error);
86         warn "error: ".$error;
87         output_html_with_http_headers $input, $cookie, $template->output;
88         exit;
89     }
90     my @results;
91     warn "total=".$total;
92     
93     for(my $i=0;$i<$total;$i++) {
94         my %resultsloop;
95         my $marcrecord = MARC::File::USMARC::decode($marcrecords->[$i]);
96         my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
97
98         #build the hash for the template.
99         $resultsloop{highlight}       = ($i % 2)?(1):(0);
100         $resultsloop{title}           = $biblio->{'title'};
101         $resultsloop{subtitle}        = $biblio->{'subtitle'};
102         $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
103         $resultsloop{author}          = $biblio->{'author'};
104         $resultsloop{publishercode}   = $biblio->{'publishercode'};
105         $resultsloop{publicationyear} = $biblio->{'publicationyear'};
106
107         push @results, \%resultsloop;
108     }
109     
110     ($template, $loggedinuser, $cookie)
111         = get_template_and_user({template_name => "serials/result.tmpl",
112                 query => $input,
113                 type => "intranet",
114                 authnotrequired => 0,
115                 flagsrequired => {serials => 1},
116                 flagsrequired => {catalogue => 1},
117                 debug => 1,
118                 });
119
120     # multi page display gestion
121     my $displaynext=0;
122     my $displayprev=$startfrom;
123     if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
124         $displaynext = 1;
125     }
126
127
128     my @numbers = ();
129
130     if ($total>$resultsperpage)
131     {
132         for (my $i=1; $i<$total/$resultsperpage+1; $i++)
133         {
134             if ($i<16)
135             {
136                 my $highlight=0;
137                 ($startfrom==($i-1)) && ($highlight=1);
138                 push @numbers, { number => $i,
139                     highlight => $highlight ,
140                     searchdata=> \@results,
141                     startfrom => ($i-1)};
142             }
143         }
144     }
145
146     my $from = $startfrom*$resultsperpage+1;
147     my $to;
148
149     if($total < (($startfrom+1)*$resultsperpage))
150     {
151         $to = $total;
152     } else {
153         $to = (($startfrom+1)*$resultsperpage);
154     }
155     $template->param(
156                             query => $query,
157                             resultsloop => \@results,
158                             startfrom=> $startfrom,
159                             displaynext=> $displaynext,
160                             displayprev=> $displayprev,
161                             resultsperpage => $resultsperpage,
162                             startfromnext => $startfrom+1,
163                             startfromprev => $startfrom-1,
164                             total=>$total,
165                             from=>$from,
166                             to=>$to,
167                             numbers=>\@numbers,
168                             );
169 } # end of if ($op eq "do_search" & $query)
170  elsif ($op eq "do_search") {
171     ($template, $loggedinuser, $cookie)
172         = get_template_and_user({template_name => "serials/subscription-bib-search.tmpl",
173                 query => $input,
174                 type => "intranet",
175                 authnotrequired => 0,
176                 flagsrequired => {catalogue => 1, serials=>1},
177                 debug => 1,
178                 });
179     # load the itemtypes
180     my $itemtypes = GetItemTypes;
181     my @itemtypesloop;
182     my $selected=1;
183     my $cnt;
184     my $imgdir = getitemtypeimagesrc('intranet');
185     foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
186         my %row =(
187                     code => $thisitemtype,
188                     selected => $selected,
189                     description => $itemtypes->{$thisitemtype}->{'description'},
190                 );
191         $selected = 0 if ($selected) ;
192         push @itemtypesloop, \%row;
193     }
194     $template->param(itemtypeloop => \@itemtypesloop);
195     $template->param("no_query" => 1);
196 }
197  else {
198     ($template, $loggedinuser, $cookie)
199         = get_template_and_user({template_name => "serials/subscription-bib-search.tmpl",
200                 query => $input,
201                 type => "intranet",
202                 authnotrequired => 0,
203                 flagsrequired => {catalogue => 1, serials=>1},
204                 debug => 1,
205                 });
206     # load the itemtypes
207     my $itemtypes = GetItemTypes;
208     my @itemtypesloop;
209     my $selected=1;
210     my $cnt;
211     my $imgdir = getitemtypeimagesrc('intranet');
212     foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
213         my %row =(
214                     code => $thisitemtype,
215                     selected => $selected,
216                     description => $itemtypes->{$thisitemtype}->{'description'},
217                 );
218         $selected = 0 if ($selected) ;
219         push @itemtypesloop, \%row;
220     }
221     $template->param(itemtypeloop => \@itemtypesloop);
222     $template->param("no_query" => 0);
223 }
224
225 # Print the page
226 output_html_with_http_headers $input, $cookie, $template->output;
227
228 # Local Variables:
229 # tab-width: 4
230 # End: