C4::SearchMarc is deprecated now. it is totaly replaced by C4::Search.
[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::Acquisition;
33 use C4::Koha; # XXX subfield_is_koha_internal_p
34
35 # Creates the list of active tags using the active MARC configuration
36 sub create_marclist {
37         my $dbh = C4::Context->dbh;
38         my $tagslib = &MARCgettagslib($dbh,1);
39         my @marcarray;
40         push @marcarray,"";
41         my $widest_menu_item_width = 0;
42         for (my $pass = 1; $pass <= 2; $pass += 1)
43         {
44                 for (my $tabloop = 0; $tabloop<=10;$tabloop++)
45                 {
46                         my $separator_inserted_p = 0; # FIXME... should not use!!
47                         foreach my $tag (sort(keys (%{$tagslib})))
48                         {
49                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}}))
50                                 {
51                                         next if subfield_is_koha_internal_p($subfield);
52                                         next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop);
53                                         my $menu_item = "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}";
54                                         if ($pass == 1)
55                                         {
56                                                 $widest_menu_item_width = length $menu_item if($widest_menu_item_width < length $menu_item);
57                                         } else {
58                                                 if (!$separator_inserted_p)
59                                                 {
60                                                         my $w = int(($widest_menu_item_width - 3 + 0.5)/2);
61                                                         my $s = ('-' x ($w * 4/5));
62                                                         push @marcarray,  "$s $tabloop $s";
63                                                         $separator_inserted_p = 1;
64                                                 }
65                                         push @marcarray, $menu_item;
66                                         }
67                                 }
68                         }
69                 }
70         }
71         return \@marcarray;
72 }
73
74 # Creates a scrolling list with the associated default value.
75 # Using more than one scrolling list in a CGI assigns the same default value to all the
76 # scrolling lists on the page !?!? That's why this function was written.
77 sub create_scrolling_list {
78         my ($params) = @_;
79         my $scrollist = sprintf("<select name=\"%s\" size=\"%d\" onChange='%s'>\n", $params->{'name'}, $params->{'size'}, $params->{'onChange'});
80
81         foreach my $tag (@{$params->{'values'}})
82         {
83                 my $selected = "selected " if($params->{'default'} eq $tag);
84                 $scrollist .= sprintf("<option %svalue=\"%s\">%s</option>\n", $selected, $tag, $tag);
85         }
86
87         $scrollist .= "</select>\n";
88
89         return $scrollist;
90 }
91
92 my $query=new CGI;
93 my $type=$query->param('type');
94 my $op = $query->param('op') || "";
95 my $dbh = C4::Context->dbh;
96
97 my $startfrom=$query->param('startfrom');
98 $startfrom=0 if(!defined $startfrom);
99 my ($template, $loggedinuser, $cookie);
100 my $searchdesc;
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                 for (my $i=0;$i<=$#marclist;$i++) {
111                 if ($searchdesc) { # don't put the and_or on the 1st search term
112                         $searchdesc .= $and_or[$i]." ".$excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
113                         } else {
114                         $searchdesc = $excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
115                         }
116                 }
117         
118         $resultsperpage= $query->param('resultsperpage');
119         $resultsperpage = 19 if(!defined $resultsperpage);
120         my $orderby = $query->param('orderby');
121         my $desc_or_asc = $query->param('desc_or_asc');
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                                 if ($marc =~ /^(\d){3}(. -)(.)*/)
133                                 {
134                                         # The user is using the search catalogue part, more fields
135                                         push @tags, $dbh->quote(substr($marc,0,4));
136                                 }
137                                 else
138                                 {
139                                         push @tags, $marc;
140                                 }
141                         }
142                 } else {
143                         push @tags, "";
144                 }
145         }
146         findseealso($dbh,\@tags);
147         my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
148                                                                                 \@excluding, \@operator, \@value,
149                                                                                 $startfrom*$resultsperpage, $resultsperpage,$orderby,$desc_or_asc);
150         if ($total == 1) {
151          # if only 1 answer, jump directly to the biblio
152         if (C4::Context->preference("IntranetBiblioDefaultView") eq "normal") {
153              print $query->redirect("/cgi-bin/koha/detail.pl?bib=".@$results[0]->{biblionumber});
154         } elsif (C4::Context->preference("IntranetBiblioDefaultView") eq "marc") {
155              print $query->redirect("/cgi-bin/koha/MARCdetail.pl?bib=".@$results[0]->{biblionumber});
156         } else {
157              print $query->redirect("/cgi-bin/koha/ISBDdetail.pl?bib=".@$results[0]->{biblionumber});
158         }
159          exit
160         }
161         ($template, $loggedinuser, $cookie)
162                 = get_template_and_user({template_name => "search.marc/result.tmpl",
163                                 query => $query,
164                                 type => $type,
165                                 authnotrequired => 0,
166                                 flagsrequired => {borrowers => 1},
167                                 flagsrequired => {catalogue => 1},
168                                 debug => 1,
169                                 });
170
171         # multi page display gestion
172         my $displaynext=0;
173         my $displayprev=$startfrom;
174         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
175                 $displaynext = 1;
176         }
177
178         my @field_data = ();
179
180         for(my $i = 0 ; $i <= $#marclist ; $i++) {
181                 push @field_data, { term => "marclist", val=>$marclist[$i] };
182                 push @field_data, { term => "and_or", val=>$and_or[$i] };
183                 push @field_data, { term => "excluding", val=>$excluding[$i] };
184                 push @field_data, { term => "operator", val=>$operator[$i] };
185                 push @field_data, { term => "value", val=>$value[$i] };
186         }
187
188         my @numbers = ();
189
190         if ($total>$resultsperpage) {
191                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
192                         if ($i<16) {
193                         my $highlight=0;
194                         ($startfrom==($i-1)) && ($highlight=1);
195                         push @numbers, { number => $i,
196                                         highlight => $highlight ,
197                                         searchdata=> \@field_data,
198                                         startfrom => ($i-1)};
199                         }
200         }
201         }
202
203         my $from = $startfrom*$resultsperpage+1;
204         my $to;
205
206         if($total < (($startfrom+1)*$resultsperpage))
207         {
208                 $to = $total;
209         } else {
210                 $to = (($startfrom+1)*$resultsperpage);
211         }
212         my $defaultview = 'BiblioDefaultView'.C4::Context->preference('IntranetBiblioDefaultView');
213         $template->param(result => $results,
214                                                         startfrom=> $startfrom,
215                                                         displaynext=> $displaynext,
216                                                         displayprev=> $displayprev,
217                                                         resultsperpage => $resultsperpage,
218                                                         startfromnext => $startfrom+1,
219                                                         startfromprev => $startfrom-1,
220                                                         searchdata=>\@field_data,
221                                                         total=>$total,
222                                                         from=>$from,
223                                                         to=>$to,
224                                                         numbers=>\@numbers,
225                                                         searchdesc=> $searchdesc,
226                                                         desc_asc=>$desc_or_asc,
227                                                         orderby=>$orderby,
228                                                         MARC_ON => C4::Context->preference("marc"),
229                                                         $defaultview => 1,
230                                                         );
231
232 } elsif ($op eq "AddStatement") {
233         ($template, $loggedinuser, $cookie)
234                 = get_template_and_user({template_name => "search.marc/search.tmpl",
235                                 query => $query,
236                                 type => $type,
237                                 authnotrequired => 0,
238                                 flagsrequired => {catalogue => 1},
239                                 debug => 1,
240                                 });
241
242         # Gets the entered information
243         my @marcfields = $query->param('marclist');
244         my @and_or = $query->param('and_or');
245         my @excluding = $query->param('excluding');
246         my @operator = $query->param('operator');
247         my @value = $query->param('value');
248
249         my @statements = ();
250
251         # List of the marc tags to display
252         my $marcarray = create_marclist();
253
254         my $nbstatements = $query->param('nbstatements');
255         $nbstatements = 1 if(!defined $nbstatements);
256
257         for(my $i = 0 ; $i < $nbstatements ; $i++)
258         {
259                 my %fields = ();
260
261                 # Recreates the old scrolling lists with the previously selected values
262                 my $marclist = create_scrolling_list({name=>"marclist",
263                                         values=> $marcarray,
264                                         size=> 1,
265                                         default=>$marcfields[$i],
266                                         onChange => "sql_update()"}
267                                         );
268
269                 $fields{'marclist'} = $marclist;
270                 $fields{'first'} = 1 if($i == 0);
271
272                 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
273                 $fields{'or'} = 1 if($and_or[$i] eq "or");
274
275                 #Restores the "not" parameters
276                 $fields{'not'} = 1 if($excluding[$i]);
277
278                 #Restores the operators (most common operators first);
279                 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
280                 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
281                 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
282                 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; }      #greater than
283                 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
284                 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
285                 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
286
287                 #Restores the value
288                 $fields{'value'} = $value[$i];
289
290                 push @statements, \%fields;
291         }
292         $nbstatements++;
293
294         # The new scrolling list
295         my $marclist = create_scrolling_list({name=>"marclist",
296                                 values=> $marcarray,
297                                 size=>1,
298                                 onChange => "sql_update()"});
299         push @statements, {"marclist" => $marclist };
300
301         $template->param("statements" => \@statements,
302                                                 "nbstatements" => $nbstatements);
303
304 }
305 else {
306         ($template, $loggedinuser, $cookie)
307                 = get_template_and_user({template_name => "search.marc/search.tmpl",
308                                 query => $query,
309                                 type => $type,
310                                 authnotrequired => 0,
311                                 flagsrequired => {catalogue => 1},
312                                 debug => 1,
313                                 });
314         #$template->param(loggedinuser => $loggedinuser);
315
316         my $marcarray = create_marclist();
317
318         my $marclist = CGI::scrolling_list(-name=>"marclist",
319                                         -values=> $marcarray,
320                                         -size=>1,
321                                         -tabindex=>'',
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                                 -tabindex=>'',
349                                 -multiple => 0 );
350         $sth->finish;
351
352         my @branches;
353         my @select_branch;
354         my %select_branches;
355         my $branches=GetBranches();
356         my @branchloop;
357         foreach my $thisbranch (sort keys %$branches) {
358 #               my $selected = 1 if $thisbranch eq $branch;
359                 my %row =(value => $thisbranch,
360 #                                       selected => $selected,
361                                         branchname => $branches->{$thisbranch}->{'branchname'},
362                                 );
363                 push @branchloop, \%row;
364         }
365         $sth->finish;
366
367
368         $template->param('Disable_Dictionary'=>C4::Context->preference("Disable_Dictionary")) if (C4::Context->preference("Disable_Dictionary"));
369         $template->param("statements" => \@statements,
370                         "nbstatements" => 3,
371                         CGIitemtype => $CGIitemtype,
372                         branchloop => \@branchloop,
373                         );
374 }
375
376
377 # Print the page
378 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
379                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
380                 IntranetNav => C4::Context->preference("IntranetNav"),
381                 );
382 output_html_with_http_headers $query, $cookie, $template->output;
383
384 # Local Variables:
385 # tab-width: 4
386 # End: