Bug 30194: (follow-up) Remove invalid tests
[koha-ffzg.git] / C4 / Search.pm
index 5814c5f..85a02bf 100644 (file)
@@ -31,6 +31,7 @@ use Koha::ItemTypes;
 use Koha::Libraries;
 use Koha::Logger;
 use Koha::Patrons;
+use Koha::Recalls;
 use Koha::RecordProcessor;
 use URI::Escape;
 use Business::ISBN;
@@ -99,7 +100,7 @@ sub FindDuplicate {
 
         my $titleindex = 'ti,ext';
         my $authorindex = 'au,ext';
-        my $op = 'and';
+        my $op = 'AND';
 
         $result->{title} =~ s /\\//g;
         $result->{title} =~ s /\"//g;
@@ -307,7 +308,7 @@ sub getRecords {
     my $results_hashref = ();
 
     # TODO simplify this structure ( { branchcode => $branchname } is enought) and remove this parameter
-    $branches ||= { map { $_->branchcode => { branchname => $_->branchname } } Koha::Libraries->search };
+    $branches ||= { map { $_->branchcode => { branchname => $_->branchname } } Koha::Libraries->search->as_list };
 
     # Initialize variables for the faceted results objects
     my $facets_counter = {};
@@ -586,7 +587,10 @@ sub getRecords {
     # This sorts the facets into alphabetical order
     if (@facets_loop) {
         foreach my $f (@facets_loop) {
-            $f->{facets} = [ sort { uc($a->{facet_label_value}) cmp uc($b->{facet_label_value}) } @{ $f->{facets} } ];
+            if( C4::Context->preference('FacetOrder') eq 'Alphabetical' ){
+                $f->{facets} =
+                    [ sort { uc($a->{facet_label_value}) cmp uc($b->{facet_label_value}) } @{ $f->{facets} } ];
+            }
         }
     }
 
@@ -885,7 +889,7 @@ sub _build_weighted_query {
     my $fuzzy_enabled = C4::Context->preference("QueryFuzzy")        || 0;
     $operand =~ s/"/ /g;    # Bug 7518: searches with quotation marks don't work
 
-    my $weighted_query .= "(rk=(";    # Specifies that we're applying rank
+    my $weighted_query = "(rk=(";    # Specifies that we're applying rank
 
     # Keyword, or, no index specified
     if ( ( $index eq 'kw' ) || ( !$index ) ) {
@@ -1062,6 +1066,8 @@ sub getIndexes{
                     'mc-itemtype',
                     'mc-rtype',
                     'mus',
+                    'Multipart-resource-level',
+                    'mrl',
                     'name',
                     'Music-number',
                     'Name-geographic',
@@ -1472,23 +1478,43 @@ sub buildQuery {
             $limit_cgi       .= "&limit=" . uri_escape_utf8($this_limit);
             $limit_desc      .= " $this_limit";
         }
+        elsif ( $this_limit =~ '^multibranchlimit:|^branch:' ) {
+            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
+            $limit .= " and " if $limit || $query;
+            my $branchfield  = C4::Context->preference('SearchLimitLibrary');
+            my @branchcodes;
+            if(  $this_limit =~ '^multibranchlimit:' ){
+                my ($group_id) = ( $this_limit =~ /^multibranchlimit:(.*)$/ );
+                my $search_group = Koha::Library::Groups->find( $group_id );
+                @branchcodes  = map { $_->branchcode } $search_group->all_libraries;
+                @branchcodes = sort { $a cmp $b } @branchcodes;
+            } else {
+                @branchcodes = ( $this_limit =~ /^branch:(.*)$/ );
+            }
+
+            if (@branchcodes) {
+                if ( $branchfield eq "homebranch" ) {
+                    $this_limit = sprintf "(%s)", join " or ", map { 'homebranch: ' . $_ } @branchcodes;
+                }
+                elsif ( $branchfield eq "holdingbranch" ) {
+                    $this_limit = sprintf "(%s)", join " or ", map { 'holdingbranch: ' . $_ } @branchcodes;
+                }
+                else {
+                    $this_limit =  sprintf "(%s or %s)",
+                      join( " or ", map { 'homebranch: ' . $_ } @branchcodes ),
+                      join( " or ", map { 'holdingbranch: ' . $_ } @branchcodes );
+                }
+            }
+            $limit .= "$this_limit";
+            $limit_desc .= " $this_limit";
+        }
 
         # Regular old limits
         else {
             $limit .= " and " if $limit || $query;
             $limit      .= "$this_limit";
             $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
-            if ($this_limit =~ /^branch:(.+)/) {
-                my $branchcode = $1;
-                my $library = Koha::Libraries->find( $branchcode );
-                if (defined $library) {
-                    $limit_desc .= " branch:" . $library->branchname;
-                } else {
-                    $limit_desc .= " $this_limit";
-                }
-            } else {
-                $limit_desc .= " $this_limit";
-            }
+            $limit_desc .= " $this_limit";
         }
     }
     foreach my $k (keys (%group_OR_limits)) {
@@ -1560,7 +1586,7 @@ sub _build_initial_query {
     my $operator = "";
     if ($params->{previous_operand}){
         #If there is a previous operand, add a supplied operator or the default 'and'
-        $operator = ($params->{operator}) ? " ".($params->{operator})." " : ' and ';
+        $operator = ($params->{operator}) ? ($params->{operator}) : 'AND';
     }
 
     #NOTE: indexes_set is typically set when doing truncation or field weighting
@@ -1568,14 +1594,14 @@ sub _build_initial_query {
 
     #e.g. "kw,wrdl:test"
     #e.g. " and kw,wrdl:test"
-    $params->{query} .= $operator . $operand;
+    $params->{query} .= " " . $operator . " " . $operand;
 
     $params->{query_cgi} .= "&op=".uri_escape_utf8($operator) if $operator;
     $params->{query_cgi} .= "&idx=".uri_escape_utf8($params->{index}) if $params->{index};
-    $params->{query_cgi} .= "&q=".uri_escape_utf8($params->{original_operand}) if $params->{original_operand};
+    $params->{query_cgi} .= "&q=".uri_escape_utf8($params->{original_operand}) if ( $params->{original_operand} ne '' );
 
     #e.g. " and kw,wrdl: test"
-    $params->{query_desc} .= $operator . ( $params->{index_plus} // q{} ) . " " . ( $params->{original_operand} // q{} );
+    $params->{query_desc} .= " " . $operator . " " . ( $params->{index_plus} // q{} ) . " " . ( $params->{original_operand} // q{} );
 
     $params->{previous_operand} = 1 unless $params->{previous_operand}; #If there is no previous operand, mark this as one
 
@@ -1613,7 +1639,7 @@ sub searchResults {
     });
 
     #Build branchnames hash
-    my %branches = map { $_->branchcode => $_->branchname } Koha::Libraries->search({}, { order_by => 'branchname' });
+    my %branches = map { $_->branchcode => $_->branchname } Koha::Libraries->search({}, { order_by => 'branchname' })->as_list;
 
 # FIXME - We build an authorised values hash here, using the default framework
 # though it is possible to have different authvals for different fws.
@@ -1718,7 +1744,7 @@ sub searchResults {
         # adding linked items that belong to host records
         if ( C4::Context->preference('EasyAnalyticalRecords') ) {
             my $analyticsfield = '773';
-            if ($marcflavor eq 'MARC21' || $marcflavor eq 'NORMARC') {
+            if ($marcflavor eq 'MARC21') {
                 $analyticsfield = '773';
             } elsif ($marcflavor eq 'UNIMARC') {
                 $analyticsfield = '461';
@@ -1758,9 +1784,9 @@ sub searchResults {
         my $itembinding_count     = 0;
         my $itemdamaged_count     = 0;
         my $item_in_transit_count = 0;
-        my $can_place_holds       = 0;
         my $item_onhold_count     = 0;
         my $notforloan_count      = 0;
+        my $item_recalled_count   = 0;
         my $items_count           = scalar(@fields);
         my $maxitems_pref = C4::Context->preference('maxItemsinSearchResults');
         my $maxitems = $maxitems_pref ? $maxitems_pref - 1 : 1;
@@ -1780,6 +1806,7 @@ sub searchResults {
             if ($is_opac) {
                 # hidden because lost
                 if ($hidelostitems && $item->{itemlost}) {
+                    push @hiddenitems, $item->{itemnumber};
                     $hideatopac_count++;
                     next;
                 }
@@ -1829,9 +1856,6 @@ sub searchResults {
                     $onloan_items->{$key}->{longoverdue}++;
                     $longoverdue_count++;
                 }
-                else {    # can place holds as long as item isn't lost
-                    $can_place_holds = 1;
-                }
             }
 
          # items not on loan, but still unavailable ( lost, withdrawn, damaged )
@@ -1855,6 +1879,9 @@ sub searchResults {
                 # is item on the reserve shelf?
                 my $reservestatus = '';
 
+                # is item a waiting recall?
+                my $recallstatus = '';
+
                 unless ($item->{withdrawn}
                         || $item->{itemlost}
                         || $item->{damaged}
@@ -1876,6 +1903,11 @@ sub searchResults {
                     #
                     ($transfertwhen, $transfertfrom, $transfertto) = C4::Circulation::GetTransfers($item->{itemnumber});
                     $reservestatus = C4::Reserves::GetReserveStatus( $item->{itemnumber} );
+                    if ( C4::Context->preference('UseRecalls') ) {
+                        if ( Koha::Recalls->search({ itemnumber => $item->{itemnumber}, status => 'waiting' })->count ) {
+                            $recallstatus = 'Waiting';
+                        }
+                    }
                 }
 
                 # item is withdrawn, lost, damaged, not for loan, reserved or in transit
@@ -1884,6 +1916,7 @@ sub searchResults {
                     || $item->{damaged}
                     || $item->{notforloan}
                     || $reservestatus eq 'Waiting'
+                    || $recallstatus eq 'Waiting'
                     || ($transfertwhen && $transfertwhen ne ''))
                 {
                     $withdrawn_count++        if $item->{withdrawn};
@@ -1891,20 +1924,9 @@ sub searchResults {
                     $itemdamaged_count++     if $item->{damaged};
                     $item_in_transit_count++ if $transfertwhen && $transfertwhen ne '';
                     $item_onhold_count++     if $reservestatus eq 'Waiting';
+                    $item_recalled_count++   if $recallstatus eq 'Waiting';
                     $item->{status} = ($item->{withdrawn}//q{}) . "-" . ($item->{itemlost}//q{}) . "-" . ($item->{damaged}//q{}) . "-" . ($item->{notforloan}//q{});
 
-                    # can place a hold on a item if
-                    # not lost nor withdrawn
-                    # not damaged unless AllowHoldsOnDamagedItems is true
-                    # item is either for loan or on order (notforloan < 0)
-                    $can_place_holds = 1
-                      if (
-                           !$item->{itemlost}
-                        && !$item->{withdrawn}
-                        && ( !$item->{damaged} || C4::Context->preference('AllowHoldsOnDamagedItems') )
-                        && ( !$item->{notforloan} || $item->{notforloan} < 0 )
-                      );
-
                     $other_count++;
 
                     my $key = $prefix . $item->{status};
@@ -1912,6 +1934,7 @@ sub searchResults {
                         $other_items->{$key}->{$_} = $item->{$_};
                     }
                     $other_items->{$key}->{intransit} = ( $transfertwhen ne '' ) ? 1 : 0;
+                    $other_items->{$key}->{recalled} = ($recallstatus) ? 1 : 0;
                     $other_items->{$key}->{onhold} = ($reservestatus) ? 1 : 0;
                     $other_items->{$key}->{notforloan} = GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value) if $notforloan_authorised_value and $item->{notforloan};
                     $other_items->{$key}->{count}++ if $item->{$hbranch};
@@ -1921,7 +1944,6 @@ sub searchResults {
                 }
                 # item is available
                 else {
-                    $can_place_holds = 1;
                     $available_count++;
                     $available_items->{$prefix}->{count}++ if $item->{$hbranch};
                     foreach (qw(branchname itemcallnumber description)) {
@@ -1978,11 +2000,17 @@ sub searchResults {
             );
         }
 
+        my $biblio_object = Koha::Biblios->find( $oldbiblio->{biblionumber} );
+        $oldbiblio->{biblio_object} = $biblio_object;
+
+        my $can_place_holds = 1;
         # if biblio level itypes are used and itemtype is notforloan, it can't be reserved either
         if (!C4::Context->preference("item-level_itypes")) {
             if ($itemtype && $itemtype->{notforloan}) {
                 $can_place_holds = 0;
             }
+        } else {
+            $can_place_holds = $biblio_object->items->filter_by_for_hold()->count if $biblio_object;
         }
         $oldbiblio->{norequests} = 1 unless $can_place_holds;
         $oldbiblio->{items_count}          = $items_count;
@@ -2000,6 +2028,7 @@ sub searchResults {
         $oldbiblio->{damagedcount}         = $itemdamaged_count;
         $oldbiblio->{intransitcount}       = $item_in_transit_count;
         $oldbiblio->{onholdcount}          = $item_onhold_count;
+        $oldbiblio->{recalledcount}        = $item_recalled_count;
         $oldbiblio->{orderedcount}         = $ordered_count;
         $oldbiblio->{notforloancount}      = $notforloan_count;
 
@@ -2031,8 +2060,6 @@ sub searchResults {
             $oldbiblio->{'alternateholdings_count'} = $alternateholdingscount;
         }
 
-        $oldbiblio->{biblio_object} = Koha::Biblios->find( $oldbiblio->{biblionumber} );
-
         push( @newresults, $oldbiblio );
     }