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