New files, and changes to add associated webstes to a biblio.
[koha_gimpoz] / C4 / Search.pm
index ece95df..d594e14 100755 (executable)
@@ -8,18 +8,20 @@ require Exporter;
 use DBI;
 use C4::Database;
 use C4::Reserves2;
+use Set::Scalar;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
   
 # set the version for version checking
-$VERSION = 0.01;
+$VERSION = 0.02;
     
 @ISA = qw(Exporter);
 @EXPORT = qw(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
-&itemdata &bibdata &GetItems &borrdata &getacctlist &itemnodata &itemcount
-&OpacSearch &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
-&getboracctrecord &ItemType &itemissues &FrontSearch &subject &subtitle
-&addauthor &bibitems &barcodes &findguarantees &allissues &systemprefs); 
+&itemdata &bibdata &GetItems &borrdata &itemnodata &itemcount
+&borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
+&getboracctrecord &ItemType &itemissues &subject &subtitle
+&addauthor &bibitems &barcodes &findguarantees &allissues &systemprefs
+&findguarantor &getwebsites);
 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
                  
 # your exported package globals go here,
@@ -71,7 +73,24 @@ sub findguarantees{
   $dbh->disconnect;         
   return($i,\@dat);             
 }
-
+sub findguarantor{  
+  my ($bornum)=@_;  
+  my $dbh=C4Connect;    
+  my $query="select guarantor from borrowers where      
+  borrowernumber='$bornum'";        
+  my $sth=$dbh->prepare($query);          
+  $sth->execute;            
+  my $data=$sth->fetchrow_hashref;              
+  $sth->finish;                
+  $query="Select * from borrowers where
+  borrowernumber='$data->{'guarantor'}'";  
+  $sth=$dbh->prepare($query);  
+  $sth->execute;    
+  $data=$sth->fetchrow_hashref;      
+  $sth->finish;        
+  $dbh->disconnect;          
+  return($data);            
+}
 
 sub systemprefs {
     my %systemprefs;
@@ -97,271 +116,189 @@ sub NewBorrowerNumber {
   return($data->{'max(borrowernumber)'}); 
 }    
 
-sub OpacSearch {
+  
+sub KeywordSearch {
   my ($env,$type,$search,$num,$offset)=@_;
   my $dbh = &C4Connect;
-  $search->{'keyword'}=~ s/'/\\'/g;
+  $search->{'keyword'}=~ s/ +$//;
+  $search->{'keyword'}=~ s/'/\\'/;
   my @key=split(' ',$search->{'keyword'});
   my $count=@key;
   my $i=1;
   my @results;
-  my $query ="Select count(*) from biblio where 
-  ((title like '$key[0]%' or title like '% $key[0]%')";
-  while ($i < $count){
-    $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
-    $i++;
+  my $query="Select biblionumber from biblio
+  where ((title like '$key[0]%' or title like '% $key[0]%')";
+  while ($i < $count){                                                  
+      $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";                                                   
+      $i++;                                                  
   }
-  $query=$query.") or ((author like '$key[0]%' or author like '% $key[0]%')";
-  $i=1;
-  while ($i < $count){
-    $query=$query." and (author like '$key[$i]%' or author like '% $key[$i]%')";
-    $i++;
-  }
-  $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
-  for ($i=1;$i<$count;$i++){
-    $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
+  $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";                                             
+  for ($i=1;$i<$count;$i++){                                                  
+      $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";                                           
   }
-  $query.= ") or ((notes like '$key[0]%' or notes like '% $key[0]%')";
-  for ($i=1;$i<$count;$i++){
-    $query.=" and (notes like '$key[$i]%' or notes like '% $key[$i]%')";
+   $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";                                               
+  for ($i=1;$i<$count;$i++){                                                  
+      $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";                                             
   }
-  $query=$query.") order by title";
+  $query.=" )";
+#  print $query;
   my $sth=$dbh->prepare($query);
   $sth->execute;
-  my $data=$sth->fetchrow_hashref;
-  my $count=$data->{'count(*)'};
+  my $i=0;
+  while (my @res=$sth->fetchrow_array){
+    $results[$i]=$res[0];
+    $i++;
+  }
   $sth->finish;
-  $query=~ s/count\(\*\)/\*/;
-  $query= $query." limit $offset,$num";
-  $sth=$dbh->prepare($query);
+  my $set1=Set::Scalar->new(@results);
+  $query="Select biblionumber from bibliosubtitle where
+  ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";                 
+  for ($i=1;$i<$count;$i++){   
+        $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";                                                  
+  }                   
+  $query.=" )";
 #  print $query;
+  $sth=$dbh->prepare($query);
   $sth->execute;
   $i=0;
-  while (my $data=$sth->fetchrow_hashref){
-      my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}");
-      $sti->execute;
-      my ($dewey, $subclass) = $sti->fetchrow;
-      $dewey=~s/0*$//;
-      ($dewey == 0) && ($dewey='');
-      ($dewey) && ($dewey.=" $subclass");
-      $sti->finish;
-    $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$dewey";
+  while (my @res=$sth->fetchrow_array){
+    $results[$i]=$res[0];
     $i++;
   }
   $sth->finish;
-  $dbh->disconnect;
-  return($count,@results);
-}
-
-
-  
-sub FrontSearch {
-  my ($env,$type,$search,$num,$offset)=@_;
-  my $dbh = &C4Connect;
-  $search->{'front'}=~ s/ +$//;
-  $search->{'front'}=~ s/'/\\'/;
-  my @key=split(' ',$search->{'front'});
-  my $count=@key;
-  my $i=1;
-  my @results;
-  my $query ="Select * from biblio,bibliosubtitle where
-  biblio.biblionumber=bibliosubtitle.biblionumber and
-  ((title like '$key[0]%' or title like '% $key[0]%'
-  or subtitle like '$key[0]%' or subtitle like '% $key[0]%'
-  or author like '$key[0]%' or author like '% $key[0]%')";
-  while ($i < $count){
-    $query=$query." and (title like '%$key[$i]%' or subtitle like '%$key[$i]%')";
-    $i++;
+  my $set2=Set::Scalar->new(@results);
+  if ($i > 0){
+    $set1=$set1+$set2;
   }
-  $query=$query.") group by biblio.biblionumber order by author,title";
-  print $query;
-  my $sth=$dbh->prepare($query);
+  $query ="Select biblionumber from biblioitems where
+  ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";                                   
+  for ($i=1;$i<$count;$i++){                                                  
+      $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";                                 
+  }            
+  $query.=" )";
+#  print $query;
+  $sth=$dbh->prepare($query);
   $sth->execute;
   $i=0;
-  while (my $data=$sth->fetchrow_hashref){
-    my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}");
-    $sti->execute;
-    my ($dewey, $subclass) = $sti->fetchrow;
-    $dewey=~s/\.*0*$//;
-    ($dewey == 0) && ($dewey='');
-    ($dewey) && ($dewey.=" $subclass");
-    $sti->finish;
-    $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
-#      print $results[$i];
+  while (my @res=$sth->fetchrow_array){
+    $results[$i]=$res[0];
     $i++;
   }
   $sth->finish;
-  $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
-  like '%$search->{'keyword'}%'");
-  $sth->execute;
-  while (my $data=$sth->fetchrow_hashref){
-    my $sth2=$dbh->prepare("Select * from biblio where
-    biblionumber=$data->{'biblionumber'}");
-    $sth2->execute;
-    while (my $data2=$sth2->fetchrow_hashref){
-
-$results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data->{'copyrightdate'}";
-#      print $results[$i];
-      $i++;   
-    }
-    $sth2->finish;
-  }    
-  my $i2=1;
-  @results=sort @results;
-  my @res;
-  my $count=@results;
-  $i=1;
-  $res[0]=$results[0];
-  while ($i2 < $count){
-    if ($results[$i2] ne $res[$i-1]){
-      $res[$i]=$results[$i2];
-      $i++;
-    }
-    $i2++;
+  my $set3=Set::Scalar->new(@results);    
+  if ($i > 0){
+    $set1=$set1+$set3;
   }
-  $i2=0;
-  my @res2;
-  $count=@res;
-  while ($i2 < $num && $i2 < $count){
-    $res2[$i2]=$res[$i2+$offset];
-#    print $res2[$i2];
-    $i2++;
-  }
-  $sth->finish;
-  $dbh->disconnect;
-  return($i,@res2);
-}
-
-  
-sub KeywordSearch {
-  my ($env,$type,$search,$num,$offset)=@_;
-  my $dbh = &C4Connect;
-  $search->{'keyword'}=~ s/ +$//;
-  $search->{'keyword'}=~ s/'/\\'/;
-  my @key=split(' ',$search->{'keyword'});
-  my $count=@key;
-  my $i=1;
-  my @results;
-  my $query ="Select * from biblio,bibliosubtitle,biblioitems where
-  biblio.biblionumber=bibliosubtitle.biblionumber and
-  biblioitems.biblionumber=biblio.biblionumber and
-  (((title like '$key[0]%' or title like '% $key[0]%')";
-  while ($i < $count){
-    $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
-    $i++;
-  }
-  $query.= ") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
-  for ($i=1;$i<$count;$i++){
-    $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
-  }
-  $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
-  for ($i=1;$i<$count;$i++){
-    $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
-  }
-  $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
-  for ($i=1;$i<$count;$i++){
-    $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
-  }
-  $query.= ") or ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
-  for ($i=1;$i<$count;$i++){
-    $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
-  }
-  if ($search->{'keyword'} =~ /new zealand/i){
-    $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
-    or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %' 
-    or author like '% nz %' or author like '% nz')"
-  }
-  if ($search->{'keyword'} eq  'nz' || $search->{'keyword'} eq 'NZ' ||
-  $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
-  $search->{'keyword'} =~ / nz/i){
-    $query.= "or (title like 'new zealand%' or title like '% new zealand %'
-    or title like '% new zealand' or subtitle like 'new zealand%' or
-    subtitle like '% new zealand %'
-    or subtitle like '% new zealand' or author like 'new zealand%' 
-    or author like '% new zealand %' or author like '% new zealand' or 
-    seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
-    or seriestitle like '% new zealand')"
-  }
-  $query=$query."))";
-    if ($search->{'class'} ne ''){
-    my @temp=split(/\|/,$search->{'class'});
-    my $count=@temp;
-    $query.= "and ( itemtype='$temp[0]'";
-    for (my $i=1;$i<$count;$i++){
-      $query.=" or itemtype='$temp[$i]'";
-    }
-    $query.=")"; 
-  }
-   $query.="group by biblio.biblionumber order by author,title";
-#  print $query;
-  my $sth=$dbh->prepare($query);
+  $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
+  like '%$search->{'keyword'}%' group by biblionumber");
   $sth->execute;
   $i=0;
-  while (my $data=$sth->fetchrow_hashref){
-    my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}");
-    $sti->execute;
-    my ($dewey, $subclass) = $sti->fetchrow;
-    $dewey=~s/\.*0*$//;
-    ($dewey == 0) && ($dewey='');
-    ($dewey) && ($dewey.=" $subclass");
-    $sti->finish;
-    $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
-#      print $results[$i];
+  while (my @res=$sth->fetchrow_array){
+    $results[$i]=$res[0];
     $i++;
   }
   $sth->finish;
-  $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
-  like '%$search->{'keyword'}%'");
-  $sth->execute;
-  while (my $data=$sth->fetchrow_hashref){
-    $query="Select * from biblio,biblioitems where
-    biblio.biblionumber=$data->{'biblionumber'} and biblio.biblionumber=biblioitems.biblionumber";
+  my $set4=Set::Scalar->new(@results);    
+  if ($i > 0){
+    $set1=$set1+$set4;
+  }
+  my $i2=0;
+  my $i3=0;
+  my $i4=0;
+
+  my @res2;
+  my @res = $set1->members;
+  $count=@res;
+#  print $set1;
+  $i=0;
+#  print "count $count";
+  if ($search->{'class'} ne ''){ 
+    while ($i2 <$count){
+      my $query="select * from biblio,biblioitems where 
+      biblio.biblionumber='$res[$i2]' and     
+      biblio.biblionumber=biblioitems.biblionumber ";         
+      if ($search->{'class'} ne ''){             
+      my @temp=split(/\|/,$search->{'class'});
+      my $count=@temp;                       
+      $query.= "and ( itemtype='$temp[0]'";     
+      for (my $i=1;$i<$count;$i++){     
+        $query.=" or itemtype='$temp[$i]'";                                     
+      } 
+      $query.=")"; 
+      }
+       my $sth=$dbh->prepare($query);    
+       #    print $query;        
+       $sth->execute;        
+       if (my $data2=$sth->fetchrow_hashref){            
+         my $dewey= $data2->{'dewey'};                
+         my $subclass=$data2->{'subclass'};          
+         $dewey=~s/\.*0*$//;           
+         ($dewey == 0) && ($dewey=''); 
+         ($dewey) && ($dewey.=" $subclass") ;                                    
+          $sth->finish;
+         my $end=$offset +$num;
+         if ($i4 <= $offset){
+           $i4++;
+         }
+#        print $i4;
+         if ($i4 <=$end && $i4 > $offset){
+           $data2->{'dewey'}=$dewey;
+           $res2[$i3]=$data2;
+           
+#          $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";    
+            $i3++;
+            $i4++;
+#          print "in here $i3<br>";
+         } else {
+#          print $end;
+         }
+         $i++; 
+        }         
+     $i2++;
+     }
+     $count=$i;
+  
+   } else {
+  while ($i2 < $num && $i2 < $count){
+    my $query="select * from biblio,biblioitems where
+    biblio.biblionumber='$res[$i2+$offset]' and        
+    biblio.biblionumber=biblioitems.biblionumber ";
     if ($search->{'class'} ne ''){
       my @temp=split(/\|/,$search->{'class'});
       my $count=@temp;
-      $query.= " and ( itemtype='$temp[0]'";
+      $query.= "and ( itemtype='$temp[0]'";
       for (my $i=1;$i<$count;$i++){
         $query.=" or itemtype='$temp[$i]'";
       }
       $query.=")"; 
     }
-    my $sth2=$dbh->prepare($query);
-    $sth2->execute;
-#    print $query;
-    while (my $data2=$sth2->fetchrow_hashref){
-       $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}";
-#      print $results[$i];
-      $i++;   
+    if ($search->{'dewey'} ne ''){
+      $query.= "and (dewey like '$search->{'dewey'}%') ";
     }
-    $sth2->finish;
-  }    
-  my $i2=1;
-  @results=sort @results;
-  my @res;
-  my $count=@results;
-  $i=0;
-  if ($count > 0){
-    $res[0]=$results[0];
-  }
-  while ($i2 < $count){
-    if ($results[$i2] ne $res[$i-1]){
-      $res[$i]=$results[$i2];
-      $i++;
+
+    my $sth=$dbh->prepare($query);
+#    print $query;
+    $sth->execute;
+    if (my $data2=$sth->fetchrow_hashref){
+        my $dewey= $data2->{'dewey'};               
+        my $subclass=$data2->{'subclass'};                   
+       $dewey=~s/\.*0*$//;     
+        ($dewey == 0) && ($dewey='');               
+        ($dewey) && ($dewey.=" $subclass") ;                      
+        $sth->finish;                                             
+       $data2->{'dewey'}=$dewey;
+       $res2[$i]=$data2;
+#      $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
+        $i++;
     }
     $i2++;
+    
   }
-  $i2=0;
-  my @res2;
-  $count=@res;
-  while ($i2 < $num && $i2 < $count){
-    $res2[$i2]=$res[$i2+$offset];
-#    print $res2[$i2];
-    $i2++;
   }
-  $sth->finish;
   $dbh->disconnect;
-#  $i--;
-  return($i,@res2);
+
+  #$count=$i;
+  return($count,@res2);
 }
 
 sub CatSearch  {
@@ -380,11 +317,10 @@ sub CatSearch  {
        my $count=@key;
        my $i=1;
         $query="select *,biblio.author,biblio.biblionumber from
-         biblioitems,biblio
+         biblio
         left join additionalauthors
         on additionalauthors.biblionumber =biblio.biblionumber
-         where biblioitems.biblionumber=biblio.biblionumber 
-        and
+        where
          ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
         additionalauthors.author like '$key[0]%' or additionalauthors.author 
         like '% $key[0]%'
@@ -398,25 +334,31 @@ sub CatSearch  {
         }   
         $query=$query.")";
          if ($search->{'title'} ne ''){ 
-          $query=$query. " and (title like '%$search->{'title'}%' 
-          or seriestitle like '%$search->{'title'}%')";
-        }
-        if ($search->{'class'} ne ''){
-          my @temp=split(/\|/,$search->{'class'});
-          my $count=@temp;
-          $query.= "and ( itemtype='$temp[0]'";
-          for (my $i=1;$i<$count;$i++){
-            $query.=" or itemtype='$temp[$i]'";
-          }
-          $query.=") ";
+          my @key=split(' ',$search->{'title'});
+          my $count=@key;
+           my $i=0;
+          $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
+            while ($i<$count){            
+             $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
+              $i++; 
+           }                       
+#          $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')"; 
+#            for ($i=1;$i<$count;$i++){
+#            $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";   
+#            }
+           $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";  
+            for ($i=1;$i<$count;$i++){                    
+               $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
+            }                                                             
+           $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";                         
+            for ($i=1;$i<$count;$i++){                    
+               $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";   
+            }                                                             
+           $query=$query."))"; 
+          #$query=$query. " and (title like '%$search->{'title'}%' 
+          #or seriestitle like '%$search->{'title'}%')";
         }
-        if ($search->{'dewey'} ne ''){
-             $query.=" and dewey='$search->{'dewey'}' ";
-         }           
-         if ($search->{'illustrator'} ne ''){
-           $query.=" and illus like '%".$search->{'illustrator'}."%' ";
-         }
-        
+                
         $query.=" group by biblio.biblionumber";
       } else {
           if ($search->{'title'} ne '') {
@@ -436,43 +378,28 @@ sub CatSearch  {
            my @key=split(' ',$search->{'title'});
            my $count=@key;
            my $i=1;
-            $query="select * from biblio,bibliosubtitle,biblioitems
+            $query="select * from biblio
+           left join bibliosubtitle on
+           biblio.biblionumber=bibliosubtitle.biblionumber
            where
-            (biblio.biblionumber=bibliosubtitle.biblionumber and
-            biblioitems.biblionumber=biblio.biblionumber) and
            (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
            while ($i<$count){
              $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
              $i++;
            }
-           $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')";
+           $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
            for ($i=1;$i<$count;$i++){
-             $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";
+             $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
            }
-           $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0] %' or seriestitle like '% $key[0]')";
+           $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
            for ($i=1;$i<$count;$i++){
-             $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i] %')";
+             $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
            }
-           $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0] %' or unititle like '% $key[0]')";
+           $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
            for ($i=1;$i<$count;$i++){
-             $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i] %')";
+             $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
            }
            $query=$query."))";
-           if ($search->{'class'} ne ''){
-             my @temp=split(/\|/,$search->{'class'});
-             my $count=@temp;
-             $query.= " and ( itemtype='$temp[0]'";
-             for (my $i=1;$i<$count;$i++){
-              $query.=" or itemtype='$temp[$i]'";
-             }
-             $query.=")";
-           }
-           if ($search->{'dewey'} ne ''){
-             $query.=" and dewey='$search->{'dewey'}' ";
-           }
-           if ($search->{'illustrator'} ne ''){
-             $query.=" and illus like '%".$search->{'illustrator'}."%' ";
-           }
           }
          } elsif ($search->{'class'} ne ''){
             $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
@@ -499,7 +426,7 @@ sub CatSearch  {
             where biblio.biblionumber=biblioitems.biblionumber
             and biblioitems.illus like '%".$search->{'illustrator'}."%'";
          }
-         }
+       }
           $query .=" group by biblio.biblionumber";     
       }
   } 
@@ -550,7 +477,9 @@ sub CatSearch  {
           $dewey=~s/\.*0*$//;
           ($dewey == 0) && ($dewey='');
           ($dewey) && ($dewey.=" $subclass");
-           $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
+          $data->{'dewey'}=$dewey;
+          $results[$i2]=$data;
+#           $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
            $i2++; 
           $sth->finish;
        }
@@ -569,30 +498,57 @@ if ($type ne 'precise' && $type ne 'subject'){
       $query=$query." order by subject";
   }
 }
+#print $query;
 my $sth=$dbh->prepare($query);
 $sth->execute;
 my $count=1;
 my $i=0;
 my $limit= $num+$offset;
 while (my $data=$sth->fetchrow_hashref){
-  my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}");
+  my $query="select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}";
+           if ($search->{'class'} ne ''){
+             my @temp=split(/\|/,$search->{'class'});
+             my $count=@temp;
+             $query.= " and ( itemtype='$temp[0]'";
+             for (my $i=1;$i<$count;$i++){
+              $query.=" or itemtype='$temp[$i]'";
+             }
+             $query.=")";
+           }
+           if ($search->{'dewey'} ne ''){
+             $query.=" and dewey='$search->{'dewey'}' ";
+           }
+           if ($search->{'illustrator'} ne ''){
+             $query.=" and illus like '%".$search->{'illustrator'}."%' ";
+           }
+#print $query;
+  my $sti=$dbh->prepare($query);
   $sti->execute;
-  my ($dewey, $subclass) = $sti->fetchrow;
+  my $dewey;
+  my $subclass;
+  my $true=0;
+  if (($dewey, $subclass) = $sti->fetchrow || $type eq 'subject'){
+    $true=1;
+  }
   $dewey=~s/\.*0*$//;
   ($dewey == 0) && ($dewey='');
   ($dewey) && ($dewey.=" $subclass");
+  $data->{'dewey'}=$dewey;
   $sti->finish;
+  if ($true == 1){
   if ($count > $offset && $count <= $limit){
-    if ($type ne 'subject' && $type ne 'precise'){
-       $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
-    } elsif ($search->{'isbn'} ne '' || $search->{'item'} ne ''){
-       $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
-    } else {  
-     $results[$i]="$data->{'author'}\t$data->{'subject'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
-    }
+#    if ($type ne 'subject' && $type ne 'precise'){
+#       $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
+#    } elsif ($search->{'isbn'} ne '' || $search->{'item'} ne ''){
+#       $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
+#    } else {  
+#     $results[$i]="$data->{'author'}\t$data->{'subject'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
+#    }
+    $results[$i]=$data;
     $i++;
   }
   $count++;
+  }
 }
 $sth->finish;
 #if ($type ne 'precise'){
@@ -610,9 +566,10 @@ sub updatesearchstats{
 sub subsearch {
   my ($env,$subject)=@_;
   my $dbh=C4Connect();
+  $subject=$dbh->quote($subject);
   my $query="Select * from biblio,bibliosubject where
   biblio.biblionumber=bibliosubject.biblionumber and
-  bibliosubject.subject='$subject' group by biblio.biblionumber
+  bibliosubject.subject=$subject group by biblio.biblionumber
   order by biblio.title";
   my $sth=$dbh->prepare($query);
   $sth->execute;
@@ -639,7 +596,8 @@ sub ItemInfo {
   items.holdingbranch ";
 #  print $type;
   if ($type ne 'intra'){
-    $query.=" and (items.itemlost<>1 or items.itemlost is NULL)
+    $query.=" and ((items.itemlost<>1 and items.itemlost <> 2)
+    or items.itemlost is NULL)
     and (wthdrawn <> 1 or wthdrawn is NULL)";
   }
   $query=$query."order by items.dateaccessioned desc";
@@ -659,14 +617,15 @@ sub ItemInfo {
       my @temp=split('-',$idata->{'date_due'});
       $datedue = "$temp[2]/$temp[1]/$temp[0]";
     }
-    if ($data->{'itemlost'} eq '1'){
+    if ($data->{'itemlost'} eq '1' || $data->{'itemlost'} eq '2'){
         $datedue='Itemlost';
     }
     if ($data->{'wthdrawn'} eq '1'){
       $datedue="Cancelled";
     }
     if ($datedue eq ''){
-       my ($rescount,$reserves)=FindReserves($biblionumber,'');   
+       my ($rescount,$reserves)=Findgroupreserve($data->{'biblioitemnumber'},$biblionumber);
+
        if ($rescount >0){                                
           $datedue='Request';
        }
@@ -683,7 +642,9 @@ sub ItemInfo {
     }
     $dewey=~ s/\.$//;
     $class = $class.$dewey;
-    $class = $class.$data->{'subclass'};
+    if ($dewey ne ''){
+      $class = $class.$data->{'subclass'};
+    }
  #   $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
     my @temp=split('-',$data->{'datelastseen'});
     my $date="$temp[2]/$temp[1]/$temp[0]";
@@ -691,7 +652,20 @@ sub ItemInfo {
 #    print "$results[$i] <br>";
     $i++;
   }
-  $sth->finish;
+ $sth->finish;
+  my $query2="Select * from aqorders where biblionumber=$biblionumber";
+  my $sth2=$dbh->prepare($query2);         
+  $sth2->execute;                                        
+  my $data;
+  my $ocount;
+  if ($data=$sth2->fetchrow_hashref){                   
+    $ocount=$data->{'quantity'} - $data->{'quantityreceived'};                                                  
+    if ($ocount > 0){
+      $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$ocount\tOn Order\t\t$data->{'itemnumber'}\t$data->{'itemtype'}\t\t$data->{'biblioitemnumber'}\t$data->{'volumeddesc'}";
+    }
+  } 
+  $sth2->finish;
+
   $dbh->disconnect;
   return(@results);
 }
@@ -751,24 +725,28 @@ sub itemdata {
 }
 
 sub bibdata {
-  my ($bibnum,$type)=@_;
-  my $dbh=C4Connect;
-  my $query="Select *,biblio.notes from biblio,biblioitems,bibliosubtitle where biblio.biblionumber=$bibnum
-  and biblioitems.biblionumber=$bibnum and 
-(bibliosubtitle.biblionumber=$bibnum)"; 
-#  print $query;
-  my $sth=$dbh->prepare($query);
+  my ($bibnum, $type)=@_;
+  my $dbh   = C4Connect;
+  my $query = "Select *, biblio.notes  
+from biblio, biblioitems 
+left join bibliosubtitle on
+biblio.biblionumber = bibliosubtitle.biblionumber
+where biblio.biblionumber = $bibnum
+and biblioitems.biblionumber = $bibnum";
+
+  my $sth   = $dbh->prepare($query);
   $sth->execute;
-  my $data=$sth->fetchrow_hashref;
+  my $data  = $sth->fetchrow_hashref;
   $sth->finish;
-  $query="Select * from bibliosubject where biblionumber='$bibnum'";
+
+  $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
   $sth=$dbh->prepare($query);
   $sth->execute;
-  while (my $dat=$sth->fetchrow_hashref){
-    $data->{'subject'}.=" | $dat->{'subject'}";
+  while (my $dat = $sth->fetchrow_hashref){
+    $data->{'subject'} .= " | $dat->{'subject'}";
 
   }
-  #print $query;
+
   $sth->finish;
   $dbh->disconnect;
   return($data);
@@ -777,7 +755,7 @@ sub bibdata {
 sub bibitemdata {
   my ($bibitem)=@_;
   my $dbh=C4Connect;
-  my $query="Select * from biblio,biblioitems,itemtypes where biblio.biblionumber=
+  my $query="Select *,biblioitems.notes as bnotes from biblio,biblioitems,itemtypes where biblio.biblionumber=
   biblioitems.biblionumber and biblioitemnumber=$bibitem and
   biblioitems.itemtype=itemtypes.itemtype";
 #  print $query;
@@ -843,7 +821,7 @@ sub subtitle {
 
 
 sub itemissues {
-  my ($bibitem,$biblio)=@_;
+  my ($bibitem, $biblio)=@_;
   my $dbh=C4Connect;
   my $query="Select * from items where 
   items.biblioitemnumber='$bibitem'";
@@ -980,30 +958,33 @@ by date_due";
   return($i,\@result);
 }
 
-sub allissues {
-  my ($bornum)=@_;
-  my $dbh=C4Connect;
-  my $query;
-  $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
-items.itemnumber=issues.itemnumber and
-items.biblionumber=biblio.biblionumber order
-by date_due";
-  #print $query;
-  my $sth=$dbh->prepare($query);
-    $sth->execute;
-  my @result;
-  my $i=0;
-  while (my $data=$sth->fetchrow_hashref){
-    $result[$i]=$data;;
-    $i++;
-  }
-  $sth->finish;
-  $dbh->disconnect;
-  return($i,\@result);
+sub allissues { 
+  my ($bornum,$order,$limit)=@_; 
+  my $dbh=C4Connect;   
+  my $query;     
+  $query="Select * from issues,biblio,items,biblioitems       
+  where borrowernumber='$bornum' and         
+  items.biblioitemnumber=biblioitems.biblioitemnumber and           
+  items.itemnumber=issues.itemnumber and             
+  items.biblionumber=biblio.biblionumber";               
+  $query.=" order by $order";                 
+  if ($limit !=0){                   
+    $query.=" limit $limit";                     
+  }                         
+  #print $query;                           
+  my $sth=$dbh->prepare($query);          
+  $sth->execute;
+  my @result;   
+  my $i=0;    
+  while (my $data=$sth->fetchrow_hashref){                                      
+    $result[$i]=$data;; 
+    $i++;     
+  }         
+  $sth->finish;           
+  $dbh->disconnect;             
+  return($i,\@result);               
 }
 
-
-
 sub borrdata2 {
   my ($env,$bornum)=@_;
   my $dbh=C4Connect;
@@ -1028,35 +1009,7 @@ sub borrdata2 {
 
 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
 }
-                 
-sub getacctlist {
-   my ($env,$params) = @_;
-   my $dbh=C4Connect;
-   my @acctlines;
-   my $numlines;
-   my $query = "Select borrowernumber, accountno, date, amount, description,
-      dispute, accounttype, amountoutstanding, barcode, title
-      from accountlines,items,biblio   
-      where borrowernumber = $params->{'borrowernumber'} ";
-   if ($params->{'acctno'} ne "") {
-      my $query = $query." and accountlines.accountno = $params->{'acctno'} ";
-      }
-   my $query = $query." and accountlines.itemnumber = items.itemnumber
-      and items.biblionumber = biblio.biblionumber
-      and accountlines.amountoutstanding<>0 order by date";
-   my $sth=$dbh->prepare($query);
-#   print $query;
-   $sth->execute;
-   my $total=0;
-   while (my $data=$sth->fetchrow_hashref){
-      $acctlines[$numlines] = $data;
-      $numlines++;
-      $total = $total+ $data->{'amountoutstanding'};
-   }
-   return ($numlines,\@acctlines,$total);
-   $sth->finish;
-   $dbh->disconnect;
-}
+       
 
 sub getboracctrecord {
    my ($env,$params) = @_;
@@ -1094,7 +1047,7 @@ sub itemcount {
   my $query="Select * from items where     
   biblionumber=$bibnum ";
   if ($type ne 'intra'){
-    $query.=" and (itemlost <>1 or itemlost is NULL) and
+    $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
     (wthdrawn <> 1 or wthdrawn is NULL)";      
   }
   my $sth=$dbh->prepare($query);         
@@ -1113,8 +1066,10 @@ sub itemcount {
     $count++;                     
     my $query2="select * from issues,items where issues.itemnumber=                          
     '$data->{'itemnumber'}' and returndate is NULL
-    and items.itemnumber=issues.itemnumber and (items.itemlost <>1 or
-    items.itemlost is NULL)"; 
+    and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
+    items.itemlost <> 2) or items.itemlost is NULL) 
+    and (wthdrawn <> 1 or wthdrawn is NULL)"; 
+    
     my $sth2=$dbh->prepare($query2);     
     $sth2->execute;         
     if (my $data2=$sth2->fetchrow_hashref){         
@@ -1132,6 +1087,9 @@ sub itemcount {
       if ($data->{'itemlost'} eq '1'){
         $lostcount++;
       }
+      if ($data->{'itemlost'} eq '2'){
+        $lostcount++;
+      }
       if ($data->{'holdingbranch'} eq 'FM'){
         $mending++;
       }
@@ -1188,10 +1146,14 @@ sub bibitems {
 }
 
 sub barcodes{
+  #called from request.pl
   my ($biblioitemnumber)=@_;
   my $dbh=C4Connect;
   my $query="Select barcode from items where
-   biblioitemnumber='$biblioitemnumber'";
+   biblioitemnumber='$biblioitemnumber'
+   and ((itemlost <> 1 and itemlost <> 2) or itemlost is NULL) and
+   (wthdrawn <> 1 or wthdrawn is NULL)";
+#   print $query;
   my $sth=$dbh->prepare($query);
   $sth->execute;
   my @barcodes;
@@ -1205,10 +1167,57 @@ sub barcodes{
   return(@barcodes);
   
 }
+
+
+sub getwebsites {
+    my ($biblionumber) = @_;
+    my $dbh   = C4Connect;
+    my $query = "Select * from websites where biblionumber = $biblionumber";
+    my $sth   = $dbh->prepare($query);
+    my $count = 0;
+    my @results;
+
+    $sth->execute;
+    while (my $data = $sth->fetchrow_hashref) {
+        $results[$count] = $data;
+       $count++;
+    } # while
+
+    $sth->finish;
+    $dbh->disconnect;
+    return($count, @results);
+} # sub getwebsites
+
+
 END { }       # module clean-up code here (global destructor)
 
+=head1 NAME
+
+C4::Search - Module that provides Catalog searching for Koha
+
+=head1 SYNOPSIS
+
+  use C4::Search;
+  my ($count,@results)=KeywordSearch($env,$type,$search,$num,$offset);
+  my ($count,@results)=CatSearch($env,$type,$search,$num,$offset);
+
+=head1 DESCRIPTION
+
+This module provides the searching facilities for the Catalog.
+Here I should go through and document each function thats exported and what it does. But I havent yet.
+
+=head2 EXPORT
+
+KeywordSearch
+CatSearch
+ItemInfo
 
+=head1 AUTHOR
 
+Koha Developement team <info@koha.org>
 
+=head1 SEE ALSO
 
+L<perl>.
 
+=cut