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