Changes to allow modifications to the website biblioitems
[koha.git] / C4 / Search.pm
1 package C4::Search; #asummes C4/Search
2
3 #requires DBI.pm to be installed
4 #uses DBD:Pg
5
6 use strict;
7 require Exporter;
8 use DBI;
9 use C4::Database;
10 use C4::Reserves2;
11 use Set::Scalar;
12
13 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
14   
15 # set the version for version checking
16 $VERSION = 0.02;
17     
18 @ISA = qw(Exporter);
19 @EXPORT = qw(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
20 &itemdata &bibdata &GetItems &borrdata &itemnodata &itemcount
21 &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
22 &getboracctrecord &ItemType &itemissues &subject &subtitle
23 &addauthor &bibitems &barcodes &findguarantees &allissues &systemprefs
24 &findguarantor &getwebsites &getwebbiblioitems);
25 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
26                   
27 # your exported package globals go here,
28 # as well as any optionally exported functions
29
30 @EXPORT_OK   = qw($Var1 %Hashit);
31
32
33 # non-exported package globals go here
34 use vars qw(@more $stuff);
35         
36 # initalize package globals, first exported ones
37
38 my $Var1   = '';
39 my %Hashit = ();
40                     
41 # then the others (which are still accessible as $Some::Module::stuff)
42 my $stuff  = '';
43 my @more   = ();
44         
45 # all file-scoped lexicals must be created before
46 # the functions below that use them.
47                 
48 # file-private lexicals go here
49 my $priv_var    = '';
50 my %secret_hash = ();
51                             
52 # here's a file-private function as a closure,
53 # callable as &$priv_func;  it cannot be prototyped.
54 my $priv_func = sub {
55   # stuff goes here.
56 };
57                                                     
58 # make all your functions, whether exported or not;
59 sub findguarantees{         
60   my ($bornum)=@_;         
61   my $dbh=C4Connect;           
62   my $query="select cardnumber,borrowernumber from borrowers where    
63   guarantor='$bornum'";               
64   my $sth=$dbh->prepare($query);                 
65   $sth->execute;                   
66   my @dat;                     
67   my $i=0;                       
68   while (my $data=$sth->fetchrow_hashref){    
69     $dat[$i]=$data;                           
70     $i++;                               
71   }                                   
72   $sth->finish; 
73   $dbh->disconnect;         
74   return($i,\@dat);             
75 }
76 sub findguarantor{  
77   my ($bornum)=@_;  
78   my $dbh=C4Connect;    
79   my $query="select guarantor from borrowers where      
80   borrowernumber='$bornum'";        
81   my $sth=$dbh->prepare($query);          
82   $sth->execute;            
83   my $data=$sth->fetchrow_hashref;              
84   $sth->finish;                
85   $query="Select * from borrowers where
86   borrowernumber='$data->{'guarantor'}'";  
87   $sth=$dbh->prepare($query);  
88   $sth->execute;    
89   $data=$sth->fetchrow_hashref;      
90   $sth->finish;        
91   $dbh->disconnect;          
92   return($data);            
93 }
94
95 sub systemprefs {
96     my %systemprefs;
97     my $dbh=C4Connect;
98     my $sth=$dbh->prepare("select variable,value from systempreferences");
99     $sth->execute;
100     while (my ($variable,$value)=$sth->fetchrow) {
101         $systemprefs{$variable}=$value;
102     }
103     $sth->finish;
104     $dbh->disconnect;
105     return(%systemprefs);
106 }
107
108 sub NewBorrowerNumber {           
109   my $dbh=C4Connect;        
110   my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");     
111   $sth->execute;            
112   my $data=$sth->fetchrow_hashref;                                  
113   $sth->finish;                   
114   $data->{'max(borrowernumber)'}++;         
115   $dbh->disconnect;
116   return($data->{'max(borrowernumber)'}); 
117 }    
118
119   
120 sub KeywordSearch {
121   my ($env,$type,$search,$num,$offset)=@_;
122   my $dbh = &C4Connect;
123   $search->{'keyword'}=~ s/ +$//;
124   $search->{'keyword'}=~ s/'/\\'/;
125   my @key=split(' ',$search->{'keyword'});
126   my $count=@key;
127   my $i=1;
128   my @results;
129   my $query="Select biblionumber from biblio
130   where ((title like '$key[0]%' or title like '% $key[0]%')";
131   while ($i < $count){                                                  
132       $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";                                                   
133       $i++;                                                  
134   }
135   $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";                                             
136   for ($i=1;$i<$count;$i++){                                                  
137       $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";                                           
138   }
139    $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";                                               
140   for ($i=1;$i<$count;$i++){                                                  
141       $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";                                             
142   }
143   $query.=" )";
144 #  print $query;
145   my $sth=$dbh->prepare($query);
146   $sth->execute;
147   my $i=0;
148   while (my @res=$sth->fetchrow_array){
149     $results[$i]=$res[0];
150     $i++;
151   }
152   $sth->finish;
153   my $set1=Set::Scalar->new(@results);
154   $query="Select biblionumber from bibliosubtitle where
155   ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";                 
156   for ($i=1;$i<$count;$i++){   
157         $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";                                                  
158   }                   
159   $query.=" )";
160 #  print $query;
161   $sth=$dbh->prepare($query);
162   $sth->execute;
163   $i=0;
164   while (my @res=$sth->fetchrow_array){
165     $results[$i]=$res[0];
166     $i++;
167   }
168   $sth->finish;
169   my $set2=Set::Scalar->new(@results);
170   if ($i > 0){
171     $set1=$set1+$set2;
172   }
173   $query ="Select biblionumber from biblioitems where
174   ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";                                   
175   for ($i=1;$i<$count;$i++){                                                  
176       $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";                                 
177   }            
178   $query.=" )";
179 #  print $query;
180   $sth=$dbh->prepare($query);
181   $sth->execute;
182   $i=0;
183   while (my @res=$sth->fetchrow_array){
184     $results[$i]=$res[0];
185     $i++;
186   }
187   $sth->finish;
188   my $set3=Set::Scalar->new(@results);    
189   if ($i > 0){
190     $set1=$set1+$set3;
191   }
192   $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
193   like '%$search->{'keyword'}%' group by biblionumber");
194   $sth->execute;
195   $i=0;
196   while (my @res=$sth->fetchrow_array){
197     $results[$i]=$res[0];
198     $i++;
199   }
200   $sth->finish;
201   my $set4=Set::Scalar->new(@results);    
202   if ($i > 0){
203     $set1=$set1+$set4;
204   }
205   my $i2=0;
206   my $i3=0;
207   my $i4=0;
208
209   my @res2;
210   my @res = $set1->members;
211   $count=@res;
212 #  print $set1;
213   $i=0;
214 #  print "count $count";
215   if ($search->{'class'} ne ''){ 
216     while ($i2 <$count){
217       my $query="select * from biblio,biblioitems where 
218       biblio.biblionumber='$res[$i2]' and     
219       biblio.biblionumber=biblioitems.biblionumber ";         
220       if ($search->{'class'} ne ''){             
221       my @temp=split(/\|/,$search->{'class'});
222       my $count=@temp;                       
223       $query.= "and ( itemtype='$temp[0]'";     
224       for (my $i=1;$i<$count;$i++){     
225         $query.=" or itemtype='$temp[$i]'";                                     
226       } 
227       $query.=")"; 
228       }
229        my $sth=$dbh->prepare($query);    
230        #    print $query;        
231        $sth->execute;        
232        if (my $data2=$sth->fetchrow_hashref){            
233          my $dewey= $data2->{'dewey'};                
234          my $subclass=$data2->{'subclass'};          
235          $dewey=~s/\.*0*$//;           
236          ($dewey == 0) && ($dewey=''); 
237          ($dewey) && ($dewey.=" $subclass") ;                                    
238           $sth->finish;
239           my $end=$offset +$num;
240           if ($i4 <= $offset){
241             $i4++;
242           }
243 #         print $i4;
244           if ($i4 <=$end && $i4 > $offset){
245             $data2->{'dewey'}=$dewey;
246             $res2[$i3]=$data2;
247             
248 #           $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";    
249             $i3++;
250             $i4++;
251 #           print "in here $i3<br>";
252           } else {
253 #           print $end;
254           }
255           $i++; 
256         }         
257      $i2++;
258      }
259      $count=$i;
260   
261    } else {
262   while ($i2 < $num && $i2 < $count){
263     my $query="select * from biblio,biblioitems where
264     biblio.biblionumber='$res[$i2+$offset]' and        
265     biblio.biblionumber=biblioitems.biblionumber ";
266     if ($search->{'class'} ne ''){
267       my @temp=split(/\|/,$search->{'class'});
268       my $count=@temp;
269       $query.= "and ( itemtype='$temp[0]'";
270       for (my $i=1;$i<$count;$i++){
271         $query.=" or itemtype='$temp[$i]'";
272       }
273       $query.=")"; 
274     }
275     if ($search->{'dewey'} ne ''){
276       $query.= "and (dewey like '$search->{'dewey'}%') ";
277     }
278
279     my $sth=$dbh->prepare($query);
280 #    print $query;
281     $sth->execute;
282     if (my $data2=$sth->fetchrow_hashref){
283         my $dewey= $data2->{'dewey'};               
284         my $subclass=$data2->{'subclass'};                   
285         $dewey=~s/\.*0*$//;     
286         ($dewey == 0) && ($dewey='');               
287         ($dewey) && ($dewey.=" $subclass") ;                      
288         $sth->finish;                                             
289         $data2->{'dewey'}=$dewey;
290         $res2[$i]=$data2;
291 #       $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
292         $i++;
293     }
294     $i2++;
295     
296   }
297   }
298   $dbh->disconnect;
299
300   #$count=$i;
301   return($count,@res2);
302 }
303
304 sub CatSearch  {
305   my ($env,$type,$search,$num,$offset)=@_;
306   my $dbh = &C4Connect;
307   my $query = '';
308     my @results;
309   $search->{'title'}=~ s/'/\\'/g;
310   $search->{'author'}=~ s/'/\\'/g;
311   $search->{'illustrator'}=~ s/'/\\'/g;
312   my $title = lc($search->{'title'}); 
313   
314   if ($type eq 'loose') {
315       if ($search->{'author'} ne ''){
316         my @key=split(' ',$search->{'author'});
317         my $count=@key;
318         my $i=1;
319         $query="select *,biblio.author,biblio.biblionumber from
320          biblio
321          left join additionalauthors
322          on additionalauthors.biblionumber =biblio.biblionumber
323          where
324          ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
325          additionalauthors.author like '$key[0]%' or additionalauthors.author 
326          like '% $key[0]%'
327                  )";    
328          while ($i < $count){ 
329            $query=$query." and (
330            biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
331            additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
332            )";
333            $i++;       
334          }   
335          $query=$query.")";
336          if ($search->{'title'} ne ''){ 
337            my @key=split(' ',$search->{'title'});
338            my $count=@key;
339            my $i=0;
340            $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
341             while ($i<$count){            
342               $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
343               $i++; 
344             }                       
345 #           $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')"; 
346 #            for ($i=1;$i<$count;$i++){
347 #             $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";   
348 #            }
349             $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";  
350             for ($i=1;$i<$count;$i++){                    
351                 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
352             }                                                             
353             $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";                         
354             for ($i=1;$i<$count;$i++){                    
355                 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";   
356             }                                                             
357             $query=$query."))"; 
358            #$query=$query. " and (title like '%$search->{'title'}%' 
359            #or seriestitle like '%$search->{'title'}%')";
360          }
361                  
362          $query.=" group by biblio.biblionumber";
363       } else {
364           if ($search->{'title'} ne '') {
365            if ($search->{'ttype'} eq 'exact'){
366              $query="select * from biblio
367              where                            
368              (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
369              or biblio.unititle like '$search->{'title'} |%' or 
370              biblio.unititle like '%| $search->{'title'} |%' or
371              biblio.unititle like '%| $search->{'title'}') or
372              (biblio.seriestitle = '$search->{'title'}' or
373              biblio.seriestitle like '$search->{'title'} |%' or
374              biblio.seriestitle like '%| $search->{'title'} |%' or
375              biblio.seriestitle like '%| $search->{'title'}')
376              )";
377            } else {
378             my @key=split(' ',$search->{'title'});
379             my $count=@key;
380             my $i=1;
381             $query="select * from biblio
382             left join bibliosubtitle on
383             biblio.biblionumber=bibliosubtitle.biblionumber
384             where
385             (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
386             while ($i<$count){
387               $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
388               $i++;
389             }
390             $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
391             for ($i=1;$i<$count;$i++){
392               $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
393             }
394             $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
395             for ($i=1;$i<$count;$i++){
396               $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
397             }
398             $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
399             for ($i=1;$i<$count;$i++){
400               $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
401             }
402             $query=$query."))";
403            }
404           } elsif ($search->{'class'} ne ''){
405              $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
406              my @temp=split(/\|/,$search->{'class'});
407               my $count=@temp;
408               $query.= " and ( itemtype='$temp[0]'";
409               for (my $i=1;$i<$count;$i++){
410                $query.=" or itemtype='$temp[$i]'";
411               }
412               $query.=")";
413               if ($search->{'illustrator'} ne ''){
414                 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
415               }
416               if ($search->{'dewey'} ne ''){
417                 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
418               }
419           } elsif ($search->{'dewey'} ne ''){
420              $query="select * from biblioitems,biblio 
421              where biblio.biblionumber=biblioitems.biblionumber
422              and biblioitems.dewey like '$search->{'dewey'}%'";
423           } elsif ($search->{'illustrator'} ne '') {
424           if ($search->{'illustrator'} ne ''){
425              $query="select * from biblioitems,biblio 
426              where biblio.biblionumber=biblioitems.biblionumber
427              and biblioitems.illus like '%".$search->{'illustrator'}."%'";
428           }
429         }
430           $query .=" group by biblio.biblionumber";      
431       }
432   } 
433   if ($type eq 'subject'){
434     my @key=split(' ',$search->{'subject'});
435     my $count=@key;
436     my $i=1;
437     $query="select distinct(subject) from bibliosubject where( subject like
438     '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
439     while ($i<$count){
440       $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
441       or subject like '% $key[$i]'
442       or subject like '%($key[$i])%')";
443       $i++;
444     }
445     if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){ 
446       $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
447       or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
448     } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
449       $query=~ s/ nz/ NEW ZEALAND/ig;
450       $query=~ s/nz /NEW ZEALAND /ig;
451       $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
452     }  
453   }
454   if ($type eq 'precise'){
455       $query="select * from items,biblio ";
456       if ($search->{'item'} ne ''){
457         my $search2=uc $search->{'item'};
458         $query=$query." where 
459         items.biblionumber=biblio.biblionumber 
460         and barcode='$search2'";
461       }
462       if ($search->{'isbn'} ne ''){
463         my $search2=uc $search->{'isbn'};
464         my $query1 = "select * from biblioitems where isbn='$search2'";
465         my $sth1=$dbh->prepare($query1);
466 #       print $query1;
467         $sth1->execute;
468         my $i2=0;
469         while (my $data=$sth1->fetchrow_hashref) {
470            $query="select * from biblioitems,biblio where
471            biblio.biblionumber = $data->{'biblionumber'}
472            and biblioitems.biblionumber = biblio.biblionumber";
473            my $sth=$dbh->prepare($query);
474            $sth->execute;
475            my $data=$sth->fetchrow_hashref;
476            my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
477            $dewey=~s/\.*0*$//;
478            ($dewey == 0) && ($dewey='');
479            ($dewey) && ($dewey.=" $subclass");
480            $data->{'dewey'}=$dewey;
481            $results[$i2]=$data;
482 #           $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
483            $i2++; 
484            $sth->finish;
485         }
486         $sth1->finish;
487       }
488   }
489 #print $query;
490 if ($type ne 'precise' && $type ne 'subject'){
491   if ($search->{'author'} ne ''){   
492       $query=$query." order by biblio.author,title";
493   } else {
494       $query=$query." order by title";
495   }
496 } else {
497   if ($type eq 'subject'){
498       $query=$query." order by subject";
499   }
500 }
501 #print $query;
502 my $sth=$dbh->prepare($query);
503 $sth->execute;
504 my $count=1;
505 my $i=0;
506 my $limit= $num+$offset;
507 while (my $data=$sth->fetchrow_hashref){
508   my $query="select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}";
509             if ($search->{'class'} ne ''){
510               my @temp=split(/\|/,$search->{'class'});
511               my $count=@temp;
512               $query.= " and ( itemtype='$temp[0]'";
513               for (my $i=1;$i<$count;$i++){
514                $query.=" or itemtype='$temp[$i]'";
515               }
516               $query.=")";
517             }
518             if ($search->{'dewey'} ne ''){
519               $query.=" and dewey='$search->{'dewey'}' ";
520             }
521             if ($search->{'illustrator'} ne ''){
522               $query.=" and illus like '%".$search->{'illustrator'}."%' ";
523             }
524 #print $query;
525   my $sti=$dbh->prepare($query);
526   $sti->execute;
527   my $dewey;
528   my $subclass;
529   my $true=0;
530   if (($dewey, $subclass) = $sti->fetchrow || $type eq 'subject'){
531     $true=1;
532   }
533   $dewey=~s/\.*0*$//;
534   ($dewey == 0) && ($dewey='');
535   ($dewey) && ($dewey.=" $subclass");
536   $data->{'dewey'}=$dewey;
537   $sti->finish;
538   if ($true == 1){
539   if ($count > $offset && $count <= $limit){
540 #    if ($type ne 'subject' && $type ne 'precise'){
541 #       $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
542 #    } elsif ($search->{'isbn'} ne '' || $search->{'item'} ne ''){
543 #       $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
544 #    } else {  
545 #     $results[$i]="$data->{'author'}\t$data->{'subject'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
546 #    }
547     $results[$i]=$data;
548     $i++;
549   }
550   $count++;
551   }
552 }
553 $sth->finish;
554 #if ($type ne 'precise'){
555   $count--;
556 #}
557 #$count--;
558 return($count,@results);
559 }
560
561 sub updatesearchstats{
562   my ($dbh,$query)=@_;
563   
564 }
565
566 sub subsearch {
567   my ($env,$subject)=@_;
568   my $dbh=C4Connect();
569   $subject=$dbh->quote($subject);
570   my $query="Select * from biblio,bibliosubject where
571   biblio.biblionumber=bibliosubject.biblionumber and
572   bibliosubject.subject=$subject group by biblio.biblionumber
573   order by biblio.title";
574   my $sth=$dbh->prepare($query);
575   $sth->execute;
576   my $i=0;
577 #  print $query;
578   my @results;
579   while (my $data=$sth->fetchrow_hashref){
580     $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
581     $i++;
582   }
583   $sth->finish;
584   $dbh->disconnect;
585   return(@results);
586 }
587
588
589 sub ItemInfo {
590     my ($env,$biblionumber,$type) = @_;
591     my $dbh   = &C4Connect;
592     my $query = "Select * from items, biblio, biblioitems, branches
593 where (items.biblioitemnumber = biblioitems.biblioitemnumber)
594 and biblioitems.biblionumber = biblio.biblionumber
595 and biblio.biblionumber = '$biblionumber'
596 and branches.branchcode = items.holdingbranch";
597
598   if ($type ne 'intra'){
599     $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
600     or items.itemlost is NULL)
601     and (wthdrawn <> 1 or wthdrawn is NULL)";
602   }
603   $query .= " order by items.dateaccessioned desc";
604   my $sth=$dbh->prepare($query);
605   $sth->execute;
606   my $i=0;
607   my @results;
608 #  print $query;
609   while (my $data=$sth->fetchrow_hashref){
610     my $iquery = "Select * from issues
611     where itemnumber = '$data->{'itemnumber'}'
612     and returndate is null";
613     my $datedue = '';
614     my $isth=$dbh->prepare($iquery);
615     $isth->execute;
616     if (my $idata=$isth->fetchrow_hashref){
617       my @temp=split('-',$idata->{'date_due'});
618       $datedue = "$temp[2]/$temp[1]/$temp[0]";
619     }
620     if ($data->{'itemlost'} eq '1' || $data->{'itemlost'} eq '2'){
621         $datedue='Itemlost';
622     }
623     if ($data->{'wthdrawn'} eq '1'){
624       $datedue="Cancelled";
625     }
626     if ($datedue eq ''){
627        my ($rescount,$reserves)=Findgroupreserve($data->{'biblioitemnumber'},$biblionumber);
628
629        if ($rescount >0){                                
630           $datedue='Request';
631        }
632     }
633     $isth->finish;
634     my $class = $data->{'classification'};
635     my $dewey = $data->{'dewey'};
636     $dewey =~ s/0+$//;
637     if ($dewey eq "000.") { $dewey = "";};    
638     if ($dewey < 10){$dewey='00'.$dewey;}
639     if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
640     if ($dewey <= 0){
641       $dewey='';
642     }
643     $dewey=~ s/\.$//;
644     $class = $class.$dewey;
645     if ($dewey ne ''){
646       $class = $class.$data->{'subclass'};
647     }
648  #   $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
649     my @temp=split('-',$data->{'datelastseen'});
650     my $date="$temp[2]/$temp[1]/$temp[0]";
651     $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$class\t$data->{'itemnumber'}\t$data->{'itemtype'}\t$date\t$data->{'biblioitemnumber'}\t$data->{'volumeddesc'}";
652 #    print "$results[$i] <br>";
653     $i++;
654   }
655  $sth->finish;
656   my $query2="Select * from aqorders where biblionumber=$biblionumber";
657   my $sth2=$dbh->prepare($query2);         
658   $sth2->execute;                                        
659   my $data;
660   my $ocount;
661   if ($data=$sth2->fetchrow_hashref){                   
662     $ocount=$data->{'quantity'} - $data->{'quantityreceived'};                                                  
663     if ($ocount > 0){
664       $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$ocount\tOn Order\t\t$data->{'itemnumber'}\t$data->{'itemtype'}\t\t$data->{'biblioitemnumber'}\t$data->{'volumeddesc'}";
665     }
666   } 
667   $sth2->finish;
668
669   $dbh->disconnect;
670   return(@results);
671 }
672
673 sub GetItems {
674    my ($env,$biblionumber)=@_;
675    #debug_msg($env,"GetItems");
676    my $dbh = &C4Connect;
677    my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
678    #debug_msg($env,$query);
679    my $sth=$dbh->prepare($query);
680    $sth->execute;
681    #debug_msg($env,"executed query");      
682    my $i=0;
683    my @results;
684    while (my $data=$sth->fetchrow_hashref) {
685       #debug_msg($env,$data->{'biblioitemnumber'});
686       my $dewey = $data->{'dewey'};
687       $dewey =~ s/0+$//; 
688       my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
689       $line = $line."\t$data->{'classification'}\t$dewey";
690       $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
691       $line = $line."\t$data->{'volume'}\t$data->{number}";
692       my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
693       $isth->execute;
694       while (my $idata = $isth->fetchrow_hashref) {
695         my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
696         if ($idata->{'notforloan'} == 1) {
697           $iline = $iline."NFL ";
698         }
699         if ($idata->{'itemlost'} == 1) {
700           $iline = $iline."LOST ";
701         }        
702         $line = $line."\t$iline"; 
703       }
704       $isth->finish;
705       $results[$i] = $line;
706       $i++;      
707    }
708    $sth->finish;
709    $dbh->disconnect;
710    return(@results);
711 }            
712   
713 sub itemdata {
714   my ($barcode)=@_;
715   my $dbh=C4Connect;
716   my $query="Select * from items,biblioitems where barcode='$barcode'
717   and items.biblioitemnumber=biblioitems.biblioitemnumber";
718 #  print $query;
719   my $sth=$dbh->prepare($query);
720   $sth->execute;
721   my $data=$sth->fetchrow_hashref;
722   $sth->finish;
723   $dbh->disconnect;
724   return($data);
725 }
726
727
728 sub bibdata {
729     my ($bibnum, $type) = @_;
730     my $dbh   = C4Connect;
731     my $query = "Select *, biblio.notes  
732 from biblio, biblioitems 
733 left join bibliosubtitle on
734 biblio.biblionumber = bibliosubtitle.biblionumber
735 where biblio.biblionumber = $bibnum
736 and biblioitems.biblionumber = $bibnum";
737     my $sth   = $dbh->prepare($query);
738     my $data;
739
740     $sth->execute;
741     $data  = $sth->fetchrow_hashref;
742     $sth->finish;
743
744     $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
745     $sth   = $dbh->prepare($query);
746     $sth->execute;
747     while (my $dat = $sth->fetchrow_hashref){
748         $data->{'subject'} .= " | $dat->{'subject'}";
749     } # while
750
751     $sth->finish;
752     $dbh->disconnect;
753     return($data);
754 } # sub bibdata
755
756
757 sub bibitemdata {
758     my ($bibitem) = @_;
759     my $dbh   = C4Connect;
760     my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
761 where biblio.biblionumber = biblioitems.biblionumber
762 and biblioitemnumber = $bibitem
763 and biblioitems.itemtype = itemtypes.itemtype";
764     my $sth   = $dbh->prepare($query);
765     my $data;
766
767     $sth->execute;
768
769     $data = $sth->fetchrow_hashref;
770
771     $sth->finish;
772     $dbh->disconnect;
773     return($data);
774 } # sub bibitemdata
775
776
777 sub subject {
778   my ($bibnum)=@_;
779   my $dbh=C4Connect;
780   my $query="Select * from bibliosubject where biblionumber=$bibnum";
781   my $sth=$dbh->prepare($query);
782   $sth->execute;
783   my @results;
784   my $i=0;
785   while (my $data=$sth->fetchrow_hashref){
786     $results[$i]=$data;
787     $i++;
788   }
789   $sth->finish;
790   $dbh->disconnect;
791   return($i,\@results);
792 }
793
794 sub addauthor {
795   my ($bibnum)=@_;
796   my $dbh=C4Connect;
797   my $query="Select * from additionalauthors where biblionumber=$bibnum";
798   my $sth=$dbh->prepare($query);
799   $sth->execute;
800   my @results;
801   my $i=0;
802   while (my $data=$sth->fetchrow_hashref){
803     $results[$i]=$data;
804     $i++;
805   }
806   $sth->finish;
807   $dbh->disconnect;
808   return($i,\@results);
809 }
810
811 sub subtitle {
812   my ($bibnum)=@_;
813   my $dbh=C4Connect;
814   my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
815   my $sth=$dbh->prepare($query);
816   $sth->execute;
817   my @results;
818   my $i=0;
819   while (my $data=$sth->fetchrow_hashref){
820     $results[$i]=$data;
821     $i++;
822   }
823   $sth->finish;
824   $dbh->disconnect;
825   return($i,\@results);
826 }
827
828
829
830 sub itemissues {
831     my ($bibitem, $biblio)=@_;
832     my $dbh   = C4Connect;
833     my $query = "Select * from items where 
834 items.biblioitemnumber = '$bibitem'";
835     my $sth   = $dbh->prepare($query)
836       || die $dbh->errstr;
837     my $i     = 0;
838     my @results;
839   
840     $sth->execute
841       || die $sth->errstr;
842
843     while (my $data = $sth->fetchrow_hashref) {
844         my $query2 = "select * from issues,borrowers
845 where itemnumber = $data->{'itemnumber'}
846 and returndate is NULL
847 and issues.borrowernumber = borrowers.borrowernumber";
848         my $sth2   = $dbh->prepare($query2);
849
850         $sth2->execute; 
851         if (my $data2 = $sth2->fetchrow_hashref) {
852             $data->{'date_due'} = $data2->{'date_due'};
853             $data->{'card'}     = $data2->{'cardnumber'};
854         } else {
855             if ($data->{'wthdrawn'} eq '1') {
856                 $data->{'date_due'} = 'Cancelled';
857             } else {
858                 $data->{'date_due'} = 'Available';
859             } # else
860         } # else
861
862         $sth2->finish;
863         $query2 = "select * from issues, borrowers
864 where itemnumber = '$data->{'itemnumber'}'
865 and issues.borrowernumber = borrowers.borrowernumber 
866 order by date_due desc";
867         $sth2 = $dbh->prepare($query2)
868           || die $dbh->errstr;
869         $sth2->execute
870           || die $sth2->errstr;
871
872         for (my $i2 = 0; $i2 < 2; $i2++) {
873             if (my $data2 = $sth2->fetchrow_hashref) {
874                 $data->{"timestamp$i2"} = $data2->{'timestamp'};
875                 $data->{"card$i2"}      = $data2->{'cardnumber'};
876                 $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
877             } # if
878         } # for
879
880         $sth2->finish;
881         $results[$i] = $data;
882         $i++;
883     }
884
885     $sth->finish;
886     $dbh->disconnect;
887     return(@results);
888 }
889
890
891 sub itemnodata {
892   my ($env,$dbh,$itemnumber) = @_;
893   $dbh=C4Connect;
894   my $query="Select * from biblio,items,biblioitems
895     where items.itemnumber = '$itemnumber'
896     and biblio.biblionumber = items.biblionumber
897     and biblioitems.biblioitemnumber = items.biblioitemnumber";
898   my $sth=$dbh->prepare($query);
899 #  print $query;
900   $sth->execute;
901   my $data=$sth->fetchrow_hashref;
902   $sth->finish;  
903   $dbh->disconnect;
904   return($data);               
905 }
906
907 #used by member enquiries from the intranet
908 #called by member.pl
909 sub BornameSearch  {
910   my ($env,$searchstring,$type)=@_;
911   my $dbh = &C4Connect;
912   $searchstring=~ s/\'/\\\'/g;
913   my @data=split(' ',$searchstring);
914   my $count=@data;
915   my $query="Select * from borrowers 
916   where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\" 
917   or firstname  like \"$data[0]%\" or firstname like \"% $data[0]%\" 
918   or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
919   ";
920   for (my $i=1;$i<$count;$i++){
921     $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"                  
922     or firstname  like \"$data[$i]%\" or firstname like \"% $data[$i]%\"                    
923     or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
924   }
925   $query=$query.") or cardnumber = \"$searchstring\"
926   order by surname,firstname";
927 #  print $query,"\n";
928   my $sth=$dbh->prepare($query);
929   $sth->execute;
930   my @results;
931   my $cnt=0;
932   while (my $data=$sth->fetchrow_hashref){
933     push(@results,$data);
934     $cnt ++;
935   }
936 #  $sth->execute;
937   $sth->finish;
938   $dbh->disconnect;
939   return ($cnt,\@results);
940 }
941
942 sub borrdata {
943   my ($cardnumber,$bornum)=@_;
944   $cardnumber = uc $cardnumber;
945   my $dbh=C4Connect;
946   my $query;
947   if ($bornum eq ''){
948     $query="Select * from borrowers where cardnumber='$cardnumber'";
949   } else {
950       $query="Select * from borrowers where borrowernumber='$bornum'";
951   }
952   #print $query;
953   my $sth=$dbh->prepare($query);
954   $sth->execute;
955   my $data=$sth->fetchrow_hashref;
956   $sth->finish;
957   $dbh->disconnect;
958   return($data);
959 }
960
961 sub borrissues {
962   my ($bornum)=@_;
963   my $dbh=C4Connect;
964   my $query;
965   $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
966 items.itemnumber=issues.itemnumber and
967 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
968 by date_due";
969   #print $query;
970   my $sth=$dbh->prepare($query);
971     $sth->execute;
972   my @result;
973   my $i=0;
974   while (my $data=$sth->fetchrow_hashref){
975     $result[$i]=$data;;
976     $i++;
977   }
978   $sth->finish;
979   $dbh->disconnect;
980   return($i,\@result);
981 }
982
983 sub allissues { 
984   my ($bornum,$order,$limit)=@_; 
985   my $dbh=C4Connect;   
986   my $query;     
987   $query="Select * from issues,biblio,items,biblioitems       
988   where borrowernumber='$bornum' and         
989   items.biblioitemnumber=biblioitems.biblioitemnumber and           
990   items.itemnumber=issues.itemnumber and             
991   items.biblionumber=biblio.biblionumber";               
992   $query.=" order by $order";                 
993   if ($limit !=0){                   
994     $query.=" limit $limit";                     
995   }                         
996   #print $query;                           
997   my $sth=$dbh->prepare($query);          
998   $sth->execute;
999   my @result;   
1000   my $i=0;    
1001   while (my $data=$sth->fetchrow_hashref){                                      
1002     $result[$i]=$data;; 
1003     $i++;     
1004   }         
1005   $sth->finish;           
1006   $dbh->disconnect;             
1007   return($i,\@result);               
1008 }
1009
1010 sub borrdata2 {
1011   my ($env,$bornum)=@_;
1012   my $dbh=C4Connect;
1013   my $query="Select count(*) from issues where borrowernumber='$bornum' and
1014     returndate is NULL";
1015     # print $query;
1016   my $sth=$dbh->prepare($query);
1017   $sth->execute;
1018   my $data=$sth->fetchrow_hashref;
1019   $sth->finish;
1020   $sth=$dbh->prepare("Select count(*) from issues where
1021     borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1022   $sth->execute;
1023   my $data2=$sth->fetchrow_hashref;
1024   $sth->finish;
1025   $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1026     borrowernumber='$bornum'");
1027   $sth->execute;
1028   my $data3=$sth->fetchrow_hashref;
1029   $sth->finish;
1030   $dbh->disconnect;
1031
1032 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1033 }
1034         
1035
1036 sub getboracctrecord {
1037    my ($env,$params) = @_;
1038    my $dbh=C4Connect;
1039    my @acctlines;
1040    my $numlines=0;
1041    my $query= "Select * from accountlines where
1042 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1043    my $sth=$dbh->prepare($query);
1044 #   print $query;
1045    $sth->execute;
1046    my $total=0;
1047    while (my $data=$sth->fetchrow_hashref){
1048 #      if ($data->{'itemnumber'} ne ''){
1049 #        $query="Select * from items,biblio where items.itemnumber=
1050 #       '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1051 #       my $sth2=$dbh->prepare($query);
1052 #       $sth2->execute;
1053 #       my $data2=$sth2->fetchrow_hashref;
1054 #       $sth2->finish;
1055 #       $data=$data2;
1056  #     }
1057       $acctlines[$numlines] = $data;
1058       $numlines++;
1059       $total = $total+ $data->{'amountoutstanding'};
1060    }
1061    $sth->finish;
1062    $dbh->disconnect;
1063    return ($numlines,\@acctlines,$total);
1064 }
1065
1066 sub itemcount { 
1067   my ($env,$bibnum,$type)=@_; 
1068   my $dbh=C4Connect;   
1069   my $query="Select * from items where     
1070   biblionumber=$bibnum ";
1071   if ($type ne 'intra'){
1072     $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
1073     (wthdrawn <> 1 or wthdrawn is NULL)";      
1074   }
1075   my $sth=$dbh->prepare($query);         
1076   #  print $query;           
1077   $sth->execute;           
1078   my $count=0;             
1079   my $lcount=0;               
1080   my $nacount=0;                 
1081   my $fcount=0;
1082   my $scount=0;
1083   my $lostcount=0;
1084   my $mending=0;
1085   my $transit=0;
1086   my $ocount=0;
1087   while (my $data=$sth->fetchrow_hashref){
1088     $count++;                     
1089     my $query2="select * from issues,items where issues.itemnumber=                          
1090     '$data->{'itemnumber'}' and returndate is NULL
1091     and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
1092     items.itemlost <> 2) or items.itemlost is NULL) 
1093     and (wthdrawn <> 1 or wthdrawn is NULL)"; 
1094     
1095     my $sth2=$dbh->prepare($query2);     
1096     $sth2->execute;         
1097     if (my $data2=$sth2->fetchrow_hashref){         
1098        $nacount++;         
1099     } else {         
1100       if ($data->{'holdingbranch'} eq 'C'){         
1101         $lcount++;               
1102       }                       
1103       if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){         
1104         $fcount++;               
1105       }                       
1106       if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){         
1107         $scount++;               
1108       }                       
1109       if ($data->{'itemlost'} eq '1'){
1110         $lostcount++;
1111       }
1112       if ($data->{'itemlost'} eq '2'){
1113         $lostcount++;
1114       }
1115       if ($data->{'holdingbranch'} eq 'FM'){
1116         $mending++;
1117       }
1118       if ($data->{'holdingbranch'} eq 'TR'){
1119         $transit++;
1120       }
1121     }                             
1122     $sth2->finish;     
1123   } 
1124 #  if ($count == 0){
1125     my $query2="Select * from aqorders where biblionumber=$bibnum";
1126     my $sth2=$dbh->prepare($query2);
1127     $sth2->execute;
1128     if (my $data=$sth2->fetchrow_hashref){
1129       $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1130     }
1131 #    $count+=$ocount;
1132     $sth2->finish;
1133   $sth->finish; 
1134   $dbh->disconnect;                   
1135   return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount); 
1136 }
1137
1138 sub ItemType {
1139   my ($type)=@_;
1140   my $dbh=C4Connect;
1141   my $query="select description from itemtypes where itemtype='$type'";
1142   my $sth=$dbh->prepare($query);
1143   $sth->execute;
1144   my $dat=$sth->fetchrow_hashref;
1145   $sth->finish;
1146   $dbh->disconnect;
1147   return ($dat->{'description'});
1148 }
1149
1150
1151 sub bibitems {
1152     my ($bibnum) = @_;
1153     my $dbh   = C4Connect;
1154     my $query = "Select * from biblioitems, itemtypes, items
1155 where biblioitems.biblionumber = '$bibnum'
1156 and biblioitems.itemtype = itemtypes.itemtype
1157 and biblioitems.biblioitemnumber = items.biblioitemnumber
1158 group by items.biblioitemnumber";
1159     my $sth   = $dbh->prepare($query);
1160     my $count = 0;
1161     my @results;
1162
1163     $sth->execute;
1164
1165     while (my $data = $sth->fetchrow_hashref) {
1166         $results[$count] = $data;
1167         $count++;
1168     } # while
1169     
1170     $sth->finish;
1171     $dbh->disconnect;
1172     return($count, @results);
1173 } # sub bibitems
1174
1175
1176 sub barcodes{
1177   #called from request.pl
1178   my ($biblioitemnumber)=@_;
1179   my $dbh=C4Connect;
1180   my $query="Select barcode from items where
1181    biblioitemnumber='$biblioitemnumber'
1182    and ((itemlost <> 1 and itemlost <> 2) or itemlost is NULL) and
1183    (wthdrawn <> 1 or wthdrawn is NULL)";
1184
1185   my $sth=$dbh->prepare($query);
1186   $sth->execute;
1187   my @barcodes;
1188   my $i=0;
1189   while (my $data=$sth->fetchrow_hashref){
1190     $barcodes[$i]=$data->{'barcode'};
1191     $i++;
1192   }
1193   $sth->finish;
1194   $dbh->disconnect;
1195   return(@barcodes);
1196   
1197 }
1198
1199
1200 sub getwebsites {
1201     my ($biblionumber) = @_;
1202     my $dbh   = C4Connect;
1203     my $query = "Select * from websites where biblionumber = $biblionumber";
1204     my $sth   = $dbh->prepare($query);
1205     my $count = 0;
1206     my @results;
1207
1208     $sth->execute;
1209     while (my $data = $sth->fetchrow_hashref) {
1210         $results[$count] = $data;
1211         $count++;
1212     } # while
1213
1214     $sth->finish;
1215     $dbh->disconnect;
1216     return($count, @results);
1217 } # sub getwebsites
1218
1219
1220 sub getwebbiblioitems {
1221     my ($biblionumber) = @_;
1222     my $dbh   = C4Connect;
1223     my $query = "Select * from biblioitems where biblionumber = $biblionumber
1224 and itemtype = 'WEB'";
1225     my $sth   = $dbh->prepare($query);
1226     my $count = 0;
1227     my @results;
1228     
1229     $sth->execute;
1230     while (my $data = $sth->fetchrow_hashref) {
1231         $results[$count] = $data;
1232         $count++;
1233     } # while
1234     
1235     $sth->finish;
1236     $dbh->disconnect;
1237     return($count, @results);
1238 } # sub getwebbiblioitems
1239
1240
1241 END { }       # module clean-up code here (global destructor)
1242
1243 =head1 NAME
1244
1245 C4::Search - Module that provides Catalog searching for Koha
1246
1247 =head1 SYNOPSIS
1248
1249   use C4::Search;
1250   my ($count,@results)=KeywordSearch($env,$type,$search,$num,$offset);
1251   my ($count,@results)=CatSearch($env,$type,$search,$num,$offset);
1252
1253 =head1 DESCRIPTION
1254
1255 This module provides the searching facilities for the Catalog.
1256 Here I should go through and document each function thats exported and what it does. But I havent yet.
1257
1258 =head2 EXPORT
1259
1260 KeywordSearch
1261 CatSearch
1262 ItemInfo
1263
1264 =head1 AUTHOR
1265
1266 Koha Developement team <info@koha.org>
1267
1268 =head1 SEE ALSO
1269
1270 L<perl>.
1271
1272 =cut