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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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);
32 use C4::Search qw(SimpleSearch);
33 use C4::Biblio qw(TransformMarcToKoha);
34 use C4::Items qw(GetItemInfosOf get_itemnumbers_of);
35 use C4::Koha qw(GetItemTypes); # XXX subfield_is_koha_internal_p
36 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, $orderby, $results, $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_copynumber => {label => "Inv. no", link_field => 0}},
63 {_item_stocknumber => {label => "NEW Inv. no", link_field => 0}},
64 {_item_call_number => {label => "Call Number", link_field => 0}},
65 {_date_accessioned => {label => "Accession Date", link_field => 0}},
66 {_barcode => {label => "Barcode", link_field => 0}},
67 {select => {label => "Select", value => "_item_number"}},
70 if ( $op eq "do_search" ) {
72 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
73 $idx = $query->param('idx');
74 $ccl_textbox = $query->param('ccl_textbox');
75 if ( $ccl_textbox && $idx ) {
76 $ccl_query = "$idx:$ccl_textbox";
79 $datefrom = $query->param('datefrom');
80 $dateto = $query->param('dateto');
83 $datefrom = C4::Dates->new($datefrom);
85 $ccl_query .= ' && ' if $ccl_textbox;
87 "acqdate(" . $datefrom->output("iso") . '-)';
89 $ccl_query .= ' and ' if $ccl_textbox;
91 "acqdate,st-date-normalized,ge=" . $datefrom->output("iso");
96 $dateto = C4::Dates->new($dateto);
98 $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
99 $ccl_query .= "acqdate(-" . $dateto->output("iso") . ')';
101 $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
102 $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto->output("iso");
106 my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
107 ( $error, $marcresults, $total_hits ) =
108 SimpleSearch( $ccl_query, $offset, $resultsperpage );
110 if (!defined $error && @{$marcresults} ) {
111 $show_results = @{$marcresults};
114 $debug and warn "ERROR label-item-search: no results from SimpleSearch";
116 # leave $show_results undef
121 my $hits = $show_results;
122 my @results_set = ();
124 # This code needs to be refactored using these subs...
125 #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
126 #my $dat = &GetBiblioData( $biblio->{biblionumber} );
127 for ( my $i = 0 ; $i < $hits ; $i++ ) {
129 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
130 my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
131 #DEBUG Notes: Transform it to Koha form...
132 my $biblio = TransformMarcToKoha( C4::Context->dbh, $marcrecord, '' );
133 #DEBUG Notes: Stuff the bib into @biblio_data...
134 push (@results_set, $biblio);
135 my $biblionumber = $biblio->{'biblionumber'};
136 #DEBUG Notes: Grab the item numbers associated with this MARC record...
137 my $itemnums = get_itemnumbers_of($biblionumber);
138 #DEBUG Notes: Retrieve the item data for each number...
139 if (my $iii = $itemnums->{$biblionumber}) {
140 my $item_results = GetItemInfosOf(@$iii);
141 foreach my $item ( keys %$item_results ) {
142 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
143 if ($item_results->{$item}->{'biblionumber'} eq $results_set[$i]->{'biblionumber'}) {
145 $item_data->{'_item_number'} = $item_results->{$item}->{'itemnumber'};
146 $item_data->{'_item_call_number'} = ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA');
147 $item_data->{'_date_accessioned'} = $item_results->{$item}->{'dateaccessioned'};
148 $item_data->{'_barcode'} = ( $item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA');
149 $item_data->{'_add'} = $item_results->{$item}->{'itemnumber'};
150 $item_data->{'_item_copynumber'} = ($item_results->{$item}->{'copynumber'} ? $item_results->{$item}->{'copynumber'} : 'NA');
151 $item_data->{'_item_stocknumber'} = ($item_results->{$item}->{'stocknumber'} ? $item_results->{$item}->{'stocknumber'} : 'NA');
152 unshift (@row_data, $item_data); # item numbers are given to us in descending order by get_itemnumbers_of()...
155 $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
158 # FIXME: Some error trapping code needed
159 warn sprintf('No item numbers retrieved for biblio number: %s', $biblionumber);
163 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
165 template_name => "labels/result.tmpl",
168 authnotrequired => 0,
169 flagsrequired => { borrowers => 1 },
170 flagsrequired => { catalogue => 1 },
175 # build page nav stuff.
176 my ( @field_data, @numbers );
177 $total = $total_hits;
179 my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
182 if ( $total > $resultsperpage ) {
183 my $num_of_pages = ceil( $total / $resultsperpage + 1 );
184 for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
185 my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
189 startfrom => $startfrm
194 $startfromprev = $startfrom - $resultsperpage;
195 $startfromnext = $startfrom + $resultsperpage;
198 $startfrom + $resultsperpage > $total
200 : $startfrom + $resultsperpage - 1;
204 $displayprev = $startfrom > 1 ? $startfrom : 0;
206 $displaynext = 1 if $to < $total_hits;
215 total => $total_hits,
218 startfromnext => $startfromnext,
219 startfromprev => $startfromprev,
220 startfrom => $startfrom,
221 displaynext => $displaynext,
222 displayprev => $displayprev,
223 resultsperpage => $resultsperpage,
224 numbers => \@numbers,
228 results => ($show_results ? 1 : 0),
229 result_set=> \@results_set,
230 batch_id => $batch_id,
233 ccl_query => $ccl_query,
242 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
244 template_name => "labels/search.tmpl",
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,
270 output_html_with_http_headers $query, $cookie, $template->output;