Bug 36792: Limit POSIX imports
[koha.git] / serials / subscription-bib-search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2010 Biblibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 =head1 NAME
22
23 subscription-bib-search.pl
24
25 =head1 DESCRIPTION
26
27 this script search among all existing subscriptions.
28
29 =head1 PARAMETERS
30
31 =over 4
32
33 =item op
34 op use to know the operation to do on this template.
35  * do_search : to search the subscription.
36
37 Note that if op = do_search there are some others params specific to the search :
38     marclist,and_or,excluding,operator,value
39
40 =item startfrom
41 to multipage gestion.
42
43
44 =back
45
46 =cut
47
48 use Modern::Perl;
49
50 use CGI qw ( -utf8 );
51 use C4::Koha qw( GetAuthorisedValues );
52 use C4::Auth qw( get_template_and_user );
53 use C4::Context;
54 use C4::Output qw( output_html_with_http_headers );
55 use C4::Search qw( new_record_from_zebra );
56 use C4::Biblio qw( TransformMarcToKoha );
57
58 use Koha::ItemTypes;
59 use Koha::SearchEngine;
60 use Koha::SearchEngine::Search;
61
62 my $input = CGI->new;
63 my $op = $input->param('op') || q{};
64 my $dbh = C4::Context->dbh;
65
66 my $startfrom = $input->param('startfrom');
67 $startfrom = 0 unless $startfrom;
68 my ( $template, $loggedinuser, $cookie );
69 my $resultsperpage;
70
71 my $itype_or_itemtype =
72   ( C4::Context->preference("item-level_itypes") ) ? 'itype' : 'itemtype';
73
74 my $query = $input->param('q');
75
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         {
81             template_name   => "serials/result.tt",
82             query           => $input,
83             type            => "intranet",
84             flagsrequired   => { catalogue => 1, serials => '*' },
85         }
86     );
87
88     # add the limits if applicable
89     my $itemtypelimit = $input->param('itemtypelimit');
90     my $ccodelimit    = $input->param('ccodelimit');
91     my $op = 'AND';
92     $query .= " $op $itype_or_itemtype:$itemtypelimit" if $itemtypelimit;
93     $query .= " $op ccode:$ccodelimit" if $ccodelimit;
94     $resultsperpage = $input->param('resultsperpage');
95     $resultsperpage = 20 if ( !defined $resultsperpage );
96
97     my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
98     my ( $error, $marcrecords, $total_hits ) =
99       $searcher->simple_search_compat( $query, $startfrom * $resultsperpage, $resultsperpage );
100     my $total = 0;
101     if ( defined $marcrecords ) {
102         $total = scalar @{$marcrecords};
103     }
104
105     if ( defined $error ) {
106         $template->param( query_error => $error );
107         warn "error: " . $error;
108         output_html_with_http_headers $input, $cookie, $template->output;
109         exit;
110     }
111     my @results;
112
113     for ( my $i = 0 ; $i < $total ; $i++ ) {
114         my %resultsloop;
115         my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcrecords->[$i] );
116         my $biblio = TransformMarcToKoha({ record => $marcrecord });
117
118         #build the hash for the template.
119         $resultsloop{highlight}       = ( $i % 2 ) ? (1) : (0);
120         $resultsloop{title}           = $biblio->{'title'};
121         $resultsloop{subtitle}        = $biblio->{'subtitle'};
122         $resultsloop{medium}          = $biblio->{'medium'};
123         $resultsloop{part_number}     = $biblio->{'part_number'};
124         $resultsloop{part_name}       = $biblio->{'part_name'};
125         $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
126         $resultsloop{author}          = $biblio->{'author'};
127         $resultsloop{publishercode}   = $biblio->{'publishercode'};
128         $resultsloop{publicationyear} = $biblio->{'publicationyear'} ? $biblio->{'publicationyear'} : $biblio->{'copyrightdate'};
129         $resultsloop{issn}            = $biblio->{'issn'};
130
131         push @results, \%resultsloop;
132     }
133
134     # multi page display gestion
135     my $displaynext = 0;
136     my $displayprev = $startfrom;
137     if ( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
138         $displaynext = 1;
139     }
140
141     my @numbers = ();
142
143     if ( $total_hits > $resultsperpage ) {
144         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
145             if ( $i < 16 ) {
146                 my $highlight = 0;
147                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
148                 push @numbers,
149                   {
150                     number     => $i,
151                     highlight  => $highlight,
152                     searchdata => \@results,
153                     startfrom  => ( $i - 1 )
154                   };
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         $to = $total;
165     }
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 ) = get_template_and_user(
186         {
187             template_name   => "serials/subscription-bib-search.tt",
188             query           => $input,
189             type            => "intranet",
190             flagsrequired   => { catalogue => 1, serials => '*' },
191         }
192     );
193
194     # load the itemtypes
195     my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
196     my @itemtypesloop;
197     # FIXME This is uselessly complex, the iterator should be send to the template
198     # FIXME The translated_description should be used
199     foreach my $thisitemtype (
200         sort {
201             $itemtypes->{$a}->{'description'}
202               cmp $itemtypes->{$b}->{'description'}
203         } keys %$itemtypes
204       )
205     {
206         my %row = (
207             code        => $thisitemtype,
208             description => $itemtypes->{$thisitemtype}->{'description'},
209         );
210         push @itemtypesloop, \%row;
211     }
212
213     # load Collection Codes
214     my $authvalues = GetAuthorisedValues('CCODE');
215     my @ccodesloop;
216     for my $thisauthvalue ( sort { $a->{'lib'} cmp $b->{'lib'} } @$authvalues )
217     {
218         my %row = (
219             code        => $thisauthvalue->{'authorised_value'},
220             description => $thisauthvalue->{'lib'},
221         );
222         push @ccodesloop, \%row;
223     }
224
225     $template->param(
226         itemtypeloop => \@itemtypesloop,
227         ccodeloop    => \@ccodesloop,
228         no_query     => $op eq "do_search" ? 1 : 0,
229     );
230 }
231
232 # Print the page
233 output_html_with_http_headers $input, $cookie, $template->output;