From 9abd5975233774cc69dd92790c818f8e45fedd6c Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Thu, 18 Oct 2012 12:40:02 -0400
Subject: [PATCH] Bug 7740 - Highlight items in own library in search results/detail page in OPAC
Content-Type: text/plain; charset="utf-8"

This feature enables a particular library's items to be emphasized and moved
to the first position on the search results and details pages of the OPAC.

It is enabled by the sytem preference HighlightOwnItemsOnOPAC.

To choose which branches items are emphasized, use the system preference
HighlightOwnItemsOnOPACWhich. It has two modes.

If set to PatronBranch, the items emphasized will be those of the same
library as the patron's library. If no one is logged into the opac, no
items will be highlighted.

If set to OpacURLBranch, the library is chosen based on the Apache
environment variable BRANCHCODE.

For example, this could be added to the OPAC section of koha-httpd.conf:
SetEnv BRANCHCODE "CPL"

The point of this feature is to allow each library on a given Koha server
to have a specific subdomain for the opac where that library's items are
empasized. That was http://branch1.opac.mylibrary.org will emphasize the
items of branch1, while http://branch2.opac.mylibrary.org will emphasize
the items of branch2.
---
 installer/data/mysql/sysprefs.sql                  |    2 +
 installer/data/mysql/updatedatabase.pl             |    8 +++++
 .../prog/en/modules/admin/preferences/opac.pref    |   11 ++++++
 koha-tmpl/opac-tmpl/prog/en/css/opac.css           |    8 +++++
 koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tt |    3 +-
 .../opac-tmpl/prog/en/modules/opac-results.tt      |    2 +
 opac/opac-detail.pl                                |   32 ++++++++++++++++++
 opac/opac-search.pl                                |   34 ++++++++++++++++++++
 8 files changed, 99 insertions(+), 1 deletions(-)

diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql
index a4e4102..d52abb6 100644
--- a/installer/data/mysql/sysprefs.sql
+++ b/installer/data/mysql/sysprefs.sql
@@ -386,3 +386,5 @@ INSERT INTO systempreferences (variable,value,explanation,type) VALUES('OPACdidy
 INSERT INTO systempreferences (variable,value,explanation,type) VALUES('INTRAdidyoumean',NULL,'Did you mean? configuration for the Intranet. Do not change, as this is controlled by /cgi-bin/koha/admin/didyoumean.pl.','Free');
 INSERT INTO systempreferences (variable, value, options, explanation, type) VALUES ('BlockReturnOfWithdrawnItems', '1', '0', 'If enabled, items that are marked as withdrawn cannot be returned.', 'YesNo');
 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('HoldsToPullStartDate','2','Set the default start date for the Holds to pull list to this many days ago',NULL,'Integer');
+INSERT INTO systempreferences (variable,value,options,explanation,type) VALUES ('HighlightOwnItemsOnOPAC','0','','If on, and a patron is logged into the OPAC, items from his or her home library will be emphasized and shown first in search results and item details.','YesNo');
+INSERT INTO systempreferences (variable,value,options,explanation,type) VALUES ('HighlightOwnItemsOnOPACWhich','PatronBranch','PatronBranch|OpacURLBranch','Decides which branch''s items to emphasize. If PatronBranch, emphasize the logged in user''s library''s items. If OpacURLBranch, highlight the items of the Apache var BRANCHCODE defined in Koha''s Apache configuration file.','Choice')
\ No newline at end of file
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index 7ab48ee..96dfa94 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -6020,6 +6020,14 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
    SetVersion ($DBversion);
 }
 
+$DBversion = "3.09.00.XXX";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO systempreferences (variable,value,options,explanation,type) VALUES ('HighlightOwnItemsOnOPAC','0','','If on, and a patron is logged into the OPAC, items from his or her home library will be emphasized and shown first in search results and item details.','YesNo')");
+    $dbh->do("INSERT INTO systempreferences (variable,value,options,explanation,type) VALUES ('HighlightOwnItemsOnOPACWhich','PatronBranch','PatronBranch|OpacURLBranch','Decides which branch''s items to emphasize. If PatronBranch, emphasize the logged in user''s library''s items. If OpacURLBranch, highlight the items of the Apache var BRANCHCODE defined in Koha''s Apache configuration file.','Choice')");
+    print "Upgrade to $DBversion done (Add syspref HighlightOwnItemsOnOPAC)\n";
+    SetVersion ($DBversion);
+}
+
 =head1 FUNCTIONS
 
 =head2 TableExists($table)
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
index 1a9fa09..95d4ec7 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
@@ -22,6 +22,17 @@ OPAC:
                   no: Disable
             - "Koha OPAC as public. Private OPAC requires authentification before accessing the OPAC."
         -
+            - pref: HighlightOwnItemsOnOPAC
+              choices:
+                  yes: Emphasize
+                  no: "Don't emphasize"
+            - "results from the "
+            - pref: HighlightOwnItemsOnOPACWhich
+              choices:
+                  PatronBranch: "patron's home branch"
+                  OpacURLBranch: "OPAC's branch via the URL"
+            - " by moving the results to the front and increasing the size or highlighting the rows for those results."
+        -
             - "Show star-ratings on"
             - pref: OpacStarRatings
               choices:
diff --git a/koha-tmpl/opac-tmpl/prog/en/css/opac.css b/koha-tmpl/opac-tmpl/prog/en/css/opac.css
index 7ab95e0..947d421 100644
--- a/koha-tmpl/opac-tmpl/prog/en/css/opac.css
+++ b/koha-tmpl/opac-tmpl/prog/en/css/opac.css
@@ -2820,3 +2820,11 @@ body#opac-main #opacmainuserblockmobile {
 .tagweight9 {
     font-size: 30px;
 }
