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