[18/40] Work on label item search.
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use HTML::Template::Pro;
25 use List::Util qw( max min );
26 use POSIX;
27 use Data::Dumper;
28
29 use C4::Auth;
30 use C4::Context;
31 use C4::Search;
32 use C4::Auth;
33 use C4::Output;
34 use C4::Biblio;
35 use C4::Items;
36 use C4::Acquisition;
37 use C4::Search;
38 use C4::Dates;
39 use C4::Koha;    # XXX subfield_is_koha_internal_p
40 use C4::Labels::Lib qw(html_table);
41 use C4::Debug;
42
43 BEGIN {
44     $debug = $debug || $cgi_debug;
45     if ($debug) {
46         require Data::Dumper;
47         import Data::Dumper qw(Dumper);
48     }
49 }
50
51 # Creates a scrolling list with the associated default value.
52 # Using more than one scrolling list in a CGI assigns the same default value to all the
53 # scrolling lists on the page !?!? That's why this function was written.
54
55 my $query = new CGI;
56
57 my $type      = $query->param('type');
58 my $op        = $query->param('op') || '';
59 my $batch_id  = $query->param('batch_id');
60 my $ccl_query = $query->param('ccl_query') || '';
61
62 my $dbh = C4::Context->dbh;
63
64 my $startfrom = $query->param('startfrom') || 1;
65 my ( $template, $loggedinuser, $cookie );
66 my (
67     $total_hits,  $orderby, $results,  $total,  $error,
68     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
69 );
70
71 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
72 my $show_results = 0;
73
74 my $display_columns = [ {_add                   => {label => "Add Item", link_field => 1}},
75                         {_item_call_number      => {label => "Call Number", link_field => 0}},
76                         {_date_accessioned      => {label => "Accession Date", link_field => 0}},
77                         {_barcode               => {label => "Barcode", link_field => 0}},
78                         {select                 => {label => "Select", value => "_item_number"}},
79                       ];
80
81 if ( $op eq "do_search" ) {
82     my @params = $query->param();
83     $idx         = $query->param('idx');
84     $ccl_textbox = $query->param('ccl_textbox');
85     if ( $ccl_textbox && $idx ) {
86         $ccl_query = "$idx=$ccl_textbox";
87     }
88
89     $datefrom = $query->param('datefrom');
90     $dateto   = $query->param('dateto');
91
92     if ($datefrom) {
93         $datefrom = C4::Dates->new($datefrom);
94         $ccl_query .= ' and ' if $ccl_textbox;
95         $ccl_query .=
96           "acqdate,st-date-normalized,ge=" . $datefrom->output("iso");
97     }
98
99     if ($dateto) {
100         $dateto = C4::Dates->new($dateto);
101         $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
102         $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto->output("iso");
103     }
104
105     my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
106     ( $error, $marcresults, $total_hits ) =
107       SimpleSearch( $ccl_query, $offset, $resultsperpage );
108
109     if (scalar($marcresults) > 0) {
110         $show_results = scalar @$marcresults;
111     }
112     else {
113         $debug and warn "ERROR label-item-search: no results from SimpleSearch";
114
115         # leave $show_results undef
116     }
117 }
118
119 if ($show_results) {
120     my $hits = $show_results;
121     my @results_set = ();
122     my @items =();
123     # This code needs to be refactored using these subs...
124     #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
125     #my $dat = &GetBiblioData( $biblio->{biblionumber} );
126     for ( my $i = 0 ; $i < $hits ; $i++ ) {
127         my @row_data= ();
128         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
129         my $marcrecord = MARC::File::USMARC::decode( $marcresults->[$i] );
130         #DEBUG Notes: Transform it to Koha form...
131         my $biblio = TransformMarcToKoha( C4::Context->dbh, $marcrecord, '' );
132         #DEBUG Notes: Stuff the bib into @biblio_data...
133         push (@results_set, $biblio);
134         my $biblionumber = $biblio->{'biblionumber'};
135         #DEBUG Notes: Grab the item numbers associated with this MARC record...
136         my $itemnums = get_itemnumbers_of($biblionumber);
137         #DEBUG Notes: Retrieve the item data for each number...
138         if (my $iii = $itemnums->{$biblionumber}) {
139             my $item_results = GetItemInfosOf(@$iii);
140             foreach my $item ( keys %$item_results ) {
141                 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
142                 if ($item_results->{$item}->{'biblionumber'} eq $results_set[$i]->{'biblionumber'}) {
143                     my $item_data->{'_item_number'} = $item_results->{$item}->{'itemnumber'};
144                     $item_data->{'_item_call_number'} = ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA');
145                     $item_data->{'_date_accessioned'} = $item_results->{$item}->{'dateaccessioned'};
146                     $item_data->{'_barcode'} = ( $item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA');
147                     $item_data->{'_add'} = $item_results->{$item}->{'itemnumber'};
148                     unshift (@row_data, $item_data);    # item numbers are given to us in descending order by get_itemnumbers_of()...
149                 }
150             }
151             $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
152         }
153         else {
154             # FIXME: Some error trapping code needed
155             syslog("LOG_ERR", "labels/label-item-search.pl : No item numbers retrieved for biblio number: %s", $biblionumber);
156         }
157     }
158
159     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
160         {
161             template_name   => "labels/result.tmpl",
162             query           => $query,
163             type            => "intranet",
164             authnotrequired => 0,
165             flagsrequired   => { borrowers => 1 },
166             flagsrequired   => { catalogue => 1 },
167             debug           => 1,
168         }
169     );
170
171     # build page nav stuff.
172     my ( @field_data, @numbers );
173     $total = $total_hits;
174
175     my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
176         $displayprev );
177
178     if ( $total > $resultsperpage ) {
179         my $num_of_pages = ceil( $total / $resultsperpage + 1 );
180         for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
181             my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
182             push @numbers,
183               {
184                 number    => $page,
185                 startfrom => $startfrm
186               };
187         }
188
189         $from          = $startfrom;
190         $startfromprev = $startfrom - $resultsperpage;
191         $startfromnext = $startfrom + $resultsperpage;
192
193         $to =
194             $startfrom + $resultsperpage > $total
195           ? $total
196           : $startfrom + $resultsperpage - 1;
197
198         # multi page display
199         $displaynext = 0;
200         $displayprev = $startfrom > 1 ? $startfrom : 0;
201
202         $displaynext = 1 if $to < $total_hits;
203
204     }
205     else {
206         $displayprev = 0;
207         $displaynext = 0;
208     }
209
210     $template->param(
211         total          => $total_hits,
212         from           => $from,
213         to             => $to,
214         startfromnext  => $startfromnext,
215         startfromprev  => $startfromprev,
216         startfrom      => $startfrom,
217         displaynext    => $displaynext,
218         displayprev    => $displayprev,
219         resultsperpage => $resultsperpage,
220         numbers        => \@numbers,
221     );
222
223     $template->param(
224         results   => ($show_results ? 1 : 0),
225         result_set=> \@results_set,
226         batch_id  => $batch_id,
227         type      => $type,
228         idx       => $idx,
229         ccl_query => $ccl_query,
230     );
231 }
232
233 #
234 #   search section
235 #
236
237 else {
238     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
239         {
240             template_name   => "labels/search.tmpl",
241             query           => $query,
242             type            => "intranet",
243             authnotrequired => 0,
244             flagsrequired   => { catalogue => 1 },
245             debug           => 1,
246         }
247     );
248     my $itemtypes = GetItemTypes;
249     my @itemtypeloop;
250     foreach my $thisitemtype ( keys %$itemtypes ) {
251         my %row = (
252             value       => $thisitemtype,
253             description => $itemtypes->{$thisitemtype}->{'description'},
254         );
255         push @itemtypeloop, \%row;
256     }
257     $template->param(
258         itemtypeloop => \@itemtypeloop,
259         batch_id     => $batch_id,
260         type         => $type,
261     );
262
263 }
264
265 # Print the page
266 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), );
267 output_html_with_http_headers $query, $cookie, $template->output;