+
+.embiggen {
+    font-size: 120%;
+}
+
+.highlight-row {
+    font-weight: bold;
+}
diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tt
index 1d0523a..12f8f27 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tt
@@ -754,7 +754,8 @@ YAHOO.util.Event.onContentReady("furtherm", function () {
         [% END %]
         </tr></thead>
 	    <tbody>[% FOREACH ITEM_RESULT IN ITEM_RESULTS %]
-      <tr>[% IF ( item_level_itypes ) %]<td class="itype">[% UNLESS ( noItemTypeImages ) %][% IF ( ITEM_RESULT.imageurl ) %]<img src="[% ITEM_RESULT.imageurl %]" title="[% ITEM_RESULT.description %]" alt="[% ITEM_RESULT.description %]" />[% END %][% END %] [% ITEM_RESULT.description %]</td>[% END %]
+      [% IF ITEM_RESULT.this_branch %]<tr class="highlight-row">[% ELSE %]<tr>[% END %]
+      [% IF ( item_level_itypes ) %]<td class="itype">[% UNLESS ( noItemTypeImages ) %][% IF ( ITEM_RESULT.imageurl ) %]<img src="[% ITEM_RESULT.imageurl %]" title="[% ITEM_RESULT.description %]" alt="[% ITEM_RESULT.description %]" />[% END %][% END %] [% ITEM_RESULT.description %]</td>[% END %]
              <td class="location">
     [% UNLESS ( singleBranchMode ) %]
         <span class="[% ITEM_RESULT.branch_opac_info ? 'branch-info-tooltip-trigger' : '' %]">
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 53e6e4e..cb3e72e 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt
@@ -489,6 +489,7 @@ $(document).ready(function(){
                     [% IF ( SEARCH_RESULT.available_items_loop.size() ) %]
                     <span class="available"><strong>Copies available:</strong>
                     [% FOREACH available_items_loo IN SEARCH_RESULT.available_items_loop %]
+                    [% IF available_items_loo.this_branch %]<span class="embiggen">[% ELSE %]<span>[% END %]
                     [% IF ( singleBranchMode ) %]
                         [% available_items_loo.location %]
                     [% ELSE %]
@@ -500,6 +501,7 @@ $(document).ready(function(){
                         [% IF ( available_items_loo.itemcallnumber ) %][<a href="/cgi-bin/koha/opac-search.pl?q=callnum:[% available_items_loo.itemcallnumber |url %]">[% available_items_loo.itemcallnumber %]</a>][% END %]
                     [% END %]
                     ([% available_items_loo.count %]),
+                    </span>
                     [% END %]
                     </span>
                     [% ELSE %]
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index d9ecdfa..8d21ef3 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -440,6 +440,38 @@ if ($hideitems) {
     @items = @all_items;
 }
 
+my $branches = GetBranches();
+my $branch = C4::Context->userenv->{branch};
+if ( C4::Context->preference('HighlightOwnItemsOnOPAC') ) {
+    if (
+        ( ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'PatronBranch' ) && $branch )
+        ||
+        C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'OpacURLBranch'
+    ) {
+        my $branchname;
+        if ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'PatronBranch' ) {
+            $branchname = $branches->{$branch}->{'branchname'};
+        }
+        elsif (  C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'OpacURLBranch' ) {
+            $branchname = $branches->{ $ENV{'BRANCHCODE'} }->{'branchname'};
+        }
+
+        my @our_items;
+        my @other_items;
+
+        foreach my $item ( @items ) {
+           if ( $item->{'branchname'} eq $branchname ) {
+               $item->{'this_branch'} = 1;
+               push( @our_items, $item );
+           } else {
+               push( @other_items, $item );
+           }
+        }
+
+        @items = ( @our_items, @other_items );
+    }
+}
+
 my $dat = &GetBiblioData($biblionumber);
 
 my $itemtypes = GetItemTypes();
diff --git a/opac/opac-search.pl b/opac/opac-search.pl
index 7677d86..ef9b532 100755
--- a/opac/opac-search.pl
+++ b/opac/opac-search.pl
@@ -694,6 +694,40 @@ for (my $i=0;$i<@servers;$i++) {
             $template->param(results_per_page =>  $results_per_page);
             my $hide = C4::Context->preference('OpacHiddenItems');
             $hide = ($hide =~ m/\S/) if $hide; # Just in case it has some spaces/new lines
+
+            my $branch = C4::Context->userenv->{branch};
+            if ( C4::Context->preference('HighlightOwnItemsOnOPAC') ) {
+                if (
+                    ( ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'PatronBranch' ) && $branch )
+                    ||
+                    C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'OpacURLBranch'
+                ) {
+                    my $branchname;
+                    if ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'PatronBranch' ) {
+                        $branchname = $branches->{$branch}->{'branchname'};
+                    }
+                    elsif (  C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'OpacURLBranch' ) {
+                        $branchname = $branches->{ $ENV{'BRANCHCODE'} }->{'branchname'};
+                    }
+
+                    foreach my $res ( @newresults ) {
+                        my @new_loop;
+                        my @top_loop;
+                        my @old_loop = @{$res->{'available_items_loop'}};
+                        foreach my $item ( @old_loop ) {
+                            if ( $item->{'branchname'} eq $branchname ) {
+                                $item->{'this_branch'} = 1;
+                                push( @top_loop, $item );
+                            } else {
+                                push( @new_loop, $item );
+                            }
+                        }
+                        my @complete_loop = ( @top_loop, @new_loop );
+                        $res->{'available_items_loop'} = \@complete_loop;
+                    }
+                }
+            }
+
             $template->param(
                 SEARCH_RESULTS => \@newresults,
                 OPACItemsResultsDisplay => (C4::Context->preference("OPACItemsResultsDisplay")),
-- 
1.7.2.5