refactored slashifyDate function out of inline code, created C4/Koha.pm
[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'};
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 my @results;
98 if ($itemnumber ne '' || $isbn ne ''){
99     ($count,@results)=&CatSearch(\$blah,'precise',\%search,$num,$offset);
100 } else {
101   if ($subject ne ''){
102     ($count,@results)=&CatSearch(\$blah,'subject',\%search,$num,$offset);
103   } else {
104     if ($keyword ne ''){
105       ($count,@results)=&KeywordSearch(\$blah,'intra',\%search,$num,$offset);
106     }elsif ($title ne '' || $author ne '' || $illustrator ne '' || $dewey ne '' || $class ne '') {
107       ($count,@results)=&CatSearch(\$blah,'loose',\%search,$num,$offset);
108     }
109   }
110 }
111 print "You searched on ";
112 while ( my ($key, $value) = each %search) {                                 
113   if ($value ne '' && $key ne 'ttype'){
114     $value=~ s/\\//g;
115     print bold("$key $value,");
116   }                          
117 }
118 print " $count results found";
119 my $offset2=$num+$offset;
120 my $dispnum=$offset+1;
121 print "<br> Results $dispnum to $offset2 displayed";
122 print mktablehdr;
123 if ($type ne 'opac'){
124   if ($subject ne ''){
125    print mktablerow(1,$main,'<b>SUBJECT</b>','/images/background-mem.gif');
126   } elsif ($illustrator ne '') {
127    print mktablerow(7,$main,'<b>TITLE</b>','<b>AUTHOR</b>', '<b>ILLUSTRATOR<b>', bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
128   } else {
129    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
130   }
131 } else {
132   if ($subject ne ''){
133    print mktablerow(6,$main,'<b>SUBJECT</b>',' &nbsp; ',' &nbsp; ');
134   } elsif ($illustrator ne '') {
135    print mktablerow(7,$main,'<b>TITLE</b>','<b>AUTHOR</b>','<b>ILLUSTRATOR</b>', bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
136   } else {
137    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
138   }
139 }
140 my $count2=@results;
141 if ($keyword ne '' && $offset > 0){
142   $count2=$count-$offset;
143   if ($count2 > 10){
144     $count2=10;
145   }
146 }
147 #print $count2;
148 my $i=0;
149 my $colour=1;
150 while ($i < $count2){
151 #    print $results[$i]."\n";
152     my @stuff=split('\t',$results[$i]);
153     $stuff[1]=~ s/\`/\\\'/g;
154     my $title2=$stuff[1];
155     $title2=~ s/ /%20/g;
156     if ($subject eq ''){
157 #      print $stuff[0];
158       $stuff[1]=mklink("/cgi-bin/koha/detail.pl?type=$type&bib=$stuff[2]&title=$title2",$stuff[1]);
159       my $word=$stuff[0];
160 #      print $word;
161       $word=~ s/([a-z]) +([a-z])/$1%20$2/ig;
162       $word=~ s/  //g;
163       $word=~ s/ /%20/g;
164       $word=~ s/\,/\,%20/g;
165       $word=~ s/\n//g;
166       my $url="/cgi-bin/koha/search.pl?author=$word&type=$type";
167       $stuff[7]=$stuff[5];
168       $stuff[5]='';
169       $stuff[0]=mklink($url,$stuff[0]);
170       my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount)=itemcount($env,$stuff[2],$type);
171       $stuff[4]=$count;
172       if ($nacount > 0){
173         $stuff[5]=$stuff[5]."On Loan";
174         if ($nacount >1 ){                                                                                                         
175           $stuff[5]=$stuff[5]." ($nacount)";                                                                                            
176          }                                                                                                                         
177          $stuff[5].=" ";
178       }
179       if ($lcount > 0){
180          $stuff[5]=$stuff[5]."Levin";
181          if ($lcount >1 ){                                                                                                         
182           $stuff[5]=$stuff[5]." ($lcount)";                                                                                            
183          }                                                                                                                         
184          $stuff[5].=" ";
185       }
186       if ($fcount > 0){
187         $stuff[5]=$stuff[5]."Foxton";
188          if ($fcount >1 ){                                                                                                         
189           $stuff[5]=$stuff[5]." ($fcount)";                                                                                            
190          }                                                                                                                         
191          $stuff[5].=" ";        
192       }
193       if ($scount > 0){
194         $stuff[5]=$stuff[5]."Shannon";
195          if ($scount >1 ){                                                                                                         
196           $stuff[5]=$stuff[5]." ($scount)";                                                                                            
197          }                                                                                                                         
198          $stuff[5].=" ";        
199       }
200       if ($lostcount > 0){
201         $stuff[5]=$stuff[5]."Lost";
202          if ($lostcount >1 ){                                                                                                         
203           $stuff[5]=$stuff[5]." ($lostcount)";                                                                                            
204          }                                                                                                                         
205          $stuff[5].=" ";        
206       }
207       if ($mending > 0){
208         $stuff[5]=$stuff[5]."Mending";
209          if ($mending >1 ){                                                                                                         
210           $stuff[5]=$stuff[5]." ($mending)";                                                                                            
211          }                                                                                                                         
212          $stuff[5].=" ";        
213       }
214       if ($transit > 0){
215         $stuff[5]=$stuff[5]."In Transiit";
216          if ($transit >1 ){                                                                                                         
217           $stuff[5]=$stuff[5]." ($transit)";                                                                                            
218          }                                                                                                                         
219          $stuff[5].=" ";        
220       }
221       if ($ocount > 0){
222         $stuff[5]=$stuff[5]."On Order";
223          if ($ocount >1 ){                                                                                                         
224           $stuff[5]=$stuff[5]." ($ocount)";                                                                                            
225          }                                                                                                                         
226          $stuff[5].=" ";        
227       }
228       
229       if ($type ne 'opac'){
230         $stuff[6]=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
231       }
232     } else {
233       my $word=$stuff[1];
234       $word=~ s/ /%20/g;
235       
236         $stuff[1]=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$stuff[1]);
237
238     }
239
240     if ($colour == 1){
241       if ($illustrator) {
242           print mktablerow(7,$secondary,$stuff[1],$stuff[0],$stuff[7],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
243       } else {
244           print mktablerow(6,$secondary,$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
245       }
246       $colour=0;
247     } else {
248       if ($illustrator) {
249           print mktablerow(7,'white',$stuff[1],$stuff[0],$stuff[7],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
250       } else {
251           print mktablerow(6,'white',$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
252       }
253       $colour=1;
254     }
255     $i++;
256 }
257 $offset=$num+$offset;
258 if ($type ne 'opac'){
259     if ($illustrator) {
260          print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','','/images/background-mem.gif');
261     } else {
262          print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
263     }
264 } else {
265  if ($illustrator) {
266      print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','', '','');
267  } else {
268      print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
269  }
270 }
271 print mktableft();
272 my $search;
273
274     $search="num=$num&offset=$offset&type=$type";
275     if ($subject ne ''){
276       $subject=~ s/ /%20/g;
277       $search=$search."&subject=$subject";
278     }
279     if ($title ne ''){
280       $title=~ s/ /%20/g;
281       $search=$search."&title=$title";
282     }
283     if ($author ne ''){
284       $author=~ s/ /%20/g;
285       $search=$search."&author=$author";
286     }
287     if ($keyword ne ''){
288       $keyword=~ s/ /%20/g;
289       $search=$search."&keyword=$keyword";
290     }
291     if ($class ne ''){
292       $keyword=~ s/ /%20/g;
293       $search=$search."&class=$class";
294     }
295     if ($dewey ne ''){
296       $search=$search."&dewey=$dewey";
297     }
298     $search.="&ttype=$ttype";    
299 if ($offset < $count){    
300     my $stuff=mklink("/cgi-bin/koha/search.pl?$search",'Next');
301     print $stuff;
302 }
303 print "<br>";
304 my $pages=$count/10;
305 $pages++;
306 for (my $i=1;$i<$pages;$i++){
307   my $temp=$i*10;
308   $temp=$temp-10;
309   $search=~ s/offset=[0-9]+/offset=$temp/;
310   my $stuff=mklink("/cgi-bin/koha/search.pl?$search",$i);
311   print "$stuff ";
312 }
313   
314 print endcenter();
315 print endmenu($type);
316 print endpage();
317
318
319 sub validate {
320   my ($input)=@_;
321   $input=~ s/\<[a-z]+\>//gi;
322   $input=~ s/\<\/[a-z]+\>//gi;
323   $input=~ s/\<//g;
324   $input=~ s/\>//g;
325   $input=~ s/^%//g;
326   return($input);
327 }