Updating table row color toggle to pass variable to the template instead of HTML...
[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::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 the list of active tags using the active MARC configuration
38 sub create_marclist {
39         my $dbh = C4::Context->dbh;
40         my $tagslib = &MARCgettagslib($dbh,1);
41         my @marcarray;
42         push @marcarray,"";
43         my $widest_menu_item_width = 0;
44         for (my $pass = 1; $pass <= 2; $pass += 1)
45         {
46                 for (my $tabloop = 0; $tabloop<=9;$tabloop++)
47                 {
48                         my $separator_inserted_p = 0; # FIXME... should not use!!
49                         foreach my $tag (sort(keys (%{$tagslib})))
50                         {
51                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}}))
52                                 {
53                                         next if subfield_is_koha_internal_p($subfield);
54                                         next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop);
55                                         my $menu_item = "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}";
56                                         if ($pass == 1)
57                                         {
58                                                 $widest_menu_item_width = length $menu_item if($widest_menu_item_width < length $menu_item);
59                                         } else {
60                                                 if (!$separator_inserted_p)
61                                                 {
62                                                         my $w = int(($widest_menu_item_width - 3 + 0.5)/2);
63                                                         my $s = ('-' x ($w * 4/5));
64                                                         push @marcarray,  "$s $tabloop $s";
65                                                         $separator_inserted_p = 1;
66                                                 }
67                                         push @marcarray, $menu_item;
68                                         }
69                                 }
70                         }
71                 }
72         }
73         return \@marcarray;
74 }
75
76 # Creates a scrolling list with the associated default value.
77 # Using more than one scrolling list in a CGI assigns the same default value to all the
78 # scrolling lists on the page !?!? That's why this function was written.
79 sub create_scrolling_list {
80         my ($params) = @_;
81         my $scrollist = sprintf("<select name=\"%s\" size=\"%d\" onChange='%s'>\n", $params->{'name'}, $params->{'size'}, $params->{'onChange'});
82
83         foreach my $tag (@{$params->{'values'}})
84         {
85                 my $selected = "selected " if($params->{'default'} eq $tag);
86                 $scrollist .= sprintf("<option %svalue=\"%s\">%s</option>\n", $selected, $tag, $tag);
87         }
88
89         $scrollist .= "</select>\n";
90
91         return $scrollist;
92 }
93
94 my $query=new CGI;
95 my $type=$query->param('type');
96 my $op = $query->param('op');
97 my $dbh = C4::Context->dbh;
98
99 my $startfrom=$query->param('startfrom');
100 $startfrom=0 if(!defined $startfrom);
101 my ($template, $loggedinuser, $cookie);
102 my $resultsperpage;
103
104 if ($op eq "do_search") {
105         my @marclist = $query->param('marclist');
106         my @and_or = $query->param('and_or');
107         my @excluding = $query->param('excluding');
108         my @operator = $query->param('operator');
109         my @value = $query->param('value');
110
111         $resultsperpage= $query->param('resultsperpage');
112         $resultsperpage = 19 if(!defined $resultsperpage);
113         my $orderby = $query->param('orderby');
114
115         # builds tag and subfield arrays
116         my @tags;
117
118         foreach my $marc (@marclist) {
119                 if ($marc) {
120                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc);
121                         if ($tag) {
122                                 push @tags,$dbh->quote("$tag$subfield");
123                         } else {
124                                 push @tags, $dbh->quote(substr($marc,0,4));
125                         }
126                 } else {
127                         push @tags, "";
128                 }
129         }
130         my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
131                                                                                 \@excluding, \@operator, \@value,
132                                                                                 $startfrom*$resultsperpage, $resultsperpage,$orderby);
133
134         ($template, $loggedinuser, $cookie)
135                 = get_template_and_user({template_name => "bull/result.tmpl",
136                                 query => $query,
137                                 type => "intranet",
138                                 authnotrequired => 0,
139                                 flagsrequired => {borrowers => 1},
140                                 flagsrequired => {catalogue => 1},
141                                 debug => 1,
142                                 });
143
144         # multi page display gestion
145         my $displaynext=0;
146         my $displayprev=$startfrom;
147         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
148                 $displaynext = 1;
149         }
150
151         my @field_data = ();
152
153
154         for(my $i = 0 ; $i <= $#marclist ; $i++)
155         {
156                 push @field_data, { term => "marclist", val=>$marclist[$i] };
157                 push @field_data, { term => "and_or", val=>$and_or[$i] };
158                 push @field_data, { term => "excluding", val=>$excluding[$i] };
159                 push @field_data, { term => "operator", val=>$operator[$i] };
160                 push @field_data, { term => "value", val=>$value[$i] };
161         }
162
163         my @numbers = ();
164
165         if ($total>$resultsperpage)
166         {
167                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
168                 {
169                         if ($i<16)
170                         {
171                         my $highlight=0;
172                         ($startfrom==($i-1)) && ($highlight=1);
173                         push @numbers, { number => $i,
174                                         highlight => $highlight ,
175                                         searchdata=> \@field_data,
176                                         startfrom => ($i-1)};
177                         }
178         }
179         }
180
181         my $from = $startfrom*$resultsperpage+1;
182         my $to;
183
184         if($total < (($startfrom+1)*$resultsperpage))
185         {
186                 $to = $total;
187         } else {
188                 $to = (($startfrom+1)*$resultsperpage);
189         }
190         $template->param(result => $results,
191                                                         startfrom=> $startfrom,
192                                                         displaynext=> $displaynext,
193                                                         displayprev=> $displayprev,
194                                                         resultsperpage => $resultsperpage,
195                                                         startfromnext => $startfrom+1,
196                                                         startfromprev => $startfrom-1,
197                                                         searchdata=>\@field_data,
198                                                         total=>$total,
199                                                         from=>$from,
200                                                         to=>$to,
201                                                         numbers=>\@numbers,
202                                                         );
203 } else {
204         ($template, $loggedinuser, $cookie)
205                 = get_template_and_user({template_name => "bull/search.tmpl",
206                                 query => $query,
207                                 type => "intranet",
208                                 authnotrequired => 0,
209                                 flagsrequired => {catalogue => 1},
210                                 debug => 1,
211                                 });
212         #$template->param(loggedinuser => $loggedinuser);
213
214         my $marcarray = create_marclist();
215
216         my $marclist = CGI::scrolling_list(-name=>"marclist",
217                                         -values=> $marcarray,
218                                         -size=>1,
219                                         -multiple=>0,
220                                         -onChange => "sql_update()",
221                                         );
222
223         my @statements = ();
224
225         # Considering initial search with 3 criterias
226         push @statements, { "marclist" => $marclist, "first" => 1 };
227         push @statements, { "marclist" => $marclist, "first" => 0 };
228         push @statements, { "marclist" => $marclist, "first" => 0 };
229         my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
230         $sth->execute;
231         my  @itemtype;
232         my %itemtypes;
233         push @itemtype, "";
234         $itemtypes{''} = "";
235         while (my ($value,$lib) = $sth->fetchrow_array) {
236                 push @itemtype, $value;
237                 $itemtypes{$value}=$lib;
238         }
239
240         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
241                                 -values   => \@itemtype,
242                                 -labels   => \%itemtypes,
243                                 -size     => 1,
244                                 -multiple => 0 );
245         $sth->finish;
246
247         my @branches;
248         my @select_branch;
249         my %select_branches;
250         my ($count2,@branches)=branches();
251         push @select_branch, "";
252         $select_branches{''} = "";
253         for (my $i=0;$i<$count2;$i++){
254                 push @select_branch, $branches[$i]->{'branchcode'};#
255                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
256         }
257         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
258                                 -values   => \@select_branch,
259                                 -labels   => \%select_branches,
260                                 -size     => 1,
261                                 -multiple => 0 );
262         $sth->finish;
263
264
265         $template->param("statements" => \@statements,
266                         "nbstatements" => 3,
267                         CGIitemtype => $CGIitemtype,
268                         CGIbranch => $CGIbranch,
269                         );
270 }
271
272
273 # Print the page
274 output_html_with_http_headers $query, $cookie, $template->output;
275
276 # Local Variables:
277 # tab-width: 4
278 # End: