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);
34 use C4::Creators::Lib qw(html_table);
40 use Koha::SearchEngine::Search;
43 $debug = $debug || $cgi_debug;
46 import Data::Dumper qw(Dumper);
52 my $type = $query->param('type');
53 my $op = $query->param('op') || '';
54 my $batch_id = $query->param('batch_id');
55 my $ccl_query = $query->param('ccl_query');
56 my $startfrom = $query->param('startfrom') || 1;
57 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
59 $total_hits, $total, $error,
60 $marcresults, $idx, $datefrom, $dateto, $ccl_textbox
62 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
64 my $display_columns = [ {_add => {label => "Add Item", link_field => 1}},
65 {_item_call_number => {label => "Call Number", link_field => 0}},
66 {_date_accessioned => {label => "Accession Date", link_field => 0}},
67 {_barcode => {label => "Barcode", link_field => 0}},
68 {select => {label => "Select", value => "_item_number"}},
71 if ( $op eq "do_search" ) {
73 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
74 $idx = $query->param('idx');
75 $ccl_textbox = $query->param('ccl_textbox');
76 if ( $ccl_textbox && $idx ) {
77 $ccl_query = "$idx:$ccl_textbox";
80 $datefrom = $query->param('datefrom');
81 $dateto = $query->param('dateto');
84 $datefrom = eval { dt_from_string ( $datefrom ) };
86 $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
88 $ccl_query .= ' && ' if $ccl_textbox;
90 "acqdate(" . $datefrom . '-)';
92 $ccl_query .= ' and ' if $ccl_textbox;
93 $ccl_query .= "acqdate,st-date-normalized,ge=" . $datefrom;
99 $dateto = eval { dt_from_string ( $dateto ) };
101 $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
103 $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
104 $ccl_query .= "acqdate(-" . $dateto . ')';
106 $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
107 $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto;
112 my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
113 my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
114 ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($ccl_query, $offset, $resultsperpage);
116 if (!defined $error && @{$marcresults} ) {
117 $show_results = @{$marcresults};
120 $debug and warn "ERROR label-item-search: no results from simple_search_compat";
122 # leave $show_results undef
127 my $hits = $show_results;
128 my @results_set = ();
130 # This code needs to be refactored using these subs...
131 #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
132 #my $dat = &GetBiblioData( $biblio->{biblionumber} );
133 for ( my $i = 0 ; $i < $hits ; $i++ ) {
135 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
136 my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
137 #DEBUG Notes: Transform it to Koha form...
138 my $biblio = TransformMarcToKoha( $marcrecord, '' );
139 #DEBUG Notes: Stuff the bib into @biblio_data...
140 push (@results_set, $biblio);
141 my $biblionumber = $biblio->{'biblionumber'};
142 #DEBUG Notes: Grab the item numbers associated with this MARC record...
143 my $items = Koha::Items->search({ biblionumber => $biblionumber }, { order_by => { -desc => 'itemnumber' }});
144 #DEBUG Notes: Retrieve the item data for each number...
145 while ( my $item = $items->next ) {
146 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
147 if ( $item->biblionumber eq $results_set[$i]->{'biblionumber'} ) {
149 $item_data->{'_item_number'} = $item->itemnumber;
150 $item_data->{'_item_call_number'} = ( $item->itemcallnumber || 'NA' );
151 $item_data->{'_date_accessioned'} = $item->dateaccessioned;
152 $item_data->{'_barcode'} = ( $item->barcode || 'NA' );
153 $item_data->{'_add'} = $item->itemnumber;
154 push @row_data, $item_data;
156 $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
160 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
162 template_name => "labels/result.tt",
165 authnotrequired => 0,
166 flagsrequired => { borrowers => 1 },
167 flagsrequired => { catalogue => 1 },
172 # build page nav stuff.
174 $total = $total_hits;
176 my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
179 if ( $total > $resultsperpage ) {
180 my $num_of_pages = ceil( $total / $resultsperpage + 1 );
181 for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
182 my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
186 startfrom => $startfrm
191 $startfromprev = $startfrom - $resultsperpage;
192 $startfromnext = $startfrom + $resultsperpage;
195 $startfrom + $resultsperpage > $total
197 : $startfrom + $resultsperpage - 1;
201 $displayprev = $startfrom > 1 ? $startfrom : 0;
203 $displaynext = 1 if $to < $total_hits;
212 total => $total_hits,
215 startfromnext => $startfromnext,
216 startfromprev => $startfromprev,
217 startfrom => $startfrom,
218 displaynext => $displaynext,
219 displayprev => $displayprev,
220 resultsperpage => $resultsperpage,
221 numbers => \@numbers,
225 results => ($show_results ? 1 : 0),
226 result_set=> \@results_set,
227 batch_id => $batch_id,
229 ccl_query => $ccl_query,
238 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
240 template_name => "labels/search.tt",
243 authnotrequired => 0,
244 flagsrequired => { catalogue => 1 },
248 my $itemtypes = Koha::ItemTypes->search;
250 while ( my $itemtype = $itemtypes->next ) {
251 # FIXME This must be improved:
252 # - pass the iterator to the template
253 # - display the translated_description
255 value => $itemtype->itemtype,
256 description => $itemtype->description,
258 push @itemtypeloop, \%row;
261 itemtypeloop => \@itemtypeloop,
262 batch_id => $batch_id,
268 $template->param( idx => $idx );
271 output_html_with_http_headers $query, $cookie, $template->output;