HTML & template bugfixes
[koha.git] / search.marc / 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::Auth;
25 use HTML::Template;
26 use C4::Context;
27 use C4::Search;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Interface::CGI::Output;
31 use C4::Biblio;
32 use C4::SearchMarc;
33 use C4::Catalogue;
34 use C4::Koha; # XXX subfield_is_koha_internal_p
35
36 # Creates the list of active tags using the active MARC configuration
37 sub create_marclist {
38         my $dbh = C4::Context->dbh;
39         my $tagslib = &MARCgettagslib($dbh,1);
40         my @marcarray;
41         push @marcarray,"";
42         my $widest_menu_item_width = 0;
43         for (my $pass = 1; $pass <= 2; $pass += 1)
44         {
45                 for (my $tabloop = 0; $tabloop<=9;$tabloop++)
46                 {
47                         my $separator_inserted_p = 0; # FIXME... should not use!!
48                         foreach my $tag (sort(keys (%{$tagslib})))
49                         {
50                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}}))
51                                 {
52                                         next if subfield_is_koha_internal_p($subfield);
53                                         next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop);
54                                         my $menu_item = "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}";
55                                         if ($pass == 1)
56                                         {
57                                                 $widest_menu_item_width = length $menu_item if($widest_menu_item_width < length $menu_item);
58                                         } else {
59                                                 if (!$separator_inserted_p)
60                                                 {
61                                                         my $w = int(($widest_menu_item_width - 3 + 0.5)/2);
62                                                         my $s = ('-' x ($w * 4/5));
63                                                         push @marcarray,  "$s $tabloop $s";
64                                                         $separator_inserted_p = 1;
65                                                 }
66                                         push @marcarray, $menu_item;
67                                         }
68                                 }
69                         }
70                 }
71         }
72         return \@marcarray;
73 }
74
75 # Creates a scrolling list with the associated default value.
76 # Using more than one scrolling list in a CGI assigns the same default value to all the
77 # scrolling lists on the page !?!? That's why this function was written.
78 sub create_scrolling_list {
79         my ($params) = @_;
80         my $scrollist = sprintf("<select name=\"%s\" size=\"%d\" onChange='%s'>\n", $params->{'name'}, $params->{'size'}, $params->{'onChange'});
81
82         foreach my $tag (@{$params->{'values'}})
83         {
84                 my $selected = "selected " if($params->{'default'} eq $tag);
85                 $scrollist .= sprintf("<option %svalue=\"%s\">%s</option>\n", $selected, $tag, $tag);
86         }
87
88         $scrollist .= "</select>\n";
89
90         return $scrollist;
91 }
92
93 my $query=new CGI;
94 my $type=$query->param('type');
95 my $op = $query->param('op');
96 my $dbh = C4::Context->dbh;
97
98 my $startfrom=$query->param('startfrom');
99 $startfrom=0 if(!defined $startfrom);
100 my ($template, $loggedinuser, $cookie);
101 my $resultsperpage;
102
103 if ($op eq "do_search") {
104         my @marclist = $query->param('marclist');
105         my @and_or = $query->param('and_or');
106         my @excluding = $query->param('excluding');
107         my @operator = $query->param('operator');
108         my @value = $query->param('value');
109
110         $resultsperpage= $query->param('resultsperpage');
111         $resultsperpage = 19 if(!defined $resultsperpage);
112         my $orderby = $query->param('orderby');
113
114         # builds tag and subfield arrays
115         my @tags;
116
117         foreach my $marc (@marclist) {
118                 if ($marc) {
119                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc);
120                         if ($tag) {
121                                 push @tags,$dbh->quote("$tag$subfield");
122                         } else {
123                                 push @tags, $dbh->quote(substr($marc,0,4));
124                         }
125                 } else {
126                         push @tags, "";
127                 }
128         }
129         findseealso($dbh,\@tags);
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 => "search.marc/result.tmpl",
136                                 query => $query,
137                                 type => $type,
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
204 } elsif ($op eq "AddStatement") {
205
206         ($template, $loggedinuser, $cookie)
207                 = get_template_and_user({template_name => "search.marc/search.tmpl",
208                                 query => $query,
209                                 type => $type,
210                                 authnotrequired => 0,
211                                 flagsrequired => {catalogue => 1},
212                                 debug => 1,
213                                 });
214
215         # Gets the entered information
216         my @marcfields = $query->param('marclist');
217         my @and_or = $query->param('and_or');
218         my @excluding = $query->param('excluding');
219         my @operator = $query->param('operator');
220         my @value = $query->param('value');
221
222         my @statements = ();
223
224         # List of the marc tags to display
225         my $marcarray = create_marclist();
226
227         my $nbstatements = $query->param('nbstatements');
228         $nbstatements = 1 if(!defined $nbstatements);
229
230         for(my $i = 0 ; $i < $nbstatements ; $i++)
231         {
232                 my %fields = ();
233
234                 # Recreates the old scrolling lists with the previously selected values
235                 my $marclist = create_scrolling_list({name=>"marclist",
236                                         values=> $marcarray,
237                                         size=> 1,
238                                         default=>$marcfields[$i],
239                                         onChange => "sql_update()"}
240                                         );
241
242                 $fields{'marclist'} = $marclist;
243                 $fields{'first'} = 1 if($i == 0);
244
245                 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
246                 $fields{'or'} = 1 if($and_or[$i] eq "or");
247
248                 #Restores the "not" parameters
249                 $fields{'not'} = 1 if($excluding[$i]);
250
251                 #Restores the operators (most common operators first);
252                 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
253                 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
254                 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
255                 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; }      #greater than
256                 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
257                 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
258                 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
259
260                 #Restores the value
261                 $fields{'value'} = $value[$i];
262
263                 push @statements, \%fields;
264         }
265         $nbstatements++;
266
267         # The new scrolling list
268         my $marclist = create_scrolling_list({name=>"marclist",
269                                 values=> $marcarray,
270                                 size=>1,
271                                 onChange => "sql_update()"});
272         push @statements, {"marclist" => $marclist };
273
274         $template->param("statements" => \@statements,
275                                                 "nbstatements" => $nbstatements);
276
277 }
278 else {
279         ($template, $loggedinuser, $cookie)
280                 = get_template_and_user({template_name => "search.marc/search.tmpl",
281                                 query => $query,
282                                 type => $type,
283                                 authnotrequired => 0,
284                                 flagsrequired => {catalogue => 1},
285                                 debug => 1,
286                                 });
287         #$template->param(loggedinuser => $loggedinuser);
288
289         my $marcarray = create_marclist();
290
291         my $marclist = CGI::scrolling_list(-name=>"marclist",
292                                         -values=> $marcarray,
293                                         -size=>1,
294                                         -multiple=>0,
295                                         -onChange => "sql_update()",
296                                         );
297
298         my @statements = ();
299
300         # Considering initial search with 3 criterias
301         push @statements, { "marclist" => $marclist, "first" => 1 };
302         push @statements, { "marclist" => $marclist, "first" => 0 };
303         push @statements, { "marclist" => $marclist, "first" => 0 };
304         my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
305         $sth->execute;
306         my  @itemtype;
307         my %itemtypes;
308         push @itemtype, "";
309         $itemtypes{''} = "";
310         while (my ($value,$lib) = $sth->fetchrow_array) {
311                 push @itemtype, $value;
312                 $itemtypes{$value}=$lib;
313         }
314
315         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
316                                 -values   => \@itemtype,
317                                 -labels   => \%itemtypes,
318                                 -size     => 1,
319                                 -multiple => 0 );
320         $sth->finish;
321
322         my @branches;
323         my @select_branch;
324         my %select_branches;
325         my ($count2,@branches)=branches();
326         push @select_branch, "";
327         $select_branches{''} = "";
328         for (my $i=0;$i<$count2;$i++){
329                 push @select_branch, $branches[$i]->{'branchcode'};#
330                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
331         }
332         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
333                                 -values   => \@select_branch,
334                                 -labels   => \%select_branches,
335                                 -size     => 1,
336                                 -multiple => 0 );
337         $sth->finish;
338
339
340         $template->param("statements" => \@statements,
341                         "nbstatements" => 3,
342                         CGIitemtype => $CGIitemtype,
343                         CGIbranch => $CGIbranch,
344                         );
345 }
346
347
348 # Print the page
349 output_html_with_http_headers $query, $cookie, $template->output;
350
351 # Local Variables:
352 # tab-width: 4
353 # End: