uses the gettemplate method from Output.pm now. Has only ten lines :-)
[koha.git] / tmpl / 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; # now contains picktemplate
10   
11 my $query=new CGI;
12 #my $type=$query->param('type');
13 #(-e "opac") && ($type='opac');
14 my $type = 'opac';
15
16 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
17
18
19 my $language='french';
20
21
22 my %configfile;
23 open (KC, "/etc/koha.conf");
24 while (<KC>) {
25  chomp;
26  (next) if (/^\s*#/);
27  if (/(.*)\s*=\s*(.*)/) {
28    my $variable=$1;
29    my $value=$2;
30    # Clean up white space at beginning and end
31    $variable=~s/^\s*//g;
32    $variable=~s/\s*$//g;
33    $value=~s/^\s*//g;
34    $value=~s/\s*$//g;
35    $configfile{$variable}=$value;
36  }
37 }
38 #print $query->header;
39
40 my $includes=$configfile{'includes'};
41 ($includes) || ($includes="/usr/local/www/hdl/htdocs/includes");
42 my $templatebase="opac/searchresults.tmpl";
43 my $startfrom=$query->param('startfrom');
44 ($startfrom) || ($startfrom=0);
45 my $theme=picktemplate($includes, $templatebase);
46
47 my $subject=$query->param('subject');
48 # if its a subject we need to use the subject.tmpl
49 if ($subject) {
50     $templatebase=~ s/searchresults\.tmpl/subject\.tmpl/;
51     $theme=picktemplate($includes, $templatebase);
52 }
53
54 my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
55
56 my $env;
57 $env->{itemcount}=1;
58
59 # get all the search variables
60 # we assume that C4::Search will validate these values for us
61 my %search;
62 my $keyword=$query->param('keyword');
63 $search{'keyword'}=$keyword;
64
65 $search{'subject'}=$subject;
66 my $author=$query->param('author');
67 $search{'author'}=$author;
68 $search{'authoresc'}=$author;
69 #$search{'authorhtmlescaped'}=~s/ /%20/g;
70 my $illustrator=$query->param('illustrator');
71 $search{'param'}=$illustrator;
72 my $itemnumber=$query->param('itemnumber');
73 $search{'itemnumber'}=$itemnumber;
74 my $isbn=$query->param('isbn');
75 $search{'isbn'}=$isbn;
76 my $datebefore=$query->param('date-before');
77 $search{'date-before'}=$datebefore;
78 my $class=$query->param('class');
79 $search{'class'}=$class;
80 my $dewey=$query->param('dewey');
81 $search{'dewey'};
82 my $branch=$query->param('branch');
83 $search{'branch'}=$branch;
84 my $title=$query->param('title');
85 $search{'title'}=$title;
86 my $abstract=$query->param('abstract');
87 $search{'abstract'}=$abstract;
88 my $publisher=$query->param('publisher');
89 $search{'publisher'}=$publisher;
90
91 my $ttype=$query->param('ttype');
92 $search{'ttype'}=$ttype;
93
94 my $forminputs;
95 ($keyword) && (push @$forminputs, { line => "keyword=$keyword"});
96 ($subject) && (push @$forminputs, { line => "subject=$subject"});
97 ($author) && (push @$forminputs, { line => "author=$author"});
98 ($illustrator) && (push @$forminputs, { line => "illustrator=$illustrator"});
99 ($itemnumber) && (push @$forminputs, { line => "itemnumber=$itemnumber"});
100 ($isbn) && (push @$forminputs, { line => "isbn=$isbn"});
101 ($datebefore) && (push @$forminputs, { line => "date-before=$datebefore"});
102 ($class) && (push @$forminputs, { line => "class=$class"});
103 ($dewey) && (push @$forminputs, { line => "dewey=$dewey"});
104 ($branch) && (push @$forminputs, { line => "branch=$branch"});
105 ($title) && (push @$forminputs, { line => "title=$title"});
106 ($ttype) && (push @$forminputs, { line => "ttype=$ttype"});
107 ($abstract) && (push @$forminputs, { line => "abstract=$abstract"});
108 ($publisher) && (push @$forminputs, { line => "publisher=$publisher"});
109 ($forminputs) || (@$forminputs=());
110 $template->param(FORMINPUTS => $forminputs);
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 ($keyword){
140     $search=$search."&keyword=$keyword";
141     $searchdesc.="keyword $keyword, ";
142 }
143 if (my $subjectitems=$query->param('subjectitems')){
144     $search=$search."&subjectitems=$subjectitems";
145     $searchdesc.="subject $subjectitems, ";
146 }
147 if ($subject){
148     $search=$search."&subject=$subject";
149     $searchdesc.="subject $subject, ";
150 }
151 if ($author){
152     $search=$search."&author=$author";
153     $searchdesc.="author $author, ";
154 }
155 if ($class){
156     $search=$search."&class=$class";
157     $searchdesc.="class $class, ";
158 }
159 if ($title){
160     $search=$search."&title=$title";
161     $searchdesc.="title $title, ";
162 }
163 if ($dewey){
164     $search=$search."&dewey=$dewey";
165     $searchdesc.="dewey $dewey, ";
166 }
167 $search.="&ttype=$ttype";
168
169 $search=~ s/ /%20/g;
170 $template->param(startfrom => $startfrom+1);
171 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
172 $template->param(numrecords => $count);
173 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
174 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
175 $template->param(nextstartfrom => $nextstartfrom);
176 my $displaynext=1;
177 my $displayprev=0;
178 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
179 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
180 $template->param(displaynext => $displaynext);
181 $template->param(displayprev => $displayprev);
182 ($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
183 $template->param(prevstartfrom => $prevstartfrom);
184 $template->param(search => $search);
185 $template->param(searchdesc => $searchdesc);
186 $template->param(SEARCH_RESULTS => $resultsarray);
187 $template->param(includesdir => $includes);
188 $template->param(loggedinuser => $loggedinuser);
189
190 my $numbers;
191 @$numbers=();
192 if ($count>10) {
193     for (my $i=1; $i<$count/10+1; $i++) {
194     ($title) && (push @$forminputs, { line => "title=$title"});
195         my $highlight=0;
196         ($startfrom==($i-1)*10) && ($highlight=1);
197         my $formelements='';
198         foreach (@$forminputs) {
199             my $line=$_->{line};
200             $formelements.="$line&";
201         }
202         $formelements=~s/ /+/g;
203         push @$numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => $forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
204     }
205 }
206
207 $template->param(numbers => $numbers);
208
209
210
211 print $query->header(-cookie => $cookie), $template->output;
212