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