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