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