2 # WARNING: 4-character tab stops here
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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
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.
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
30 use C4::Interface::CGI::Output;
34 use C4::Koha; # XXX subfield_is_koha_internal_p
36 # Creates the list of active tags using the active MARC configuration
38 my $dbh = C4::Context->dbh;
39 my $tagslib = &MARCgettagslib($dbh,1);
42 my $widest_menu_item_width = 0;
43 for (my $pass = 1; $pass <= 2; $pass += 1)
45 for (my $tabloop = 0; $tabloop<=10;$tabloop++)
47 my $separator_inserted_p = 0; # FIXME... should not use!!
48 foreach my $tag (sort(keys (%{$tagslib})))
50 foreach my $subfield (sort(keys %{$tagslib->{$tag}}))
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}";
57 $widest_menu_item_width = length $menu_item if($widest_menu_item_width < length $menu_item);
59 if (!$separator_inserted_p)
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;
66 push @marcarray, $menu_item;
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 {
80 my $scrollist = sprintf("<select name=\"%s\" size=\"%d\" onChange='%s'>\n", $params->{'name'}, $params->{'size'}, $params->{'onChange'});
82 foreach my $tag (@{$params->{'values'}})
84 my $selected = "selected " if($params->{'default'} eq $tag);
85 $scrollist .= sprintf("<option %svalue=\"%s\">%s</option>\n", $selected, $tag, $tag);
88 $scrollist .= "</select>\n";
94 my $type=$query->param('type');
95 my $op = $query->param('op');
96 my $dbh = C4::Context->dbh;
98 my $startfrom=$query->param('startfrom');
99 $startfrom=0 if(!defined $startfrom);
100 my ($template, $loggedinuser, $cookie);
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');
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]);
115 $searchdesc = $excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
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');
124 # builds tag and subfield arrays
127 foreach my $marc (@marclist) {
129 my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
131 push @tags,$dbh->quote("$tag$subfield");
133 push @tags, $dbh->quote(substr($marc,0,4));
139 findseealso($dbh,\@tags);
140 my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
141 \@excluding, \@operator, \@value,
142 $startfrom*$resultsperpage, $resultsperpage,$orderby,$desc_or_asc);
144 # if only 1 answer, jump directly to the biblio
145 # here we need to check if MARC searching is turned on or off.
146 # if on, go to MARCdetail.pl else go to
148 my $marc_bool = C4::Context->boolean_preference("MARC") || 0;
149 if ($marc_bool eq "1") {
150 print $query->redirect("/cgi-bin/koha/MARCdetail.pl?biblionumber=".@$results[0]->{biblionumber});
153 print $query->redirect("/cgi-bin/koha/detail.pl?biblionumber=".@$results[0]->{biblionumber});
157 ($template, $loggedinuser, $cookie)
158 = get_template_and_user({template_name => "search.marc/result.tmpl",
161 authnotrequired => 0,
162 flagsrequired => {borrowers => 1},
163 flagsrequired => {catalogue => 1},
167 # multi page display gestion
169 my $displayprev=$startfrom;
170 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
176 for(my $i = 0 ; $i <= $#marclist ; $i++) {
177 push @field_data, { term => "marclist", val=>$marclist[$i] };
178 push @field_data, { term => "and_or", val=>$and_or[$i] };
179 push @field_data, { term => "excluding", val=>$excluding[$i] };
180 push @field_data, { term => "operator", val=>$operator[$i] };
181 push @field_data, { term => "value", val=>$value[$i] };
186 if ($total>$resultsperpage) {
187 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
190 ($startfrom==($i-1)) && ($highlight=1);
191 push @numbers, { number => $i,
192 highlight => $highlight ,
193 searchdata=> \@field_data,
194 startfrom => ($i-1)};
199 my $from = $startfrom*$resultsperpage+1;
202 if($total < (($startfrom+1)*$resultsperpage))
206 $to = (($startfrom+1)*$resultsperpage);
208 $template->param(result => $results,
209 startfrom=> $startfrom,
210 displaynext=> $displaynext,
211 displayprev=> $displayprev,
212 resultsperpage => $resultsperpage,
213 startfromnext => $startfrom+1,
214 startfromprev => $startfrom-1,
215 searchdata=>\@field_data,
220 searchdesc=> $searchdesc,
221 MARC_ON => C4::Context->preference("marc"),
224 } elsif ($op eq "AddStatement") {
226 ($template, $loggedinuser, $cookie)
227 = get_template_and_user({template_name => "search.marc/search.tmpl",
230 authnotrequired => 0,
231 flagsrequired => {catalogue => 1},
235 # Gets the entered information
236 my @marcfields = $query->param('marclist');
237 my @and_or = $query->param('and_or');
238 my @excluding = $query->param('excluding');
239 my @operator = $query->param('operator');
240 my @value = $query->param('value');
244 # List of the marc tags to display
245 my $marcarray = create_marclist();
247 my $nbstatements = $query->param('nbstatements');
248 $nbstatements = 1 if(!defined $nbstatements);
250 for(my $i = 0 ; $i < $nbstatements ; $i++)
254 # Recreates the old scrolling lists with the previously selected values
255 my $marclist = create_scrolling_list({name=>"marclist",
258 default=>$marcfields[$i],
259 onChange => "sql_update()"}
262 $fields{'marclist'} = $marclist;
263 $fields{'first'} = 1 if($i == 0);
265 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
266 $fields{'or'} = 1 if($and_or[$i] eq "or");
268 #Restores the "not" parameters
269 $fields{'not'} = 1 if($excluding[$i]);
271 #Restores the operators (most common operators first);
272 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
273 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
274 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
275 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; } #greater than
276 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
277 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
278 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
281 $fields{'value'} = $value[$i];
283 push @statements, \%fields;
287 # The new scrolling list
288 my $marclist = create_scrolling_list({name=>"marclist",
291 onChange => "sql_update()"});
292 push @statements, {"marclist" => $marclist };
294 $template->param("statements" => \@statements,
295 "nbstatements" => $nbstatements);
299 ($template, $loggedinuser, $cookie)
300 = get_template_and_user({template_name => "search.marc/search.tmpl",
303 authnotrequired => 0,
304 flagsrequired => {catalogue => 1},
307 #$template->param(loggedinuser => $loggedinuser);
309 my $marcarray = create_marclist();
311 my $marclist = CGI::scrolling_list(-name=>"marclist",
312 -values=> $marcarray,
315 -onChange => "sql_update()",
320 # Considering initial search with 3 criterias
321 push @statements, { "marclist" => $marclist, "first" => 1 };
322 push @statements, { "marclist" => $marclist, "first" => 0 };
323 push @statements, { "marclist" => $marclist, "first" => 0 };
324 my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
330 while (my ($value,$lib) = $sth->fetchrow_array) {
331 push @itemtype, $value;
332 $itemtypes{$value}=$lib;
335 my $CGIitemtype=CGI::scrolling_list( -name => 'value',
337 -values => \@itemtype,
338 -labels => \%itemtypes,
346 my ($count2,@branches)=branches();
347 push @select_branch, "";
348 $select_branches{''} = "";
349 for (my $i=0;$i<$count2;$i++){
350 push @select_branch, $branches[$i]->{'branchcode'};#
351 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
353 my $CGIbranch=CGI::scrolling_list( -name => 'value',
355 -values => \@select_branch,
356 -labels => \%select_branches,
362 $template->param("statements" => \@statements,
364 CGIitemtype => $CGIitemtype,
365 CGIbranch => $CGIbranch,
371 output_html_with_http_headers $query, $cookie, $template->output;