3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
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
22 use vars qw($debug $cgi_debug);
25 use HTML::Template::Pro;
26 use List::Util qw( max min );
27 use Sys::Syslog qw(syslog);
30 use C4::Auth qw(get_template_and_user);
31 use C4::Output qw(output_html_with_http_headers);
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);
42 $debug = $debug || $cgi_debug;
45 import Data::Dumper qw(Dumper);
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);
58 $total_hits, $orderby, $results, $total, $error,
59 $marcresults, $idx, $datefrom, $dateto, $ccl_textbox
61 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
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"}},
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";
77 $datefrom = $query->param('datefrom');
78 $dateto = $query->param('dateto');
81 $datefrom = C4::Dates->new($datefrom);
82 $ccl_query .= ' and ' if $ccl_textbox;
84 "acqdate,st-date-normalized,ge=" . $datefrom->output("iso");
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");
93 my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
94 ( $error, $marcresults, $total_hits ) =
95 SimpleSearch( $ccl_query, $offset, $resultsperpage );
97 if (scalar($marcresults) > 0) {
98 $show_results = scalar @$marcresults;
101 $debug and warn "ERROR label-item-search: no results from SimpleSearch";
103 # leave $show_results undef
108 my $hits = $show_results;
109 my @results_set = ();
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++ ) {
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()...
139 $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
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);
147 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
149 template_name => "labels/result.tmpl",
152 authnotrequired => 0,
153 flagsrequired => { borrowers => 1 },
154 flagsrequired => { catalogue => 1 },
159 # build page nav stuff.
160 my ( @field_data, @numbers );
161 $total = $total_hits;
163 my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
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;
173 startfrom => $startfrm
178 $startfromprev = $startfrom - $resultsperpage;
179 $startfromnext = $startfrom + $resultsperpage;
182 $startfrom + $resultsperpage > $total
184 : $startfrom + $resultsperpage - 1;
188 $displayprev = $startfrom > 1 ? $startfrom : 0;
190 $displaynext = 1 if $to < $total_hits;
199 total => $total_hits,
202 startfromnext => $startfromnext,
203 startfromprev => $startfromprev,
204 startfrom => $startfrom,
205 displaynext => $displaynext,
206 displayprev => $displayprev,
207 resultsperpage => $resultsperpage,
208 numbers => \@numbers,
212 results => ($show_results ? 1 : 0),
213 result_set=> \@results_set,
214 batch_id => $batch_id,
217 ccl_query => $ccl_query,
226 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
228 template_name => "labels/search.tmpl",
231 authnotrequired => 0,
232 flagsrequired => { catalogue => 1 },
236 my $itemtypes = GetItemTypes;
238 foreach my $thisitemtype ( keys %$itemtypes ) {
240 value => $thisitemtype,
241 description => $itemtypes->{$thisitemtype}->{'description'},
243 push @itemtypeloop, \%row;
246 itemtypeloop => \@itemtypeloop,
247 batch_id => $batch_id,
254 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), );
255 output_html_with_http_headers $query, $cookie, $template->output;