Added some FIXME comments.
[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 (-e "opac") && ($type='opac');
32
33 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
34
35
36 my $startfrom=$query->param('startfrom');
37 ($startfrom) || ($startfrom=0);
38
39 my $subject=$query->param('subject');
40 my $template;
41 # if it's a subject we need to use the subject.tmpl
42 if ($subject) {
43         $template = gettemplate("catalogue/subject.tmpl",0);
44 } else {
45         $template = gettemplate("catalogue/searchresults.tmpl", 0);
46 }
47
48 my $env;
49 $env->{itemcount}=1;
50
51 # get all the search variables
52 # we assume that C4::Search will validate these values for us
53 # FIXME - This whole section, up to the &catalogsearch call, is crying
54 # out for
55 #       foreach $search_term (qw(keyword subject author ...))
56 #       { ... }
57 my %search;
58 my $keyword=$query->param('keyword');
59 $search{'keyword'}=$keyword;
60
61 $search{'subject'}=$subject;
62 my $author=$query->param('author');
63 $search{'author'}=$author;
64 $search{'authoresc'}=$author;
65 #$search{'authorhtmlescaped'}=~s/ /%20/g;
66 my $illustrator=$query->param('illustrator');
67 $search{'param'}=$illustrator;
68 my $itemnumber=$query->param('itemnumber');
69 $search{'itemnumber'}=$itemnumber;
70 my $isbn=$query->param('isbn');
71 $search{'isbn'}=$isbn;
72 my $datebefore=$query->param('date-before');
73 $search{'date-before'}=$datebefore;
74 my $class=$query->param('class');
75 $search{'class'}=$class;
76 my $dewey=$query->param('dewey');
77 $search{'dewey'}=$dewey;
78 my $branch=$query->param('branch');
79 $search{'branch'}=$branch;
80 my $title=$query->param('title');
81 $search{'title'}=$title;
82 my $abstract=$query->param('abstract');
83 $search{'abstract'}=$abstract;
84 my $publisher=$query->param('publisher');
85 $search{'publisher'}=$publisher;
86
87 my $ttype=$query->param('ttype');
88 $search{'ttype'}=$ttype;
89
90 my $forminputs;
91 ($keyword) && (push @$forminputs, { line => "keyword=$keyword"});
92 ($subject) && (push @$forminputs, { line => "subject=$subject"});
93 ($author) && (push @$forminputs, { line => "author=$author"});
94 ($illustrator) && (push @$forminputs, { line => "illustrator=$illustrator"});
95 ($itemnumber) && (push @$forminputs, { line => "itemnumber=$itemnumber"});
96 ($isbn) && (push @$forminputs, { line => "isbn=$isbn"});
97 ($datebefore) && (push @$forminputs, { line => "date-before=$datebefore"});
98 ($class) && (push @$forminputs, { line => "class=$class"});
99 ($dewey) && (push @$forminputs, { line => "dewey=$dewey"});
100 ($branch) && (push @$forminputs, { line => "branch=$branch"});
101 ($title) && (push @$forminputs, { line => "title=$title"});
102 ($ttype) && (push @$forminputs, { line => "ttype=$ttype"});
103 ($abstract) && (push @$forminputs, { line => "abstract=$abstract"});
104 ($publisher) && (push @$forminputs, { line => "publisher=$publisher"});
105 ($forminputs) || (@$forminputs=());
106 $template->param(FORMINPUTS => $forminputs);
107 # whats this for?
108 # I think it is (or was) a search from the "front" page...   [st]
109 $search{'front'}=$query->param('front');
110
111 my $num=10;
112 my @results;
113 my $count;
114 if (my $subject=$query->param('subjectitems')) {
115     my $blah;
116     @results=subsearch(\$blah,$subject);
117     $count=$#results+1;
118 } else {
119     ($count,@results)=catalogsearch($env,'',\%search,$num,$startfrom);
120 }
121
122 #my $resultsarray=\@results;
123 my $resultsarray;
124
125 foreach my $result (@results) {
126     $result->{'authorhtmlescaped'}=$result->{'author'};
127     $result->{'authorhtmlescaped'}=~s/ /%20/g;
128     ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
129     ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
130     push (@$resultsarray, $result);
131 }
132 ($resultsarray) || (@$resultsarray=());
133 my $search="num=20";
134 my $searchdesc='';
135 if ($keyword){
136     $search=$search."&keyword=$keyword";                # FIXME - .=
137     $searchdesc.="keyword $keyword, ";
138 }
139 if (my $subjectitems=$query->param('subjectitems')){
140     $search=$search."&subjectitems=$subjectitems";      # FIXME - .=
141     $searchdesc.="subject $subjectitems, ";
142 }
143 if ($subject){
144     $search=$search."&subject=$subject";                # FIXME - .=
145     $searchdesc.="subject $subject, ";
146 }
147 if ($author){
148     $search=$search."&author=$author";                  # FIXME - .=
149     $searchdesc.="author $author, ";
150 }
151 if ($class){
152     $search=$search."&class=$class";                    # FIXME - .=
153     $searchdesc.="class $class, ";
154 }
155 if ($title){
156     $search=$search."&title=$title";                    # FIXME - .=
157     $searchdesc.="title $title, ";
158 }
159 if ($dewey){
160     $search=$search."&dewey=$dewey";                    # FIXME - .=
161     $searchdesc.="dewey $dewey, ";
162 }
163 $search.="&ttype=$ttype";
164
165 $search=~ s/ /%20/g;
166 $template->param(startfrom => $startfrom+1);
167 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
168 $template->param(numrecords => $count);
169 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
170 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
171 $template->param(nextstartfrom => $nextstartfrom);
172 my $displaynext=1;
173 my $displayprev=0;
174 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
175 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
176 $template->param(displaynext => $displaynext);
177 $template->param(displayprev => $displayprev);
178 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
179 $template->param(prevstartfrom => $prevstartfrom);
180 $template->param(search => $search);
181 $template->param(searchdesc => $searchdesc);
182 $template->param(SEARCH_RESULTS => $resultsarray);
183 #$template->param(includesdir => $includes);
184 $template->param(loggedinuser => $loggedinuser);
185
186 my $numbers;
187 @$numbers=();
188 if ($count>10) {
189     for (my $i=1; $i<$count/10+1; $i++) {
190         if ($i<16) {
191             ($title) && (push @$forminputs, { line => "title=$title"});
192             my $highlight=0;
193             ($startfrom==($i-1)*10) && ($highlight=1);
194             my $formelements='';
195             foreach (@$forminputs) {
196                 my $line=$_->{line};
197                 $formelements.="$line&";
198             }
199             $formelements=~s/ /+/g;
200             push @$numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => $forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
201         }
202     }
203 }
204
205 $template->param(numbers => $numbers);
206
207
208
209 print $query->header(-cookie => $cookie), $template->output;
210