Added checking for option to run unsafe database tests. The idea is that tests
[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     my $result=$results[$i];
154     $result->{'title'}=~ s/\`/\\\'/g;
155     my $title2=$result->{'title'};
156     $title2=~ s/ /%20/g;
157     my $location='';
158     my $itemcount;
159     if ($subject eq ''){
160       $result->{'title'}=mklink("/cgi-bin/koha/detail.pl?type=$type&bib=$result->{'biblionumber'}&title=$title2",$result->{'title'});
161       my $word=$result->{'author'};
162       $word=~ s/([a-z]) +([a-z])/$1%20$2/ig;
163       $word=~ s/  //g;
164       $word=~ s/ /%20/g;
165       $word=~ s/\,/\,%20/g;
166       $word=~ s/\n//g;
167       my $url="/cgi-bin/koha/search.pl?author=$word&type=$type";
168       $result->{'author'}=mklink($url,$result->{'author'});
169       my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount)=itemcount($env,$result->{'biblionumber'},$type);
170       $itemcount=$count;
171       ####
172       # Fix this chunk below, remove all hardcoded branch references
173       # need to fix itemcount as well
174       ###
175       if ($nacount > 0){
176         $location=$location."On Loan";
177         if ($nacount >1 ){                                                                                                         
178           $location=$location." ($nacount)";                                                                                            
179          }                                                                                                                         
180          $location.=" ";
181       }
182       if ($lcount > 0){
183          $location=$location."Levin";
184          if ($lcount >1 ){                                                                                                         
185           $location=$location." ($lcount)";                                                                                            
186          }                                                                                                                         
187          $location.=" ";
188       }
189       if ($fcount > 0){
190         $location=$location."Foxton";
191          if ($fcount >1 ){                                                                                                         
192           $location=$location." ($fcount)";                                                                                            
193          }                                                                                                                         
194          $location.=" ";        
195       }
196       if ($scount > 0){
197         $location=$location."Shannon";
198          if ($scount >1 ){                                                                                                         
199           $location=$location." ($scount)";                                                                                            
200          }                                                                                                                         
201          $location.=" ";        
202       }
203       if ($lostcount > 0){
204         $location=$location."Lost";
205          if ($lostcount >1 ){                                                                                                         
206           $location=$location." ($lostcount)";                                                                                            
207          }                                                                                                                         
208          $location.=" ";        
209       }
210       if ($mending > 0){
211         $location=$location."Mending";
212          if ($mending >1 ){                                                                                                         
213           $location=$location." ($mending)";                                                                                            
214          }                                                                                                                         
215          $location.=" ";        
216       }
217       if ($transit > 0){
218         $location=$location."In Transiit";
219          if ($transit >1 ){                                                                                                         
220           $location=$location." ($transit)";                                                                                            
221          }                                                                                                                         
222          $location.=" ";        
223       }
224       if ($ocount > 0){
225         $location=$location."On Order";
226          if ($ocount >1 ){                                                                                                         
227           $location=$location." ($ocount)";                                                                                            
228          }                                                                                                                         
229          $location.=" ";        
230       }
231       
232 #      if ($type ne 'opac'){
233 #        $result->{'request'}=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
234 #      }
235     } else {
236       my $word=$result->{'subject'};
237       $word=~ s/ /%20/g;
238       
239         $result->{'title'}=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$result->{'subject'});
240
241     }
242
243     if ($colour == 1){
244       if ($illustrator) {
245           print mktablerow(7,$secondary,$result->{'title'},$result->{'author'},$result->{'illus'},$result->{'copyrightdate'},$itemcount,$location);
246       } else {
247           print mktablerow(6,$secondary,$result->{'title'},$result->{'author'},$result->{'copyrightdate'},$itemcount,$location);
248       }
249       $colour=0;
250     } else {
251       if ($illustrator) {
252           print mktablerow(7,'white',$result->{'title'},$result->{'author'},$result->{'illus'},$result->{'copyrightdate'},$itemcount,$location);
253       } else {
254           print mktablerow(6,'white',$result->{'title'},$result->{'author'},$result->{'copyrightdate'},$itemcount,$location);
255       }
256       $colour=1;
257     }
258     $i++;
259 }
260 $offset=$num+$offset;
261 if ($type ne 'opac'){
262     if ($illustrator) {
263          print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','','/images/background-mem.gif');
264     } else {
265          print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
266     }
267 } else {
268  if ($illustrator) {
269      print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','', '','');
270  } else {
271      print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
272  }
273 }
274 print mktableft();
275 my $search;
276
277     $search="num=$num&offset=$offset&type=$type";
278     if ($subject ne ''){
279       $subject=~ s/ /%20/g;
280       $search=$search."&subject=$subject";
281     }
282     if ($title ne ''){
283       $title=~ s/ /%20/g;
284       $search=$search."&title=$title";
285     }
286     if ($author ne ''){
287       $author=~ s/ /%20/g;
288       $search=$search."&author=$author";
289     }
290     if ($keyword ne ''){
291       $keyword=~ s/ /%20/g;
292       $search=$search."&keyword=$keyword";
293     }
294     if ($class ne ''){
295       $keyword=~ s/ /%20/g;
296       $search=$search."&class=$class";
297     }
298     if ($dewey ne ''){
299       $search=$search."&dewey=$dewey";
300     }
301     $search.="&ttype=$ttype";    
302 if ($offset < $count){    
303     my $stuff=mklink("/cgi-bin/koha/search.pl?$search",'Next');
304     print $stuff;
305 }
306 print "<br>";
307 my $pages=$count/10;
308 $pages++;
309 for (my $i=1;$i<$pages;$i++){
310   my $temp=$i*10;
311   $temp=$temp-10;
312   $search=~ s/offset=[0-9]+/offset=$temp/;
313   my $stuff=mklink("/cgi-bin/koha/search.pl?$search",$i);
314   print "$stuff ";
315 }
316   
317 print endcenter();
318 print endmenu($type);
319 print endpage();
320
321
322 sub validate {
323   my ($input)=@_;
324   $input=~ s/\<[a-z]+\>//gi;
325   $input=~ s/\<\/[a-z]+\>//gi;
326   $input=~ s/\<//g;
327   $input=~ s/\>//g;
328   $input=~ s/^%//g;
329   return($input);
330 }