Inital add of spine label perl files.
[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 use HTML::Template;
27 use C4::Context;
28 use C4::Search;
29 use C4::Auth;
30 use C4::Output;
31 use C4::Interface::CGI::Output;
32 use C4::Biblio;
33 use C4::Acquisition;
34 use C4::SearchMarc;
35 use C4::Koha;    # XXX subfield_is_koha_internal_p
36
37 # Creates a scrolling list with the associated default value.
38 # Using more than one scrolling list in a CGI assigns the same default value to all the
39 # scrolling lists on the page !?!? That's why this function was written.
40
41 my $query = new CGI;
42 my $type  = $query->param('type');
43 my $op    = $query->param('op');
44 my $dbh   = C4::Context->dbh;
45
46 my $startfrom = $query->param('startfrom');
47 $startfrom = 0 if ( !defined $startfrom );
48 my ( $template, $loggedinuser, $cookie );
49 my $resultsperpage;
50
51 if ( $op eq "do_search" ) {
52     my @marclist  = $query->param('marclist');
53     my @and_or    = $query->param('and_or');
54     my @excluding = $query->param('excluding');
55     my @operator  = $query->param('operator');
56     my @value     = $query->param('value');
57
58     $resultsperpage = $query->param('resultsperpage');
59     $resultsperpage = 19 if ( !defined $resultsperpage );
60     my $orderby = $query->param('orderby');
61
62     # builds tag and subfield arrays
63     my @tags;
64
65     foreach my $marc (@marclist) {
66         if ($marc) {
67             my ( $tag, $subfield ) =
68               MARCfind_marc_from_kohafield( $dbh, $marc );
69             if ($tag) {
70                 push @tags, $dbh->quote("$tag$subfield");
71             }
72             else {
73                 push @tags, $dbh->quote( substr( $marc, 0, 4 ) );
74             }
75         }
76         else {
77             push @tags, "";
78         }
79     }
80     my ( $results, $total ) =
81       catalogsearch( $dbh, \@tags, \@and_or, \@excluding, \@operator, \@value,
82         $startfrom * $resultsperpage,
83         $resultsperpage, $orderby );
84
85     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
86         {
87             template_name   => "barcodes/result.tmpl",
88             query           => $query,
89             type            => "intranet",
90             authnotrequired => 0,
91             flagsrequired   => { borrowers => 1 },
92             flagsrequired   => { catalogue => 1 },
93             debug           => 1,
94         }
95     );
96
97     # multi page display gestion
98     my $displaynext = 0;
99     my $displayprev = $startfrom;
100     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
101         $displaynext = 1;
102     }
103
104     my @field_data = ();
105
106     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
107         push @field_data, { term => "marclist",  val => $marclist[$i] };
108         push @field_data, { term => "and_or",    val => $and_or[$i] };
109         push @field_data, { term => "excluding", val => $excluding[$i] };
110         push @field_data, { term => "operator",  val => $operator[$i] };
111         push @field_data, { term => "value",     val => $value[$i] };
112     }
113
114     my @numbers = ();
115
116     if ( $total > $resultsperpage ) {
117         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
118             if ( $i < 16 ) {
119                 my $highlight = 0;
120                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
121                 push @numbers,
122                   {
123                     number     => $i,
124                     highlight  => $highlight,
125                     searchdata => \@field_data,
126                     startfrom  => ( $i - 1 )
127                   };
128             }
129         }
130     }
131
132     my $from = $startfrom * $resultsperpage + 1;
133     my $to;
134
135     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
136         $to = $total;
137     }
138     else {
139         $to = ( ( $startfrom + 1 ) * $resultsperpage );
140     }
141
142     # this gets the results of the search (which are bibs)
143     # and then does a lookup on all items that exist for that bib
144     # then pushes the items onto a new array, as we really want the
145     # items attached to the bibs not thew bibs themselves
146
147     my @results2;
148     my $i;
149     for ( $i = 0 ; $i <= ( $total - 1 ) ; $i++ )
150     {    #total-1 coz the array starts at 0
151             #warn $i;
152             #warn Dumper $results->[$i]{'bibid'};
153         my $type         = 'intra';
154         my @item_results = &ItemInfo( 0, $results->[$i]{'bibid'}, $type );
155
156         foreach my $item (@item_results) {
157
158             #warn Dumper $item;
159             push @results2, $item;
160         }
161
162     }
163
164     $template->param(
165         result         => \@results2,
166         startfrom      => $startfrom,
167         displaynext    => $displaynext,
168         displayprev    => $displayprev,
169         resultsperpage => $resultsperpage,
170         startfromnext  => $startfrom + 1,
171         startfromprev  => $startfrom - 1,
172         searchdata     => \@field_data,
173         total          => $total,
174         from           => $from,
175         to             => $to,
176         numbers        => \@numbers,
177     );
178 }
179 else {
180     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
181         {
182             template_name   => "barcodes/search.tmpl",
183             query           => $query,
184             type            => "intranet",
185             authnotrequired => 0,
186             flagsrequired   => { catalogue => 1 },
187             debug           => 1,
188         }
189     );
190     my $sth =
191       $dbh->prepare(
192         "Select itemtype,description from itemtypes order by description");
193     $sth->execute;
194     my @itemtype;
195     my %itemtypes;
196     push @itemtype, "";
197     $itemtypes{''} = "";
198     while ( my ( $value, $lib ) = $sth->fetchrow_array ) {
199         push @itemtype, $value;
200         $itemtypes{$value} = $lib;
201     }
202
203     my $CGIitemtype = CGI::scrolling_list(
204         -name     => 'value',
205         -values   => \@itemtype,
206         -labels   => \%itemtypes,
207         -size     => 1,
208         -multiple => 0
209     );
210     $sth->finish;
211
212     $template->param( CGIitemtype => $CGIitemtype, );
213 }
214
215 # Print the page
216 $template->param(
217     intranetcolorstylesheet =>
218       C4::Context->preference("intranetcolorstylesheet"),
219     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
220     IntranetNav        => C4::Context->preference("IntranetNav"),
221 );
222 output_html_with_http_headers $query, $cookie, $template->output;
223
224 # Local Variables:
225 # tab-width: 4
226 # End: