Some minor changes to get detail.pl and moredetail.pl working nicely with
[koha-ffzg.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, branches
643 where (items.biblioitemnumber = biblioitems.biblioitemnumber)
644 and biblioitems.biblionumber = biblio.biblionumber
645 and biblio.biblionumber = '$biblionumber'
646 and branches.branchcode = items.holdingbranch";
647
648   if ($type ne 'intra'){
649     $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
650     or items.itemlost is NULL)
651     and (wthdrawn <> 1 or wthdrawn is NULL)";
652   }
653   $query .= " order by items.dateaccessioned desc";
654   my $sth=$dbh->prepare($query);
655   $sth->execute;
656   my $i=0;
657   my @results;
658 #  print $query;
659   while (my $data=$sth->fetchrow_hashref){
660     my $iquery = "Select * from issues
661     where itemnumber = '$data->{'itemnumber'}'
662     and returndate is null";
663     my $datedue = '';
664     my $isth=$dbh->prepare($iquery);
665     $isth->execute;
666     if (my $idata=$isth->fetchrow_hashref){
667       my @temp=split('-',$idata->{'date_due'});
668       $datedue = "$temp[2]/$temp[1]/$temp[0]";
669     }
670     if ($data->{'itemlost'} eq '1' || $data->{'itemlost'} eq '2'){
671         $datedue='Itemlost';
672     }
673     if ($data->{'wthdrawn'} eq '1'){
674       $datedue="Cancelled";
675     }
676     if ($datedue eq ''){
677        $datedue="Available";
678        my ($rescount,$reserves)=Findgroupreserve($data->{'biblioitemnumber'},$biblionumber);
679
680        if ($rescount >0){                                
681           $datedue='Request';
682        }
683     }
684     $isth->finish;
685     my $class = $data->{'classification'};
686     my $dewey = $data->{'dewey'};
687     $dewey =~ s/0+$//;
688     if ($dewey eq "000.") { $dewey = "";};    
689     if ($dewey < 10){$dewey='00'.$dewey;}
690     if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
691     if ($dewey <= 0){
692       $dewey='';
693     }
694     $dewey=~ s/\.$//;
695     $class = $class.$dewey;
696     if ($dewey ne ''){
697       $class = $class.$data->{'subclass'};
698     }
699  #   $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
700     my @temp=split('-',$data->{'datelastseen'});
701     my $date="$temp[2]/$temp[1]/$temp[0]";
702     $data->{'datelastseen'}=$date;
703     $data->{'datedue'}=$datedue;
704     $data->{'class'}=$class;
705     $results[$i]=$data;
706     $i++;
707   }
708  $sth->finish;
709   my $query2="Select * from aqorders where biblionumber=$biblionumber";
710   my $sth2=$dbh->prepare($query2);         
711   $sth2->execute;                                        
712   my $data;
713   my $ocount;
714   if ($data=$sth2->fetchrow_hashref){                   
715     $ocount=$data->{'quantity'} - $data->{'quantityreceived'};                                                  
716     if ($ocount > 0){
717       $data->{'ocount'}=$ocount;
718       $data->{'order'}="One Order";
719       $results[$i]=$data;
720     }
721   } 
722   $sth2->finish;
723
724   $dbh->disconnect;
725   return(@results);
726 }
727
728 sub GetItems {
729    my ($env,$biblionumber)=@_;
730    #debug_msg($env,"GetItems");
731    my $dbh = &C4Connect;
732    my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
733    #debug_msg($env,$query);
734    my $sth=$dbh->prepare($query);
735    $sth->execute;
736    #debug_msg($env,"executed query");      
737    my $i=0;
738    my @results;
739    while (my $data=$sth->fetchrow_hashref) {
740       #debug_msg($env,$data->{'biblioitemnumber'});
741       my $dewey = $data->{'dewey'};
742       $dewey =~ s/0+$//; 
743       my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
744       $line = $line."\t$data->{'classification'}\t$dewey";
745       $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
746       $line = $line."\t$data->{'volume'}\t$data->{number}";
747       my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
748       $isth->execute;
749       while (my $idata = $isth->fetchrow_hashref) {
750         my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
751         if ($idata->{'notforloan'} == 1) {
752           $iline = $iline."NFL ";
753         }
754         if ($idata->{'itemlost'} == 1) {
755           $iline = $iline."LOST ";
756         }        
757         $line = $line."\t$iline"; 
758       }
759       $isth->finish;
760       $results[$i] = $line;
761       $i++;      
762    }
763    $sth->finish;
764    $dbh->disconnect;
765    return(@results);
766 }            
767   
768 sub itemdata {
769   my ($barcode)=@_;
770   my $dbh=C4Connect;
771   my $query="Select * from items,biblioitems where barcode='$barcode'
772   and items.biblioitemnumber=biblioitems.biblioitemnumber";
773 #  print $query;
774   my $sth=$dbh->prepare($query);
775   $sth->execute;
776   my $data=$sth->fetchrow_hashref;
777   $sth->finish;
778   $dbh->disconnect;
779   return($data);
780 }
781
782
783 sub bibdata {
784     my ($bibnum, $type) = @_;
785     my $dbh   = C4Connect;
786     my $query = "Select *, biblio.notes  
787     from biblio, biblioitems 
788     left join bibliosubtitle on
789     biblio.biblionumber = bibliosubtitle.biblionumber
790     where biblio.biblionumber = $bibnum
791     and biblioitems.biblionumber = $bibnum";
792     my $sth   = $dbh->prepare($query);
793     my $data;
794
795     $sth->execute;
796     $data  = $sth->fetchrow_hashref;
797     $sth->finish;
798
799     $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
800     $sth   = $dbh->prepare($query);
801     $sth->execute;
802     while (my $dat = $sth->fetchrow_hashref){
803         $data->{'subject'} .= " | $dat->{'subject'}";
804     } # while
805
806     $sth->finish;
807     $dbh->disconnect;
808     return($data);
809 } # sub bibdata
810
811
812 sub bibitemdata {
813     my ($bibitem) = @_;
814     my $dbh   = C4Connect;
815     my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
816 where biblio.biblionumber = biblioitems.biblionumber
817 and biblioitemnumber = $bibitem
818 and biblioitems.itemtype = itemtypes.itemtype";
819     my $sth   = $dbh->prepare($query);
820     my $data;
821
822     $sth->execute;
823
824     $data = $sth->fetchrow_hashref;
825
826     $sth->finish;
827     $dbh->disconnect;
828     return($data);
829 } # sub bibitemdata
830
831
832 sub subject {
833   my ($bibnum)=@_;
834   my $dbh=C4Connect;
835   my $query="Select * from bibliosubject where biblionumber=$bibnum";
836   my $sth=$dbh->prepare($query);
837   $sth->execute;
838   my @results;
839   my $i=0;
840   while (my $data=$sth->fetchrow_hashref){
841     $results[$i]=$data;
842     $i++;
843   }
844   $sth->finish;
845   $dbh->disconnect;
846   return($i,\@results);
847 }
848
849 sub addauthor {
850   my ($bibnum)=@_;
851   my $dbh=C4Connect;
852   my $query="Select * from additionalauthors where biblionumber=$bibnum";
853   my $sth=$dbh->prepare($query);
854   $sth->execute;
855   my @results;
856   my $i=0;
857   while (my $data=$sth->fetchrow_hashref){
858     $results[$i]=$data;
859     $i++;
860   }
861   $sth->finish;
862   $dbh->disconnect;
863   return($i,\@results);
864 }
865
866 sub subtitle {
867   my ($bibnum)=@_;
868   my $dbh=C4Connect;
869   my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
870   my $sth=$dbh->prepare($query);
871   $sth->execute;
872   my @results;
873   my $i=0;
874   while (my $data=$sth->fetchrow_hashref){
875     $results[$i]=$data;
876     $i++;
877   }
878   $sth->finish;
879   $dbh->disconnect;
880   return($i,\@results);
881 }
882
883
884
885 sub itemissues {
886     my ($bibitem, $biblio)=@_;
887     my $dbh   = C4Connect;
888     my $query = "Select * from items where 
889 items.biblioitemnumber = '$bibitem'";
890     my $sth   = $dbh->prepare($query)
891       || die $dbh->errstr;
892     my $i     = 0;
893     my @results;
894   
895     $sth->execute
896       || die $sth->errstr;
897
898     while (my $data = $sth->fetchrow_hashref) {
899         my $query2 = "select * from issues,borrowers
900 where itemnumber = $data->{'itemnumber'}
901 and returndate is NULL
902 and issues.borrowernumber = borrowers.borrowernumber";
903         my $sth2   = $dbh->prepare($query2);
904
905         $sth2->execute; 
906         if (my $data2 = $sth2->fetchrow_hashref) {
907             $data->{'date_due'} = $data2->{'date_due'};
908             $data->{'card'}     = $data2->{'cardnumber'};
909         } else {
910             if ($data->{'wthdrawn'} eq '1') {
911                 $data->{'date_due'} = 'Cancelled';
912             } else {
913                 $data->{'date_due'} = 'Available';
914             } # else
915         } # else
916
917         $sth2->finish;
918         $query2 = "select * from issues, borrowers
919 where itemnumber = '$data->{'itemnumber'}'
920 and issues.borrowernumber = borrowers.borrowernumber 
921 order by date_due desc";
922         $sth2 = $dbh->prepare($query2)
923           || die $dbh->errstr;
924         $sth2->execute
925           || die $sth2->errstr;
926
927         for (my $i2 = 0; $i2 < 2; $i2++) {
928             if (my $data2 = $sth2->fetchrow_hashref) {
929                 $data->{"timestamp$i2"} = $data2->{'timestamp'};
930                 $data->{"card$i2"}      = $data2->{'cardnumber'};
931                 $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
932             } # if
933         } # for
934
935         $sth2->finish;
936         $results[$i] = $data;
937         $i++;
938     }
939
940     $sth->finish;
941     $dbh->disconnect;
942     return(@results);
943 }
944
945
946 sub itemnodata {
947   my ($env,$dbh,$itemnumber) = @_;
948   $dbh=C4Connect;
949   my $query="Select * from biblio,items,biblioitems
950     where items.itemnumber = '$itemnumber'
951     and biblio.biblionumber = items.biblionumber
952     and biblioitems.biblioitemnumber = items.biblioitemnumber";
953   my $sth=$dbh->prepare($query);
954 #  print $query;
955   $sth->execute;
956   my $data=$sth->fetchrow_hashref;
957   $sth->finish;  
958   $dbh->disconnect;
959   return($data);               
960 }
961
962 #used by member enquiries from the intranet
963 #called by member.pl
964 sub BornameSearch  {
965   my ($env,$searchstring,$type)=@_;
966   my $dbh = &C4Connect;
967   $searchstring=~ s/\'/\\\'/g;
968   my @data=split(' ',$searchstring);
969   my $count=@data;
970   my $query="Select * from borrowers 
971   where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\" 
972   or firstname  like \"$data[0]%\" or firstname like \"% $data[0]%\" 
973   or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
974   ";
975   for (my $i=1;$i<$count;$i++){
976     $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"                  
977     or firstname  like \"$data[$i]%\" or firstname like \"% $data[$i]%\"                    
978     or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
979   }
980   $query=$query.") or cardnumber = \"$searchstring\"
981   order by surname,firstname";
982 #  print $query,"\n";
983   my $sth=$dbh->prepare($query);
984   $sth->execute;
985   my @results;
986   my $cnt=0;
987   while (my $data=$sth->fetchrow_hashref){
988     push(@results,$data);
989     $cnt ++;
990   }
991 #  $sth->execute;
992   $sth->finish;
993   $dbh->disconnect;
994   return ($cnt,\@results);
995 }
996
997 sub borrdata {
998   my ($cardnumber,$bornum)=@_;
999   $cardnumber = uc $cardnumber;
1000   my $dbh=C4Connect;
1001   my $query;
1002   if ($bornum eq ''){
1003     $query="Select * from borrowers where cardnumber='$cardnumber'";
1004   } else {
1005       $query="Select * from borrowers where borrowernumber='$bornum'";
1006   }
1007   #print $query;
1008   my $sth=$dbh->prepare($query);
1009   $sth->execute;
1010   my $data=$sth->fetchrow_hashref;
1011   $sth->finish;
1012   $dbh->disconnect;
1013   return($data);
1014 }
1015
1016 sub borrissues {
1017   my ($bornum)=@_;
1018   my $dbh=C4Connect;
1019   my $query;
1020   $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
1021 items.itemnumber=issues.itemnumber and
1022 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
1023 by date_due";
1024   #print $query;
1025   my $sth=$dbh->prepare($query);
1026     $sth->execute;
1027   my @result;
1028   my $i=0;
1029   while (my $data=$sth->fetchrow_hashref){
1030     $result[$i]=$data;;
1031     $i++;
1032   }
1033   $sth->finish;
1034   $dbh->disconnect;
1035   return($i,\@result);
1036 }
1037
1038 sub allissues { 
1039   my ($bornum,$order,$limit)=@_; 
1040   my $dbh=C4Connect;   
1041   my $query;     
1042   $query="Select * from issues,biblio,items,biblioitems       
1043   where borrowernumber='$bornum' and         
1044   items.biblioitemnumber=biblioitems.biblioitemnumber and           
1045   items.itemnumber=issues.itemnumber and             
1046   items.biblionumber=biblio.biblionumber";               
1047   $query.=" order by $order";                 
1048   if ($limit !=0){                   
1049     $query.=" limit $limit";                     
1050   }                         
1051   #print $query;                           
1052   my $sth=$dbh->prepare($query);          
1053   $sth->execute;
1054   my @result;   
1055   my $i=0;    
1056   while (my $data=$sth->fetchrow_hashref){                                      
1057     $result[$i]=$data;; 
1058     $i++;     
1059   }         
1060   $sth->finish;           
1061   $dbh->disconnect;             
1062   return($i,\@result);               
1063 }
1064
1065 sub borrdata2 {
1066   my ($env,$bornum)=@_;
1067   my $dbh=C4Connect;
1068   my $query="Select count(*) from issues where borrowernumber='$bornum' and
1069     returndate is NULL";
1070     # print $query;
1071   my $sth=$dbh->prepare($query);
1072   $sth->execute;
1073   my $data=$sth->fetchrow_hashref;
1074   $sth->finish;
1075   $sth=$dbh->prepare("Select count(*) from issues where
1076     borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1077   $sth->execute;
1078   my $data2=$sth->fetchrow_hashref;
1079   $sth->finish;
1080   $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1081     borrowernumber='$bornum'");
1082   $sth->execute;
1083   my $data3=$sth->fetchrow_hashref;
1084   $sth->finish;
1085   $dbh->disconnect;
1086
1087 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1088 }
1089         
1090
1091 sub getboracctrecord {
1092    my ($env,$params) = @_;
1093    my $dbh=C4Connect;
1094    my @acctlines;
1095    my $numlines=0;
1096    my $query= "Select * from accountlines where
1097 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1098    my $sth=$dbh->prepare($query);
1099 #   print $query;
1100    $sth->execute;
1101    my $total=0;
1102    while (my $data=$sth->fetchrow_hashref){
1103 #      if ($data->{'itemnumber'} ne ''){
1104 #        $query="Select * from items,biblio where items.itemnumber=
1105 #       '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1106 #       my $sth2=$dbh->prepare($query);
1107 #       $sth2->execute;
1108 #       my $data2=$sth2->fetchrow_hashref;
1109 #       $sth2->finish;
1110 #       $data=$data2;
1111  #     }
1112       $acctlines[$numlines] = $data;
1113       $numlines++;
1114       $total = $total+ $data->{'amountoutstanding'};
1115    }
1116    $sth->finish;
1117    $dbh->disconnect;
1118    return ($numlines,\@acctlines,$total);
1119 }
1120
1121
1122 sub itemcount { 
1123   my ($env,$bibnum,$type)=@_; 
1124   my $dbh=C4Connect;   
1125   my $query="Select * from items where     
1126   biblionumber=$bibnum ";
1127   if ($type ne 'intra'){
1128     $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
1129     (wthdrawn <> 1 or wthdrawn is NULL)";      
1130   }
1131   my $sth=$dbh->prepare($query);         
1132   #  print $query;           
1133   $sth->execute;           
1134   my $count=0;             
1135   my $lcount=0;               
1136   my $nacount=0;                 
1137   my $fcount=0;
1138   my $scount=0;
1139   my $lostcount=0;
1140   my $mending=0;
1141   my $transit=0;
1142   my $ocount=0;
1143   while (my $data=$sth->fetchrow_hashref){
1144     $count++;                     
1145     my $query2="select * from issues,items where issues.itemnumber=                          
1146     '$data->{'itemnumber'}' and returndate is NULL
1147     and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
1148     items.itemlost <> 2) or items.itemlost is NULL) 
1149     and (wthdrawn <> 1 or wthdrawn is NULL)"; 
1150     
1151     my $sth2=$dbh->prepare($query2);     
1152     $sth2->execute;         
1153     if (my $data2=$sth2->fetchrow_hashref){         
1154        $nacount++;         
1155     } else {         
1156       if ($data->{'holdingbranch'} eq 'C'){         
1157         $lcount++;               
1158       }                       
1159       if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){         
1160         $fcount++;               
1161       }                       
1162       if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){         
1163         $scount++;               
1164       }                       
1165       if ($data->{'itemlost'} eq '1'){
1166         $lostcount++;
1167       }
1168       if ($data->{'itemlost'} eq '2'){
1169         $lostcount++;
1170       }
1171       if ($data->{'holdingbranch'} eq 'FM'){
1172         $mending++;
1173       }
1174       if ($data->{'holdingbranch'} eq 'TR'){
1175         $transit++;
1176       }
1177     }                             
1178     $sth2->finish;     
1179   } 
1180 #  if ($count == 0){
1181     my $query2="Select * from aqorders where biblionumber=$bibnum";
1182     my $sth2=$dbh->prepare($query2);
1183     $sth2->execute;
1184     if (my $data=$sth2->fetchrow_hashref){
1185       $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1186     }
1187 #    $count+=$ocount;
1188     $sth2->finish;
1189   $sth->finish; 
1190   $dbh->disconnect;                   
1191   return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount); 
1192 }
1193
1194
1195 sub itemcount2 { 
1196   my ($env,$bibnum,$type)=@_; 
1197   my $dbh=C4Connect;   
1198   my $query="Select * from items,branches where     
1199   biblionumber=$bibnum and items.holdingbranch=branches.branchcode";
1200   if ($type ne 'intra'){
1201     $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
1202     (wthdrawn <> 1 or wthdrawn is NULL)";      
1203   }
1204   my $sth=$dbh->prepare($query);         
1205   #  print $query;           
1206   $sth->execute;           
1207   my %counts;
1208   $counts{'total'}=0;
1209   while (my $data=$sth->fetchrow_hashref){
1210     $counts{'total'}++;                     
1211     my $query2="select * from issues,items where issues.itemnumber=                          
1212     '$data->{'itemnumber'}' and returndate is NULL
1213     and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
1214     items.itemlost <> 2) or items.itemlost is NULL) 
1215     and (wthdrawn <> 1 or wthdrawn is NULL)"; 
1216     
1217     my $sth2=$dbh->prepare($query2);     
1218     $sth2->execute;         
1219     if (my $data2=$sth2->fetchrow_hashref){         
1220        $counts{'notavailable'}++;         
1221     } else {         
1222        $counts{$data->{'branchname'}}++;
1223     }                             
1224     $sth2->finish;     
1225   } 
1226   my $query2="Select * from aqorders where biblionumber=$bibnum and
1227   datecancellationprinted is NULL and quantity > quantityreceived";
1228   my $sth2=$dbh->prepare($query2);
1229   $sth2->execute;
1230   if (my $data=$sth2->fetchrow_hashref){
1231       $counts{'order'}=$data->{'quantity'} - $data->{'quantityreceived'};
1232   }
1233   $sth2->finish;
1234   $sth->finish; 
1235   $dbh->disconnect;                   
1236   return (\%counts); 
1237 }
1238
1239
1240 sub ItemType {
1241   my ($type)=@_;
1242   my $dbh=C4Connect;
1243   my $query="select description from itemtypes where itemtype='$type'";
1244   my $sth=$dbh->prepare($query);
1245   $sth->execute;
1246   my $dat=$sth->fetchrow_hashref;
1247   $sth->finish;
1248   $dbh->disconnect;
1249   return ($dat->{'description'});
1250 }
1251
1252
1253 sub bibitems {
1254     my ($bibnum) = @_;
1255     my $dbh   = C4Connect;
1256     my $query = "Select * from biblioitems, itemtypes, items
1257 where biblioitems.biblionumber = '$bibnum'
1258 and biblioitems.itemtype = itemtypes.itemtype
1259 and biblioitems.biblioitemnumber = items.biblioitemnumber
1260 group by items.biblioitemnumber";
1261     my $sth   = $dbh->prepare($query);
1262     my $count = 0;
1263     my @results;
1264
1265     $sth->execute;
1266
1267     while (my $data = $sth->fetchrow_hashref) {
1268         $results[$count] = $data;
1269         $count++;
1270     } # while
1271     
1272     $sth->finish;
1273     $dbh->disconnect;
1274     return($count, @results);
1275 } # sub bibitems
1276
1277
1278 sub barcodes{
1279   #called from request.pl
1280   my ($biblioitemnumber)=@_;
1281   my $dbh=C4Connect;
1282   my $query="Select barcode from items where
1283    biblioitemnumber='$biblioitemnumber'
1284    and ((itemlost <> 1 and itemlost <> 2) or itemlost is NULL) and
1285    (wthdrawn <> 1 or wthdrawn is NULL)";
1286
1287   my $sth=$dbh->prepare($query);
1288   $sth->execute;
1289   my @barcodes;
1290   my $i=0;
1291   while (my $data=$sth->fetchrow_hashref){
1292     $barcodes[$i]=$data->{'barcode'};
1293     $i++;
1294   }
1295   $sth->finish;
1296   $dbh->disconnect;
1297   return(@barcodes);
1298   
1299 }
1300
1301
1302 sub getwebsites {
1303     my ($biblionumber) = @_;
1304     my $dbh   = C4Connect;
1305     my $query = "Select * from websites where biblionumber = $biblionumber";
1306     my $sth   = $dbh->prepare($query);
1307     my $count = 0;
1308     my @results;
1309
1310     $sth->execute;
1311     while (my $data = $sth->fetchrow_hashref) {
1312         $data->{'url'} =~ s/^http:\/\///;
1313         $results[$count] = $data;
1314         $count++;
1315     } # while
1316
1317     $sth->finish;
1318     $dbh->disconnect;
1319     return($count, @results);
1320 } # sub getwebsites
1321
1322
1323 sub getwebbiblioitems {
1324     my ($biblionumber) = @_;
1325     my $dbh   = C4Connect;
1326     my $query = "Select * from biblioitems where biblionumber = $biblionumber
1327 and itemtype = 'WEB'";
1328     my $sth   = $dbh->prepare($query);
1329     my $count = 0;
1330     my @results;
1331     
1332     $sth->execute;
1333     while (my $data = $sth->fetchrow_hashref) {
1334         $data->{'url'} =~ s/^http:\/\///;
1335         $results[$count] = $data;
1336         $count++;
1337     } # while
1338     
1339     $sth->finish;
1340     $dbh->disconnect;
1341     return($count, @results);
1342 } # sub getwebbiblioitems
1343
1344
1345 END { }       # module clean-up code here (global destructor)
1346
1347 =head1 NAME
1348
1349 C4::Search - Module that provides Catalog searching for Koha
1350
1351 =head1 SYNOPSIS
1352
1353   use C4::Search;
1354   my ($count,@results)=catalogsearch($env,$type,$search,$num,$offset);
1355
1356 =head1 DESCRIPTION
1357
1358 This module provides the searching facilities for the Catalog.
1359 Here I should go through and document each function thats exported and what it does. But I havent yet.
1360
1361 my ($count,@results)=catalogsearch($env,$type,$search,$num,$offset);
1362 This is a front end to all the other searches, depending on what is passed
1363 to it, it calls the appropriate search
1364
1365 =head2 EXPORT
1366
1367 catalogsearch
1368
1369 =head1 AUTHOR
1370
1371 Koha Developement team <info@koha.org>
1372
1373 =head1 SEE ALSO
1374
1375 L<perl>.
1376
1377 =cut