Changing the front page search of the intranet site to just be a keyword search
[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
44 my $author=validate($input->param('author'));
45 $search{'author'}=$author;
46 my $subject=validate($input->param('subject'));
47 $search{'subject'}=$subject;
48 my $itemnumber=validate($input->param('item'));
49 $search{'item'}=$itemnumber;
50 my $isbn=validate($input->param('isbn'));
51 $search{'isbn'}=$isbn;
52 my $datebefore=validate($input->param('date-before'));
53 $search{'date-before'};
54 my $class=$input->param('class');
55 $search{'class'}=$class;
56 $search{'ttype'}=$ttype;
57 my $dewey=validate($input->param('dewey'));
58 $search{'dewey'}=$dewey;
59 my @results;
60 my $offset=$input->param('offset');
61 if ($offset eq ''){
62   $offset=0;
63 }
64 my $num=$input->param('num');
65 if ($num eq ''){
66   $num=10;
67 }
68 print startpage();
69 print startmenu($type);
70 #print $search{'ttype'};
71 if ($type ne 'opac'){
72   print mkheadr(1,'Catalogue Search Results');
73 } else {
74   print mkheadr(1,'Opac Search Results');
75 }
76 print center();
77 my $count;
78 my @results;
79 if ($itemnumber ne '' || $isbn ne ''){
80     ($count,@results)=&CatSearch(\$blah,'precise',\%search,$num,$offset);
81 } else {
82   if ($subject ne ''){
83     ($count,@results)=&CatSearch(\$blah,'subject',\%search,$num,$offset);
84   } else {
85     if ($keyword ne ''){
86 #      print "hey";
87       ($count,@results)=&KeywordSearch(\$blah,'intra',\%search,$num,$offset);
88     } elsif ($search{'front'} ne '') {
89      $search{'keyword'}=$search{'front'};
90     ($count,@results)&KeywordSearch(\$blah,'intra',\%search,$num,$offset);
91 #    print "hey";
92     }elsif ($title ne '' || $author 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   } else {
114    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
115   }
116 } else {
117   if ($subject ne ''){
118    print mktablerow(6,$main,'<b>SUBJECT</b>',' &nbsp; ',' &nbsp; ');
119   } else {
120    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
121   }
122 }
123 my $count2=@results;
124 if ($keyword ne '' && $offset > 0){
125   $count2=$count-$offset;
126   if ($count2 > 10){
127     $count2=10;
128   }
129 }
130 #print $count2;
131 my $i=0;
132 my $colour=1;
133 while ($i < $count2){
134 #    print $results[$i]."\n";
135     my @stuff=split('\t',$results[$i]);
136     $stuff[1]=~ s/\`/\\\'/g;
137     my $title2=$stuff[1];
138     $title2=~ s/ /%20/g;
139     if ($subject eq ''){
140 #      print $stuff[0];
141       $stuff[1]=mklink("/cgi-bin/koha/detail.pl?type=$type&bib=$stuff[2]&title=$title2",$stuff[1]);
142       my $word=$stuff[0];
143 #      print $word;
144       $word=~ s/([a-z]) +([a-z])/$1%20$2/ig;
145       $word=~ s/  //g;
146       $word=~ s/ /%20/g;
147       $word=~ s/\,/\,%20/g;
148       $word=~ s/\n//g;
149       my $url="/cgi-bin/koha/search.pl?author=$word&type=$type";
150       $stuff[0]=mklink($url,$stuff[0]);
151       my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount)=itemcount($env,$stuff[2],$type);
152       $stuff[4]=$count;
153       if ($nacount > 0){
154         $stuff[5]=$stuff[5]."On Loan";
155         if ($nacount >1 ){                                                                                                         
156           $stuff[5]=$stuff[5]." ($nacount)";                                                                                            
157          }                                                                                                                         
158          $stuff[5].=" ";
159       }
160       if ($lcount > 0){
161          $stuff[5]=$stuff[5]."Levin";
162          if ($lcount >1 ){                                                                                                         
163           $stuff[5]=$stuff[5]." ($lcount)";                                                                                            
164          }                                                                                                                         
165          $stuff[5].=" ";
166       }
167       if ($fcount > 0){
168         $stuff[5]=$stuff[5]."Foxton";
169          if ($fcount >1 ){                                                                                                         
170           $stuff[5]=$stuff[5]." ($fcount)";                                                                                            
171          }                                                                                                                         
172          $stuff[5].=" ";        
173       }
174       if ($scount > 0){
175         $stuff[5]=$stuff[5]."Shannon";
176          if ($scount >1 ){                                                                                                         
177           $stuff[5]=$stuff[5]." ($scount)";                                                                                            
178          }                                                                                                                         
179          $stuff[5].=" ";        
180       }
181       if ($lostcount > 0){
182         $stuff[5]=$stuff[5]."Lost";
183          if ($lostcount >1 ){                                                                                                         
184           $stuff[5]=$stuff[5]." ($lostcount)";                                                                                            
185          }                                                                                                                         
186          $stuff[5].=" ";        
187       }
188       if ($mending > 0){
189         $stuff[5]=$stuff[5]."Mending";
190          if ($mending >1 ){                                                                                                         
191           $stuff[5]=$stuff[5]." ($mending)";                                                                                            
192          }                                                                                                                         
193          $stuff[5].=" ";        
194       }
195       if ($transit > 0){
196         $stuff[5]=$stuff[5]."In Transiit";
197          if ($transit >1 ){                                                                                                         
198           $stuff[5]=$stuff[5]." ($transit)";                                                                                            
199          }                                                                                                                         
200          $stuff[5].=" ";        
201       }
202       if ($ocount > 0){
203         $stuff[5]=$stuff[5]."On Order";
204          if ($ocount >1 ){                                                                                                         
205           $stuff[5]=$stuff[5]." ($ocount)";                                                                                            
206          }                                                                                                                         
207          $stuff[5].=" ";        
208       }
209       
210       if ($type ne 'opac'){
211         $stuff[6]=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
212       }
213     } else {
214       my $word=$stuff[1];
215       $word=~ s/ /%20/g;
216       
217         $stuff[1]=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$stuff[1]);
218
219     }
220
221     if ($colour == 1){
222       print mktablerow(6,$secondary,$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
223       $colour=0;
224     } else{
225       print mktablerow(6,'white',$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
226       $colour=1;
227     }
228     $i++;
229 }
230 $offset=$num+$offset;
231 if ($type ne 'opac'){
232  print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
233 } else {
234  print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
235 }
236 print mktableft();
237 my $search;
238
239     $search="num=$num&offset=$offset&type=$type";
240     if ($subject ne ''){
241       $subject=~ s/ /%20/g;
242       $search=$search."&subject=$subject";
243     }
244     if ($title ne ''){
245       $title=~ s/ /%20/g;
246       $search=$search."&title=$title";
247     }
248     if ($author ne ''){
249       $author=~ s/ /%20/g;
250       $search=$search."&author=$author";
251     }
252     if ($keyword ne ''){
253       $keyword=~ s/ /%20/g;
254       $search=$search."&keyword=$keyword";
255     }
256     if ($class ne ''){
257       $keyword=~ s/ /%20/g;
258       $search=$search."&class=$class";
259     }
260     if ($dewey ne ''){
261       $search=$search."&dewey=$dewey";
262     }
263     $search.="&ttype=$ttype";    
264 if ($offset < $count){    
265     my $stuff=mklink("/cgi-bin/koha/search.pl?$search",'Next');
266     print $stuff;
267 }
268 print "<br>";
269 my $pages=$count/10;
270 $pages++;
271 for (my $i=1;$i<$pages;$i++){
272   my $temp=$i*10;
273   $temp=$temp-10;
274   $search=~ s/offset=[0-9]+/offset=$temp/;
275   my $stuff=mklink("/cgi-bin/koha/search.pl?$search",$i);
276   print "$stuff ";
277 }
278   
279 print endcenter();
280 print endmenu($type);
281 print endpage();
282
283
284 sub validate {
285   my ($input)=@_;
286   $input=~ s/\<[a-z]+\>//gi;
287   $input=~ s/\<\/[a-z]+\>//gi;
288   $input=~ s/\<//g;
289   $input=~ s/\>//g;
290   $input=~ s/^%//g;
291   return($input);
292 }