synch'ing head and rel_2_2 (from 2.2.5, including npl templates)
[koha.git] / bull / subscription-bib-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::Output;
29 use C4::Interface::CGI::Output;
30 use C4::SearchMarc;
31
32 my $query=new CGI;
33 my $type=$query->param('type');
34 my $op = $query->param('op');
35 my $dbh = C4::Context->dbh;
36
37 my $startfrom=$query->param('startfrom');
38 $startfrom=0 if(!defined $startfrom);
39 my ($template, $loggedinuser, $cookie);
40 my $resultsperpage;
41
42 if ($op eq "do_search") {
43         my @marclist = $query->param('marclist');
44         my @and_or = $query->param('and_or');
45         my @excluding = $query->param('excluding');
46         my @operator = $query->param('operator');
47         my @value = $query->param('value');
48
49         $resultsperpage= $query->param('resultsperpage');
50         $resultsperpage = 19 if(!defined $resultsperpage);
51         my $orderby = $query->param('orderby');
52
53         # builds tag and subfield arrays
54         my @tags;
55
56         foreach my $marc (@marclist) {
57                 if ($marc) {
58                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc);
59                         if ($tag) {
60                                 push @tags,$dbh->quote("$tag$subfield");
61                         } else {
62                                 push @tags, $dbh->quote(substr($marc,0,4));
63                         }
64                 } else {
65                         push @tags, "";
66                 }
67         }
68         my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
69                                                                                 \@excluding, \@operator, \@value,
70                                                                                 $startfrom*$resultsperpage, $resultsperpage,$orderby);
71
72         ($template, $loggedinuser, $cookie)
73                 = get_template_and_user({template_name => "bull/result.tmpl",
74                                 query => $query,
75                                 type => "intranet",
76                                 authnotrequired => 0,
77                                 flagsrequired => {borrowers => 1},
78                                 flagsrequired => {catalogue => 1},
79                                 debug => 1,
80                                 });
81
82         # multi page display gestion
83         my $displaynext=0;
84         my $displayprev=$startfrom;
85         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
86                 $displaynext = 1;
87         }
88
89         my @field_data = ();
90
91
92         for(my $i = 0 ; $i <= $#marclist ; $i++)
93         {
94                 push @field_data, { term => "marclist", val=>$marclist[$i] };
95                 push @field_data, { term => "and_or", val=>$and_or[$i] };
96                 push @field_data, { term => "excluding", val=>$excluding[$i] };
97                 push @field_data, { term => "operator", val=>$operator[$i] };
98                 push @field_data, { term => "value", val=>$value[$i] };
99         }
100
101         my @numbers = ();
102
103         if ($total>$resultsperpage)
104         {
105                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
106                 {
107                         if ($i<16)
108                         {
109                         my $highlight=0;
110                         ($startfrom==($i-1)) && ($highlight=1);
111                         push @numbers, { number => $i,
112                                         highlight => $highlight ,
113                                         searchdata=> \@field_data,
114                                         startfrom => ($i-1)};
115                         }
116         }
117         }
118
119         my $from = $startfrom*$resultsperpage+1;
120         my $to;
121
122         if($total < (($startfrom+1)*$resultsperpage))
123         {
124                 $to = $total;
125         } else {
126                 $to = (($startfrom+1)*$resultsperpage);
127         }
128         $template->param(result => $results,
129                                                         startfrom=> $startfrom,
130                                                         displaynext=> $displaynext,
131                                                         displayprev=> $displayprev,
132                                                         resultsperpage => $resultsperpage,
133                                                         startfromnext => $startfrom+1,
134                                                         startfromprev => $startfrom-1,
135                                                         searchdata=>\@field_data,
136                                                         total=>$total,
137                                                         from=>$from,
138                                                         to=>$to,
139                                                         numbers=>\@numbers,
140                                                         );
141 } else {
142         ($template, $loggedinuser, $cookie)
143                 = get_template_and_user({template_name => "bull/search.tmpl",
144                                 query => $query,
145                                 type => "intranet",
146                                 authnotrequired => 0,
147                                 flagsrequired => {catalogue => 1},
148                                 debug => 1,
149                                 });
150         my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
151         $sth->execute;
152         my  @itemtype;
153         my %itemtypes;
154         push @itemtype, "";
155         $itemtypes{''} = "";
156         while (my ($value,$lib) = $sth->fetchrow_array) {
157                 push @itemtype, $value;
158                 $itemtypes{$value}=$lib;
159         }
160
161         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
162                                 -values   => \@itemtype,
163                                 -labels   => \%itemtypes,
164                                 -size     => 1,
165                                 -multiple => 0 );
166         $sth->finish;
167
168         $template->param(
169                         CGIitemtype => $CGIitemtype,
170                         );
171 }
172
173
174 # Print the page
175 output_html_with_http_headers $query, $cookie, $template->output;
176
177 # Local Variables:
178 # tab-width: 4
179 # End: