Added copyright statement to all .pl and .pm files
[koha.git] / search.pl
1 #!/usr/bin/perl
2 #script to provide intranet (librarian) advanced search facility
3
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Search;
24 use CGI;
25 use C4::Output;
26
27 my $env;
28 my $input = new CGI;
29 print $input->header;
30 #print $input->dump;
31
32 #whether it is called from the opac or the intranet
33 my $type=$input->param('type');if ($type eq ''){
34   $type = 'intra';
35 }
36
37 my $ttype=$input->param('ttype');
38
39 #setup colours                 
40 my $main;
41 my $secondary;
42
43 if ($type eq 'opac'){
44   $main='#99cccc';    
45   $secondary='#efe5ef';
46 } else {
47   $main='#cccc99';
48   $secondary='#ffffcc';
49 }       
50
51 #print $input->Dump;
52 my $blah;
53 my %search;
54
55 #build hash of users input
56 my $title=validate($input->param('title'));
57 $search{'title'}=$title;
58
59 my $keyword=validate($input->param('keyword'));
60 $search{'keyword'}=$keyword;
61
62 $search{'front'}=validate($input->param('front'));
63
64 my $author=validate($input->param('author'));
65 $search{'author'}=$author;
66
67 my $illustrator=validate($input->param('illustrator'));
68 $search{'illustrator'}=$illustrator;
69
70 my $subject=validate($input->param('subject'));
71 $search{'subject'}=$subject;
72
73 my $itemnumber=validate($input->param('item'));
74 $search{'item'}=$itemnumber;
75
76 my $isbn=validate($input->param('isbn'));
77 $search{'isbn'}=$isbn;
78
79 my $datebefore=validate($input->param('date-before'));
80 $search{'date-before'};
81
82 my $class=$input->param('class');
83 $search{'class'}=$class;
84
85 $search{'ttype'}=$ttype;
86
87 my $dewey=validate($input->param('dewey'));
88 $search{'dewey'}=$dewey;
89
90 my $branch=validate($input->param('branch'));
91 $search{'branch'}=$branch;
92
93 my @results;
94 my $offset=$input->param('offset');
95 if ($offset eq ''){
96   $offset=0;
97 }
98 my $num=$input->param('num');
99 if ($num eq ''){
100   $num=10;
101 }
102 print startpage();
103 print startmenu($type);
104 #print $type;
105 #print $search{'ttype'};
106 if ($type eq 'intra'){
107   print mkheadr(1,'Catalogue Search Results');
108 } elsif ($type eq 'catmain'){
109   print mkheadr(1,'Catalogue Maintenance');
110 } else {
111   print mkheadr(1,'Opac Search Results');
112 }
113 print center();
114 my $count;
115 my @results;
116 if ($itemnumber ne '' || $isbn ne ''){
117     ($count,@results)=&CatSearch(\$blah,'precise',\%search,$num,$offset);
118 } else {
119   if ($subject ne ''){
120     ($count,@results)=&CatSearch(\$blah,'subject',\%search,$num,$offset);
121   } else {
122     if ($keyword ne ''){
123       ($count,@results)=&KeywordSearch(\$blah,'intra',\%search,$num,$offset);
124     }elsif ($title ne '' || $author ne '' || $illustrator ne '' || $dewey ne '' || $class ne '') {
125       ($count,@results)=&CatSearch(\$blah,'loose',\%search,$num,$offset);
126     }
127   }
128 }
129 print "You searched on ";
130 while ( my ($key, $value) = each %search) {                                 
131   if ($value ne '' && $key ne 'ttype'){
132     $value=~ s/\\//g;
133     print bold("$key $value,");
134   }                          
135 }
136 print " $count results found";
137 my $offset2=$num+$offset;
138 my $dispnum=$offset+1;
139 print "<br> Results $dispnum to $offset2 displayed";
140 print mktablehdr;
141 if ($type ne 'opac'){
142   if ($subject ne ''){
143    print mktablerow(1,$main,'<b>SUBJECT</b>','/images/background-mem.gif');
144   } elsif ($illustrator ne '') {
145    print mktablerow(7,$main,'<b>TITLE</b>','<b>AUTHOR</b>', '<b>ILLUSTRATOR<b>', bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
146   } else {
147    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
148   }
149 } else {
150   if ($subject ne ''){
151    print mktablerow(6,$main,'<b>SUBJECT</b>',' &nbsp; ',' &nbsp; ');
152   } elsif ($illustrator ne '') {
153    print mktablerow(7,$main,'<b>TITLE</b>','<b>AUTHOR</b>','<b>ILLUSTRATOR</b>', bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
154   } else {
155    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
156   }
157 }
158 my $count2=@results;
159 if ($keyword ne '' && $offset > 0){
160   $count2=$count-$offset;
161   if ($count2 > 10){
162     $count2=10;
163   }
164 }
165 #print $count2;
166 my $i=0;
167 my $colour=1;
168 while ($i < $count2){
169 #    print $results[$i]."\n";
170 #    my @stuff=split('\t',$results[$i]);
171     my $result=$results[$i];
172     $result->{'title'}=~ s/\`/\\\'/g;
173     my $title2=$result->{'title'};
174     $title2=~ s/ /%20/g;
175     my $location='';
176     my $itemcount;
177     if ($subject eq ''){
178       $result->{'title'}=mklink("/cgi-bin/koha/detail.pl?type=$type&bib=$result->{'biblionumber'}&title=$title2",$result->{'title'});
179       my $word=$result->{'author'};
180       $word=~ s/([a-z]) +([a-z])/$1%20$2/ig;
181       $word=~ s/  //g;
182       $word=~ s/ /%20/g;
183       $word=~ s/\,/\,%20/g;
184       $word=~ s/\n//g;
185       my $url="/cgi-bin/koha/search.pl?author=$word&type=$type";
186       $result->{'author'}=mklink($url,$result->{'author'});
187       my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount)=itemcount($env,$result->{'biblionumber'},$type);
188       $itemcount=$count;
189       ####
190       # Fix this chunk below, remove all hardcoded branch references
191       # need to fix itemcount as well
192       ###
193       if ($nacount > 0){
194         $location=$location."On Loan";
195         if ($nacount >1 ){                                                                                                         
196           $location=$location." ($nacount)";                                                                                            
197          }                                                                                                                         
198          $location.=" ";
199       }
200       if ($lcount > 0){
201          $location=$location."Levin";
202          if ($lcount >1 ){                                                                                                         
203           $location=$location." ($lcount)";                                                                                            
204          }                                                                                                                         
205          $location.=" ";
206       }
207       if ($fcount > 0){
208         $location=$location."Foxton";
209          if ($fcount >1 ){                                                                                                         
210           $location=$location." ($fcount)";                                                                                            
211          }                                                                                                                         
212          $location.=" ";        
213       }
214       if ($scount > 0){
215         $location=$location."Shannon";
216          if ($scount >1 ){                                                                                                         
217           $location=$location." ($scount)";                                                                                            
218          }                                                                                                                         
219          $location.=" ";        
220       }
221       if ($lostcount > 0){
222         $location=$location."Lost";
223          if ($lostcount >1 ){                                                                                                         
224           $location=$location." ($lostcount)";                                                                                            
225          }                                                                                                                         
226          $location.=" ";        
227       }
228       if ($mending > 0){
229         $location=$location."Mending";
230          if ($mending >1 ){                                                                                                         
231           $location=$location." ($mending)";                                                                                            
232          }                                                                                                                         
233          $location.=" ";        
234       }
235       if ($transit > 0){
236         $location=$location."In Transiit";
237          if ($transit >1 ){                                                                                                         
238           $location=$location." ($transit)";                                                                                            
239          }                                                                                                                         
240          $location.=" ";        
241       }
242       if ($ocount > 0){
243         $location=$location."On Order";
244          if ($ocount >1 ){                                                                                                         
245           $location=$location." ($ocount)";                                                                                            
246          }                                                                                                                         
247          $location.=" ";        
248       }
249       
250 #      if ($type ne 'opac'){
251 #        $result->{'request'}=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
252 #      }
253     } else {
254       my $word=$result->{'subject'};
255       $word=~ s/ /%20/g;
256       
257         $result->{'title'}=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$result->{'subject'});
258
259     }
260
261     if ($colour == 1){
262       if ($illustrator) {
263           print mktablerow(7,$secondary,$result->{'title'},$result->{'author'},$result->{'illus'},$result->{'copyrightdate'},$itemcount,$location);
264       } else {
265           print mktablerow(6,$secondary,$result->{'title'},$result->{'author'},$result->{'copyrightdate'},$itemcount,$location);
266       }
267       $colour=0;
268     } else {
269       if ($illustrator) {
270           print mktablerow(7,'white',$result->{'title'},$result->{'author'},$result->{'illus'},$result->{'copyrightdate'},$itemcount,$location);
271       } else {
272           print mktablerow(6,'white',$result->{'title'},$result->{'author'},$result->{'copyrightdate'},$itemcount,$location);
273       }
274       $colour=1;
275     }
276     $i++;
277 }
278 $offset=$num+$offset;
279 if ($type ne 'opac'){
280     if ($illustrator) {
281          print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','','/images/background-mem.gif');
282     } else {
283          print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
284     }
285 } else {
286  if ($illustrator) {
287      print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','', '','');
288  } else {
289      print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
290  }
291 }
292 print mktableft();
293 my $search;
294
295     $search="num=$num&offset=$offset&type=$type";
296     if ($subject ne ''){
297       $subject=~ s/ /%20/g;
298       $search=$search."&subject=$subject";
299     }
300     if ($title ne ''){
301       $title=~ s/ /%20/g;
302       $search=$search."&title=$title";
303     }
304     if ($author ne ''){
305       $author=~ s/ /%20/g;
306       $search=$search."&author=$author";
307     }
308     if ($keyword ne ''){
309       $keyword=~ s/ /%20/g;
310       $search=$search."&keyword=$keyword";
311     }
312     if ($class ne ''){
313       $keyword=~ s/ /%20/g;
314       $search=$search."&class=$class";
315     }
316     if ($dewey ne ''){
317       $search=$search."&dewey=$dewey";
318     }
319     $search.="&ttype=$ttype";    
320 if ($offset < $count){    
321     my $stuff=mklink("/cgi-bin/koha/search.pl?$search",'Next');
322     print $stuff;
323 }
324 print "<br>";
325 my $pages=$count/10;
326 $pages++;
327 for (my $i=1;$i<$pages;$i++){
328   my $temp=$i*10;
329   $temp=$temp-10;
330   $search=~ s/offset=[0-9]+/offset=$temp/;
331   my $stuff=mklink("/cgi-bin/koha/search.pl?$search",$i);
332   print "$stuff ";
333 }
334   
335 print endcenter();
336 print endmenu($type);
337 print endpage();
338
339
340 sub validate {
341   my ($input)=@_;
342   $input=~ s/\<[a-z]+\>//gi;
343   $input=~ s/\<\/[a-z]+\>//gi;
344   $input=~ s/\<//g;
345   $input=~ s/\>//g;
346   $input=~ s/^%//g;
347   return($input);
348 }