Merging from rel-1-2 to trunk
[koha.git] / search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 require Exporter;
22 use CGI;
23 use HTML::Template;
24 use C4::Context;
25 use C4::Search;
26 use C4::Auth;
27 use C4::Output;
28
29 my $query=new CGI;
30 my $type=$query->param('type');
31 warn $type;             # FIXME - What's this for?
32
33 #(-e "opac") && ($type='opac');
34
35 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
36 warn $type;             # FIXME - What's this for?
37
38 my $startfrom=$query->param('startfrom');
39 ($startfrom) || ($startfrom=0);
40
41 my $subject=$query->param('subject');
42 my $template;
43 # if it's a subject we need to use the subject.tmpl
44 if ($subject) {
45         $template = gettemplate("catalogue/subject.tmpl",0);
46 } else {
47         $template = gettemplate("catalogue/searchresults.tmpl", 0);
48 }
49
50 # %env
51 # Additional parameters for &catalogsearch
52 my %env = (
53         itemcount       => 1,   # If set to 1, &catalogsearch enumerates
54                                 # the results found and returns the number
55                                 # of items found, where they're located,
56                                 # etc.
57         );
58
59 # get all the search variables
60 # we assume that C4::Search will validate these values for us
61 my %search;                     # Search terms. If the key is "author",
62                                 # then $search{author} is the author the
63                                 # user is looking for.
64
65 my @forminputs;                 # This is used in the form template.
66
67 foreach my $term (qw(keyword subject author illustrator itemnumber
68                      isbn date-before class dewey branch title abstract
69                      publisher ttype))
70 {
71         my $value = $query->param($term);
72
73         next unless defined $value && $value ne "";
74                                 # Skip blank search terms
75         $search{$term} = $value;
76         push @forminputs, { line => "$term=$value" };
77 }
78
79 $template->param(FORMINPUTS => \@forminputs);
80
81 # whats this for?
82 # I think it is (or was) a search from the "front" page...   [st]
83 $search{'front'}=$query->param('front');
84
85 my $num=10;
86 my @results;
87 my $count;
88 if (my $subject=$query->param('subjectitems')) {
89     my $blah;
90     @results=subsearch(\$blah,$subject);
91     $count=$#results+1;
92 } else {
93     ($count,@results)=catalogsearch(\%env,'',\%search,$num,$startfrom);
94 }
95
96 #my $resultsarray=\@results;
97 my $resultsarray;
98
99 foreach my $result (@results) {
100     $result->{'authorhtmlescaped'}=$result->{'author'};
101     $result->{'authorhtmlescaped'}=~s/ /%20/g;
102     ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
103     ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
104     push (@$resultsarray, $result);
105 }
106 ($resultsarray) || (@$resultsarray=());
107 my $search="num=20";
108 my $searchdesc='';
109 if ($search{"keyword"}) {
110     $search .= "&keyword=$search{keyword}";
111     $searchdesc.="keyword $search{keyword}, ";
112 }
113 if (my $subjectitems=$query->param('subjectitems')){
114     $search .= "&subjectitems=$subjectitems";
115     $searchdesc.="subject $subjectitems, ";
116 }
117 if ($subject){
118     $search .= "&subject=$subject";
119     $searchdesc.="subject $subject, ";
120 }
121 if ($search{"author"}){
122     $search .= "&author=$search{author}";
123     $searchdesc.="author $search{author}, ";
124 }
125 if ($search{"class"}){
126     $search .= "&class=$search{class}";
127     $searchdesc.="class $search{class}, ";
128 }
129 if ($search{"title"}){
130     $search .= "&title=$search{title}";
131     $searchdesc.="title $search{title}, ";
132 }
133 if ($search{"dewey"}){
134     $search .= "&dewey=$search{dewey}";
135     $searchdesc.="dewey $search{dewey}, ";
136 }
137 $search.="&ttype=$search{ttype}";
138
139 $search=~ s/ /%20/g;
140 $template->param(startfrom => $startfrom+1);
141 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
142 $template->param(numrecords => $count);
143 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
144 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
145 $template->param(nextstartfrom => $nextstartfrom);
146 my $displaynext=1;
147 my $displayprev=0;
148 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
149 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
150 $template->param(displaynext => $displaynext);
151 $template->param(displayprev => $displayprev);
152 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
153 $template->param(prevstartfrom => $prevstartfrom);
154 $template->param(search => $search);
155 $template->param(searchdesc => $searchdesc);
156 $template->param(SEARCH_RESULTS => $resultsarray);
157 #$template->param(includesdir => $includes);
158 $template->param(loggedinuser => $loggedinuser);
159
160 my @numbers = ();
161 if ($count>10) {
162     for (my $i=1; $i<$count/10+1; $i++) {
163         if ($i<16) {
164             if ($search{"title"})
165             {
166                 push @forminputs, { line => "title=$search{title}"};
167             }
168             my $highlight=0;
169             ($startfrom==($i-1)*10) && ($highlight=1);
170             my $formelements='';
171             foreach (@forminputs) {
172                 my $line=$_->{line};
173                 $formelements.="$line&";
174             }
175             $formelements=~s/ /+/g;
176             push @numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => \@forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
177         }
178     }
179 }
180
181 $template->param(numbers => \@numbers);
182 if (C4::Context->preference('acquisitions') eq 'simple') {
183         $template->param(script => "MARCdetail.pl");
184 } else {
185         $template->param(script => "detail.pl");
186 }
187
188 # Print the page
189 print $query->header(-cookie => $cookie), $template->output;
190