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