From feac0aa5db4c0faab1638b6c24e2017713f36b83 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 18 Dec 2012 12:52:46 -0500
Subject: [PATCH] Added CollectionSideFacets recommendation module and set it
 up to be active by default in collection view.

---
 config/vufind/Collection.ini                  | 18 ++++
 languages/en-gb.ini                           |  4 +
 languages/en.ini                              |  4 +
 module/VuFind/config/module.config.php        |  1 +
 .../VuFind/Recommend/CollectionSideFacets.php | 89 +++++++++++++++++++
 .../VuFind/Search/SolrCollection/Params.php   |  2 +-
 .../Recommend/CollectionSideFacets.phtml      | 37 ++++++++
 .../templates/Recommend/SideFacets.phtml      | 12 ++-
 .../Recommend/CollectionSideFacets.phtml      |  1 +
 9 files changed, 164 insertions(+), 4 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/Recommend/CollectionSideFacets.php
 create mode 100644 themes/blueprint/templates/Recommend/CollectionSideFacets.phtml
 create mode 100644 themes/jquerymobile/templates/Recommend/CollectionSideFacets.phtml

diff --git a/config/vufind/Collection.ini b/config/vufind/Collection.ini
index 55338e814a2..d664a03e43a 100644
--- a/config/vufind/Collection.ini
+++ b/config/vufind/Collection.ini
@@ -5,6 +5,24 @@
 ; details on recommendation modules).
 [Recommend]
 ;top[] = "PubDateVisAjax:true:publishDate"
+side[]  = "CollectionSideFacets:Facets::Collection:true"
+
+; This is a facet section similar to those found in facets.ini, but this is used for
+; the Collection module screen.
+[Facets]
+geographic_facet       = Location
+topic_facet            = Subjects
+authorStr              = Author
+hierarchy_parent_title = Subcollection
+genre_facet            = Genre
+era_facet              = Era
+publishDate            = "adv_search_year"  ; share year string w/advanced search page
+
+; This section is used to identify facets for special treatment by the SideFacets
+; recommendations module.
+[SpecialFacets]
+; Any fields listed below will be treated as date ranges rather than plain facets:
+dateRange[] = publishDate
 
 ; These settings control which fields are available to sort on in the Collection
 ; module.
diff --git a/languages/en-gb.ini b/languages/en-gb.ini
index 34d47c1454e..500e882a3a9 100644
--- a/languages/en-gb.ini
+++ b/languages/en-gb.ini
@@ -174,6 +174,7 @@ Identifier = "Identifier"
 Illustrated = Illustrated
 Import Record = "Import Record"
 Import to = "Import to "
+In This Collection = "In This Collection"
 Institution = Institution
 Institutional Login = "Institutional Login"
 Instructor = Instructor
@@ -192,6 +193,8 @@ Journal Articles = "Journal Articles"
 Journal Title = "Journal Title"
 Journals = Journals
 Jump to = "Jump to"
+Keyword = Keyword
+Keyword Filter = "Keyword Filter"
 Kit = Kit
 Language = Language
 Last Modified = "Last Modified"
@@ -351,6 +354,7 @@ Start Page = "Start Page"
 Start a new Advanced Search = "Start a new Advanced Search"
 Start a new Basic Search = "Start a new Basic Search"
 Status = Status
+Subcollection = Subcollection
 Subject = Subject
 Subject Area = "Subject Area"
 Subject Recommendations = "Subject Recommendations"
diff --git a/languages/en.ini b/languages/en.ini
index d9a1fe47364..c2c55514ad6 100644
--- a/languages/en.ini
+++ b/languages/en.ini
@@ -174,6 +174,7 @@ Identifier = "Identifier"
 Illustrated = Illustrated
 Import Record = "Import Record"
 Import to = "Import to "
+In This Collection = "In This Collection"
 Institution = Institution
 Institutional Login = "Institutional Login"
 Instructor = Instructor
@@ -192,6 +193,8 @@ Journal Articles = "Journal Articles"
 Journal Title = "Journal Title"
 Journals = Journals
 Jump to = "Jump to"
+Keyword = Keyword
+Keyword Filter = "Keyword Filter"
 Kit = Kit
 Language = Language
 Last Modified = "Last Modified"
@@ -351,6 +354,7 @@ Start Page = "Start Page"
 Start a new Advanced Search = "Start a new Advanced Search"
 Start a new Basic Search = "Start a new Basic Search"
 Status = Status
+Subcollection = Subcollection
 Subject = Subject
 Subject Area = "Subject Area"
 Subject Recommendations = "Subject Recommendations"
diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index d054f4a3f99..4b47642d213 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -255,6 +255,7 @@ $config = array(
             'authorfacets' => 'VuFind\Recommend\AuthorFacets',
             'authorityrecommend' => 'VuFind\Recommend\AuthorityRecommend',
             'catalogresults' => 'VuFind\Recommend\CatalogResults',
+            'collectionsidefacets' => 'VuFind\Recommend\CollectionSideFacets',
             'europeanaresults' => 'VuFind\Recommend\EuropeanaResults',
             'europeanaresultsdeferred' => 'VuFind\Recommend\EuropeanaResultsDeferred',
             'expandfacets' => 'VuFind\Recommend\ExpandFacets',
diff --git a/module/VuFind/src/VuFind/Recommend/CollectionSideFacets.php b/module/VuFind/src/VuFind/Recommend/CollectionSideFacets.php
new file mode 100644
index 00000000000..dc986154531
--- /dev/null
+++ b/module/VuFind/src/VuFind/Recommend/CollectionSideFacets.php
@@ -0,0 +1,89 @@
+<?php
+/**
+ * CollectionSideFacets Recommendations Module
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2010.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind2
+ * @package  Recommendations
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
+ */
+namespace VuFind\Recommend;
+
+/**
+ * CollectionSideFacets Recommendations Module
+ *
+ * This class extends the SideFacets functionality for use in Collection display.
+ *
+ * @category VuFind2
+ * @package  Recommendations
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
+ */
+class CollectionSideFacets extends SideFacets
+{
+    /**
+     * Is the keyword filter box active?
+     *
+     * @var bool
+     */
+    protected $keywordFilter = false;
+
+    /**
+     * setConfig
+     *
+     * Store the configuration of the recommendation module.
+     *
+     * @param string $settings Settings from searches.ini.
+     *
+     * @return void
+     */
+    public function setConfig($settings)
+    {
+        parent::setConfig($settings);
+
+        // Parse the additional settings:
+        $settings = explode(':', $settings);
+        if (isset($settings[3]) && $settings[3] !== 'false') {
+            $this->keywordFilter = true;
+        }
+    }
+
+    /**
+     * Get the current value of the keyword filter.
+     *
+     * @return string
+     */
+    public function getKeywordFilter()
+    {
+        return $this->results->getParams()->getDisplayQuery();
+    }
+
+    /**
+     * Is the keyword filter box enabled?
+     *
+     * @return bool
+     */
+    public function keywordFilterEnabled()
+    {
+        return $this->keywordFilter;
+    }
+}
diff --git a/module/VuFind/src/VuFind/Search/SolrCollection/Params.php b/module/VuFind/src/VuFind/Search/SolrCollection/Params.php
index 22b9972f22b..9d8ed38a1e2 100644
--- a/module/VuFind/src/VuFind/Search/SolrCollection/Params.php
+++ b/module/VuFind/src/VuFind/Search/SolrCollection/Params.php
@@ -122,6 +122,6 @@ class Params extends \VuFind\Search\Solr\Params
         $searchSettings = \VuFind\Config\Reader::getConfig('Collection');
         return isset($searchSettings->Recommend)
             ? $searchSettings->Recommend->toArray()
-            : array();
+            : array('side' => array('CollectionSideFacets:Facets::Collection:true'));
     }
 }
