Bug 28591: Don't pass debug to get_template_and_user
[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 # Parts Copyright 2010 Biblibre
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 use Modern::Perl;
50
51 use CGI qw ( -utf8 );
52 use C4::Koha;
53 use C4::Auth;
54 use C4::Context;
55 use C4::Output;
56 use C4::Search;
57 use C4::Biblio;
58 use C4::Debug;
59
60 use Koha::ItemTypes;
61 use Koha::SearchEngine;
62 use Koha::SearchEngine::Search;
63
64 my $input = CGI->new;
65 my $op = $input->param('op') || q{};
66 my $dbh = C4::Context->dbh;
67
68 my $startfrom = $input->param('startfrom');
69 $startfrom = 0 unless $startfrom;
70 my ( $template, $loggedinuser, $cookie );
71 my $resultsperpage;
72
73 my $itype_or_itemtype =
74   ( C4::Context->preference("item-level_itypes") ) ? 'itype' : 'itemtype';
75
76 my $query = $input->param('q');
77
78 # don't run the search if no search term !
79 if ( $op eq "do_search" && $query ) {
80
81     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
82         {
83             template_name   => "serials/result.tt",
84             query           => $input,
85             type            => "intranet",
86             flagsrequired   => { catalogue => 1, serials => '*' },
87         }
88     );
89
90     # add the limits if applicable
91     my $itemtypelimit = $input->param('itemtypelimit');
92     my $ccodelimit    = $input->param('ccodelimit');
93     my $op = 'and';
94     $query .= " $op $itype_or_itemtype:$itemtypelimit" if $itemtypelimit;
95     $query .= " $op ccode:$ccodelimit" if $ccodelimit;
96     $debug && warn $query;
97     $resultsperpage = $input->param('resultsperpage');
98     $resultsperpage = 20 if ( !defined $resultsperpage );
99
100     my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
101     my ( $error, $marcrecords, $total_hits ) =
102       $searcher->simple_search_compat( $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 = C4::Search::new_record_from_zebra( 'biblioserver', $marcrecords->[$i] );
119         my $biblio = TransformMarcToKoha( $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{medium}          = $biblio->{'medium'};
126         $resultsloop{part_number}     = $biblio->{'part_number'};
127         $resultsloop{part_name}       = $biblio->{'part_name'};
128         $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
129         $resultsloop{author}          = $biblio->{'author'};
130         $resultsloop{publishercode}   = $biblio->{'publishercode'};
131         $resultsloop{publicationyear} = $biblio->{'publicationyear'} ? $biblio->{'publicationyear'} : $biblio->{'copyrightdate'};
132         $resultsloop{issn}            = $biblio->{'issn'};
133
134         push @results, \%resultsloop;
135     }
136
137     # multi page display gestion
138     my $displaynext = 0;
139     my $displayprev = $startfrom;
140     if ( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
141         $displaynext = 1;
142     }
143
144     my @numbers = ();
145
146     if ( $total_hits > $resultsperpage ) {
147         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
148             if ( $i < 16 ) {
149                 my $highlight = 0;
150                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
151                 push @numbers,
152                   {
153                     number     => $i,
154                     highlight  => $highlight,
155                     searchdata => \@results,
156                     startfrom  => ( $i - 1 )
157                   };
158             }
159         }
160     }
161
162     my $from = 0;
163     $from = $startfrom * $resultsperpage + 1 if ( $total_hits > 0 );
164     my $to;
165
166     if ( $total_hits < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
167         $to = $total;
168     }
169     else {
170         $to = ( ( $startfrom + 1 ) * $resultsperpage );
171     }
172     $template->param(
173         query          => $query,
174         resultsloop    => \@results,
175         startfrom      => $startfrom,
176         displaynext    => $displaynext,
177         displayprev    => $displayprev,
178         resultsperpage => $resultsperpage,
179         startfromnext  => $startfrom + 1,
180         startfromprev  => $startfrom - 1,
181         total          => $total_hits,
182         from           => $from,
183         to             => $to,
184         numbers        => \@numbers,
185     );
186 }    # end of if ($op eq "do_search" & $query)
187 else {
188     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
189         {
190             template_name   => "serials/subscription-bib-search.tt",
191             query           => $input,
192             type            => "intranet",
193             flagsrequired   => { catalogue => 1, serials => '*' },
194         }
195     );
196
197     # load the itemtypes
198     my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
199     my @itemtypesloop;
200     # FIXME This is uselessly complex, the iterator should be send to the template
201     # FIXME The translated_description should be used
202     foreach my $thisitemtype (
203         sort {
204             $itemtypes->{$a}->{'description'}
205               cmp $itemtypes->{$b}->{'description'}
206         } keys %$itemtypes
207       )
208     {
209         my %row = (
210             code        => $thisitemtype,
211             description => $itemtypes->{$thisitemtype}->{'description'},
212         );
213         push @itemtypesloop, \%row;
214     }
215
216     # load Collection Codes
217     my $authvalues = GetAuthorisedValues('CCODE');
218     my @ccodesloop;
219     for my $thisauthvalue ( sort { $a->{'lib'} cmp $b->{'lib'} } @$authvalues )
220     {
221         my %row = (
222             code        => $thisauthvalue->{'authorised_value'},
223             description => $thisauthvalue->{'lib'},
224         );
225         push @ccodesloop, \%row;
226     }
227
228     $template->param(
229         itemtypeloop => \@itemtypesloop,
230         ccodeloop    => \@ccodesloop,
231         no_query     => $op eq "do_search" ? 1 : 0,
232     );
233 }
234
235 # Print the page
236 output_html_with_http_headers $input, $cookie, $template->output;
237
238 # Local Variables:
239 # tab-width: 4
240 # End: