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