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;
33 use C4::Koha; # XXX subfield_is_koha_internal_p
35 # Creates the list of active tags using the active MARC configuration
37 my $dbh = C4::Context->dbh;
38 my $tagslib = &MARCgettagslib($dbh,1);
41 my $widest_menu_item_width = 0;
42 for (my $pass = 1; $pass <= 2; $pass += 1)
44 for (my $tabloop = 0; $tabloop<=9;$tabloop++)
46 my $separator_inserted_p = 0; # FIXME... should not use!!
47 foreach my $tag (sort(keys (%{$tagslib})))
49 foreach my $subfield (sort(keys %{$tagslib->{$tag}}))
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}";
56 $widest_menu_item_width = length $menu_item if($widest_menu_item_width < length $menu_item);
58 if (!$separator_inserted_p)
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;
65 push @marcarray, $menu_item;
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 {
79 my $scrollist = sprintf("<select name=\"%s\" size=\"%d\" onChange='%s'>\n", $params->{'name'}, $params->{'size'}, $params->{'onChange'});
81 foreach my $tag (@{$params->{'values'}})
83 my $selected = "selected " if($params->{'default'} eq $tag);
84 $scrollist .= sprintf("<option %svalue=\"%s\">%s</option>\n", $selected, $tag, $tag);
87 $scrollist .= "</select>\n";
93 my $type=$query->param('type');
94 my $op = $query->param('op');
95 my $dbh = C4::Context->dbh;
97 my $startfrom=$query->param('startfrom');
98 $startfrom=0 if(!defined $startfrom);
99 my ($template, $loggedinuser, $cookie);
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');
109 $resultsperpage= $query->param('resultsperpage');
110 $resultsperpage = 19 if(!defined $resultsperpage);
112 # builds tag and subfield arrays
116 foreach my $marc (@marclist) {
117 push @tags, substr($marc,0,3);
118 push @subfields, substr($marc,3,1);
120 my ($results,$total) = catalogsearch($dbh, \@tags, \@subfields, \@and_or,
121 \@excluding, \@operator, \@value,
122 $startfrom*$resultsperpage, $resultsperpage);
124 ($template, $loggedinuser, $cookie)
125 = get_template_and_user({template_name => "search.marc/result.tmpl",
128 authnotrequired => 0,
129 flagsrequired => {borrowers => 1},
130 flagsrequired => {catalogue => 1},
134 # multi page display gestion
136 my $displayprev=$startfrom;
137 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
144 for(my $i = 0 ; $i <= $#marclist ; $i++)
146 push @field_data, { term => "marclist", val=>$marclist[$i] };
147 push @field_data, { term => "and_or", val=>$and_or[$i] };
148 push @field_data, { term => "excluding", val=>$excluding[$i] };
149 push @field_data, { term => "operator", val=>$operator[$i] };
150 push @field_data, { term => "value", val=>$value[$i] };
155 if ($total>$resultsperpage)
157 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
162 ($startfrom==($i-1)) && ($highlight=1);
163 push @numbers, { number => $i,
164 highlight => $highlight ,
165 searchdata=> \@field_data,
166 startfrom => ($i-1)};
171 my $from = $startfrom*$resultsperpage+1;
174 if($total < (($startfrom+1)*$resultsperpage))
178 $to = (($startfrom+1)*$resultsperpage);
181 $template->param(result => $results,
182 startfrom=> $startfrom,
183 displaynext=> $displaynext,
184 displayprev=> $displayprev,
185 resultsperpage => $resultsperpage,
186 startfromnext => $startfrom+1,
187 startfromprev => $startfrom-1,
188 searchdata=>\@field_data,
195 } elsif ($op eq "AddStatement") {
197 ($template, $loggedinuser, $cookie)
198 = get_template_and_user({template_name => "search.marc/search.tmpl",
201 authnotrequired => 0,
202 flagsrequired => {catalogue => 1},
206 # Gets the entered information
207 my @marcfields = $query->param('marclist');
208 my @and_or = $query->param('and_or');
209 my @excluding = $query->param('excluding');
210 my @operator = $query->param('operator');
211 my @value = $query->param('value');
215 # List of the marc tags to display
216 my $marcarray = create_marclist();
218 my $nbstatements = $query->param('nbstatements');
219 $nbstatements = 1 if(!defined $nbstatements);
221 for(my $i = 0 ; $i < $nbstatements ; $i++)
225 # Recreates the old scrolling lists with the previously selected values
226 my $marclist = create_scrolling_list({name=>"marclist",
229 default=>$marcfields[$i],
230 onChange => "sql_update()"}
233 $fields{'marclist'} = $marclist;
234 $fields{'first'} = 1 if($i == 0);
236 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
237 $fields{'or'} = 1 if($and_or[$i] eq "or");
239 #Restores the "not" parameters
240 $fields{'not'} = 1 if($excluding[$i]);
242 #Restores the operators (most common operators first);
243 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
244 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
245 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
246 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; } #greater than
247 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
248 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
249 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
252 $fields{'value'} = $value[$i];
254 push @statements, \%fields;
258 # The new scrolling list
259 my $marclist = create_scrolling_list({name=>"marclist",
262 onChange => "sql_update()"});
263 push @statements, {"marclist" => $marclist };
265 $template->param("statements" => \@statements,
266 "nbstatements" => $nbstatements);
270 ($template, $loggedinuser, $cookie)
271 = get_template_and_user({template_name => "search.marc/search.tmpl",
274 authnotrequired => 0,
275 flagsrequired => {catalogue => 1},
278 #$template->param(loggedinuser => $loggedinuser);
280 my $marcarray = create_marclist();
282 my $marclist = CGI::scrolling_list(-name=>"marclist",
283 -values=> $marcarray,
286 -onChange => "sql_update()",
291 # Considering initial search with 3 criterias
292 push @statements, { "marclist" => $marclist, "first" => 1 };
293 push @statements, { "marclist" => $marclist, "first" => 0 };
294 push @statements, { "marclist" => $marclist, "first" => 0 };
296 $template->param("statements" => \@statements, "nbstatements" => 3);
301 output_html_with_http_headers $query, $cookie, $template->output;