Bug 5984 : Allowing the librarians to hide items in the Opac using a system preference
authorChris Cormack <chrisc@catalyst.net.nz>
Tue, 29 Mar 2011 02:46:29 +0000 (15:46 +1300)
committerChris Cormack <chrisc@catalyst.net.nz>
Sun, 10 Apr 2011 07:32:06 +0000 (19:32 +1200)
Squashed commit of the following:

commit 72b578928b287ba3b5fa8965e42ff1ccbdf9013a
Author: Matthias Meusburger <matthias.meusburger@biblibre.com>
Date:   Wed Oct 20 11:26:46 2010 +0200

    Bug 5984 : MT4587, Follow-up: Hiding rules are now in a syspref

commit d95329955f976900afdafa82fe0a1699e8e892dc
Author: Matthias Meusburger <matthias.meusburger@biblibre.com>
Date:   Tue Oct 19 15:19:16 2010 +0200

    Bug 5984 : MT4587, Follow-up: Adds yaml config file for custom opac items hiding

commit 6777c1a97700b77095e4e499ca73040bf94271e2
Author: Matthias Meusburger <matthias.meusburger@biblibre.com>
Date:   Mon Oct 18 11:14:03 2010 +0200

    Bug 5984 : MT4587 : Adds yaml config file for custom opac items hiding

Signed-off-by: Claire Hernandez <claire.hernandez@biblibre.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
C4/Items.pm
C4/Search.pm
docs/opac/OpacHiddenItems.txt [new file with mode: 0644]
installer/data/mysql/updatedatabase.pl
koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl
opac/opac-detail.pl

index 939e682..01509c2 100644 (file)
@@ -33,6 +33,7 @@ use C4::Branch;
 require C4::Reserves;
 use C4::Charset;
 use C4::Acquisition;
