(bug #4363) Fix subscription search error when no results
[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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 use warnings;
52
53 use CGI;
54 use C4::Koha;
55 use C4::Auth;
56 use C4::Context;
57 use C4::Output;
58 use C4::Search;
59 use C4::Biblio;
60 use C4::Debug;
61
62 my $input=new CGI;
63 # my $type=$query->param('type');
64 my $op = $input->param('op') || q{};
65 my $dbh = C4::Context->dbh;
66
67 my $startfrom=$input->param('startfrom');
68 $startfrom=0 unless $startfrom;
69 my ($template, $loggedinuser, $cookie);
70 my $resultsperpage;
71
72 my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes");
73 my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
74
75 my $query = $input->param('q');
76 # don't run the search if no search term !
77 if ($op eq "do_search" && $query) {
78
79     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
80         {   template_name   => "serials/result.tmpl",
81             query           => $input,
82             type            => "intranet",
83             authnotrequired => 0,
84             flagsrequired => {catalogue => 1, serials => '*'},
85             debug           => 1,
86         }
87     );
88
89     # add the itemtype limit if applicable
90     my $itemtypelimit = $input->param('itemtypelimit');
91     if ( $itemtypelimit ) {
92         if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
93             $query .= " AND $itype_or_itemtype=$itemtypelimit";
94         } else {
95             $query .= " AND $advanced_search_types=$itemtypelimit";
96         }
97     }
98     $debug && warn $query;
99     $resultsperpage= $input->param('resultsperpage');
100     $resultsperpage = 20 if(!defined $resultsperpage);
101
102     my ($error, $marcrecords, $total_hits) = SimpleSearch($query, $startfrom*$resultsperpage, $resultsperpage);
103     my $total = 0;
104     if (defined $marcrecords ) {
105         $total = scalar @{$marcrecords};
106     }
107
108     if (defined $error) {
109         $template->param(query_error => $error);
110         warn "error: ".$error;
111         output_html_with_http_headers $input, $cookie, $template->output;
112         exit;
113     }
114     my @results;
115
116     for(my $i=0;$i<$total;$i++) {
117         my %resultsloop;
118         my $marcrecord = MARC::File::USMARC::decode($marcrecords->[$i]);
119         my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
120
121         #build the hash for the template.
122         $resultsloop{highlight}       = ($i % 2)?(1):(0);
123         $resultsloop{title}           = $biblio->{'title'};
124         $resultsloop{subtitle}        = $biblio->{'subtitle'};
125         $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
126         $resultsloop{author}          = $biblio->{'author'};
127         $resultsloop{publishercode}   = $biblio->{'publishercode'};
128         $resultsloop{publicationyear} = $biblio->{'publicationyear'};
129
130         push @results, \%resultsloop;
131     }
132
133     # multi page display gestion
134     my $displaynext=0;
135     my $displayprev=$startfrom;
136     if(($total_hits - (($startfrom+1)*($resultsperpage))) > 0 ){
137         $displaynext = 1;
138     }
139
140
141     my @numbers = ();
142
143     if ($total_hits>$resultsperpage)
144     {
145         for (my $i=1; $i<$total/$resultsperpage+1; $i++)
146         {
147             if ($i<16)
148             {
149                 my $highlight=0;
150                 ($startfrom==($i-1)) && ($highlight=1);
151                 push @numbers, { number => $i,
152                     highlight => $highlight ,
153                     searchdata=> \@results,
154                     startfrom => ($i-1)};
155             }
156         }
157     }
158
159     my $from = 0;
160     $from = $startfrom*$resultsperpage+1 if($total_hits > 0);
161     my $to;
162
163     if($total_hits < (($startfrom+1)*$resultsperpage))
164     {
165         $to = $total;
166     } else {
167         $to = (($startfrom+1)*$resultsperpage);
168     }
169     $template->param(
170                             query => $query,
171                             resultsloop => \@results,
172                             startfrom=> $startfrom,
173                             displaynext=> $displaynext,
174                             displayprev=> $displayprev,
175                             resultsperpage => $resultsperpage,
176                             startfromnext => $startfrom+1,
177                             startfromprev => $startfrom-1,
178                             total=>$total_hits,
179                             from=>$from,
180                             to=>$to,
181                             numbers=>\@numbers,
182                             );
183 } # end of if ($op eq "do_search" & $query)
184 else {
185     ($template, $loggedinuser, $cookie)
186         = get_template_and_user({template_name => "serials/subscription-bib-search.tmpl",
187                 query => $input,
188                 type => "intranet",
189                 authnotrequired => 0,
190                 flagsrequired => {catalogue => 1, serials => '*'},
191                 debug => 1,
192                 });
193     # load the itemtypes
194     my $itemtypes = GetItemTypes;
195     my @itemtypesloop;
196     if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
197         # load the itemtypes
198         my $itemtypes = GetItemTypes;
199         my $selected=1;
200         my $cnt;
201         foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
202             my %row =(
203                         code => $thisitemtype,
204                         selected => $selected,
205                         description => $itemtypes->{$thisitemtype}->{'description'},
206                     );
207             $selected = 0 if ($selected) ;
208             push @itemtypesloop, \%row;
209         }
210
211
212     } else {
213         my $advsearchtypes = GetAuthorisedValues($advanced_search_types);
214         my $cnt;
215         my $selected=1;
216         for my $thisitemtype (sort {$a->{'lib'} cmp $b->{'lib'}} @$advsearchtypes) {
217             my %row =(
218                     number=>$cnt++,
219                     ccl => $advanced_search_types,
220                     code => $thisitemtype->{authorised_value},
221                     selected => $selected,
222                     description => $thisitemtype->{'lib'},
223                     count5 => $cnt % 4,
224                     imageurl=> getitemtypeimagelocation( 'intranet', $thisitemtype->{'imageurl'} ),
225                 );
226             push @itemtypesloop, \%row;
227         }
228     }
229
230
231     if ($op eq "do_search") {
232        $template->param("no_query" => 1);
233     } else {
234        $template->param("no_query" => 0);
235     }
236     $template->param(itemtypeloop => \@itemtypesloop);
237 }
238 # Print the page
239 output_html_with_http_headers $input, $cookie, $template->output;
240
241 # Local Variables:
242 # tab-width: 4
243 # End: