Bug 14788: Add unit tests for GetTopIssues
[koha.git] / labels / label-item-search.pl
blob9bbfa60c8ffd67171d67c24be72fc25800a09356
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 strict;
21 use warnings;
22 use vars qw($debug $cgi_debug);
24 use CGI qw ( -utf8 );
25 use List::Util qw( max min );
26 use POSIX qw(ceil);
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use C4::Context;
31 use C4::Search qw(SimpleSearch);
32 use C4::Biblio qw(TransformMarcToKoha);
33 use C4::Items qw(GetItemInfosOf get_itemnumbers_of);
34 use C4::Koha qw(GetItemTypes); # XXX subfield_is_koha_internal_p
35 use C4::Creators::Lib qw(html_table);
36 use C4::Debug;
37 use Koha::DateUtils;
39 BEGIN {
40 $debug = $debug || $cgi_debug;
41 if ($debug) {
42 require Data::Dumper;
43 import Data::Dumper qw(Dumper);
47 my $query = new CGI;
49 my $type = $query->param('type');
50 my $op = $query->param('op') || '';
51 my $batch_id = $query->param('batch_id');
52 my $ccl_query = $query->param('ccl_query');
53 my $startfrom = $query->param('startfrom') || 1;
54 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
55 my (
56 $total_hits, $orderby, $results, $total, $error,
57 $marcresults, $idx, $datefrom, $dateto, $ccl_textbox
59 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
60 my $show_results = 0;
61 my $display_columns = [ {_add => {label => "Add Item", link_field => 1}},
62 {_item_call_number => {label => "Call Number", link_field => 0}},
63 {_date_accessioned => {label => "Accession Date", link_field => 0}},
64 {_barcode => {label => "Barcode", link_field => 0}},
65 {select => {label => "Select", value => "_item_number"}},
68 if ( $op eq "do_search" ) {
69 my $QParser;
70 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
71 $idx = $query->param('idx');
72 $ccl_textbox = $query->param('ccl_textbox');
73 if ( $ccl_textbox && $idx ) {
74 $ccl_query = "$idx:$ccl_textbox";
77 $datefrom = $query->param('datefrom');
78 $dateto = $query->param('dateto');
80 if ($datefrom) {
81 $datefrom = eval { dt_from_string ( $datefrom ) };
82 if ($datefrom) {
83 $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
84 if ($QParser) {
85 $ccl_query .= ' && ' if $ccl_textbox;
86 $ccl_query .=
87 "acqdate(" . $datefrom . '-)';
88 } else {
89 $ccl_query .= ' and ' if $ccl_textbox;
90 $ccl_query .= "acqdate,st-date-normalized,ge=" . $datefrom;
95 if ($dateto) {
96 $dateto = eval { dt_from_string ( $dateto ) };
97 if ($dateto) {
98 $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
99 if ($QParser) {
100 $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
101 $ccl_query .= "acqdate(-" . $dateto . ')';
102 } else {
103 $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
104 $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto;
109 my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
110 ( $error, $marcresults, $total_hits ) =
111 SimpleSearch( $ccl_query, $offset, $resultsperpage );
113 if (!defined $error && @{$marcresults} ) {
114 $show_results = @{$marcresults};
116 else {
117 $debug and warn "ERROR label-item-search: no results from SimpleSearch";
119 # leave $show_results undef
123 if ($show_results) {
124 my $hits = $show_results;
125 my @results_set = ();
126 my @items =();
127 # This code needs to be refactored using these subs...
128 #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
129 #my $dat = &GetBiblioData( $biblio->{biblionumber} );
130 for ( my $i = 0 ; $i < $hits ; $i++ ) {
131 my @row_data= ();
132 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
133 my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
134 #DEBUG Notes: Transform it to Koha form...
135 my $biblio = TransformMarcToKoha( C4::Context->dbh, $marcrecord, '' );
136 #DEBUG Notes: Stuff the bib into @biblio_data...
137 push (@results_set, $biblio);
138 my $biblionumber = $biblio->{'biblionumber'};
139 #DEBUG Notes: Grab the item numbers associated with this MARC record...
140 my $itemnums = get_itemnumbers_of($biblionumber);
141 #DEBUG Notes: Retrieve the item data for each number...
142 if (my $iii = $itemnums->{$biblionumber}) {
143 my $item_results = GetItemInfosOf(@$iii);
144 foreach my $item ( keys %$item_results ) {
145 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
146 if ($item_results->{$item}->{'biblionumber'} eq $results_set[$i]->{'biblionumber'}) {
147 my $item_data;
148 $item_data->{'_item_number'} = $item_results->{$item}->{'itemnumber'};
149 $item_data->{'_item_call_number'} = ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA');
150 $item_data->{'_date_accessioned'} = $item_results->{$item}->{'dateaccessioned'};
151 $item_data->{'_barcode'} = ( $item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA');
152 $item_data->{'_add'} = $item_results->{$item}->{'itemnumber'};
153 unshift (@row_data, $item_data); # item numbers are given to us in descending order by get_itemnumbers_of()...
156 $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
158 else {
159 # FIXME: Some error trapping code needed
160 warn sprintf('No item numbers retrieved for biblio number: %s', $biblionumber);
164 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
166 template_name => "labels/result.tt",
167 query => $query,
168 type => "intranet",
169 authnotrequired => 0,
170 flagsrequired => { borrowers => 1 },
171 flagsrequired => { catalogue => 1 },
172 debug => 1,
176 # build page nav stuff.
177 my ( @field_data, @numbers );
178 $total = $total_hits;
180 my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
181 $displayprev );
183 if ( $total > $resultsperpage ) {
184 my $num_of_pages = ceil( $total / $resultsperpage + 1 );
185 for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
186 my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
187 push @numbers,
189 number => $page,
190 startfrom => $startfrm
194 $from = $startfrom;
195 $startfromprev = $startfrom - $resultsperpage;
196 $startfromnext = $startfrom + $resultsperpage;
198 $to =
199 $startfrom + $resultsperpage > $total
200 ? $total
201 : $startfrom + $resultsperpage - 1;
203 # multi page display
204 $displaynext = 0;
205 $displayprev = $startfrom > 1 ? $startfrom : 0;
207 $displaynext = 1 if $to < $total_hits;
210 else {
211 $displayprev = 0;
212 $displaynext = 0;
215 $template->param(
216 total => $total_hits,
217 from => $from,
218 to => $to,
219 startfromnext => $startfromnext,
220 startfromprev => $startfromprev,
221 startfrom => $startfrom,
222 displaynext => $displaynext,
223 displayprev => $displayprev,
224 resultsperpage => $resultsperpage,
225 numbers => \@numbers,
228 $template->param(
229 results => ($show_results ? 1 : 0),
230 result_set=> \@results_set,
231 batch_id => $batch_id,
232 type => $type,
233 ccl_query => $ccl_query,
238 # search section
241 else {
242 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
244 template_name => "labels/search.tt",
245 query => $query,
246 type => "intranet",
247 authnotrequired => 0,
248 flagsrequired => { catalogue => 1 },
249 debug => 1,
252 my $itemtypes = GetItemTypes;
253 my @itemtypeloop;
254 foreach my $thisitemtype ( keys %$itemtypes ) {
255 my %row = (
256 value => $thisitemtype,
257 description => $itemtypes->{$thisitemtype}->{'description'},
259 push @itemtypeloop, \%row;
261 $template->param(
262 itemtypeloop => \@itemtypeloop,
263 batch_id => $batch_id,
264 type => $type,
269 $template->param( idx => $idx );
271 # Print the page
272 output_html_with_http_headers $query, $cookie, $template->output;