Enhancement Bug 4444: Centralize Code Handling Perl Dependencies
[koha.git] / C4 / Search.pm
index 14e85ce..229ee40 100644 (file)
@@ -16,7 +16,7 @@ package C4::Search;
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
-# use warnings; # FIXME
+#use warnings; FIXME - Bug 2505
 require Exporter;
 use C4::Context;
 use C4::Biblio;    # GetMarcFromKohaField, GetBiblioData
@@ -27,6 +27,7 @@ use XML::Simple;
 use C4::Dates qw(format_date);
 use C4::XSLT;
 use C4::Branch;
+use C4::Reserves;    # CheckReserves
 use C4::Debug;
 use YAML;
 use URI::Escape;
@@ -944,6 +945,7 @@ sub getIndexes{
                     'holdingbranch',
                     'homebranch',
                     'issues',
+                    'item',
                     'itemnumber',
                     'itype',
                     'Local-classification',
@@ -1077,6 +1079,7 @@ sub buildQuery {
 
             # COMBINE OPERANDS, INDEXES AND OPERATORS
             if ( $operands[$i] ) {
+               $operands[$i]=~s/^\s+//;
 
               # A flag to determine whether or not to add the index to the query
                 my $indexes_set;
@@ -1120,14 +1123,14 @@ sub buildQuery {
                 }
 
                 # Set default structure attribute (word list)
-                my $struct_attr;
+                my $struct_attr = q{};
                 unless ( $indexes_set || !$index || $index =~ /(st-|phr|ext|wrdl)/ ) {
                     $struct_attr = ",wrdl";
                 }
 
                 # Some helpful index variants
-                my $index_plus       = $index . $struct_attr . ":" if $index;
-                my $index_plus_comma = $index . $struct_attr . "," if $index;
+                my $index_plus       = $index . $struct_attr . ':';
+                my $index_plus_comma = $index . $struct_attr . ',';
 
                 # Remove Stopwords
                 if ($remove_stopwords) {
@@ -1515,6 +1518,7 @@ sub searchResults {
         my $itemdamaged_count     = 0;
         my $item_in_transit_count = 0;
         my $can_place_holds       = 0;
+       my $item_onhold_count     = 0;
         my $items_count           = scalar(@fields);
         my $maxitems =
           ( C4::Context->preference('maxItemsinSearchResults') )
@@ -1572,6 +1576,10 @@ sub searchResults {
                 my $transfertwhen = '';
                 my ($transfertfrom, $transfertto);
 
+                # is item on the reserve shelf?
+               my $reservestatus = 0;
+               my $reserveitem;
+
                 unless ($item->{wthdrawn}
                         || $item->{itemlost}
                         || $item->{damaged}
@@ -1591,6 +1599,7 @@ sub searchResults {
                     #        should map transit status to record indexed in Zebra.
                     #
                     ($transfertwhen, $transfertfrom, $transfertto) = C4::Circulation::GetTransfers($item->{itemnumber});
+                   ($reservestatus, $reserveitem) = C4::Reserves::CheckReserves($item->{itemnumber});
                 }
 
                 # item is withdrawn, lost or damaged
@@ -1598,12 +1607,14 @@ sub searchResults {
                     || $item->{itemlost}
                     || $item->{damaged}
                     || $item->{notforloan}
+                   || $reservestatus eq 'Waiting'
                     || ($transfertwhen ne ''))
                 {
                     $wthdrawn_count++        if $item->{wthdrawn};
                     $itemlost_count++        if $item->{itemlost};
                     $itemdamaged_count++     if $item->{damaged};
                     $item_in_transit_count++ if $transfertwhen ne '';
+                   $item_onhold_count++     if $reservestatus eq 'Waiting';
                     $item->{status} = $item->{wthdrawn} . "-" . $item->{itemlost} . "-" . $item->{damaged} . "-" . $item->{notforloan};
                     $other_count++;
 
@@ -1612,6 +1623,7 @@ sub searchResults {
                        $other_items->{$key}->{$_} = $item->{$_};
                                        }
                     $other_items->{$key}->{intransit} = ($transfertwhen ne '') ? 1 : 0;
+                    $other_items->{$key}->{onhold} = ($reservestatus) ? 1 : 0;
                                        $other_items->{$key}->{notforloan} = GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value) if $notforloan_authorised_value;
                                        $other_items->{$key}->{count}++ if $item->{$hbranch};
                                        $other_items->{$key}->{location} = $shelflocations->{ $item->{location} };
@@ -1676,6 +1688,7 @@ sub searchResults {
         $oldbiblio->{itemlostcount}        = $itemlost_count;
         $oldbiblio->{damagedcount}         = $itemdamaged_count;
         $oldbiblio->{intransitcount}       = $item_in_transit_count;
+        $oldbiblio->{onholdcount}          = $item_onhold_count;
         $oldbiblio->{orderedcount}         = $ordered_count;
         $oldbiblio->{isbn} =~
           s/-//g;    # deleting - in isbn to enable amazon content
@@ -2595,7 +2608,7 @@ sub GetDistinctValues {
     if ($fieldname=~/\./){
                        my ($table,$column)=split /\./, $fieldname;
                        my $dbh = C4::Context->dbh;
-                       warn "select DISTINCT($column) as value, count(*) as cnt from $table group by lib order by $column ";
+                       warn "select DISTINCT($column) as value, count(*) as cnt from $table group by lib order by $column " if $DEBUG;
                        my $sth = $dbh->prepare("select DISTINCT($column) as value, count(*) as cnt from $table ".($string?" where $column like \"$string%\"":"")."group by value order by $column ");
                        $sth->execute;
                        my $elements=$sth->fetchall_arrayref({});