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