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>.
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
28 use C4::Search qw( new_record_from_zebra );
29 use C4::Biblio qw( TransformMarcToKoha );
30 use C4::Creators::Lib qw( html_table );
33 use Koha::DateUtils qw( dt_from_string output_pref );
36 use Koha::SearchEngine::Search;
40 my $type = $query->param('type');
41 my $op = $query->param('op') || '';
42 my $batch_id = $query->param('batch_id');
43 my $ccl_query = $query->param('ccl_query');
44 my $startfrom = $query->param('startfrom') || 1;
45 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
47 $total_hits, $total, $error,
48 $marcresults, $idx, $datefrom, $dateto, $ccl_textbox
50 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
52 my $display_columns = [ {_add => {label => "Add Item", link_field => 1}},
53 {_item_call_number => {label => "Call Number", link_field => 0}},
54 {_date_accessioned => {label => "Accession Date", link_field => 0}},
55 {_barcode => {label => "Barcode", link_field => 0}},
56 {select => {label => "Select", value => "_item_number"}},
59 if ( $op eq "do_search" ) {
60 $idx = $query->param('idx');
61 $ccl_textbox = $query->param('ccl_textbox');
62 if ( $ccl_textbox && $idx ) {
63 $ccl_query = "$idx:$ccl_textbox";
66 $datefrom = $query->param('datefrom');
67 $dateto = $query->param('dateto');
70 $datefrom = eval { dt_from_string ( $datefrom ) };
72 $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
73 $ccl_query .= ' AND ' if $ccl_textbox;
74 $ccl_query .= "acqdate,ge,st-date-normalized=" . $datefrom;
79 $dateto = eval { dt_from_string ( $dateto ) };
81 $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
82 $ccl_query .= ' AND ' if ( $ccl_textbox || $datefrom );
83 $ccl_query .= "acqdate,le,st-date-normalized=" . $dateto;
87 my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
88 my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
89 ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($ccl_query, $offset, $resultsperpage);
91 if (!defined $error && @{$marcresults} ) {
92 $show_results = @{$marcresults};
95 Koha::Logger->get->warn("ERROR label-item-search: no results from simple_search_compat");
97 # leave $show_results undef
102 my $hits = $show_results;
103 my @results_set = ();
105 # This code needs to be refactored using these subs...
106 #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
107 for ( my $i = 0 ; $i < $hits ; $i++ ) {
109 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
110 my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
111 #DEBUG Notes: Transform it to Koha form...
112 my $biblio = TransformMarcToKoha({ record => $marcrecord });
113 #DEBUG Notes: Stuff the bib into @biblio_data...
114 push (@results_set, $biblio);
115 my $biblionumber = $biblio->{'biblionumber'};
116 #DEBUG Notes: Grab the item numbers associated with this MARC record...
117 my $items = Koha::Items->search({ biblionumber => $biblionumber }, { order_by => { -desc => 'itemnumber' }});
118 #DEBUG Notes: Retrieve the item data for each number...
119 while ( my $item = $items->next ) {
120 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
121 if ( $item->biblionumber eq $results_set[$i]->{'biblionumber'} ) {
123 $item_data->{'_item_number'} = $item->itemnumber;
124 $item_data->{'_item_call_number'} = ( $item->itemcallnumber || 'NA' );
125 $item_data->{'_date_accessioned'} = $item->dateaccessioned;
126 $item_data->{'_barcode'} = ( $item->barcode || 'NA' );
127 $item_data->{'_add'} = $item->itemnumber;
128 push @row_data, $item_data;
130 $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
134 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
136 template_name => "labels/result.tt",
139 flagsrequired => { borrowers => 'edit_borrowers' },
140 flagsrequired => { catalogue => 1 },
144 # build page nav stuff.
146 $total = $total_hits;
148 my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
151 if ( $total > $resultsperpage ) {
152 my $num_of_pages = ceil( $total / $resultsperpage + 1 );
153 for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
154 my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
158 startfrom => $startfrm
163 $startfromprev = $startfrom - $resultsperpage;
164 $startfromnext = $startfrom + $resultsperpage;
167 $startfrom + $resultsperpage > $total
169 : $startfrom + $resultsperpage - 1;
173 $displayprev = $startfrom > 1 ? $startfrom : 0;
175 $displaynext = 1 if $to < $total_hits;
186 total => $total_hits,
189 startfromnext => $startfromnext,
190 startfromprev => $startfromprev,
191 startfrom => $startfrom,
192 displaynext => $displaynext,
193 displayprev => $displayprev,
194 resultsperpage => $resultsperpage,
195 numbers => \@numbers,
199 results => ($show_results ? 1 : 0),
200 result_set=> \@results_set,
201 batch_id => $batch_id,
203 ccl_query => $ccl_query,
212 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
214 template_name => "labels/search.tt",
217 flagsrequired => { catalogue => 1 },
220 my $itemtypes = Koha::ItemTypes->search;
222 while ( my $itemtype = $itemtypes->next ) {
223 # FIXME This must be improved:
224 # - pass the iterator to the template
225 # - display the translated_description
227 value => $itemtype->itemtype,
228 description => $itemtype->description,
230 push @itemtypeloop, \%row;
233 itemtypeloop => \@itemtypeloop,
234 batch_id => $batch_id,
240 $template->param( idx => $idx );
243 output_html_with_http_headers $query, $cookie, $template->output;