Convenience function to check for 1/0/true/false/on/off/yes/no
[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 C4::Auth;
24 use HTML::Template;
25 use C4::Context;
26 use C4::Search;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Interface::CGI::Output;
30
31 my $query=new CGI;
32 my $type=$query->param('type');
33
34 #(-e "opac") && ($type='opac');
35
36 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
37
38 my $startfrom=$query->param('startfrom');
39 ($startfrom) || ($startfrom=0);
40
41 my $subject=$query->param('subject');
42 # if it's a subject we need to use the subject.tmpl
43 my ($template, $loggedinuser, $cookie);
44 if ($subject) {
45         ($template, $loggedinuser, $cookie)
46                 = get_template_and_user({template_name => "catalogue/subject.tmpl",
47                              query => $query,
48                              type => "intranet",
49                              authnotrequired => 0,
50                              flagsrequired => {catalogue => 1},
51                              debug => 1,
52                              });
53 } else {
54         ($template, $loggedinuser, $cookie)
55                 = get_template_and_user({template_name => "catalogue/searchresults.tmpl",
56                              query => $query,
57                              type => "intranet",
58                              authnotrequired => 0,
59                              flagsrequired => {catalogue => 1},
60                              debug => 1,
61                              });
62 }
63
64 # %env
65 # Additional parameters for &catalogsearch
66 my %env = (
67         itemcount       => 1,   # If set to 1, &catalogsearch enumerates
68                                 # the results found and returns the number
69                                 # of items found, where they're located,
70                                 # etc.
71         );
72
73 # get all the search variables
74 # we assume that C4::Search will validate these values for us
75 my %search;                     # Search terms. If the key is "author",
76                                 # then $search{author} is the author the
77                                 # user is looking for.
78
79 my @forminputs;                 # This is used in the form template.
80
81 foreach my $term (qw(keyword subject author illustrator itemnumber
82                      isbn date-before class dewey branch title abstract
83                      publisher ttype))
84 {
85         my $value = $query->param($term);
86         next unless defined $value && $value ne "";
87                                 # Skip blank search terms
88         $search{$term} = $value;
89         push @forminputs, { line => "$term=$value" };
90 }
91
92 $template->param(FORMINPUTS => \@forminputs);
93
94 # whats this for?
95 # I think it is (or was) a search from the "front" page...   [st]
96 $search{'front'}=$query->param('front');
97
98 my $num=10;
99 my @results;
100 my $count;
101 if (my $subject=$query->param('subjectitems')) {
102     my $blah;
103     @results=subsearch(\$blah,$subject);
104     $count=$#results+1;
105 } else {
106     ($count,@results)=catalogsearch(\%env,'',\%search,$num,$startfrom);
107 }
108
109 #my $resultsarray=\@results;
110 my $resultsarray;
111
112 foreach my $result (@results) {
113     $result->{'authorhtmlescaped'}=$result->{'author'};
114     $result->{'authorhtmlescaped'}=~s/ /%20/g;
115     ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
116     ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
117     push (@$resultsarray, $result);
118 }
119 ($resultsarray) || (@$resultsarray=());
120 my $search="num=20";
121 my $searchdesc='';
122 if ($search{"keyword"}) {
123     $search .= "&keyword=$search{keyword}";
124     $searchdesc.="keyword $search{keyword}, ";
125 }
126 if (my $subjectitems=$query->param('subjectitems')){
127     $search .= "&subjectitems=$subjectitems";
128     $searchdesc.="subject $subjectitems, ";
129 }
130 if ($subject){
131     $search .= "&subject=$subject";
132     $searchdesc.="subject $subject, ";
133 }
134 if ($search{"author"}){
135     $search .= "&author=$search{author}";
136     $searchdesc.="author $search{author}, ";
137 }
138 if ($search{"class"}){
139     $search .= "&class=$search{class}";
140     $searchdesc.="class $search{class}, ";
141 }
142 if ($search{"title"}){
143     $search .= "&title=$search{title}";
144     $searchdesc.="title $search{title}, ";
145 }
146 if ($search{"dewey"}){
147     $search .= "&dewey=$search{dewey}";
148     $searchdesc.="dewey $search{dewey}, ";
149 }
150 $search.="&ttype=$search{ttype}";
151
152 $search=~ s/ /%20/g;
153 $template->param(startfrom => $startfrom+1);
154 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
155 $template->param(numrecords => $count);
156 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
157 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
158 $template->param(nextstartfrom => $nextstartfrom);
159 my $displaynext=1;
160 my $displayprev=0;
161 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
162 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
163 $template->param(displaynext => $displaynext);
164 $template->param(displayprev => $displayprev);
165 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
166 $template->param(prevstartfrom => $prevstartfrom);
167 $template->param(search => $search);
168 $template->param(searchdesc => $searchdesc);
169 $template->param(SEARCH_RESULTS => $resultsarray);
170 #$template->param(includesdir => $includes);
171 $template->param(loggedinuser => $loggedinuser);
172
173 my @numbers = ();
174 if ($count>10) {
175     for (my $i=1; $i<$count/10+1; $i++) {
176         if ($i<16) {
177             if ($search{"title"})
178             {
179                 push @forminputs, { line => "title=$search{title}"};
180             }
181             my $highlight=0;
182             ($startfrom==($i-1)*10) && ($highlight=1);
183             my $formelements='';
184             foreach (@forminputs) {
185                 my $line=$_->{line};
186                 $formelements.="$line&";
187             }
188             $formelements=~s/ /+/g;
189             push @numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => \@forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
190         }
191     }
192 }
193
194 $template->param(numbers => \@numbers);
195 if (C4::Context->preference('acquisitions') eq 'simple') {
196         $template->param(script => "MARCdetail.pl");
197 } else {
198         $template->param(script => "detail.pl");
199 }
200
201 # Print the page
202 output_html_with_http_headers $query, $cookie, $template->output;
203