\ No newline at end of file
diff --git a/themes/blueprint/templates/Recommend/CollectionSideFacets.phtml b/themes/blueprint/templates/Recommend/CollectionSideFacets.phtml
new file mode 100644
index 00000000000..7ec7927674c
--- /dev/null
+++ b/themes/blueprint/templates/Recommend/CollectionSideFacets.phtml
@@ -0,0 +1,37 @@
+<?
+    $this->overrideSideFacetCaption = 'In This Collection';
+?>
+<? if ($this->recommend->keywordFilterEnabled()): ?>
+  <?
+    $keywordFilter = $this->recommend->getKeywordFilter();
+    if (!empty($keywordFilter)) {
+      $this->extraSideFacetFilters = array(
+        'Keyword' => array(
+            array(
+              'value' => $keywordFilter,
+              'displayText' => $keywordFilter,
+              'specialType' => 'keyword'
+            )
+        )
+      );
+    }
+  ?>
+  <? ob_start() ?>
+    <dl class="narrowList navmenu">
+      <dt><?=$this->transEsc('Keyword Filter')?></dt>
+      <dd style="padding: 0">
+      <form method="get" action="" name="keywordFilterForm" id="keywordFilterForm" class="keywordFilterForm">
+        <input id="keywordFilter_lookfor" type="text" name="lookfor" size="27" value="<?=$this->escapeHtml($keywordFilter)?>"/>
+        <? foreach ($this->recommend->getResults()->getParams()->getFilterList(true) as $field => $filters): ?>
+          <? foreach ($filters as $filter): ?>
+            <input type="hidden" name="filter[]" value="<?=$this->escapeHtml($filter['field'])?>:&quot;<?=$this->escapeHtml($filter['value'])?>&quot;" />
+          <? endforeach; ?>
+        <? endforeach; ?>
+        <input type="submit" name="submit" value="<?=$this->transEsc('Set')?>"/>
+      </form>
+      </dd>
+    </dl>
+  <? $this->sideFacetExtraControls = ob_get_contents(); ?>
+  <? ob_end_clean(); ?>
+<? endif; ?>
+<?=$this->render('Recommend/SideFacets.phtml')?>
\ No newline at end of file
diff --git a/themes/blueprint/templates/Recommend/SideFacets.phtml b/themes/blueprint/templates/Recommend/SideFacets.phtml
index 42646d34a00..375a3a5b692 100644
--- a/themes/blueprint/templates/Recommend/SideFacets.phtml
+++ b/themes/blueprint/templates/Recommend/SideFacets.phtml
@@ -1,6 +1,6 @@
 <? $results = $this->recommend->getResults(); ?>
 <div class="sidegroup">
-  <? if ($results->getResultTotal() > 0): ?><h4><?=$this->transEsc('Narrow Search')?></h4><? endif; ?>
+  <? if ($results->getResultTotal() > 0): ?><h4><?=$this->transEsc(isset($this->overrideSideFacetCaption) ? $this->overrideSideFacetCaption : 'Narrow Search')?></h4><? endif; ?>
   <? $checkboxFilters = $results->getParams()->getCheckboxFacets(); if (count($checkboxFilters) > 0): ?>
     <? foreach ($checkboxFilters as $current): ?>
         <div class="checkboxFilter<?=($results->getResultTotal() < 1 && !$current['selected'] && !$current['alwaysVisible']) ? ' hide' : ''?>">
@@ -11,13 +11,18 @@
         </div>
     <? endforeach; ?>
   <? endif; ?>
-  <? $filterList = $results->getParams()->getFilterList(true); if (!empty($filterList)): ?>
+  <? $extraFilters = isset($this->extraSideFacetFilters) ? $this->extraSideFacetFilters : array(); ?>
+  <? $filterList = array_merge($results->getParams()->getFilterList(true), $extraFilters); if (!empty($filterList)): ?>
     <strong><?=$this->transEsc('Remove Filters')?></strong>
     <ul class="filters">
     <? foreach ($filterList as $field => $filters): ?>
       <? foreach ($filters as $filter): ?>
         <?
-            $removeLink = $this->currentPath().$results->getUrlQuery()->removeFacet($filter['field'], $filter['value']);
+            if (isset($filter['specialType']) && $filter['specialType'] == 'keyword') {
+                $removeLink = $this->currentPath().$results->getUrlQuery()->replaceTerm($filter['value'], '');
+            } else {
+                $removeLink = $this->currentPath().$results->getUrlQuery()->removeFacet($filter['field'], $filter['value']);
+            }
             if ($filter['displayText'] == '[* TO *]') $filter['displayText'] = $this->translate('filter_wildcard');
         ?>
         <li>
@@ -28,6 +33,7 @@
     <? endforeach; ?>
     </ul>
   <? endif; ?>
+  <?= isset($this->sideFacetExtraControls) ? $this->sideFacetExtraControls : '' ?>
   <? $sideFacetSet = $this->recommend->getFacetSet(); $dateFacets = $this->recommend->getDateFacets(); ?>
   <? if (!empty($sideFacetSet) && $results->getResultTotal() > 0): ?>
     <? foreach ($sideFacetSet as $title => $cluster): ?>
diff --git a/themes/jquerymobile/templates/Recommend/CollectionSideFacets.phtml b/themes/jquerymobile/templates/Recommend/CollectionSideFacets.phtml
new file mode 100644
index 00000000000..0df1e74df18
--- /dev/null
+++ b/themes/jquerymobile/templates/Recommend/CollectionSideFacets.phtml
@@ -0,0 +1 @@
+<? /* Not supported in mobile theme. */ ?>
\ No newline at end of file
-- 
GitLab