Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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 = new CGI;
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             authnotrequired => 0,
87             flagsrequired   => { catalogue => 1, serials => '*' },
88             debug           => 1,
89         }
90     );
91
92     # add the limits if applicable
93     my $itemtypelimit = $input->param('itemtypelimit');
94     my $ccodelimit    = $input->param('ccodelimit');
95     my $op = C4::Context->preference('UseQueryParser') ? '&&' : 'and';
96     $query .= " $op $itype_or_itemtype:$itemtypelimit" if $itemtypelimit;
97     $query .= " $op ccode:$ccodelimit" if $ccodelimit;
98     $debug && warn $query;
99     $resultsperpage = $input->param('resultsperpage');
100     $resultsperpage = 20 if ( !defined $resultsperpage );
101
102     my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
103     my ( $error, $marcrecords, $total_hits ) =
104       $searcher->simple_search_compat( $query, $startfrom * $resultsperpage, $resultsperpage );
105     my $total = 0;
106     if ( defined $marcrecords ) {
107         $total = scalar @{$marcrecords};
108     }
109
110     if ( defined $error ) {
111         $template->param( query_error => $error );
112         warn "error: " . $error;
113         output_html_with_http_headers $input, $cookie, $template->output;
114         exit;
115     }
116     my @results;
117
118     for ( my $i = 0 ; $i < $total ; $i++ ) {
119         my %resultsloop;
120         my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcrecords->[$i] );
121         my $biblio = TransformMarcToKoha( $marcrecord, '' );
122
123         #build the hash for the template.
124         $resultsloop{highlight}       = ( $i % 2 ) ? (1) : (0);
125         $resultsloop{title}           = $biblio->{'title'};
126         $resultsloop{subtitle}        = $biblio->{'subtitle'};
127         $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
128         $resultsloop{author}          = $biblio->{'author'};
129         $resultsloop{publishercode}   = $biblio->{'publishercode'};
130         $resultsloop{publicationyear} = $biblio->{'publicationyear'} ? $biblio->{'publicationyear'} : $biblio->{'copyrightdate'};
131         $resultsloop{issn}            = $biblio->{'issn'};
132
133         push @results, \%resultsloop;
134     }
135
136     # multi page display gestion
137     my $displaynext = 0;
138     my $displayprev = $startfrom;
139     if ( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
140         $displaynext = 1;
141     }
142
143     my @numbers = ();
144
145     if ( $total_hits > $resultsperpage ) {
146         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
147             if ( $i < 16 ) {
148                 my $highlight = 0;
149                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
150                 push @numbers,
151                   {
152                     number     => $i,
153                     highlight  => $highlight,
154                     searchdata => \@results,
155                     startfrom  => ( $i - 1 )
156                   };
157             }
158         }
159     }
160
161     my $from = 0;
162     $from = $startfrom * $resultsperpage + 1 if ( $total_hits > 0 );
163     my $to;
164
165     if ( $total_hits < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
166         $to = $total;
167     }
168     else {
169         $to = ( ( $startfrom + 1 ) * $resultsperpage );
170     }
171     $template->param(
172         query          => $query,
173         resultsloop    => \@results,
174         startfrom      => $startfrom,
175         displaynext    => $displaynext,
176         displayprev    => $displayprev,
177         resultsperpage => $resultsperpage,
178         startfromnext  => $startfrom + 1,
179         startfromprev  => $startfrom - 1,
180         total          => $total_hits,
181         from           => $from,
182         to             => $to,
183         numbers        => \@numbers,
184     );
185 }    # end of if ($op eq "do_search" & $query)
186 else {
187     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
188         {
189             template_name   => "serials/subscription-bib-search.tt",
190             query           => $input,
191             type            => "intranet",
192             authnotrequired => 0,
193             flagsrequired   => { catalogue => 1, serials => '*' },
194             debug           => 1,
195         }
196     );
197
198     # load the itemtypes
199     my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
200     my @itemtypesloop;
201     # FIXME This is uselessly complex, the iterator should be send to the template
202     # FIXME The translated_description should be used
203     foreach my $thisitemtype (
204         sort {
205             $itemtypes->{$a}->{'description'}
206               cmp $itemtypes->{$b}->{'description'}
207         } keys %$itemtypes
208       )
209     {
210         my %row = (
211             code        => $thisitemtype,
212             description => $itemtypes->{$thisitemtype}->{'description'},
213         );
214         push @itemtypesloop, \%row;
215     }
216
217     # load Collection Codes
218     my $authvalues = GetAuthorisedValues('CCODE');
219     my @ccodesloop;
220     for my $thisauthvalue ( sort { $a->{'lib'} cmp $b->{'lib'} } @$authvalues )
221     {
222         my %row = (
223             code        => $thisauthvalue->{'authorised_value'},
224             description => $thisauthvalue->{'lib'},
225         );
226         push @ccodesloop, \%row;
227     }
228
229     $template->param(
230         itemtypeloop => \@itemtypesloop,
231         ccodeloop    => \@ccodesloop,
232         no_query     => $op eq "do_search" ? 1 : 0,
233     );
234 }
235
236 # Print the page
237 output_html_with_http_headers $input, $cookie, $template->output;
238
239 # Local Variables:
240 # tab-width: 4
241 # End: