]> git.koha-community.org Git - koha.git/blob - search.pl
Fixed bug that broke searches with spaces in them.
[koha.git] / search.pl
1 #!/usr/bin/perl
2 use HTML::Template;
3 use strict;
4 require Exporter;
5 use C4::Database;
6 use CGI;
7 use C4::Search;
8 use C4::Auth;
9 use C4::Output; # no contains picktemplate
10   
11 my $query=new CGI;
12 my $type=$query->param('type');
13 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
14
15
16 my $language='french';
17
18
19 my %configfile;
20 open (KC, "/etc/koha.conf");
21 while (<KC>) {
22  chomp;
23  (next) if (/^\s*#/);
24  if (/(.*)\s*=\s*(.*)/) {
25    my $variable=$1;
26    my $value=$2;
27    # Clean up white space at beginning and end
28    $variable=~s/^\s*//g;
29    $variable=~s/\s*$//g;
30    $value=~s/^\s*//g;
31    $value=~s/\s*$//g;
32    $configfile{$variable}=$value;
33  }
34 }
35 #print $query->header;
36
37 my $includes=$configfile{'includes'};
38 ($includes) || ($includes="/usr/local/www/hdl/htdocs/includes");
39 my $templatebase="catalogue/searchresults.tmpl";
40 my $startfrom=$query->param('startfrom');
41 ($startfrom) || ($startfrom=0);
42 my $theme=picktemplate($includes, $templatebase);
43
44 my $subject=$query->param('subject');
45 # if its a subject we need to use the subject.tmpl
46 if ($subject) {
47     $templatebase=~ s/searchresults\.tmpl/subject\.tmpl/;
48     $theme=picktemplate($includes, $templatebase);
49 }
50
51 my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
52
53 my $env;
54 $env->{itemcount}=1;
55
56 # get all the search variables
57 # we assume that C4::Search will validate these values for us
58 my %search;
59 my $keyword=$query->param('keyword');
60 $search{'keyword'}=$keyword;
61
62 $search{'subject'}=$subject;
63 my $author=$query->param('author');
64 $search{'author'}=$author;
65 $search{'authoresc'}=$author;
66 #$search{'authorhtmlescaped'}=~s/ /%20/g;
67 my $illustrator=$query->param('illustrator');
68 $search{'param'}=$illustrator;
69 my $itemnumber=$query->param('itemnumber');
70 $search{'itemnumber'}=$itemnumber;
71 my $isbn=$query->param('isbn');
72 $search{'isbn'}=$isbn;
73 my $datebefore=$query->param('date-before');
74 $search{'date-before'}=$datebefore;
75 my $class=$query->param('class');
76 $search{'class'}=$class;
77 my $dewey=$query->param('dewey');
78 $search{'dewey'};
79 my $branch=$query->param('branch');
80 $search{'branch'}=$branch;
81 my $title=$query->param('title');
82 $search{'title'}=$title;
83 my $abstract=$query->param('abstract');
84 $search{'abstract'}=$abstract;
85 my $publisher=$query->param('publisher');
86 $search{'publisher'}=$publisher;
87
88 my $ttype=$query->param('ttype');
89 $search{'ttype'}=$ttype;
90
91 my $forminputs;
92 ($keyword) && (push @$forminputs, { line => "keyword=$keyword"});
93 ($subject) && (push @$forminputs, { line => "subject=$subject"});
94 ($author) && (push @$forminputs, { line => "author=$author"});
95 ($illustrator) && (push @$forminputs, { line => "illustrator=$illustrator"});
96 ($itemnumber) && (push @$forminputs, { line => "itemnumber=$itemnumber"});
97 ($isbn) && (push @$forminputs, { line => "isbn=$isbn"});
98 ($datebefore) && (push @$forminputs, { line => "date-before=$datebefore"});
99 ($class) && (push @$forminputs, { line => "class=$class"});
100 ($dewey) && (push @$forminputs, { line => "dewey=$dewey"});
101 ($branch) && (push @$forminputs, { line => "branch=$branch"});
102 ($title) && (push @$forminputs, { line => "title=$title"});
103 ($ttype) && (push @$forminputs, { line => "ttype=$ttype"});
104 ($abstract) && (push @$forminputs, { line => "abstract=$abstract"});
105 ($publisher) && (push @$forminputs, { line => "publisher=$publisher"});
106 ($forminputs) || (@$forminputs=());
107 $template->param(FORMINPUTS => $forminputs);
108 # whats this for?
109 # I think it is (or was) a search from the "front" page...   [st]
110 $search{'front'}=$query->param('front');
111
112 my $num=10;
113 my @results;
114 my $count;
115 if (my $subject=$query->param('subjectitems')) {
116     my $blah;
117     @results=subsearch(\$blah,$subject);
118     $count=$#results+1;
119 } else {
120     ($count,@results)=catalogsearch($env,'',\%search,$num,$startfrom);
121 }
122
123 #my $resultsarray=\@results;
124 my $resultsarray;
125
126 foreach my $result (@results) {
127     $result->{'authorhtmlescaped'}=$result->{'author'};
128     $result->{'authorhtmlescaped'}=~s/ /%20/g;
129     ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
130     ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
131     push (@$resultsarray, $result);
132 }
133 ($resultsarray) || (@$resultsarray=());
134 my $search="num=20";
135 my $searchdesc='';
136 if ($keyword){
137     $search=$search."&keyword=$keyword";
138     $searchdesc.="keyword $keyword, ";
139 }
140 if (my $subjectitems=$query->param('subjectitems')){
141     $search=$search."&subjectitems=$subjectitems";
142     $searchdesc.="subject $subjectitems, ";
143 }
144 if ($subject){
145     $search=$search."&subject=$subject";
146     $searchdesc.="subject $subject, ";
147 }
148 if ($author){
149     $search=$search."&author=$author";
150     $searchdesc.="author $author, ";
151 }
152 if ($class){
153     $search=$search."&class=$class";
154     $searchdesc.="class $class, ";
155 }
156 if ($title){
157     $search=$search."&title=$title";
158     $searchdesc.="title $title, ";
159 }
160 if ($dewey){
161     $search=$search."&dewey=$dewey";
162     $searchdesc.="dewey $dewey, ";
163 }
164 $search.="&ttype=$ttype";
165
166 $search=~ s/ /%20/g;
167 $template->param(startfrom => $startfrom+1);
168 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
169 $template->param(numrecords => $count);
170 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
171 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
172 $template->param(nextstartfrom => $nextstartfrom);
173 my $displaynext=1;
174 my $displayprev=0;
175 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
176 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
177 $template->param(displaynext => $displaynext);
178 $template->param(displayprev => $displayprev);
179 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
180 $template->param(prevstartfrom => $prevstartfrom);
181 $template->param(search => $search);
182 $template->param(searchdesc => $searchdesc);
183 $template->param(SEARCH_RESULTS => $resultsarray);
184 $template->param(includesdir => $includes);
185 $template->param(loggedinuser => $loggedinuser);
186
187 my $numbers;
188 @$numbers=();
189 if ($count>10) {
190     for (my $i=1; $i<$count/10+1; $i++) {
191     ($title) && (push @$forminputs, { line => "title=$title"});
192         my $highlight=0;
193         ($startfrom==($i-1)*10) && ($highlight=1);
194         my $formelements='';
195         foreach (@$forminputs) {
196             my $line=$_->{line};
197             $formelements.="$line&";
198         }
199         $formelements=~s/ /+/g;
200         push @$numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => $forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
201     }
202 }
203
204 $template->param(numbers => $numbers);
205
206
207
208 print $query->header(-cookie => $cookie), $template->output;
209