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