Bug 29844: Fix ->search occurrences
[srvgit] / C4 / Search.pm
index 529f834..18a2977 100644 (file)
@@ -307,7 +307,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 +586,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} } ];
+            }
         }
     }
 
@@ -996,6 +999,8 @@ sub getIndexes{
                     'Conference-name-seealso',
                     'Content-type',
                     'Control-number',
+                    'Control-number-identifier',
+                    'cni',
                     'copydate',
                     'Corporate-name',
                     'Corporate-name-heading',
@@ -1060,6 +1065,8 @@ sub getIndexes{
                     'mc-itemtype',
                     'mc-rtype',
                     'mus',
+                    'Multipart-resource-level',
+                    'mrl',
                     'name',
                     'Music-number',
                     'Name-geographic',
@@ -1470,23 +1477,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)) {
@@ -1570,7 +1597,7 @@ sub _build_initial_query {
 
     $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{} );
@@ -1611,7 +1638,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.
@@ -1655,10 +1682,6 @@ sub searchResults {
     my ($bibliotag,$bibliosubf)=GetMarcFromKohaField( 'biblio.biblionumber' );
 
     # set stuff for XSLT processing here once, not later again for every record we retrieved
-    my $xslsyspref = $is_opac ? 'OPACXSLTResultsDisplay' : 'XSLTResultsDisplay';
-    my $xslfile = C4::Context->preference( $xslsyspref ) || "default";
-    my $lang   = C4::Languages::getlanguage();
-    my $sysxml = C4::XSLT::get_xslt_sysprefs();
 
     my $userenv = C4::Context->userenv;
     my $logged_in_user
@@ -1714,12 +1737,13 @@ sub searchResults {
 
         # Pull out the items fields
         my @fields = $marcrecord->field($itemtag);
+        $marcrecord->delete_fields( @fields ) unless C4::Context->preference('PassItemMarcToXSLT');
         my $marcflavor = C4::Context->preference("marcflavour");
 
         # 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';
@@ -1759,7 +1783,6 @@ 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 $items_count           = scalar(@fields);
@@ -1830,9 +1853,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 )
@@ -1894,18 +1914,6 @@ sub searchResults {
                     $item_onhold_count++     if $reservestatus 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};
@@ -1922,7 +1930,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)) {
@@ -1955,21 +1962,41 @@ sub searchResults {
 
         # XSLT processing of some stuff
         # we fetched the sysprefs already before the loop through all retrieved record!
-        if (!$scan && $xslfile) {
+        if (!$scan) {
             $record_processor->options({
                 frameworkcode => $fw,
                 interface     => $search_context->{'interface'}
             });
 
             $record_processor->process($marcrecord);
-            $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display($oldbiblio->{biblionumber}, $marcrecord, $xslsyspref, 1, \@hiddenitems, $sysxml, $xslfile, $lang, $xslt_variables);
+
+            $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display(
+                {
+                    biblionumber => $oldbiblio->{biblionumber},
+                    record       => $marcrecord,
+                    xsl_syspref  => (
+                        $is_opac
+                        ? 'OPACXSLTResultsDisplay'
+                        : 'XSLTResultsDisplay'
+                    ),
+                    fix_amps       => 1,
+                    hidden_items   => \@hiddenitems,
+                    xslt_variables => $xslt_variables
+                }
+            );
         }
 
+        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;
@@ -2018,8 +2045,6 @@ sub searchResults {
             $oldbiblio->{'alternateholdings_count'} = $alternateholdingscount;
         }
 
-        $oldbiblio->{biblio_object} = Koha::Biblios->find( $oldbiblio->{biblionumber} );
-
         push( @newresults, $oldbiblio );
     }