[Koha-patches] [PATCH] bug_6488: Take in account opachiddenitems when searching in opac

Srdjan Jankovic srdjan at catalyst.net.nz
Fri Dec 9 05:13:12 CET 2011


Changed searchResults() interface
Added trailing \n when parsing OpacHiddenItems to make YAML happy
XSLTParse4Display() and buildKohaItemsNamespace() take hidden
items as input param
Removed numbering from the search results, looks wrong with
hidden items
---
 C4/Items.pm                                        |   52 +++++++------
 C4/Search.pm                                       |   83 +++++++++++---------
 C4/XSLT.pm                                         |   11 ++-
 catalogue/search.pl                                |    2 +-
 cataloguing/addbooks.pl                            |    2 +-
 .../opac-tmpl/prog/en/modules/opac-results.tt      |    1 -
 opac/opac-search.pl                                |    4 +-
 7 files changed, 86 insertions(+), 69 deletions(-)

diff --git a/C4/Items.pm b/C4/Items.pm
index 0756832..a8801ca 100644
--- a/C4/Items.pm
+++ b/C4/Items.pm
@@ -1630,41 +1630,45 @@ sub GetHiddenItemnumbers {
     my @resultitems;
 
     my $yaml = C4::Context->preference('OpacHiddenItems');
+    $yaml = "$yaml\n"; # YAML is anal on ending \n. Surplus does not hurt
     my $hidingrules;
     eval {
-    	$hidingrules = YAML::Load($yaml);
+        $hidingrules = YAML::Load($yaml);
     };
     if ($@) {
-    	warn "Unable to parse OpacHiddenItems syspref : $@";
-    	return ();
-    } else {
+        warn "Unable to parse OpacHiddenItems syspref : $@";
+        return ();
+    }
     my $dbh = C4::Context->dbh;
 
-	# For each item
-	foreach my $item (@items) {
+    # For each item
+    foreach my $item (@items) {
 
-	    # We check each rule
-	    foreach my $field (keys %$hidingrules) {
-		my $query = "SELECT $field from items where itemnumber = ?";
-		my $sth = $dbh->prepare($query);	
-		$sth->execute($item->{'itemnumber'});
-		my ($result) = $sth->fetchrow;
+        # We check each rule
+        foreach my $field (keys %$hidingrules) {
+            my $val;
+            if (exists $item->{$field}) {
+                $val = $item->{$field};
+            }
+            else {
+                my $query = "SELECT $field from items where itemnumber = ?";
+                $val = $dbh->selectrow_array($query, undef, $item->{'itemnumber'});
+            }
+            $val = '' unless defined $val;
 
-		# If the results matches the values in the yaml file
-		if (any { $result eq $_ } @{$hidingrules->{$field}}) {
+            # If the results matches the values in the yaml file
+            if (any { $val eq $_ } @{$hidingrules->{$field}}) {
 
-		    # We add the itemnumber to the list
-		    push @resultitems, $item->{'itemnumber'};	    
+                # We add the itemnumber to the list
+                push @resultitems, $item->{'itemnumber'};	    
 
-		    # If at least one rule matched for an item, no need to test the others
-		    last;
-		}
-	    }
-	}
-	return @resultitems;
+                # If at least one rule matched for an item, no need to test the others
+                last;
+            }
+        }
     }
-
- }
+    return @resultitems;
+}
 
 =head3 get_item_authorised_values
 
diff --git a/C4/Search.pm b/C4/Search.pm
index fdb3479..a9d47ac 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -31,6 +31,7 @@ use C4::Branch;
 use C4::Reserves;    # CheckReserves
 use C4::Debug;
 use C4::Items;
+use C4::Charset;
 use YAML;
 use URI::Escape;
 
