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