Bug 12478: make things using SimpleSearch use the new version
[koha.git] / labels / label-item-search.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22 use vars qw($debug $cgi_debug);
23
24 use CGI qw ( -utf8 );
25 use List::Util qw( max min );
26 use POSIX qw(ceil);
27
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::Dates;
33 use C4::Biblio qw(TransformMarcToKoha);
34 use C4::Items qw(GetItemInfosOf get_itemnumbers_of);
35 use C4::Koha qw(GetItemTypes);
36 use C4::Creators::Lib qw(html_table);
37 use C4::Debug;
38 use Koha::DateUtils;
39
40 use Koha::SearchEngine::Search;
41
42 BEGIN {
43     $debug = $debug || $cgi_debug;
44     if ($debug) {
45         require Data::Dumper;
46         import Data::Dumper qw(Dumper);
47     }
48 }
49
50 my $query = new CGI;
51
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);
58 my (
59     $total_hits,  $total,  $error,
60     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
61 );
62 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
63 my $show_results = 0;
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"}},
69                       ];
70
71 if ( $op eq "do_search" ) {
72     my $QParser;
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";
78     }
79
80     $datefrom = $query->param('datefrom');
81     $dateto   = $query->param('dateto');
82
83     if ($datefrom) {
84         $datefrom = eval { dt_from_string ( $datefrom ) };
85         if ($datefrom) {
86             $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
87             if ($QParser) {
88                 $ccl_query .= ' && ' if $ccl_textbox;
89                 $ccl_query .=
90                     "acqdate(" . $datefrom . '-)';
91             } else {
92                 $ccl_query .= ' and ' if $ccl_textbox;
93                 $ccl_query .= "acqdate,st-date-normalized,ge=" . $datefrom;
94             }
95         }
96     }
97
98     if ($dateto) {
99         $dateto = eval { dt_from_string ( $dateto ) };
100         if ($dateto) {
101            $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
102             if ($QParser) {
103                 $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
104                 $ccl_query .= "acqdate(-" . $dateto . ')';
105             } else {
106                 $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
107                 $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto;
108             }
109         }
110     }
111
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);
115
116     if (!defined $error && @{$marcresults} ) {
117         $show_results = @{$marcresults};
118     }
119     else {
120         $debug and warn "ERROR label-item-search: no results from simple_search_compat";
121
122         # leave $show_results undef
123     }
124 }
125
126 if ($show_results) {
127     my $hits = $show_results;
128     my @results_set = ();
129     my @items =();
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++ ) {
134         my @row_data= ();
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 $itemnums = get_itemnumbers_of($biblionumber);
144         #DEBUG Notes: Retrieve the item data for each number...
145         if (my $iii = $itemnums->{$biblionumber}) {
146             my $item_results = GetItemInfosOf(@$iii);
147             foreach my $item ( keys %$item_results ) {
148                 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
149                 if ($item_results->{$item}->{'biblionumber'} eq $results_set[$i]->{'biblionumber'}) {
150                     my $item_data;
151                     $item_data->{'_item_number'} = $item_results->{$item}->{'itemnumber'};
152                     $item_data->{'_item_call_number'} = ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA');
153                     $item_data->{'_date_accessioned'} = $item_results->{$item}->{'dateaccessioned'};
154                     $item_data->{'_barcode'} = ( $item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA');
155                     $item_data->{'_add'} = $item_results->{$item}->{'itemnumber'};
156                     unshift (@row_data, $item_data);    # item numbers are given to us in descending order by get_itemnumbers_of()...
157                 }
158             }
159             $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
160         }
161         else {
162             # FIXME: Some error trapping code needed
163             warn sprintf('No item numbers retrieved for biblio number: %s', $biblionumber);
164         }
165     }
166
167     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
168         {
169             template_name   => "labels/result.tt",
170             query           => $query,
171             type            => "intranet",
172             authnotrequired => 0,
173             flagsrequired   => { borrowers => 1 },
174             flagsrequired   => { catalogue => 1 },
175             debug           => 1,
176         }
177     );
178
179     # build page nav stuff.
180     my @numbers;
181     $total = $total_hits;
182
183     my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
184         $displayprev );
185
186     if ( $total > $resultsperpage ) {
187         my $num_of_pages = ceil( $total / $resultsperpage + 1 );
188         for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
189             my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
190             push @numbers,
191               {
192                 number    => $page,
193                 startfrom => $startfrm
194               };
195         }
196
197         $from          = $startfrom;
198         $startfromprev = $startfrom - $resultsperpage;
199         $startfromnext = $startfrom + $resultsperpage;
200
201         $to =
202             $startfrom + $resultsperpage > $total
203           ? $total
204           : $startfrom + $resultsperpage - 1;
205
206         # multi page display
207         $displaynext = 0;
208         $displayprev = $startfrom > 1 ? $startfrom : 0;
209
210         $displaynext = 1 if $to < $total_hits;
211
212     }
213     else {
214         $displayprev = 0;
215         $displaynext = 0;
216     }
217
218     $template->param(
219         total          => $total_hits,
220         from           => $from,
221         to             => $to,
222         startfromnext  => $startfromnext,
223         startfromprev  => $startfromprev,
224         startfrom      => $startfrom,
225         displaynext    => $displaynext,
226         displayprev    => $displayprev,
227         resultsperpage => $resultsperpage,
228         numbers        => \@numbers,
229     );
230
231     $template->param(
232         results   => ($show_results ? 1 : 0),
233         result_set=> \@results_set,
234         batch_id  => $batch_id,
235         type      => $type,
236         ccl_query => $ccl_query,
237     );
238 }
239
240 #
241 #   search section
242 #
243
244 else {
245     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
246         {
247             template_name   => "labels/search.tt",
248             query           => $query,
249             type            => "intranet",
250             authnotrequired => 0,
251             flagsrequired   => { catalogue => 1 },
252             debug           => 1,
253         }
254     );
255     my $itemtypes = GetItemTypes;
256     my @itemtypeloop;
257     foreach my $thisitemtype ( keys %$itemtypes ) {
258         my %row = (
259             value       => $thisitemtype,
260             description => $itemtypes->{$thisitemtype}->{'description'},
261         );
262         push @itemtypeloop, \%row;
263     }
264     $template->param(
265         itemtypeloop => \@itemtypeloop,
266         batch_id     => $batch_id,
267         type         => $type,
268     );
269
270 }
271
272 $template->param( idx => $idx );
273
274 # Print the page
275 output_html_with_http_headers $query, $cookie, $template->output;