Bug 28572: Remove C4::Debug
[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 Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use List::Util qw( max min );
24 use POSIX qw(ceil);
25
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Context;
29 use C4::Search qw(SimpleSearch);
30 use C4::Biblio qw(TransformMarcToKoha);
31 use C4::Creators::Lib qw(html_table);
32
33 use Koha::Logger;
34 use Koha::DateUtils;
35 use Koha::Items;
36 use Koha::ItemTypes;
37 use Koha::SearchEngine::Search;
38
39 my $query = CGI->new;
40
41 my $type      = $query->param('type');
42 my $op        = $query->param('op') || '';
43 my $batch_id  = $query->param('batch_id');
44 my $ccl_query = $query->param('ccl_query');
45 my $startfrom = $query->param('startfrom') || 1;
46 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
47 my (
48     $total_hits,  $total,  $error,
49     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
50 );
51 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
52 my $show_results = 0;
53 my $display_columns = [ {_add                   => {label => "Add Item", link_field => 1}},
54                         {_item_call_number      => {label => "Call Number", link_field => 0}},
55                         {_date_accessioned      => {label => "Accession Date", link_field => 0}},
56                         {_barcode               => {label => "Barcode", link_field => 0}},
57                         {select                 => {label => "Select", value => "_item_number"}},
58                       ];
59
60 if ( $op eq "do_search" ) {
61     $idx         = $query->param('idx');
62     $ccl_textbox = $query->param('ccl_textbox');
63     if ( $ccl_textbox && $idx ) {
64         $ccl_query = "$idx:$ccl_textbox";
65     }
66
67     $datefrom = $query->param('datefrom');
68     $dateto   = $query->param('dateto');
69
70     if ($datefrom) {
71         $datefrom = eval { dt_from_string ( $datefrom ) };
72         if ($datefrom) {
73             $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
74             $ccl_query .= ' and ' if $ccl_textbox;
75             $ccl_query .= "acqdate,ge,st-date-normalized=" . $datefrom;
76         }
77     }
78
79     if ($dateto) {
80         $dateto = eval { dt_from_string ( $dateto ) };
81         if ($dateto) {
82            $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
83             $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
84             $ccl_query .= "acqdate,le,st-date-normalized=" . $dateto;
85         }
86     }
87
88     my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
89     my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
90     ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($ccl_query, $offset, $resultsperpage);
91
92     if (!defined $error && @{$marcresults} ) {
93         $show_results = @{$marcresults};
94     }
95     else {
96         Koha::Logger->get->warn("ERROR label-item-search: no results from simple_search_compat");
97
98         # leave $show_results undef
99     }
100 }
101
102 if ($show_results) {
103     my $hits = $show_results;
104     my @results_set = ();
105     my @items =();
106     # This code needs to be refactored using these subs...
107     #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
108     for ( my $i = 0 ; $i < $hits ; $i++ ) {
109         my @row_data= ();
110         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
111         my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
112         #DEBUG Notes: Transform it to Koha form...
113         my $biblio = TransformMarcToKoha( $marcrecord, '' );
114         #DEBUG Notes: Stuff the bib into @biblio_data...
115         push (@results_set, $biblio);
116         my $biblionumber = $biblio->{'biblionumber'};
117         #DEBUG Notes: Grab the item numbers associated with this MARC record...
118         my $items = Koha::Items->search({ biblionumber => $biblionumber }, { order_by => { -desc => 'itemnumber' }});
119         #DEBUG Notes: Retrieve the item data for each number...
120         while ( my $item = $items->next ) {
121             #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
122             if ( $item->biblionumber eq $results_set[$i]->{'biblionumber'} ) {
123                 my $item_data;
124                 $item_data->{'_item_number'}      = $item->itemnumber;
125                 $item_data->{'_item_call_number'} = ( $item->itemcallnumber || 'NA' );
126                 $item_data->{'_date_accessioned'} = $item->dateaccessioned;
127                 $item_data->{'_barcode'}          = ( $item->barcode || 'NA' );
128                 $item_data->{'_add'}              = $item->itemnumber;
129                 push @row_data, $item_data;
130             }
131             $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
132         }
133     }
134
135     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
136         {
137             template_name   => "labels/result.tt",
138             query           => $query,
139             type            => "intranet",
140             flagsrequired   => { borrowers => 'edit_borrowers' },
141             flagsrequired   => { catalogue => 1 },
142         }
143     );
144
145     # build page nav stuff.
146     my @numbers;
147     $total = $total_hits;
148
149     my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
150         $displayprev );
151
152     if ( $total > $resultsperpage ) {
153         my $num_of_pages = ceil( $total / $resultsperpage + 1 );
154         for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
155             my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
156             push @numbers,
157               {
158                 number    => $page,
159                 startfrom => $startfrm
160               };
161         }
162
163         $from          = $startfrom;
164         $startfromprev = $startfrom - $resultsperpage;
165         $startfromnext = $startfrom + $resultsperpage;
166
167         $to =
168             $startfrom + $resultsperpage > $total
169           ? $total
170           : $startfrom + $resultsperpage - 1;
171
172         # multi page display
173         $displaynext = 0;
174         $displayprev = $startfrom > 1 ? $startfrom : 0;
175
176         $displaynext = 1 if $to < $total_hits;
177
178     }
179     else {
180         $from = 1;
181         $to = $total_hits;
182         $displayprev = 0;
183         $displaynext = 0;
184     }
185
186     $template->param(
187         total          => $total_hits,
188         from           => $from,
189         to             => $to,
190         startfromnext  => $startfromnext,
191         startfromprev  => $startfromprev,
192         startfrom      => $startfrom,
193         displaynext    => $displaynext,
194         displayprev    => $displayprev,
195         resultsperpage => $resultsperpage,
196         numbers        => \@numbers,
197     );
198
199     $template->param(
200         results   => ($show_results ? 1 : 0),
201         result_set=> \@results_set,
202         batch_id  => $batch_id,
203         type      => $type,
204         ccl_query => $ccl_query,
205     );
206 }
207
208 #
209 #   search section
210 #
211
212 else {
213     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
214         {
215             template_name   => "labels/search.tt",
216             query           => $query,
217             type            => "intranet",
218             flagsrequired   => { catalogue => 1 },
219         }
220     );
221     my $itemtypes = Koha::ItemTypes->search;
222     my @itemtypeloop;
223     while ( my $itemtype = $itemtypes->next ) {
224         # FIXME This must be improved:
225         # - pass the iterator to the template
226         # - display the translated_description
227         my %row = (
228             value       => $itemtype->itemtype,
229             description => $itemtype->description,
230         );
231         push @itemtypeloop, \%row;
232     }
233     $template->param(
234         itemtypeloop => \@itemtypeloop,
235         batch_id     => $batch_id,
236         type         => $type,
237     );
238
239 }
240
241 $template->param( idx => $idx );
242
243 # Print the page
244 output_html_with_http_headers $query, $cookie, $template->output;