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