Glens updates to automatically increment barcode, and to remove hardcoded itemtypes...
[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 usable by opac as well
6 #modified by chris 10/11/00 to fix dewey search
7 #modified by chris@katipo.co.nz 3/2/01 to fix glitch with " in titles
8
9 use strict;
10 use C4::Search;
11 use CGI;
12 use C4::Output;
13
14 my $env;
15 my $input = new CGI;
16 print $input->header;
17 #print $input->dump;
18 #whether it is called from the opac of the intranet                                                            
19 my $type=$input->param('type');                                                  
20 if ($type eq ''){
21   $type = 'intra';
22 }
23 my $ttype=$input->param('ttype');
24 #setup colours                                                                                                 
25 my $main;                                                                                                      
26 my $secondary;                                                                                                 
27 if ($type eq 'opac'){                                                                                          
28   $main='#99cccc';    
29   $secondary='#efe5ef';
30 } else {                                                                                                       
31   $main='#cccc99';                                                                                             
32   $secondary='#ffffcc';                                                                                        
33 }       
34
35 #print $input->dump;
36 my $blah;
37 my %search;
38 #build hash of users input
39 my $title=validate($input->param('title'));
40 $search{'title'}=$title;
41 my $keyword=validate($input->param('keyword'));
42 $search{'keyword'}=$keyword;
43 $search{'front'}=validate($input->param('front'));
44
45 my $author=validate($input->param('author'));
46 $search{'author'}=$author;
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      $search{'keyword'}=$search{'front'};
91     ($count,@results)&KeywordSearch(\$blah,'intra',\%search,$num,$offset);
92 #    print "hey";
93     }elsif ($title ne '' || $author ne '' || $dewey ne '' || $class ne '') {
94       ($count,@results)=&CatSearch(\$blah,'loose',\%search,$num,$offset);
95 #            print "hey";
96     }
97   }
98 }
99 print "You searched on ";
100 while ( my ($key, $value) = each %search) {                                 
101   if ($value ne '' && $key ne 'ttype'){
102     $value=~ s/\\//g;
103     print bold("$key $value,");
104   }                          
105 }
106 print " $count results found";
107 my $offset2=$num+$offset;
108 my $dispnum=$offset+1;
109 print "<br> Results $dispnum to $offset2 displayed";
110 print mktablehdr;
111 if ($type ne 'opac'){
112   if ($subject ne ''){
113    print mktablerow(1,$main,'<b>SUBJECT</b>','/images/background-mem.gif');
114   } else {
115    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
116   }
117 } else {
118   if ($subject ne ''){
119    print mktablerow(6,$main,'<b>SUBJECT</b>',' &nbsp; ',' &nbsp; ');
120   } else {
121    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
122   }
123 }
124 my $count2=@results;
125 if ($keyword ne '' && $offset > 0){
126   $count2=$count-$offset;
127   if ($count2 > 10){
128     $count2=10;
129   }
130 }
131 #print $count2;
132 my $i=0;
133 my $colour=1;
134 while ($i < $count2){
135 #    print $results[$i]."\n";
136     my @stuff=split('\t',$results[$i]);
137     $stuff[1]=~ s/\`/\\\'/g;
138     my $title2=$stuff[1];
139     $title2=~ s/ /%20/g;
140     $title2=~ s/\W//g;
141     if ($subject eq ''){
142 #      print $stuff[0];
143       $stuff[1]=mklink("/cgi-bin/koha/detail.pl?type=$type&bib=$stuff[2]&title=$title2",$stuff[1]);
144       my $word=$stuff[0];
145 #      print $word;
146       $word=~ s/([a-z]) +([a-z])/$1%20$2/ig;
147       $word=~ s/  //g;
148       $word=~ s/ /%20/g;
149       $word=~ s/\,/\,%20/g;
150       $word=~ s/\n//g;
151       my $url="/cgi-bin/koha/search.pl?author=$word&type=$type";
152       $stuff[0]=mklink($url,$stuff[0]);
153       my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount)=itemcount($env,$stuff[2],$type);
154       $stuff[4]=$count;
155       if ($nacount > 0){
156         $stuff[5]=$stuff[5]."On Loan";
157         if ($nacount >1 ){                                                                                                         
158           $stuff[5]=$stuff[5]." ($nacount)";                                                                                            
159          }                                                                                                                         
160          $stuff[5].=" ";
161       }
162       if ($lcount > 0){
163          $stuff[5]=$stuff[5]."Levin";
164          if ($lcount >1 ){                                                                                                         
165           $stuff[5]=$stuff[5]." ($lcount)";                                                                                            
166          }                                                                                                                         
167          $stuff[5].=" ";
168       }
169       if ($fcount > 0){
170         $stuff[5]=$stuff[5]."Foxton";
171          if ($fcount >1 ){                                                                                                         
172           $stuff[5]=$stuff[5]." ($fcount)";                                                                                            
173          }                                                                                                                         
174          $stuff[5].=" ";        
175       }
176       if ($scount > 0){
177         $stuff[5]=$stuff[5]."Shannon";
178          if ($scount >1 ){                                                                                                         
179           $stuff[5]=$stuff[5]." ($scount)";                                                                                            
180          }                                                                                                                         
181          $stuff[5].=" ";        
182       }
183       if ($lostcount > 0){
184         $stuff[5]=$stuff[5]."Lost";
185          if ($lostcount >1 ){                                                                                                         
186           $stuff[5]=$stuff[5]." ($lostcount)";                                                                                            
187          }                                                                                                                         
188          $stuff[5].=" ";        
189       }
190       if ($mending > 0){
191         $stuff[5]=$stuff[5]."Mending";
192          if ($mending >1 ){                                                                                                         
193           $stuff[5]=$stuff[5]." ($mending)";                                                                                            
194          }                                                                                                                         
195          $stuff[5].=" ";        
196       }
197       if ($transit > 0){
198         $stuff[5]=$stuff[5]."In Transiit";
199          if ($transit >1 ){                                                                                                         
200           $stuff[5]=$stuff[5]." ($transit)";                                                                                            
201          }                                                                                                                         
202          $stuff[5].=" ";        
203       }
204       if ($ocount > 0){
205         $stuff[5]=$stuff[5]."On Order";
206          if ($ocount >1 ){                                                                                                         
207           $stuff[5]=$stuff[5]." ($ocount)";                                                                                            
208          }                                                                                                                         
209          $stuff[5].=" ";        
210       }
211       
212       if ($type ne 'opac'){
213         $stuff[6]=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
214       }
215     } else {
216       my $word=$stuff[1];
217       $word=~ s/ /%20/g;
218       
219         $stuff[1]=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$stuff[1]);
220
221     }
222
223     if ($colour == 1){
224       print mktablerow(6,$secondary,$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
225       $colour=0;
226     } else{
227       print mktablerow(6,'white',$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
228       $colour=1;
229     }
230     $i++;
231 }
232 $offset=$num+$offset;
233 if ($type ne 'opac'){
234  print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
235 } else {
236  print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
237 }
238 print mktableft();
239 my $search;
240
241     $search="num=$num&offset=$offset&type=$type";
242     if ($subject ne ''){
243       $subject=~ s/ /%20/g;
244       $search=$search."&subject=$subject";
245     }
246     if ($title ne ''){
247       $title=~ s/ /%20/g;
248       $search=$search."&title=$title";
249     }
250     if ($author ne ''){
251       $author=~ s/ /%20/g;
252       $search=$search."&author=$author";
253     }
254     if ($keyword ne ''){
255       $keyword=~ s/ /%20/g;
256       $search=$search."&keyword=$keyword";
257     }
258     if ($class ne ''){
259       $keyword=~ s/ /%20/g;
260       $search=$search."&class=$class";
261     }
262     if ($dewey ne ''){
263       $search=$search."&dewey=$dewey";
264     }
265     $search.="&ttype=$ttype";    
266 if ($offset < $count){    
267     my $stuff=mklink("/cgi-bin/koha/search.pl?$search",'Next');
268     print $stuff;
269 }
270 print "<br>";
271 my $pages=$count/10;
272 $pages++;
273 for (my $i=1;$i<$pages;$i++){
274   my $temp=$i*10;
275   $temp=$temp-10;
276   $search=~ s/offset=[0-9]+/offset=$temp/;
277   my $stuff=mklink("/cgi-bin/koha/search.pl?$search",$i);
278   print "$stuff ";
279 }
280   
281 print endcenter();
282 print endmenu($type);
283 print endpage();
284
285
286 sub validate {
287   my ($input)=@_;
288   $input=~ s/\<[a-z]+\>//gi;
289   $input=~ s/\<\/[a-z]+\>//gi;
290   $input=~ s/\<//g;
291   $input=~ s/\>//g;
292   $input=~ s/^%//g;
293   return($input);
294 }