Further breaking up of the TmplTokenizer module.
[koha.git] / search.pl
1 #!/usr/bin/perl
2
3 # $Id$
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 # $Log$
22 # Revision 1.34  2004/02/11 08:35:31  tipaul
23 # synch'ing 2.0.0 branch and head
24 #
25 # Revision 1.33  2003/12/19 17:28:03  tipaul
26 # fix for #683
27 #
28 # Revision 1.32.2.2  2004/01/13 17:33:39  tipaul
29 # removing useless (& buggy here) checkauth
30 #
31 # Revision 1.32.2.1  2003/12/19 17:28:42  tipaul
32 # fix for 683
33 #
34 # Revision 1.32  2003/06/11 18:37:55  tonnesen
35 # Using boolean_preference instead of preference for 'marc' setting
36 #
37 # Revision 1.31  2003/05/11 07:31:37  rangi
38 # Removing duplicate use C4::Auth
39 #
40
41 use strict;
42 require Exporter;
43 use CGI;
44 use C4::Auth;
45 use HTML::Template;
46 use C4::Context;
47 use C4::Search;
48 use C4::Output;
49 use C4::Interface::CGI::Output;
50
51 my $query=new CGI;
52 my $type=$query->param('type');
53
54 my $startfrom=$query->param('startfrom');
55 ($startfrom) || ($startfrom=0);
56
57 my $subject=$query->param('subject');
58 # if it's a subject we need to use the subject.tmpl
59 my ($template, $loggedinuser, $cookie);
60 if ($subject) {
61         ($template, $loggedinuser, $cookie)
62                 = get_template_and_user({template_name => "catalogue/subject.tmpl",
63                              query => $query,
64                              type => "intranet",
65                              authnotrequired => 0,
66                              flagsrequired => {catalogue => 1},
67                              debug => 1,
68                              });
69 } else {
70         ($template, $loggedinuser, $cookie)
71                 = get_template_and_user({template_name => "catalogue/searchresults.tmpl",
72                              query => $query,
73                              type => "intranet",
74                              authnotrequired => 0,
75                              flagsrequired => {catalogue => 1},
76                              debug => 1,
77                              });
78 }
79
80 # %env
81 # Additional parameters for &catalogsearch
82 my %env = (
83         itemcount       => 1,   # If set to 1, &catalogsearch enumerates
84                                 # the results found and returns the number
85                                 # of items found, where they're located,
86                                 # etc.
87         );
88
89 # get all the search variables
90 # we assume that C4::Search will validate these values for us
91 my %search;                     # Search terms. If the key is "author",
92                                 # then $search{author} is the author the
93                                 # user is looking for.
94
95 my @forminputs;                 # This is used in the form template.
96
97 foreach my $term (qw(keyword subject author illustrator itemnumber
98                      isbn date-before class dewey branch title abstract
99                      publisher ttype))
100 {
101         my $value = $query->param($term);
102         next unless defined $value && $value ne "";
103                                 # Skip blank search terms
104         $search{$term} = $value;
105         push @forminputs, {     term => $term,
106                                                         value =>$value };
107 }
108
109 $template->param(FORMINPUTS => \@forminputs);
110
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 ($search{"keyword"}) {
140     $search .= "&keyword=$search{keyword}";
141     $searchdesc.="keyword $search{keyword}, ";
142 }
143 if (my $subjectitems=$query->param('subjectitems')){
144     $search .= "&subjectitems=$subjectitems";
145     $searchdesc.="subject $subjectitems, ";
146 }
147 if ($subject){
148     $search .= "&subject=$subject";
149     $searchdesc.="subject $subject, ";
150 }
151 if ($search{"author"}){
152     $search .= "&author=$search{author}";
153     $searchdesc.="author $search{author}, ";
154 }
155 if ($search{"class"}){
156     $search .= "&class=$search{class}";
157     $searchdesc.="class $search{class}, ";
158 }
159 if ($search{"title"}){
160     $search .= "&title=$search{title}";
161     $searchdesc.="title $search{title}, ";
162 }
163 if ($search{"dewey"}){
164     $search .= "&dewey=$search{dewey}";
165     $searchdesc.="dewey $search{dewey}, ";
166 }
167 if ($search{"illustrator"}){
168     $search .= "&illustrator=$search{illustrator}";
169     $searchdesc.="illustrator $search{illustrator}, ";
170 }
171 if ($search{"itemnumber"}){
172     $search .= "&itemnumber=$search{itemnumber}";
173     $searchdesc.="barcode $search{itemnumber}, ";
174 }
175 $search.="&ttype=$search{ttype}";
176
177 $search=~ s/ /%20/g;
178 $template->param(startfrom => $startfrom+1);
179 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
180 $template->param(numrecords => $count);
181 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
182 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
183 $template->param(nextstartfrom => $nextstartfrom);
184 my $displaynext=1;
185 my $displayprev=0;
186 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
187 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
188 $template->param(displaynext => $displaynext);
189 $template->param(displayprev => $displayprev);
190 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
191 $template->param(prevstartfrom => $prevstartfrom);
192 $template->param(search => $search);
193 $template->param(searchdesc => $searchdesc);
194 $template->param(SEARCH_RESULTS => $resultsarray);
195 #$template->param(includesdir => $includes);
196
197 my @numbers = ();
198 if ($count>10) {
199     for (my $i=1; $i<$count/10+1; $i++) {
200         if ($i<16) {
201             if ($search{"title"})
202             {
203                 push @forminputs, { line => "title=$search{title}"};
204             }
205             my $highlight=0;
206             ($startfrom==($i-1)*10) && ($highlight=1);
207             my $formelements='';
208             foreach (@forminputs) {
209                 my $line=$_->{line};
210                 $formelements.="$line&";
211             }
212             $formelements=~s/ /+/g;
213             push @numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => \@forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
214         }
215     }
216 }
217
218 $template->param(numbers => \@numbers);
219 if (C4::Context->boolean_preference('marc') eq '1') {
220         $template->param(script => "MARCdetail.pl");
221 } else {
222         $template->param(script => "detail.pl");
223 }
224
225 # Print the page
226 output_html_with_http_headers $query, $cookie, $template->output;
227