*** empty log message ***
[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, {     term => $term,
90                                                         value =>$value };
91 }
92
93 $template->param(FORMINPUTS => \@forminputs);
94
95 # whats this for?
96 # I think it is (or was) a search from the "front" page...   [st]
97 $search{'front'}=$query->param('front');
98
99 my $num=10;
100 my @results;
101 my $count;
102 if (my $subject=$query->param('subjectitems')) {
103     my $blah;
104     @results=subsearch(\$blah,$subject);
105     $count=$#results+1;
106 } else {
107     ($count,@results)=catalogsearch(\%env,'',\%search,$num,$startfrom);
108 }
109
110 #my $resultsarray=\@results;
111 my $resultsarray;
112
113 foreach my $result (@results) {
114     $result->{'authorhtmlescaped'}=$result->{'author'};
115     $result->{'authorhtmlescaped'}=~s/ /%20/g;
116     ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
117     ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
118     push (@$resultsarray, $result);
119 }
120 ($resultsarray) || (@$resultsarray=());
121 my $search="num=20";
122 my $searchdesc='';
123 if ($search{"keyword"}) {
124     $search .= "&keyword=$search{keyword}";
125     $searchdesc.="keyword $search{keyword}, ";
126 }
127 if (my $subjectitems=$query->param('subjectitems')){
128     $search .= "&subjectitems=$subjectitems";
129     $searchdesc.="subject $subjectitems, ";
130 }
131 if ($subject){
132     $search .= "&subject=$subject";
133     $searchdesc.="subject $subject, ";
134 }
135 if ($search{"author"}){
136     $search .= "&author=$search{author}";
137     $searchdesc.="author $search{author}, ";
138 }
139 if ($search{"class"}){
140     $search .= "&class=$search{class}";
141     $searchdesc.="class $search{class}, ";
142 }
143 if ($search{"title"}){
144     $search .= "&title=$search{title}";
145     $searchdesc.="title $search{title}, ";
146 }
147 if ($search{"dewey"}){
148     $search .= "&dewey=$search{dewey}";
149     $searchdesc.="dewey $search{dewey}, ";
150 }
151 $search.="&ttype=$search{ttype}";
152
153 $search=~ s/ /%20/g;
154 $template->param(startfrom => $startfrom+1);
155 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
156 $template->param(numrecords => $count);
157 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
158 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
159 $template->param(nextstartfrom => $nextstartfrom);
160 my $displaynext=1;
161 my $displayprev=0;
162 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
163 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
164 $template->param(displaynext => $displaynext);
165 $template->param(displayprev => $displayprev);
166 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
167 $template->param(prevstartfrom => $prevstartfrom);
168 $template->param(search => $search);
169 $template->param(searchdesc => $searchdesc);
170 $template->param(SEARCH_RESULTS => $resultsarray);
171 #$template->param(includesdir => $includes);
172 $template->param(loggedinuser => $loggedinuser);
173
174 my @numbers = ();
175 if ($count>10) {
176     for (my $i=1; $i<$count/10+1; $i++) {
177         if ($i<16) {
178             if ($search{"title"})
179             {
180                 push @forminputs, { line => "title=$search{title}"};
181             }
182             my $highlight=0;
183             ($startfrom==($i-1)*10) && ($highlight=1);
184             my $formelements='';
185             foreach (@forminputs) {
186                 my $line=$_->{line};
187                 $formelements.="$line&";
188             }
189             $formelements=~s/ /+/g;
190             push @numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => \@forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
191         }
192     }
193 }
194
195 $template->param(numbers => \@numbers);
196 if (C4::Context->preference('acquisitions') eq 'simple') {
197         $template->param(script => "MARCdetail.pl");
198 } else {
199         $template->param(script => "detail.pl");
200 }
201
202 # Print the page
203 output_html_with_http_headers $query, $cookie, $template->output;
204