reordering code & debugging
[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         findseealso($dbh,\@tags);
131         
132 #               my $sth2 = $dbh->prepare("select b3.serial from biblioitems b2, biblio b3 where b3.biblionumber = ? and b2.biblionumber = b3.biblionumber and (b2.issn = ? or b3.serial like ?)");
133
134         
135         my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
136                                                                                 \@excluding, \@operator, \@value,
137                                                                                 $startfrom*$resultsperpage, $resultsperpage,$orderby);
138
139         ($template, $loggedinuser, $cookie)
140                 = get_template_and_user({template_name => "bull/result.tmpl",
141                                 query => $query,
142                                 type => "intranet",
143                                 authnotrequired => 0,
144                                 flagsrequired => {borrowers => 1},
145                                 flagsrequired => {catalogue => 1},
146                                 debug => 1,
147                                 });
148
149         # multi page display gestion
150         my $displaynext=0;
151         my $displayprev=$startfrom;
152         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
153                 $displaynext = 1;
154         }
155
156         my @field_data = ();
157
158
159         for(my $i = 0 ; $i <= $#marclist ; $i++)
160         {
161                 push @field_data, { term => "marclist", val=>$marclist[$i] };
162                 push @field_data, { term => "and_or", val=>$and_or[$i] };
163                 push @field_data, { term => "excluding", val=>$excluding[$i] };
164                 push @field_data, { term => "operator", val=>$operator[$i] };
165                 push @field_data, { term => "value", val=>$value[$i] };
166         }
167
168         my @numbers = ();
169
170         if ($total>$resultsperpage)
171         {
172                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
173                 {
174                         if ($i<16)
175                         {
176                         my $highlight=0;
177                         ($startfrom==($i-1)) && ($highlight=1);
178                         push @numbers, { number => $i,
179                                         highlight => $highlight ,
180                                         searchdata=> \@field_data,
181                                         startfrom => ($i-1)};
182                         }
183         }
184         }
185
186         my $from = $startfrom*$resultsperpage+1;
187         my $to;
188
189         if($total < (($startfrom+1)*$resultsperpage))
190         {
191                 $to = $total;
192         } else {
193                 $to = (($startfrom+1)*$resultsperpage);
194         }
195         $template->param(result => $results,
196                                                         startfrom=> $startfrom,
197                                                         displaynext=> $displaynext,
198                                                         displayprev=> $displayprev,
199                                                         resultsperpage => $resultsperpage,
200                                                         startfromnext => $startfrom+1,
201                                                         startfromprev => $startfrom-1,
202                                                         searchdata=>\@field_data,
203                                                         total=>$total,
204                                                         from=>$from,
205                                                         to=>$to,
206                                                         numbers=>\@numbers,
207                                                         );
208
209 } elsif ($op eq "AddStatement") {
210
211         ($template, $loggedinuser, $cookie)
212                 = get_template_and_user({template_name => "bull/search.tmpl",
213                                 query => $query,
214                                 type => "intranet",
215                                 authnotrequired => 0,
216                                 flagsrequired => {catalogue => 1},
217                                 debug => 1,
218                                 });
219
220         # Gets the entered information
221         my @marcfields = $query->param('marclist');
222         my @and_or = $query->param('and_or');
223         my @excluding = $query->param('excluding');
224         my @operator = $query->param('operator');
225         my @value = $query->param('value');
226
227         my @statements = ();
228
229         # List of the marc tags to display
230         my $marcarray = create_marclist();
231
232         my $nbstatements = $query->param('nbstatements');
233         $nbstatements = 1 if(!defined $nbstatements);
234
235         for(my $i = 0 ; $i < $nbstatements ; $i++)
236         {
237                 my %fields = ();
238
239                 # Recreates the old scrolling lists with the previously selected values
240                 my $marclist = create_scrolling_list({name=>"marclist",
241                                         values=> $marcarray,
242                                         size=> 1,
243                                         default=>$marcfields[$i],
244                                         onChange => "sql_update()"}
245                                         );
246
247                 $fields{'marclist'} = $marclist;
248                 $fields{'first'} = 1 if($i == 0);
249
250                 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
251                 $fields{'or'} = 1 if($and_or[$i] eq "or");
252
253                 #Restores the "not" parameters
254                 $fields{'not'} = 1 if($excluding[$i]);
255
256                 #Restores the operators (most common operators first);
257                 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
258                 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
259                 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
260                 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; }      #greater than
261                 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
262                 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
263                 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
264
265                 #Restores the value
266                 $fields{'value'} = $value[$i];
267
268                 push @statements, \%fields;
269         }
270         $nbstatements++;
271
272         # The new scrolling list
273         my $marclist = create_scrolling_list({name=>"marclist",
274                                 values=> $marcarray,
275                                 size=>1,
276                                 onChange => "sql_update()"});
277         push @statements, {"marclist" => $marclist };
278
279         $template->param("statements" => \@statements,
280                                                 "nbstatements" => $nbstatements);
281
282 }
283 else {
284         ($template, $loggedinuser, $cookie)
285                 = get_template_and_user({template_name => "bull/search.tmpl",
286                                 query => $query,
287                                 type => "intranet",
288                                 authnotrequired => 0,
289                                 flagsrequired => {catalogue => 1},
290                                 debug => 1,
291                                 });
292         #$template->param(loggedinuser => $loggedinuser);
293
294         my $marcarray = create_marclist();
295
296         my $marclist = CGI::scrolling_list(-name=>"marclist",
297                                         -values=> $marcarray,
298                                         -size=>1,
299                                         -multiple=>0,
300                                         -onChange => "sql_update()",
301                                         );
302
303         my @statements = ();
304
305         # Considering initial search with 3 criterias
306         push @statements, { "marclist" => $marclist, "first" => 1 };
307         push @statements, { "marclist" => $marclist, "first" => 0 };
308         push @statements, { "marclist" => $marclist, "first" => 0 };
309         my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
310         $sth->execute;
311         my  @itemtype;
312         my %itemtypes;
313         push @itemtype, "";
314         $itemtypes{''} = "";
315         while (my ($value,$lib) = $sth->fetchrow_array) {
316                 push @itemtype, $value;
317                 $itemtypes{$value}=$lib;
318         }
319
320         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
321                                 -values   => \@itemtype,
322                                 -labels   => \%itemtypes,
323                                 -size     => 1,
324                                 -multiple => 0 );
325         $sth->finish;
326
327         my @branches;
328         my @select_branch;
329         my %select_branches;
330         my ($count2,@branches)=branches();
331         push @select_branch, "";
332         $select_branches{''} = "";
333         for (my $i=0;$i<$count2;$i++){
334                 push @select_branch, $branches[$i]->{'branchcode'};#
335                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
336         }
337         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
338                                 -values   => \@select_branch,
339                                 -labels   => \%select_branches,
340                                 -size     => 1,
341                                 -multiple => 0 );
342         $sth->finish;
343
344
345         $template->param("statements" => \@statements,
346                         "nbstatements" => 3,
347                         CGIitemtype => $CGIitemtype,
348                         CGIbranch => $CGIbranch,
349                         );
350 }
351
352
353 # Print the page
354 output_html_with_http_headers $query, $cookie, $template->output;
355
356 # Local Variables:
357 # tab-width: 4
358 # End: