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