From d98177fe278de7bcf2547a8100eb3ac0884c756a Mon Sep 17 00:00:00 2001
From: Jared Camins-Esakov <jcamins@cpbibliography.com>
Date: Mon, 30 Jul 2012 17:16:07 -0400
Subject: [PATCH] Bug 8525: highlight matches on OPAC detail page

After doing a search and going to the details page, it can sometimes
be difficult to see exactly *why* a record was returned by a search.
By highlighting matches on the detail page as well as the results
page, we make it much easier to figure that out.

This patch uses a query_desc CGI parameter which is inserted into links
from the results page with javascript. This serves to avoid the
potential privacy implications of a cookie, and ensures that users
without javascript enabled see no change whatsoever.

To test:
1) Do a search (or two) in the OPAC with OpacHighlightedWords on.
2) View a record or two of the results, and ensure that the correct
   words are highlighted.
3) Disable OpacHighlightedWords and do another search (or two),
   this time ensuring that no words are highlighted.

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
---
 koha-tmpl/opac-tmpl/prog/en/css/opac.css           |    7 ++++++
 .../prog/en/includes/opac-detail-sidebar.inc       |    6 +++++
 koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tt |   23 ++++++++++++++++++++
 .../prog/en/modules/opac-results-grouped.tt        |    6 +++++
 .../opac-tmpl/prog/en/modules/opac-results.tt      |    6 +++++
 opac/opac-detail.pl                                |    4 +++
 6 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/koha-tmpl/opac-tmpl/prog/en/css/opac.css b/koha-tmpl/opac-tmpl/prog/en/css/opac.css
index 1e259fb..821e30e 100644
--- a/koha-tmpl/opac-tmpl/prog/en/css/opac.css
+++ b/koha-tmpl/opac-tmpl/prog/en/css/opac.css
@@ -434,6 +434,13 @@ a .term {
   background-repeat: no-repeat;
 }
 
+#action a.highlight_toggle {
+    background-image: url("../../images/sprite.png"); /* place hold icon */
+    background-position: -5px -841px;
+    background-repeat: no-repeat;
+    display: none;
+}
+
 #action a#furthersearches {
     background-image: url("../../images/sprite.png");
     background-position: 106px -295px;
diff --git a/koha-tmpl/opac-tmpl/prog/en/includes/opac-detail-sidebar.inc b/koha-tmpl/opac-tmpl/prog/en/includes/opac-detail-sidebar.inc
index 85d44d7..0f16f3a 100644
--- a/koha-tmpl/opac-tmpl/prog/en/includes/opac-detail-sidebar.inc
+++ b/koha-tmpl/opac-tmpl/prog/en/includes/opac-detail-sidebar.inc
@@ -27,6 +27,12 @@
             <li><a class="addtocart cart[% biblionumber %]" href="#" onclick="addRecord('[% biblionumber %]'); return false;">Add to your cart</a>  <a style="display:none;" class="cartRemove cartR[% biblionumber %]" href="#" onclick="delSingleRecord('[% biblionumber %]'); return false;">(remove)</a></li>
         [% END %]
     [% END %]
+    [% IF ( OpacHighlightedWords && query_desc ) %]
+    <li>
+        <a href="#" class="highlight_toggle" id="highlight_toggle_off">Unhighlight</a>
+        <a href="#" class="highlight_toggle" id="highlight_toggle_on">Highlight</a>
+    </li>
+    [% END %]
     <li style="display:none;"><a href="#" id="furthersearches">More searches</a></li>
 </ul>
 
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 54ed48e..30ced68 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tt
@@ -12,6 +12,7 @@
 [% IF ( OpacStarRatings != 'disable' ) %]<script type="text/javascript" src="/opac-tmpl/prog/en/lib/jquery/plugins/jquery.rating.js"></script>
 <link rel="stylesheet" type="text/css" href="/opac-tmpl/prog/en/css/jquery.rating.css" />[% END %]
 
+[% IF ( OpacHighlightedWords ) %]<script type="text/javascript" src="[% themelang %]/lib/jquery/plugins/jquery.highlight-3.js"></script>[% END %]
 <script type="text/JavaScript" language="JavaScript">
 //<![CDATA[
 
@@ -25,6 +26,23 @@
         [% END %]
     [% END %]
 
+    [% IF ( OpacHighlightedWords ) %]
+        var q_array = new Array();  // holds search terms if available
+
+        function highlightOff() {
+            $("#catalogue_detail_biblio").removeHighlight();
+            $(".highlight_toggle").toggle();
+        }
+        function highlightOn() {
+            var x;
+            for (x in q_array) {
+                $(".title").highlight(q_array[x]);
+                $(".author").highlight(q_array[x]);
+                $(".results_summary").highlight(q_array[x]);
+            }
+            $(".highlight_toggle").toggle();
+        }
+    [% END %]
 
      $(document).ready(function() { 
         $('#bibliodescriptions').tabs();
@@ -45,6 +63,11 @@
             widgets : ['zebra'],
             sortList: [[0,0]]
         });
+        [% IF ( query_desc ) %][% IF ( OpacHighlightedWords ) %]var query_desc = "[% query_desc |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]";
+            q_array = query_desc.split(" ");
+            highlightOn();
+            $("#highlight_toggle_on" ).hide().click(function() {highlightOn() ;});
+            $("#highlight_toggle_off").show().click(function() {highlightOff();});[% END %][% END %]
 	[% IF ( GoogleJackets ) %]
         KOHA.Google.GetCoverFromIsbn([% covernewwindow %]);
 	[% END %]
diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results-grouped.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results-grouped.tt
index 99c31bd..a3dae3c 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results-grouped.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results-grouped.tt
@@ -15,6 +15,12 @@ href="/cgi-bin/koha/opac-rss.pl?[% query_cgi %][% limit_cgi |html %]" />
 var q_array = new Array();  // will hold the search terms, if possible
 
 $(document).ready(function(){
+    [% IF ( OpacHighlightedWords ) %]
+        $('a.title').each(function() {
+            $(this).attr('href', $(this).attr('href') + '&query_desc=[% query_desc | url %]');
+        });
+    [% END %]
+
 	$('#sortsubmit').hide();
     $('#sort_by').change(function() {
         $('#bookbag_form').submit();
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 67b1686..06c3baa 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt
@@ -97,6 +97,12 @@ function highlightOn() {
 }
 [% END %]
 $(document).ready(function(){
+    [% IF ( OpacHighlightedWords ) %]
+        $('a.title').each(function() {
+            $(this).attr('href', $(this).attr('href') + '&query_desc=[% query_desc | url %]');
+        });
+    [% END %]
+
     [% IF ( opacbookbag ) %]$(".addtocart").show();[% END %]
 
 [% IF ( opacbookbag ) %]
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index 0bafabd..21fa141 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -1000,4 +1000,8 @@ if (C4::Context->preference('OPACLocalCoverImages') == 1) {
     $template->{VARS}->{localimages} = \@images;
 }
 
+if (C4::Context->preference('OpacHighlightedWords')) {
+    $template->{VARS}->{query_desc} = $query->param('query_desc');
+}
+
 output_html_with_http_headers $query, $cookie, $template->output;
-- 
1.7.7.3