Fixed bug where location wouldn't show up in search results. Should still
[koha.git] / search.pl
1 #!/usr/bin/perl
2 #script to provide intranet (librarian) advanced search facility
3
4 use strict;
5 use C4::Search;
6 use CGI;
7 use C4::Output;
8
9 my $env;
10 my $input = new CGI;
11 print $input->header;
12 #print $input->dump;
13
14 #whether it is called from the opac or the intranet
15 my $type=$input->param('type');if ($type eq ''){
16   $type = 'intra';
17 }
18
19 my $ttype=$input->param('ttype');
20
21 #setup colours                 
22 my $main;
23 my $secondary;
24
25 if ($type eq 'opac'){
26   $main='#99cccc';    
27   $secondary='#efe5ef';
28 } else {
29   $main='#cccc99';
30   $secondary='#ffffcc';
31 }       
32
33 #print $input->Dump;
34 my $blah;
35 my %search;
36
37 #build hash of users input
38 my $title=validate($input->param('title'));
39 $search{'title'}=$title;
40
41 my $keyword=validate($input->param('keyword'));
42 $search{'keyword'}=$keyword;
43
44 $search{'front'}=validate($input->param('front'));
45
46 my $author=validate($input->param('author'));
47 $search{'author'}=$author;
48
49 my $illustrator=validate($input->param('illustrator'));
50 $search{'illustrator'}=$illustrator;
51
52 my $subject=validate($input->param('subject'));
53 $search{'subject'}=$subject;
54
55 my $itemnumber=validate($input->param('item'));
56 $search{'item'}=$itemnumber;
57
58 my $isbn=validate($input->param('isbn'));
59 $search{'isbn'}=$isbn;
60
61 my $datebefore=validate($input->param('date-before'));
62 $search{'date-before'}=$datebefore;
63
64 my $class=$input->param('class');
65 $search{'class'}=$class;
66
67 $search{'ttype'}=$ttype;
68
69 my $dewey=validate($input->param('dewey'));
70 $search{'dewey'}=$dewey;
71
72 my $branch=validate($input->param('branch'));
73 $search{'branch'}=$branch;
74
75 my @results;
76 my $offset=$input->param('offset');
77 if ($offset eq ''){
78   $offset=0;
79 }
80 my $num=$input->param('num');
81 if ($num eq ''){
82   $num=10;
83 }
84 print startpage();
85 print startmenu($type);
86 #print $type;
87 #print $search{'ttype'};
88 if ($type eq 'intra'){
89   print mkheadr(1,'Catalogue Search Results');
90 } elsif ($type eq 'catmain'){
91   print mkheadr(1,'Catalogue Maintenance');
92 } else {
93   print mkheadr(1,'Opac Search Results');
94 }
95 print center();
96 my $count;
97 if ($itemnumber ne '' || $isbn ne ''){
98     ($count,@results)=&CatSearch(\$blah,'precise',\%search,$num,$offset);
99 } else {
100   if ($subject ne ''){
101     ($count,@results)=&CatSearch(\$blah,'subject',\%search,$num,$offset);
102   } else {
103     if ($keyword ne ''){
104       ($count,@results)=&KeywordSearch(\$blah,'intra',\%search,$num,$offset);
105     }elsif ($title ne '' || $author ne '' || $illustrator ne '' || $dewey ne '' || $class ne '') {
106       ($count,@results)=&CatSearch(\$blah,'loose',\%search,$num,$offset);
107     }
108   }
109 }
110 print "You searched on ";
111 while ( my ($key, $value) = each %search) {                                 
112   if ($value ne '' && $key ne 'ttype'){
113     $value=~ s/\\//g;
114     print bold("$key $value,");
115   }                          
116 }
117 print " $count results found";
118 my $offset2=$num+$offset;
119 my $dispnum=$offset+1;
120 print "<br> Results $dispnum to $offset2 displayed";
121 print mktablehdr;
122 if ($type ne 'opac'){
123   if ($subject ne ''){
124    print mktablerow(1,$main,'<b>SUBJECT</b>','/images/background-mem.gif');
125   } elsif ($illustrator ne '') {
126    print mktablerow(7,$main,'<b>TITLE</b>','<b>AUTHOR</b>', '<b>ILLUSTRATOR<b>', bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
127   } else {
128    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
129   }
130 } else {
131   if ($subject ne ''){
132    print mktablerow(6,$main,'<b>SUBJECT</b>',' &nbsp; ',' &nbsp; ');
133   } elsif ($illustrator ne '') {
134    print mktablerow(7,$main,'<b>TITLE</b>','<b>AUTHOR</b>','<b>ILLUSTRATOR</b>', bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
135   } else {
136    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
137   }
138 }
139 my $count2=@results;
140 if ($keyword ne '' && $offset > 0){
141   $count2=$count-$offset;
142   if ($count2 > 10){
143     $count2=10;
144   }
145 }
146 #print $count2;
147 my $i=0;
148 my $colour=1;
149 while ($i < $count2){
150 #    print $results[$i]."\n";
151     my @stuff=split('\t',$results[$i]);
152     $stuff[1]=~ s/\`/\\\'/g;
153     my $title2=$stuff[1];
154     $title2=~ s/ /%20/g;
155     if ($subject eq ''){
156 #      print $stuff[0];
157       $stuff[1]=mklink("/cgi-bin/koha/detail.pl?type=$type&bib=$stuff[2]&title=$title2",$stuff[1]);
158       my $word=$stuff[0];
159 #      print $word;
160       $word=~ s/([a-z]) +([a-z])/$1%20$2/ig;
161       $word=~ s/  //g;
162       $word=~ s/ /%20/g;
163       $word=~ s/\,/\,%20/g;
164       $word=~ s/\n//g;
165       my $url="/cgi-bin/koha/search.pl?author=$word&type=$type";
166       $stuff[7]=$stuff[5];
167       $stuff[5]='';
168       $stuff[0]=mklink($url,$stuff[0]);
169       my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount,$branchcount)=itemcount($env,$stuff[2],$type);
170       $stuff[4]=$count;
171       if ($nacount > 0){
172         $stuff[5]=$stuff[5]."On Loan";
173         if ($nacount >1 ){                                                                                                         
174           $stuff[5]=$stuff[5]." ($nacount)";                                                                                            
175          }                                                                                                                         
176          $stuff[5].=" ";
177       }
178       if ($lcount > 0){
179          $stuff[5]=$stuff[5]."Levin";
180          if ($lcount >1 ){                                                                                                         
181           $stuff[5]=$stuff[5]." ($lcount)";                                                                                            
182          }                                                                                                                         
183          $stuff[5].=" ";
184       }
185       if ($fcount > 0){
186         $stuff[5]=$stuff[5]."Foxton";
187          if ($fcount >1 ){                                                                                                         
188           $stuff[5]=$stuff[5]." ($fcount)";                                                                                            
189          }                                                                                                                         
190          $stuff[5].=" ";        
191       }
192       if ($scount > 0){
193         $stuff[5]=$stuff[5]."Shannon";
194          if ($scount >1 ){                                                                                                         
195           $stuff[5]=$stuff[5]." ($scount)";                                                                                            
196          }                                                                                                                         
197          $stuff[5].=" ";        
198       }
199       $stuff[5]='';
200       foreach my $branchcode (sort keys %$branchcount) {
201           my $branchname=branchname($branchcode);
202           my $c=$branchcount->{$branchcode};
203           $stuff[5].="$branchname";
204           if ($c>1) {
205               $stuff[5].=" ($c)";
206           }
207           $stuff[5].=" ";
208       }
209       if ($lostcount > 0) {
210         $stuff[5]=$stuff[5]."Lost";
211          if ($lostcount >1 ){                                                                                                         
212           $stuff[5]=$stuff[5]." ($lostcount)";                                                                                            
213          }                                                                                                                         
214          $stuff[5].=" ";        
215       }
216       if ($mending > 0){
217         $stuff[5]=$stuff[5]."Mending";
218          if ($mending >1 ){                                                                                                         
219           $stuff[5]=$stuff[5]." ($mending)";                                                                                            
220          }                                                                                                                         
221          $stuff[5].=" ";        
222       }
223       if ($transit > 0){
224         $stuff[5]=$stuff[5]."In Transiit";
225          if ($transit >1 ){                                                                                                         
226           $stuff[5]=$stuff[5]." ($transit)";                                                                                            
227          }                                                                                                                         
228          $stuff[5].=" ";        
229       }
230       if ($ocount > 0){
231         $stuff[5]=$stuff[5]."On Order";
232          if ($ocount >1 ){                                                                                                         
233           $stuff[5]=$stuff[5]." ($ocount)";                                                                                            
234          }                                                                                                                         
235          $stuff[5].=" ";        
236       }
237       
238       if ($type ne 'opac'){
239         $stuff[6]=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
240       }
241     } else {
242       my $word=$stuff[1];
243       $word=~ s/ /%20/g;
244       
245         $stuff[1]=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$stuff[1]);
246
247     }
248
249     if ($colour == 1){
250       if ($illustrator) {
251           print mktablerow(7,$secondary,$stuff[1],$stuff[0],$stuff[7],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
252       } else {
253           print mktablerow(6,$secondary,$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
254       }
255       $colour=0;
256     } else {
257       if ($illustrator) {
258           print mktablerow(7,'white',$stuff[1],$stuff[0],$stuff[7],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
259       } else {
260           print mktablerow(6,'white',$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
261       }
262       $colour=1;
263     }
264     $i++;
265 }
266 $offset=$num+$offset;
267 if ($type ne 'opac'){
268     if ($illustrator) {
269          print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','','/images/background-mem.gif');
270     } else {
271          print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
272     }
273 } else {
274  if ($illustrator) {
275      print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','', '','');
276  } else {
277      print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
278  }
279 }
280 print mktableft();
281 my $search;
282
283     $search="num=$num&offset=$offset&type=$type";
284     if ($subject ne ''){
285       $subject=~ s/ /%20/g;
286       $search=$search."&subject=$subject";
287     }
288     if ($title ne ''){
289       $title=~ s/ /%20/g;
290       $search=$search."&title=$title";
291     }
292     if ($author ne ''){
293       $author=~ s/ /%20/g;
294       $search=$search."&author=$author";
295     }
296     if ($keyword ne ''){
297       $keyword=~ s/ /%20/g;
298       $search=$search."&keyword=$keyword";
299     }
300     if ($class ne ''){
301       $keyword=~ s/ /%20/g;
302       $search=$search."&class=$class";
303     }
304     if ($dewey ne ''){
305       $search=$search."&dewey=$dewey";
306     }
307     $search.="&ttype=$ttype";    
308 if ($offset < $count){    
309     my $stuff=mklink("/cgi-bin/koha/search.pl?$search",'Next');
310     print $stuff;
311 }
312 print "<br>";
313 my $pages=$count/10;
314 $pages++;
315 for (my $i=1;$i<$pages;$i++){
316   my $temp=$i*10;
317   $temp=$temp-10;
318   $search=~ s/offset=[0-9]+/offset=$temp/;
319   my $stuff=mklink("/cgi-bin/koha/search.pl?$search",$i);
320   print "$stuff ";
321 }
322   
323 print endcenter();
324 print endmenu($type);
325 print endpage();
326
327
328 sub validate {
329   my ($input)=@_;
330   $input=~ s/\<[a-z]+\>//gi;
331   $input=~ s/\<\/[a-z]+\>//gi;
332   $input=~ s/\<//g;
333   $input=~ s/\>//g;
334   $input=~ s/^%//g;
335   return($input);
336 }