rel_3_0 moved to HEAD
[koha.git] / barcodes / label-item-search.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 require Exporter;
23 use CGI;
24 use C4::Koha;
25 use C4::Auth;
26
27 use C4::Context;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Interface::CGI::Output;
31 use C4::Biblio;
32 use C4::Acquisition;
33 use C4::Koha;    # XXX subfield_is_koha_internal_p
34
35 # Creates a scrolling list with the associated default value.
36 # Using more than one scrolling list in a CGI assigns the same default value to all the
37 # scrolling lists on the page !?!? That's why this function was written.
38
39 my $query = new CGI;
40 my $type  = $query->param('type');
41 my $op    = $query->param('op');
42 my $dbh   = C4::Context->dbh;
43
44 my $startfrom = $query->param('startfrom');
45 $startfrom = 0 if ( !defined $startfrom );
46 my ( $template, $loggedinuser, $cookie );
47 my $resultsperpage;
48
49 if ( $op eq "do_search" ) {
50     my @marclist  = $query->param('marclist');
51     my @and_or    = $query->param('and_or');
52     my @excluding = $query->param('excluding');
53     my @operator  = $query->param('operator');
54     my @value     = $query->param('value');
55
56     $resultsperpage = $query->param('resultsperpage');
57     $resultsperpage = 19 if ( !defined $resultsperpage );
58     my $orderby = $query->param('orderby');
59
60     # builds tag and subfield arrays
61     my @tags;
62
63     foreach my $marc (@marclist) {
64         if ($marc) {
65             my ( $tag, $subfield ) =
66               MARCfind_marc_from_kohafield( $dbh, $marc );
67             if ($tag) {
68                 push @tags, $dbh->quote("$tag$subfield");
69             }
70             else {
71                 push @tags, $dbh->quote( substr( $marc, 0, 4 ) );
72             }
73         }
74         else {
75             push @tags, "";
76         }
77     }
78     my ( $results, $total ) =
79       catalogsearch( $dbh, \@tags, \@and_or, \@excluding, \@operator, \@value,
80         $startfrom * $resultsperpage,
81         $resultsperpage, $orderby );
82
83     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
84         {
85             template_name   => "barcodes/result.tmpl",
86             query           => $query,
87             type            => "intranet",
88             authnotrequired => 0,
89             flagsrequired   => { tools => 1 },
90             debug           => 1,
91         }
92     );
93
94     # multi page display gestion
95     my $displaynext = 0;
96     my $displayprev = $startfrom;
97     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
98         $displaynext = 1;
99     }
100
101     my @field_data = ();
102
103     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
104         push @field_data, { term => "marclist",  val => $marclist[$i] };
105         push @field_data, { term => "and_or",    val => $and_or[$i] };
106         push @field_data, { term => "excluding", val => $excluding[$i] };
107         push @field_data, { term => "operator",  val => $operator[$i] };
108         push @field_data, { term => "value",     val => $value[$i] };
109     }
110
111     my @numbers = ();
112
113     if ( $total > $resultsperpage ) {
114         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
115             if ( $i < 16 ) {
116                 my $highlight = 0;
117                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
118                 push @numbers,
119                   {
120                     number     => $i,
121                     highlight  => $highlight,
122                     searchdata => \@field_data,
123                     startfrom  => ( $i - 1 )
124                   };
125             }
126         }
127     }
128
129     my $from = $startfrom * $resultsperpage + 1;
130     my $to;
131
132     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
133         $to = $total;
134     }
135     else {
136         $to = ( ( $startfrom + 1 ) * $resultsperpage );
137     }
138
139     # this gets the results of the search (which are bibs)
140     # and then does a lookup on all items that exist for that bib
141     # then pushes the items onto a new array, as we really want the
142     # items attached to the bibs not thew bibs themselves
143
144     my @results2;
145     my $i;
146     for ( $i = 0 ; $i <= ( $total - 1 ) ; $i++ )
147     {    #total-1 coz the array starts at 0
148             #warn $i;
149
150         my $type         = 'intra';
151         my @item_results =
152           &GetItemsInfo( $results->[$i]{'biblionumber'}, $type );
153
154         foreach my $item (@item_results) {
155
156             #warn Dumper $item;
157             push @results2, $item;
158         }
159
160     }
161
162     $template->param(
163         result         => \@results2,
164         startfrom      => $startfrom,
165         displaynext    => $displaynext,
166         displayprev    => $displayprev,
167         resultsperpage => $resultsperpage,
168         startfromnext  => $startfrom + 1,
169         startfromprev  => $startfrom - 1,
170         searchdata     => \@field_data,
171         total          => $total,
172         from           => $from,
173         to             => $to,
174         numbers        => \@numbers,
175     );
176 }
177 else {
178     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
179         {
180             template_name   => "barcodes/search.tmpl",
181             query           => $query,
182             type            => "intranet",
183             authnotrequired => 0,
184             flagsrequired   => { tools => 1 },
185             debug           => 1,
186         }
187     );
188     my $sth =
189       $dbh->prepare(
190         "Select itemtype,description from itemtypes order by description");
191     $sth->execute;
192     my @itemtype;
193     my %itemtypes;
194     push @itemtype, "";
195     $itemtypes{''} = "";
196     while ( my ( $value, $lib ) = $sth->fetchrow_array ) {
197         push @itemtype, $value;
198         $itemtypes{$value} = $lib;
199     }
200
201     my $CGIitemtype = CGI::scrolling_list(
202         -name     => 'value',
203         -values   => \@itemtype,
204         -labels   => \%itemtypes,
205         -size     => 1,
206         -multiple => 0
207     );
208     $sth->finish;
209
210     $template->param( CGIitemtype => $CGIitemtype, );
211 }
212
213 # Print the page
214 $template->param(
215     intranetcolorstylesheet =>
216       C4::Context->preference("intranetcolorstylesheet"),
217     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
218     IntranetNav        => C4::Context->preference("IntranetNav"),
219 );
220 output_html_with_http_headers $query, $cookie, $template->output;
221
222 # Local Variables:
223 # tab-width: 4
224 # End: