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