+use List::MoreUtils qw/any/;
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -69,6 +70,7 @@ BEGIN {
         get_itemnumbers_of
         GetItemnumberFromBarcode
         GetBarcodeFromItemnumber
+      GetHiddenItemnumbers
 
                DelItemCheck
                MoveItemFromBiblio 
@@ -1534,6 +1536,57 @@ sub GetBarcodeFromItemnumber {
     return ($result);
 }
 
+=head2 GetHiddenItemnumbers
+
+=over 4
+
+$result = GetHiddenItemnumbers(@items);
+
+=back
+
+=cut
+
+sub GetHiddenItemnumbers {
+    my (@items) = @_;
+    my @resultitems;
+
+    my $yaml = C4::Context->preference('OpacHiddenItems');
+    my $hidingrules;
+    eval {
+       $hidingrules = YAML::Load($yaml);
+    };
+    if ($@) {
+       warn "Unable to parse OpacHiddenItems syspref : $@";
+       return ();
+    } else {
+    my $dbh = C4::Context->dbh;
+
+       # 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;
+
+               # If the results matches the values in the yaml file
+               if (any { $result eq $_ } @{$hidingrules->{$field}}) {
+
+                   # 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;
+    }
+
+ }
+
 =head3 get_item_authorised_values
 
 find the types and values for all authorised values assigned to this item.
index a19a5b7..a8d6625 100644 (file)
@@ -30,6 +30,8 @@ use C4::XSLT;
 use C4::Branch;
 use C4::Reserves;    # CheckReserves
 use C4::Debug;
+use C4::Items;
+use YAML;
 use URI::Escape;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG);
@@ -1594,8 +1596,14 @@ sub searchResults {
                 $item->{$code} = $field->subfield( $subfieldstosearch{$code} );
             }
 
-                       my $hbranch     = C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 'homebranch'    : 'holdingbranch';
-                       my $otherbranch = C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 'holdingbranch' : 'homebranch';
+               # Hidden items
+               my @items = ($item);
+               my (@hiddenitems) = GetHiddenItemnumbers(@items);
+           $item->{'hideatopac'} = 1 if (@hiddenitems); 
+
+            my $hbranch     = C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 'homebranch'    : 'holdingbranch';
+            my $otherbranch = C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 'holdingbranch' : 'homebranch';
+
             # set item's branch name, use HomeOrHoldingBranch syspref first, fall back to the other one
             if ($item->{$hbranch}) {
                 $item->{'branchname'} = $branches{$item->{$hbranch}};
@@ -1668,6 +1676,7 @@ sub searchResults {
                     || $item->{itemlost}
                     || $item->{damaged}
                     || $item->{notforloan} > 0
+                    || $item->{hideatopac}
                    || $reservestatus eq 'Waiting'
                     || ($transfertwhen ne ''))
                 {
@@ -1679,11 +1688,11 @@ sub searchResults {
                     $item->{status} = $item->{wthdrawn} . "-" . $item->{itemlost} . "-" . $item->{damaged} . "-" . $item->{notforloan};
                     $other_count++;
 
-                                       my $key = $prefix . $item->{status};
-                                       foreach (qw(wthdrawn itemlost damaged branchname itemcallnumber)) {
-                       $other_items->{$key}->{$_} = $item->{$_};
-                                       }
-                    $other_items->{$key}->{intransit} = ($transfertwhen ne '') ? 1 : 0;
+                    my $key = $prefix . $item->{status};
+                    foreach (qw(wthdrawn itemlost damaged branchname itemcallnumber hideatopac)) {
+                        $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};
@@ -1695,7 +1704,7 @@ sub searchResults {
                     $can_place_holds = 1;
                     $available_count++;
                                        $available_items->{$prefix}->{count}++ if $item->{$hbranch};
-                                       foreach (qw(branchname itemcallnumber)) {
+                                       foreach (qw(branchname itemcallnumber hideatopac)) {
                        $available_items->{$prefix}->{$_} = $item->{$_};
                                        }
                                        $available_items->{$prefix}->{location} = $shelflocations->{ $item->{location} };
diff --git a/docs/opac/OpacHiddenItems.txt b/docs/opac/OpacHiddenItems.txt
new file mode 100644 (file)
index 0000000..625d465
--- /dev/null
@@ -0,0 +1,12 @@
+The OpacHiddenItems syspref allow the user to define custom rules
+for hiding specific items at opac.
+
+YAML syntax is used for defining such rules.
+
+Items can be hidden according to values from the items table:
+
+field: [value1, value2, ...]
+
+Example :
+wthdrawn: [0, 1]
+homebranch: [homebranch1, homebranch2]
index 7729a7e..bac3a64 100755 (executable)
@@ -4231,6 +4231,14 @@ $DBversion = '3.03.00.044';
 if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
     $dbh->do("ALTER TABLE `aqbasketgroups` ADD `freedeliveryplace` TEXT NULL AFTER `deliveryplace`;");
     print "Upgrade to $DBversion done (adding freedeliveryplace to basketgroups)\n";
+}
+
+$DBversion = "3.03.00.045";
+if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
+    $dbh->do("
+       INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('OpacHiddenItems','','This syspref allows to define custom rules for hiding specific items at opac. See docs/opac/OpacHiddenItems.txt for more informations.','','Textarea');
+       ");
+    print "Upgrade to $DBversion done (Adding OpacHiddenItems syspref)\n";
     SetVersion($DBversion);
 }
 
index 0904901..c8b76b1 100644 (file)
@@ -440,7 +440,7 @@ $(document).ready(function(){
                     <!-- TMPL_IF NAME="available_items_loop" -->
                     <span class="available"><strong>Copies available:</strong>
                     <!-- TMPL_LOOP NAME="available_items_loop" -->
-
+                    <!-- TMPL_UNLESS NAME="hideatopac" -->
                     <!-- TMPL_IF NAME="singleBranchMode" -->
                         <!-- TMPL_VAR NAME="location" -->
                     <!-- TMPL_ELSE -->
@@ -452,6 +452,7 @@ $(document).ready(function(){
                         <!-- TMPL_IF NAME="itemcallnumber" -->[<a href="/cgi-bin/koha/opac-search.pl?q=callnum:<!-- TMPL_VAR NAME="itemcallnumber" ESCAPE="URL" -->"><!-- TMPL_VAR NAME="itemcallnumber" --></a>]<!-- /TMPL_IF -->
                     <!-- /TMPL_IF -->
                     (<!-- TMPL_VAR NAME="count" -->),
+                    <!-- /TMPL_UNLESS -->
                     <!-- /TMPL_LOOP -->
                     </span>
                     <!-- TMPL_ELSE -->
index f230834..3e4c76c 100755 (executable)
@@ -44,6 +44,7 @@ use C4::ShelfBrowser;
 use C4::Charset;
 use MARC::Record;
 use MARC::Field;
+use List::MoreUtils qw/any none/;
 
 BEGIN {
        if (C4::Context->preference('BakerTaylorEnabled')) {
@@ -86,14 +87,27 @@ $template->param('OPACShowCheckoutName' => C4::Context->preference("OPACShowChec
 # change back when ive fixed request.pl
 my @all_items = &GetItemsInfo( $biblionumber, 'opac' );
 my @items;
-@items = @all_items unless C4::Context->preference('hidelostitems');
 
-if (C4::Context->preference('hidelostitems')) {
-    # Hide host items
+# Getting items to be hidden
+my @hiddenitems = GetHiddenItemnumbers(@all_items);
+
+# Are there items to hide?
+my $hideitems = 1 if C4::Context->preference('hidelostitems') or scalar(@hiddenitems) > 0;
+
+# Hide items
+if ($hideitems) {
     for my $itm (@all_items) {
-        push @items, $itm unless $itm->{itemlost};
+       if  ( C4::Context->preference('hidelostitems') ) {
+           push @items, $itm unless $itm->{itemlost} or any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
+       } else {
+           push @items, $itm unless any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
     }
 }
+} else {
+    # Or not
+    @items = @all_items;
+}
+
 my $dat = &GetBiblioData($biblionumber);
 
 my $itemtypes = GetItemTypes();