Bug 25032: Chomp stringified exception
[koha.git] / labels / label-item-search.pl
blobfdade75a4e4f9edf92d081494c838583a8fbbd1d
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
21 use vars qw($debug $cgi_debug);
23 use CGI qw ( -utf8 );
24 use List::Util qw( max min );
25 use POSIX qw(ceil);
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Context;
30 use C4::Search qw(SimpleSearch);
31 use C4::Biblio qw(TransformMarcToKoha);
32 use C4::Creators::Lib qw(html_table);
33 use C4::Debug;
35 use Koha::DateUtils;
36 use Koha::Items;
37 use Koha::ItemTypes;
38 use Koha::SearchEngine::Search;
40 BEGIN {
41 $debug = $debug || $cgi_debug;
42 if ($debug) {
43 require Data::Dumper;
44 import Data::Dumper qw(Dumper);
48 my $query = new CGI;
50 my $type = $query->param('type');
51 my $op = $query->param('op') || '';
52 my $batch_id = $query->param('batch_id');
53 my $ccl_query = $query->param('ccl_query');
54 my $startfrom = $query->param('startfrom') || 1;
55 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
56 my (
57 $total_hits, $total, $error,
58 $marcresults, $idx, $datefrom, $dateto, $ccl_textbox
60 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
61 my $show_results = 0;
62 my $display_columns = [ {_add => {label => "Add Item", link_field => 1}},
63 {_item_call_number => {label => "Call Number", link_field => 0}},
64 {_date_accessioned => {label => "Accession Date", link_field => 0}},
65 {_barcode => {label => "Barcode", link_field => 0}},
66 {select => {label => "Select", value => "_item_number"}},
69 if ( $op eq "do_search" ) {
70 $idx = $query->param('idx');
71 $ccl_textbox = $query->param('ccl_textbox');
72 if ( $ccl_textbox && $idx ) {
73 $ccl_query = "$idx:$ccl_textbox";
76 $datefrom = $query->param('datefrom');
77 $dateto = $query->param('dateto');
79 if ($datefrom) {
80 $datefrom = eval { dt_from_string ( $datefrom ) };
81 if ($datefrom) {
82 $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
83 $ccl_query .= ' and ' if $ccl_textbox;
84 $ccl_query .= "acqdate,ge,st-date-normalized=" . $datefrom;
88 if ($dateto) {
89 $dateto = eval { dt_from_string ( $dateto ) };
90 if ($dateto) {
91 $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
92 $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
93 $ccl_query .= "acqdate,le,st-date-normalized=" . $dateto;
97 my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
98 my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
99 ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($ccl_query, $offset, $resultsperpage);
101 if (!defined $error && @{$marcresults} ) {
102 $show_results = @{$marcresults};
104 else {
105 $debug and warn "ERROR label-item-search: no results from simple_search_compat";
107 # leave $show_results undef
111 if ($show_results) {
112 my $hits = $show_results;
113 my @results_set = ();
114 my @items =();
115 # This code needs to be refactored using these subs...
116 #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
117 for ( my $i = 0 ; $i < $hits ; $i++ ) {
118 my @row_data= ();
119 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
120 my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
121 #DEBUG Notes: Transform it to Koha form...
122 my $biblio = TransformMarcToKoha( $marcrecord, '' );
123 #DEBUG Notes: Stuff the bib into @biblio_data...
124 push (@results_set, $biblio);
125 my $biblionumber = $biblio->{'biblionumber'};
126 #DEBUG Notes: Grab the item numbers associated with this MARC record...
127 my $items = Koha::Items->search({ biblionumber => $biblionumber }, { order_by => { -desc => 'itemnumber' }});
128 #DEBUG Notes: Retrieve the item data for each number...
129 while ( my $item = $items->next ) {
130 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
131 if ( $item->biblionumber eq $results_set[$i]->{'biblionumber'} ) {
132 my $item_data;
133 $item_data->{'_item_number'} = $item->itemnumber;
134 $item_data->{'_item_call_number'} = ( $item->itemcallnumber || 'NA' );
135 $item_data->{'_date_accessioned'} = $item->dateaccessioned;
136 $item_data->{'_barcode'} = ( $item->barcode || 'NA' );
137 $item_data->{'_add'} = $item->itemnumber;
138 push @row_data, $item_data;
140 $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
144 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
146 template_name => "labels/result.tt",
147 query => $query,
148 type => "intranet",
149 authnotrequired => 0,
150 flagsrequired => { borrowers => 'edit_borrowers' },
151 flagsrequired => { catalogue => 1 },
152 debug => 1,
156 # build page nav stuff.
157 my @numbers;
158 $total = $total_hits;
160 my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
161 $displayprev );
163 if ( $total > $resultsperpage ) {
164 my $num_of_pages = ceil( $total / $resultsperpage + 1 );
165 for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
166 my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
167 push @numbers,
169 number => $page,
170 startfrom => $startfrm
174 $from = $startfrom;
175 $startfromprev = $startfrom - $resultsperpage;
176 $startfromnext = $startfrom + $resultsperpage;
178 $to =
179 $startfrom + $resultsperpage > $total
180 ? $total
181 : $startfrom + $resultsperpage - 1;
183 # multi page display
184 $displaynext = 0;
185 $displayprev = $startfrom > 1 ? $startfrom : 0;
187 $displaynext = 1 if $to < $total_hits;
190 else {
191 $from = 1;
192 $to = $total_hits;
193 $displayprev = 0;
194 $displaynext = 0;
197 $template->param(
198 total => $total_hits,
199 from => $from,
200 to => $to,
201 startfromnext => $startfromnext,
202 startfromprev => $startfromprev,
203 startfrom => $startfrom,
204 displaynext => $displaynext,
205 displayprev => $displayprev,
206 resultsperpage => $resultsperpage,
207 numbers => \@numbers,
210 $template->param(
211 results => ($show_results ? 1 : 0),
212 result_set=> \@results_set,
213 batch_id => $batch_id,
214 type => $type,
215 ccl_query => $ccl_query,
220 # search section
223 else {
224 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
226 template_name => "labels/search.tt",
227 query => $query,
228 type => "intranet",
229 authnotrequired => 0,
230 flagsrequired => { catalogue => 1 },
231 debug => 1,
234 my $itemtypes = Koha::ItemTypes->search;
235 my @itemtypeloop;
236 while ( my $itemtype = $itemtypes->next ) {
237 # FIXME This must be improved:
238 # - pass the iterator to the template
239 # - display the translated_description
240 my %row = (
241 value => $itemtype->itemtype,
242 description => $itemtype->description,
244 push @itemtypeloop, \%row;
246 $template->param(
247 itemtypeloop => \@itemtypeloop,
248 batch_id => $batch_id,
249 type => $type,
254 $template->param( idx => $idx );
256 # Print the page
257 output_html_with_http_headers $query, $cookie, $template->output;