Support for SearchMarc.pm v1.8
[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::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
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
113         # builds tag and subfield arrays
114         my @tags;
115         my @subfields;
116
117         foreach my $marc (@marclist) {
118                 push @tags, substr($marc,0,3);
119                 push @subfields, substr($marc,3,1);
120         }
121         my ($results,$total) = catalogsearch($dbh, \@tags, \@subfields, \@and_or,
122                                                                                         \@excluding, \@operator, \@value,
123                                                                                         $startfrom*$resultsperpage, $resultsperpage);
124
125         ($template, $loggedinuser, $cookie)
126                 = get_template_and_user({template_name => "search.marc/result.tmpl",
127                                 query => $query,
128                                 type => $type,
129                                 authnotrequired => 0,
130                                 flagsrequired => {borrowers => 1},
131                                 flagsrequired => {catalogue => 1},
132                                 debug => 1,
133                                 });
134
135         # multi page display gestion
136         my $displaynext=0;
137         my $displayprev=$startfrom;
138         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
139                 $displaynext = 1;
140         }
141
142         my @field_data = ();
143
144
145         for(my $i = 0 ; $i <= $#marclist ; $i++)
146         {
147                 push @field_data, { term => "marclist", val=>$marclist[$i] };
148                 push @field_data, { term => "and_or", val=>$and_or[$i] };
149                 push @field_data, { term => "excluding", val=>$excluding[$i] };
150                 push @field_data, { term => "operator", val=>$operator[$i] };
151                 push @field_data, { term => "value", val=>$value[$i] };
152         }
153
154         my @numbers = ();
155
156         if ($total>$resultsperpage)
157         {
158                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
159                 {
160                         if ($i<16)
161                         {
162                         my $highlight=0;
163                         ($startfrom==($i-1)) && ($highlight=1);
164                         push @numbers, { number => $i,
165                                         highlight => $highlight ,
166                                         searchdata=> \@field_data,
167                                         startfrom => ($i-1)};
168                         }
169         }
170         }
171
172         my $from = $startfrom*$resultsperpage+1;
173         my $to;
174
175         if($total < (($startfrom+1)*$resultsperpage))
176         {
177                 $to = $total;
178         } else {
179                 $to = (($startfrom+1)*$resultsperpage);
180         }
181
182         $template->param(result => $results,
183                                                         startfrom=> $startfrom,
184                                                         displaynext=> $displaynext,
185                                                         displayprev=> $displayprev,
186                                                         resultsperpage => $resultsperpage,
187                                                         startfromnext => $startfrom+1,
188                                                         startfromprev => $startfrom-1,
189                                                         searchdata=>\@field_data,
190                                                         total=>$total,
191                                                         from=>$from,
192                                                         to=>$to,
193                                                         numbers=>\@numbers
194                                                         );
195
196 } elsif ($op eq "AddStatement") {
197
198         ($template, $loggedinuser, $cookie)
199                 = get_template_and_user({template_name => "search.marc/search.tmpl",
200                                 query => $query,
201                                 type => $type,
202                                 authnotrequired => 0,
203                                 flagsrequired => {catalogue => 1},
204                                 debug => 1,
205                                 });
206
207         # Gets the entered information
208         my @marcfields = $query->param('marclist');
209         my @and_or = $query->param('and_or');
210         my @excluding = $query->param('excluding');
211         my @operator = $query->param('operator');
212         my @value = $query->param('value');
213
214         my @statements = ();
215
216         # List of the marc tags to display
217         my $marcarray = create_marclist();
218
219         my $nbstatements = $query->param('nbstatements');
220         $nbstatements = 1 if(!defined $nbstatements);
221
222         for(my $i = 0 ; $i < $nbstatements ; $i++)
223         {
224                 my %fields = ();
225
226                 # Recreates the old scrolling lists with the previously selected values
227                 my $marclist = create_scrolling_list({name=>"marclist",
228                                         values=> $marcarray,
229                                         size=> 1,
230                                         default=>$marcfields[$i],
231                                         onChange => "sql_update()"}
232                                         );
233
234                 $fields{'marclist'} = $marclist;
235                 $fields{'first'} = 1 if($i == 0);
236
237                 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
238                 $fields{'or'} = 1 if($and_or[$i] eq "or");
239
240                 #Restores the "not" parameters
241                 $fields{'not'} = 1 if($excluding[$i]);
242
243                 #Restores the operators (most common operators first);
244                 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
245                 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
246                 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
247                 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; }      #greater than
248                 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
249                 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
250                 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
251
252                 #Restores the value
253                 $fields{'value'} = $value[$i];
254
255                 push @statements, \%fields;
256         }
257         $nbstatements++;
258
259         # The new scrolling list
260         my $marclist = create_scrolling_list({name=>"marclist",
261                                 values=> $marcarray,
262                                 size=>1,
263                                 onChange => "sql_update()"});
264         push @statements, {"marclist" => $marclist };
265
266         $template->param("statements" => \@statements,
267                                                 "nbstatements" => $nbstatements);
268
269 }
270 else {
271         ($template, $loggedinuser, $cookie)
272                 = get_template_and_user({template_name => "search.marc/search.tmpl",
273                                 query => $query,
274                                 type => $type,
275                                 authnotrequired => 0,
276                                 flagsrequired => {catalogue => 1},
277                                 debug => 1,
278                                 });
279         #$template->param(loggedinuser => $loggedinuser);
280
281         my $marcarray = create_marclist();
282
283         my $marclist = CGI::scrolling_list(-name=>"marclist",
284                                         -values=> $marcarray,
285                                         -size=>1,
286                                         -multiple=>0,
287                                         -onChange => "sql_update()",
288                                         );
289
290         my @statements = ();
291
292         # Considering initial search with 3 criterias
293         push @statements, { "marclist" => $marclist, "first" => 1 };
294         push @statements, { "marclist" => $marclist, "first" => 0 };
295         push @statements, { "marclist" => $marclist, "first" => 0 };
296
297         $template->param("statements" => \@statements, "nbstatements" => 3);
298 }
299
300
301 # Print the page
302 output_html_with_http_headers $query, $cookie, $template->output;
303
304 # Local Variables:
305 # tab-width: 4
306 # End: