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>.
22 use vars qw($debug $cgi_debug);
25 use List::Util qw( max min );
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
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);
35 use C4::Creators::Lib qw(html_table);
40 $debug = $debug || $cgi_debug;
43 import Data::Dumper qw(Dumper);
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);
56 $total_hits, $total, $error,
57 $marcresults, $idx, $datefrom, $dateto, $ccl_textbox
59 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
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" ) {
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');
81 $datefrom = eval { dt_from_string ( $datefrom ) };
83 $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
85 $ccl_query .= ' && ' if $ccl_textbox;
87 "acqdate(" . $datefrom . '-)';
89 $ccl_query .= ' and ' if $ccl_textbox;
90 $ccl_query .= "acqdate,st-date-normalized,ge=" . $datefrom;
96 $dateto = eval { dt_from_string ( $dateto ) };
98 $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
100 $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
101 $ccl_query .= "acqdate(-" . $dateto . ')';
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};
117 $debug and warn "ERROR label-item-search: no results from SimpleSearch";
119 # leave $show_results undef
124 my $hits = $show_results;
125 my @results_set = ();
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++ ) {
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( $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'}) {
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);
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",
169 authnotrequired => 0,
170 flagsrequired => { borrowers => 1 },
171 flagsrequired => { catalogue => 1 },
176 # build page nav stuff.
178 $total = $total_hits;
180 my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
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;
190 startfrom => $startfrm
195 $startfromprev = $startfrom - $resultsperpage;
196 $startfromnext = $startfrom + $resultsperpage;
199 $startfrom + $resultsperpage > $total
201 : $startfrom + $resultsperpage - 1;
205 $displayprev = $startfrom > 1 ? $startfrom : 0;
207 $displaynext = 1 if $to < $total_hits;
216 total => $total_hits,
219 startfromnext => $startfromnext,
220 startfromprev => $startfromprev,
221 startfrom => $startfrom,
222 displaynext => $displaynext,
223 displayprev => $displayprev,
224 resultsperpage => $resultsperpage,
225 numbers => \@numbers,
229 results => ($show_results ? 1 : 0),
230 result_set=> \@results_set,
231 batch_id => $batch_id,
233 ccl_query => $ccl_query,
242 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
244 template_name => "labels/search.tt",
247 authnotrequired => 0,
248 flagsrequired => { catalogue => 1 },
252 my $itemtypes = GetItemTypes;
254 foreach my $thisitemtype ( keys %$itemtypes ) {
256 value => $thisitemtype,
257 description => $itemtypes->{$thisitemtype}->{'description'},
259 push @itemtypeloop, \%row;
262 itemtypeloop => \@itemtypeloop,
263 batch_id => $batch_id,
269 $template->param( idx => $idx );
272 output_html_with_http_headers $query, $cookie, $template->output;