@@ -1413,11 +1414,16 @@ Format results in a form suitable for passing to the template
 # IMO this subroutine is pretty messy still -- it's responsible for
 # building the HTML output for the template
 sub searchResults {
-    my ( $search_context, $searchdesc, $hits, $results_per_page, $offset, $scan, @marcresults, $hidelostitems ) = @_;
+    my ( $search_context, $searchdesc, $hits, $results_per_page, $offset, $scan, $marcresults ) = @_;
     my $dbh = C4::Context->dbh;
     my @newresults;
 
-    $search_context = 'opac' unless $search_context eq 'opac' or $search_context eq 'intranet';
+    $search_context = 'opac' if !$search_context || $search_context ne 'intranet';
+    my ($is_opac, $hidelostitems);
+    if ($search_context eq 'opac') {
+        $hidelostitems = C4::Context->preference('hidelostitems');
+        $is_opac       = 1;
+    }
 
     #Build branchnames hash
     #find branchname
@@ -1480,12 +1486,11 @@ sub searchResults {
 	my $marcflavour = C4::Context->preference("marcflavour");
     # We get the biblionumber position in MARC
     my ($bibliotag,$bibliosubf)=GetMarcFromKohaField('biblio.biblionumber','');
-    my $fw;
 
     # loop through all of the records we've retrieved
     for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) {
-        my $marcrecord = MARC::File::USMARC::decode( $marcresults[$i] );
-        $fw = $scan
+        my $marcrecord = MARC::File::USMARC::decode( $marcresults->[$i] );
+        my $fw = $scan
              ? undef
              : $bibliotag < 10
                ? GetFrameworkCode($marcrecord->field($bibliotag)->data)
@@ -1603,18 +1608,18 @@ sub searchResults {
         my $other_count           = 0;
         my $wthdrawn_count        = 0;
         my $itemlost_count        = 0;
+        my $hideatopac_count      = 0;
         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 $item_onhold_count     = 0;
         my $items_count           = scalar(@fields);
-        my $maxitems =
-          ( C4::Context->preference('maxItemsinSearchResults') )
-          ? C4::Context->preference('maxItemsinSearchResults') - 1
-          : 1;
+        my $maxitems_pref = C4::Context->preference('maxItemsinSearchResults');
+        my $maxitems = $maxitems_pref ? $maxitems_pref - 1 : 1;
 
         # loop through every item
+	      my @hiddenitems;
         foreach my $field (@fields) {
             my $item;
 
@@ -1624,9 +1629,11 @@ sub searchResults {
             }
 
 	        # Hidden items
-	        my @items = ($item);
-	        my (@hiddenitems) = GetHiddenItemnumbers(@items);
-    	    $item->{'hideatopac'} = 1 if (@hiddenitems); 
+            if ($is_opac) {
+	            my @hi = GetHiddenItemnumbers($item);
+    	        $item->{'hideatopac'} = @hi; 
+              push @hiddenitems, @hi;
+            }
 
             my $hbranch     = C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 'homebranch'    : 'holdingbranch';
             my $otherbranch = C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 'holdingbranch' : 'homebranch';
@@ -1710,6 +1717,7 @@ sub searchResults {
                     $wthdrawn_count++        if $item->{wthdrawn};
                     $itemlost_count++        if $item->{itemlost};
                     $itemdamaged_count++     if $item->{damaged};
+                    $hideatopac_count++      if $item->{hideatopac};
                     $item_in_transit_count++ if $transfertwhen ne '';
 		    $item_onhold_count++     if $reservestatus eq 'Waiting';
                     $item->{status} = $item->{wthdrawn} . "-" . $item->{itemlost} . "-" . $item->{damaged} . "-" . $item->{notforloan};
@@ -1748,11 +1756,11 @@ sub searchResults {
                 }
             }
         }    # notforloan, item level and biblioitem level
+
+        next if $is_opac       && $hideatopac_count >= $items_count;
+        next if $hidelostitems && $itemlost_count   >= $items_count;
+
         my ( $availableitemscount, $onloanitemscount, $otheritemscount );
-        $maxitems =
-          ( C4::Context->preference('maxItemsinSearchResults') )
-          ? C4::Context->preference('maxItemsinSearchResults') - 1
-          : 1;
         for my $key ( sort keys %$onloan_items ) {
             (++$onloanitemscount > $maxitems) and last;
             push @onloan_items_loop, $onloan_items->{$key};
@@ -1766,20 +1774,7 @@ sub searchResults {
             push @available_items_loop, $available_items->{$key}
         }
 
-        # XSLT processing of some stuff
-	use C4::Charset;
-	SetUTF8Flag($marcrecord);
-	$debug && warn $marcrecord->as_formatted;
-        if (!$scan && $search_context eq 'opac' && C4::Context->preference("OPACXSLTResultsDisplay")) {
-            # FIXME note that XSLTResultsDisplay (use of XSLT to format staff interface bib search results)
-            # is not implemented yet
-            $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display($oldbiblio->{biblionumber}, $marcrecord, 'Results', 
-                                                                $search_context, 1);
-                # the last parameter tells Koha to clean up the problematic ampersand entities that Zebra outputs
-
-        }
-
-        # if biblio level itypes are used and itemtype is notforloan, it can't be reserved either
+         # if biblio level itypes are used and itemtype is notforloan, it can't be reserved either
         if (!C4::Context->preference("item-level_itypes")) {
             if ($itemtypes{ $oldbiblio->{itemtype} }->{notforloan}) {
                 $can_place_holds = 0;
@@ -1803,8 +1798,8 @@ sub searchResults {
         $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
+        # deleting - in isbn to enable amazon content
+        $oldbiblio->{isbn} =~ s/-//g;
 
         if (C4::Context->preference("AlternateHoldingsField") && $items_count == 0) {
             my $fieldspec = C4::Context->preference("AlternateHoldingsField");
@@ -1834,10 +1829,24 @@ sub searchResults {
             $oldbiblio->{'alternateholdings_count'} = $alternateholdingscount;
         }
 
-        push( @newresults, $oldbiblio )
-            if(not $hidelostitems
-               or (($items_count > $itemlost_count )
-                    && $hidelostitems));
+       # XSLT processing of some stuff
+        if (!$scan && $search_context eq 'opac' && C4::Context->preference("OPACXSLTResultsDisplay")) {
+            SetUTF8Flag($marcrecord);
+            $debug && warn $marcrecord->as_formatted;
+            # FIXME note that XSLTResultsDisplay (use of XSLT to format staff interface bib search results)
+            # is not implemented yet
+            $oldbiblio->{XSLTResultsRecord}
+              = XSLTParse4Display($oldbiblio->{biblionumber},
+                                  $marcrecord,
+                                  'Results', 
+                                  $search_context,
+                                  1, # clean up the problematic ampersand entities that Zebra outputs
+                                  \@hiddenitems
+                                );
+
+        }
+
+        push( @newresults, $oldbiblio );
     }
 
     return @newresults;
diff --git a/C4/XSLT.pm b/C4/XSLT.pm
index 8bc4000..41fca01 100755
--- a/C4/XSLT.pm
+++ b/C4/XSLT.pm
@@ -121,12 +121,12 @@ sub getAuthorisedValues4MARCSubfields {
 my $stylesheet;
 
 sub XSLTParse4Display {
-    my ( $biblionumber, $orig_record, $xsl_suffix, $interface, $fixamps ) = @_;
+    my ( $biblionumber, $orig_record, $xsl_suffix, $interface, $fixamps, $hidden_items ) = @_;
     $interface = 'opac' unless $interface;
     # grab the XML, run it through our stylesheet, push it out to the browser
     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
     #return $record->as_formatted();
-    my $itemsxml  = buildKohaItemsNamespace($biblionumber);
+    my $itemsxml  = buildKohaItemsNamespace($biblionumber, $hidden_items);
     my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
     my $sysxml = "<sysprefs>\n";
     foreach my $syspref ( qw/ hidelostitems OPACURLOpenInNewWindow
@@ -180,8 +180,13 @@ sub XSLTParse4Display {
 }
 
 sub buildKohaItemsNamespace {
-    my ($biblionumber) = @_;
+    my ($biblionumber, $hidden_items) = @_;
+
     my @items = C4::Items::GetItemsInfo($biblionumber);
+    if ($hidden_items && @$hidden_items) {
+        my %hi = map {$_ => 1} @$hidden_items;
+        @items = grep { !$hi{$_->{itemnumber}} } @items;
+    }
     my $branches = GetBranches();
     my $itemtypes = GetItemTypes();
     my $xml = '';
diff --git a/catalogue/search.pl b/catalogue/search.pl
index 907efdb..24f2978 100755
--- a/catalogue/search.pl
+++ b/catalogue/search.pl
@@ -547,7 +547,7 @@ for (my $i=0;$i<@servers;$i++) {
         $hits = $results_hashref->{$server}->{"hits"};
         my $page = $cgi->param('page') || 0;
         my @newresults = searchResults('intranet', $query_desc, $hits, $results_per_page, $offset, $scan,
-                                       @{$results_hashref->{$server}->{"RECORDS"}});
+                                       $results_hashref->{$server}->{"RECORDS"});
         $total = $total + $results_hashref->{$server}->{"hits"};
         ## If there's just one result, redirect to the detail page
         if ($total == 1) {         
diff --git a/cataloguing/addbooks.pl b/cataloguing/addbooks.pl
index 5e89ef8..38e8024 100755
--- a/cataloguing/addbooks.pl
+++ b/cataloguing/addbooks.pl
@@ -86,7 +86,7 @@ if ($query) {
     # format output
     # SimpleSearch() give the results per page we want, so 0 offet here
     my $total = @{$marcresults};
-    my @newresults = searchResults( 'intranet', $query, $total, $results_per_page, 0, 0, @{$marcresults} );
+    my @newresults = searchResults( 'intranet', $query, $total, $results_per_page, 0, 0, $marcresults );
     $template->param(
         total          => $total_hits,
         query          => $query,
diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt
index 7201a82..deda677 100755
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt
@@ -380,7 +380,6 @@ $(document).ready(function(){
                 <td class="select selectcol">[% IF ( opacbookbag ) %]<input type="checkbox" id="bib[% SEARCH_RESULT.biblionumber %]" name="biblionumber" value="[% SEARCH_RESULT.biblionumber %]" /> <label for="bib[% SEARCH_RESULT.biblionumber %]"></label>[% ELSE %]
 [% IF ( virtualshelves ) %]<input type="checkbox" id="bib[% SEARCH_RESULT.biblionumber %]" name="biblionumber" value="[% SEARCH_RESULT.biblionumber %]" /> <label for="bib[% SEARCH_RESULT.biblionumber %]"></label>[% ELSE %]
 [% IF ( RequestOnOpac ) %][% UNLESS ( SEARCH_RESULT.norequests ) %][% IF ( opacuserlogin ) %]<input type="checkbox" id="bib[% SEARCH_RESULT.biblionumber %]" name="biblionumber" value="[% SEARCH_RESULT.biblionumber %]" /> <label for="bib[% SEARCH_RESULT.biblionumber %]"></label>[% END %][% END %][% END %][% END %][% END %]</td>
-                <td class="select selectcol">[% SEARCH_RESULT.result_number %].</td>
 
 				[% UNLESS ( item_level_itypes ) %]
                 [% UNLESS ( noItemTypeImages ) %]
diff --git a/opac/opac-search.pl b/opac/opac-search.pl
index 42eb0ec..13d9f0d 100755
--- a/opac/opac-search.pl
+++ b/opac/opac-search.pl
@@ -487,12 +487,12 @@ for (my $i=0;$i<@servers;$i++) {
                 # we want as specified by $offset and $results_per_page,
                 # we need to set the offset parameter of searchResults to 0
                 my @group_results = searchResults( 'opac', $query_desc, $group->{'group_count'},$results_per_page, 0, $scan,
-                                                   @{ $group->{"RECORDS"} }, C4::Context->preference('hidelostitems'));
+                                                   $group->{"RECORDS"});
                 push @newresults, { group_label => $group->{'group_label'}, GROUP_RESULTS => \@group_results };
             }
         } else {
             @newresults = searchResults('opac', $query_desc, $hits, $results_per_page, $offset, $scan,
-                                        @{$results_hashref->{$server}->{"RECORDS"}},, C4::Context->preference('hidelostitems'));
+                                        $results_hashref->{$server}->{"RECORDS"});
         }
 		my $tag_quantity;
 		if (C4::Context->preference('TagsEnabled') and
-- 
1.6.5



More information about the Koha-patches mailing list