Bug 26911: (bug 18936 follow-up) Remove invalid data before copying the circ rules
[koha.git] / serials / subscription-bib-search.pl
blob049da968ccfc2297fa8a4d2f5b56e49ee95ef56e
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
4 # Copyright 2000-2002 Katipo Communications
5 # Parts Copyright 2010 Biblibre
7 # This file is part of Koha.
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.
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.
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>.
22 =head1 NAME
24 subscription-bib-search.pl
26 =head1 DESCRIPTION
28 this script search among all existing subscriptions.
30 =head1 PARAMETERS
32 =over 4
34 =item op
35 op use to know the operation to do on this template.
36 * do_search : to search the subscription.
38 Note that if op = do_search there are some others params specific to the search :
39 marclist,and_or,excluding,operator,value
41 =item startfrom
42 to multipage gestion.
45 =back
47 =cut
49 use Modern::Perl;
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;
60 use Koha::ItemTypes;
61 use Koha::SearchEngine;
62 use Koha::SearchEngine::Search;
64 my $input = CGI->new;
65 my $op = $input->param('op') || q{};
66 my $dbh = C4::Context->dbh;
68 my $startfrom = $input->param('startfrom');
69 $startfrom = 0 unless $startfrom;
70 my ( $template, $loggedinuser, $cookie );
71 my $resultsperpage;
73 my $itype_or_itemtype =
74 ( C4::Context->preference("item-level_itypes") ) ? 'itype' : 'itemtype';
76 my $query = $input->param('q');
78 # don't run the search if no search term !
79 if ( $op eq "do_search" && $query ) {
81 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
83 template_name => "serials/result.tt",
84 query => $input,
85 type => "intranet",
86 flagsrequired => { catalogue => 1, serials => '*' },
87 debug => 1,
91 # add the limits if applicable
92 my $itemtypelimit = $input->param('itemtypelimit');
93 my $ccodelimit = $input->param('ccodelimit');
94 my $op = 'and';
95 $query .= " $op $itype_or_itemtype:$itemtypelimit" if $itemtypelimit;
96 $query .= " $op ccode:$ccodelimit" if $ccodelimit;
97 $debug && warn $query;
98 $resultsperpage = $input->param('resultsperpage');
99 $resultsperpage = 20 if ( !defined $resultsperpage );
101 my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
102 my ( $error, $marcrecords, $total_hits ) =
103 $searcher->simple_search_compat( $query, $startfrom * $resultsperpage, $resultsperpage );
104 my $total = 0;
105 if ( defined $marcrecords ) {
106 $total = scalar @{$marcrecords};
109 if ( defined $error ) {
110 $template->param( query_error => $error );
111 warn "error: " . $error;
112 output_html_with_http_headers $input, $cookie, $template->output;
113 exit;
115 my @results;
117 for ( my $i = 0 ; $i < $total ; $i++ ) {
118 my %resultsloop;
119 my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcrecords->[$i] );
120 my $biblio = TransformMarcToKoha( $marcrecord, '' );
122 #build the hash for the template.
123 $resultsloop{highlight} = ( $i % 2 ) ? (1) : (0);
124 $resultsloop{title} = $biblio->{'title'};
125 $resultsloop{subtitle} = $biblio->{'subtitle'};
126 $resultsloop{medium} = $biblio->{'medium'};
127 $resultsloop{part_number} = $biblio->{'part_number'};
128 $resultsloop{part_name} = $biblio->{'part_name'};
129 $resultsloop{biblionumber} = $biblio->{'biblionumber'};
130 $resultsloop{author} = $biblio->{'author'};
131 $resultsloop{publishercode} = $biblio->{'publishercode'};
132 $resultsloop{publicationyear} = $biblio->{'publicationyear'} ? $biblio->{'publicationyear'} : $biblio->{'copyrightdate'};
133 $resultsloop{issn} = $biblio->{'issn'};
135 push @results, \%resultsloop;
138 # multi page display gestion
139 my $displaynext = 0;
140 my $displayprev = $startfrom;
141 if ( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
142 $displaynext = 1;
145 my @numbers = ();
147 if ( $total_hits > $resultsperpage ) {
148 for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
149 if ( $i < 16 ) {
150 my $highlight = 0;
151 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
152 push @numbers,
154 number => $i,
155 highlight => $highlight,
156 searchdata => \@results,
157 startfrom => ( $i - 1 )
163 my $from = 0;
164 $from = $startfrom * $resultsperpage + 1 if ( $total_hits > 0 );
165 my $to;
167 if ( $total_hits < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
168 $to = $total;
170 else {
171 $to = ( ( $startfrom + 1 ) * $resultsperpage );
173 $template->param(
174 query => $query,
175 resultsloop => \@results,
176 startfrom => $startfrom,
177 displaynext => $displaynext,
178 displayprev => $displayprev,
179 resultsperpage => $resultsperpage,
180 startfromnext => $startfrom + 1,
181 startfromprev => $startfrom - 1,
182 total => $total_hits,
183 from => $from,
184 to => $to,
185 numbers => \@numbers,
187 } # end of if ($op eq "do_search" & $query)
188 else {
189 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
191 template_name => "serials/subscription-bib-search.tt",
192 query => $input,
193 type => "intranet",
194 flagsrequired => { catalogue => 1, serials => '*' },
195 debug => 1,
199 # load the itemtypes
200 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
201 my @itemtypesloop;
202 # FIXME This is uselessly complex, the iterator should be send to the template
203 # FIXME The translated_description should be used
204 foreach my $thisitemtype (
205 sort {
206 $itemtypes->{$a}->{'description'}
207 cmp $itemtypes->{$b}->{'description'}
208 } keys %$itemtypes
211 my %row = (
212 code => $thisitemtype,
213 description => $itemtypes->{$thisitemtype}->{'description'},
215 push @itemtypesloop, \%row;
218 # load Collection Codes
219 my $authvalues = GetAuthorisedValues('CCODE');
220 my @ccodesloop;
221 for my $thisauthvalue ( sort { $a->{'lib'} cmp $b->{'lib'} } @$authvalues )
223 my %row = (
224 code => $thisauthvalue->{'authorised_value'},
225 description => $thisauthvalue->{'lib'},
227 push @ccodesloop, \%row;
230 $template->param(
231 itemtypeloop => \@itemtypesloop,
232 ccodeloop => \@ccodesloop,
233 no_query => $op eq "do_search" ? 1 : 0,
237 # Print the page
238 output_html_with_http_headers $input, $cookie, $template->output;
240 # Local Variables:
241 # tab-width: 4
242 # End: