Fatal bug in installer.pl was trying to set httpduser variable in koha.conf
[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 use C4::Acquisitions;
9
10 my $env;
11 my $input = new CGI;
12 print $input->header;
13 #print $input->dump;
14
15 #whether it is called from the opac or the intranet
16 my $type=$input->param('type');if ($type eq ''){
17   $type = 'intra';
18 }
19
20 my $ttype=$input->param('ttype');
21
22 #setup colours                 
23 my $main;
24 my $secondary;
25
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
38 #build hash of users input
39 my $title=validate($input->param('title'));
40 $search{'title'}=$title;
41
42 my $keyword=validate($input->param('keyword'));
43 $search{'keyword'}=$keyword;
44
45 $search{'front'}=validate($input->param('front'));
46
47 my $author=validate($input->param('author'));
48 $search{'author'}=$author;
49
50 my $illustrator=validate($input->param('illustrator'));
51 $search{'illustrator'}=$illustrator;
52
53 my $subject=validate($input->param('subject'));
54 $search{'subject'}=$subject;
55
56 my $itemnumber=validate($input->param('item'));
57 $search{'item'}=$itemnumber;
58
59 my $isbn=validate($input->param('isbn'));
60 $search{'isbn'}=$isbn;
61
62 my $datebefore=validate($input->param('date-before'));
63 $search{'date-before'}=$datebefore;
64
65 my $class=$input->param('class');
66 $search{'class'}=$class;
67
68 $search{'ttype'}=$ttype;
69
70 my $dewey=validate($input->param('dewey'));
71 $search{'dewey'}=$dewey;
72
73 my $branch=validate($input->param('branch'));
74 $search{'branch'}=$branch;
75
76 my @results;
77 my $offset=$input->param('offset');
78 if ($offset eq ''){
79   $offset=0;
80 }
81 my $num=$input->param('num');
82 if ($num eq ''){
83   $num=10;
84 }
85 print startpage();
86 print startmenu($type);
87 #print $type;
88 #print $search{'ttype'};
89 if ($type eq 'intra'){
90   print mkheadr(1,'Catalogue Search Results');
91 } elsif ($type eq 'catmain'){
92   print mkheadr(1,'Catalogue Maintenance');
93 } else {
94   print mkheadr(1,'Opac Search Results');
95 }
96 print center();
97 my $count;
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('LOCATION'),'');
136   } else {
137    print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'');
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,$branchcount)=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       $stuff[5]='';
201       my ($numbranches, @branches) = branches();
202       my $branchinfo;
203       foreach (@branches) {
204               my $branchcode=$_->{'branchcode'};
205               my $branchname=$_->{'branchname'};
206               $branchinfo->{$branchcode}=$branchname;
207       }
208       if ($numbranches>1) {
209               foreach my $branchcode (sort keys %$branchcount) {
210                   my $c=$branchcount->{$branchcode};
211                   $stuff[5].=$branchinfo->{$branchcode};
212                   if ($c>1) {
213                       $stuff[5].=" ($c)";
214                   }
215                   $stuff[5].=" ";
216               }
217       } else {
218               my $shelfcount=$count-$nacount-$lostcount-$mending-$transit;
219               if ($nacount) {
220                       $stuff[5]="On Loan ";
221                       if ($count>1) {
222                               $stuff[5].="($nacount) ";
223                       }
224               }
225               if ($shelfcount) {
226                       $stuff[5].="Shelf ";
227                       if ($count>1) {
228                               $stuff[5].="($shelfcount) ";
229                       }
230               }
231       }
232       if ($lostcount > 0) {
233         $stuff[5]=$stuff[5]."Lost";
234          if ($count >1 ){                                                                                                         
235           $stuff[5]=$stuff[5]." ($lostcount)";                                                                                            
236          }                                                                                                                         
237          $stuff[5].=" ";        
238       }
239       if ($mending > 0){
240         $stuff[5]=$stuff[5]."Mending";
241          if ($count >1 ){                                                                                                         
242           $stuff[5]=$stuff[5]." ($mending)";                                                                                            
243          }                                                                                                                         
244          $stuff[5].=" ";        
245       }
246       if ($transit > 0){
247         $stuff[5]=$stuff[5]."In Transit";
248          if ($count >1 ){                                                                                                         
249           $stuff[5]=$stuff[5]." ($transit)";                                                                                            
250          }                                                                                                                         
251          $stuff[5].=" ";        
252       }
253       if ($ocount > 0){
254         $stuff[5]=$stuff[5]."On Order";
255          if ($ocount >1 ){                                                                                                         
256           $stuff[5]=$stuff[5]." ($ocount)";                                                                                            
257          }                                                                                                                         
258          $stuff[5].=" ";        
259       }
260       
261       if ($type ne 'opac'){
262         $stuff[6]=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
263       }
264     } else {
265       my $word=$stuff[1];
266       $word=~ s/ /%20/g;
267       
268         $stuff[1]=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$stuff[1]);
269
270     }
271
272     if ($colour == 1){
273       if ($illustrator) {
274           print mktablerow(7,$secondary,$stuff[1],$stuff[0],$stuff[7],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
275       } else {
276           print mktablerow(6,$secondary,$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
277       }
278       $colour=0;
279     } else {
280       if ($illustrator) {
281           print mktablerow(7,'white',$stuff[1],$stuff[0],$stuff[7],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
282       } else {
283           print mktablerow(6,'white',$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
284       }
285       $colour=1;
286     }
287     $i++;
288 }
289 $offset=$num+$offset;
290 if ($type ne 'opac'){
291     if ($illustrator) {
292          print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','','/images/background-mem.gif');
293     } else {
294          print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
295     }
296 } else {
297  if ($illustrator) {
298      print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','', '','');
299  } else {
300      print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
301  }
302 }
303 print mktableft();
304 my $search;
305
306     $search="num=$num&offset=$offset&type=$type";
307     if ($subject ne ''){
308       $subject=~ s/ /%20/g;
309       $search=$search."&subject=$subject";
310     }
311     if ($title ne ''){
312       $title=~ s/ /%20/g;
313       $search=$search."&title=$title";
314     }
315     if ($author ne ''){
316       $author=~ s/ /%20/g;
317       $search=$search."&author=$author";
318     }
319     if ($keyword ne ''){
320       $keyword=~ s/ /%20/g;
321       $search=$search."&keyword=$keyword";
322     }
323     if ($class ne ''){
324       $keyword=~ s/ /%20/g;
325       $search=$search."&class=$class";
326     }
327     if ($dewey ne ''){
328       $search=$search."&dewey=$dewey";
329     }
330     $search.="&ttype=$ttype";    
331 if ($offset < $count){    
332     my $stuff=mklink("/cgi-bin/koha/search.pl?$search",'Next');
333     print $stuff;
334 }
335 print "<br>";
336 my $pages=$count/10;
337 $pages++;
338 for (my $i=1;$i<$pages;$i++){
339   my $temp=$i*10;
340   $temp=$temp-10;
341   $search=~ s/offset=[0-9]+/offset=$temp/;
342   my $stuff=mklink("/cgi-bin/koha/search.pl?$search",$i);
343   print "$stuff ";
344 }
345   
346 print endcenter();
347 print endmenu($type);
348 print endpage();
349
350
351 sub validate {
352   my ($input)=@_;
353   $input=~ s/\<[a-z]+\>//gi;
354   $input=~ s/\<\/[a-z]+\>//gi;
355   $input=~ s/\<//g;
356   $input=~ s/\>//g;
357   $input=~ s/^%//g;
358   return($input);
359 }