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