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