From 058972f25dfaeb3e470fd9214b739174452b24eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Lahmann?= <lahmann@ub.uni-leipzig.de>
Date: Fri, 28 Oct 2016 14:16:39 +0200
Subject: [PATCH] refs #7019: * do not load record in collection view or
 hierarchy tab if record is the only member of hierarchy

---
 module/finc/config/module.config.php          |  3 +
 .../RecordDriver/SolrDefaultFincTrait.php     | 59 +++++++++++++-
 module/finc/src/finc/RecordTab/Factory.php    | 57 ++++++++++++++
 .../finc/src/finc/RecordTab/HierarchyTree.php | 77 +++++++++++++++++++
 4 files changed, 195 insertions(+), 1 deletion(-)
 create mode 100644 module/finc/src/finc/RecordTab/Factory.php
 create mode 100644 module/finc/src/finc/RecordTab/HierarchyTree.php

diff --git a/module/finc/config/module.config.php b/module/finc/config/module.config.php
index 86c7a94bbba..e50c59acbfa 100644
--- a/module/finc/config/module.config.php
+++ b/module/finc/config/module.config.php
@@ -48,6 +48,9 @@ $config = [
                 ],
             ],
             'recordtab' => [
+                'factories' => [
+                    'hierarchytree' => 'finc\RecordTab\Factory::getHierarchyTree',
+                ],
                 'invokables' => [
                     'staffviewai' => 'finc\RecordTab\StaffViewAI',
                     'acquisitionpda' => 'finc\RecordTab\AcquisitionPDA',
diff --git a/module/finc/src/finc/RecordDriver/SolrDefaultFincTrait.php b/module/finc/src/finc/RecordDriver/SolrDefaultFincTrait.php
index cdcbcc2c57d..959e854588f 100644
--- a/module/finc/src/finc/RecordDriver/SolrDefaultFincTrait.php
+++ b/module/finc/src/finc/RecordDriver/SolrDefaultFincTrait.php
@@ -31,7 +31,8 @@
  * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  */
 namespace finc\RecordDriver;
-use VuFindSearch\ParamBag;
+use VuFindSearch\ParamBag,
+    VuFindSearch\Query\Query as Query;
 
 /**
  * finc specific model for Solr records based on the stock
@@ -50,6 +51,27 @@ use VuFindSearch\ParamBag;
 trait SolrDefaultFincTrait
 {
 
+    /**
+     * Customized isCollection() to add a check if the record is a single element
+     * collection.
+     *
+     * @return bool
+     */
+    public function isCollection()
+    {
+        // first check as always if we have a collection
+        $isCollection = parent::isCollection();
+
+        if ($isCollection) {
+            // if we have a collection only return true if
+            // isSingleElementHierarchyRecord is false
+            return !$this->isSingleElementHierarchyRecord();
+        }
+
+        // if we've come so far this record is no collection
+        return false;
+    }
+
     /**
      * Get all call numbers associated with the record (empty string if none).
      *
@@ -1045,4 +1067,39 @@ trait SolrDefaultFincTrait
             $this->fields['zdb'] : null;
     }
 
+    /**
+     * Checks the record for having no hierarchy children. Returns true if record is
+     * top element of hierarchy and has no children.
+     *
+     * @return bool
+     */
+    public function isSingleElementHierarchyRecord()
+    {
+        $hierId = $this->getHierarchyTopID();
+        $currId = $this->getUniqueID();
+
+        // is the record's id indexed as its hierarchy_top_id
+        if (in_array($currId, $hierId)) {
+
+            $query = 'hierarchy_top_id:' . $currId;
+            $result = $this->searchService->search('Solr', new Query($query));
+            if (count($result) === 0) {
+                // for debugging only
+                $this->debug(
+                    'Problem retrieving total number of records with ' .
+                    'hierarchy_top_id ' . $currId
+                );
+            }
+            // number of records
+            $numFound = count($result->getRecords());
+            if ($numFound > 1) {
+                return false;
+            }
+        }
+
+        // either record is no top element of any hierarchy or we have come so far
+        // because it's the only element of its hierarchy
+        return true;
+    }
+
 }
diff --git a/module/finc/src/finc/RecordTab/Factory.php b/module/finc/src/finc/RecordTab/Factory.php
new file mode 100644
index 00000000000..726d9139a06
--- /dev/null
+++ b/module/finc/src/finc/RecordTab/Factory.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Record Tab Factory Class
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2014.
+ *
+ * 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 VuFind
+ * @package  RecordDrivers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
+ */
+namespace finc\RecordTab;
+use Zend\ServiceManager\ServiceManager;
+
+/**
+ * Record Tab Factory Class
+ *
+ * @category VuFind
+ * @package  RecordDrivers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
+ *
+ * @codeCoverageIgnore
+ */
+class Factory
+{
+    /**
+     * Factory for HierarchyTree tab plugin.
+     *
+     * @param ServiceManager $sm Service manager.
+     *
+     * @return HierarchyTree
+     */
+    public static function getHierarchyTree(ServiceManager $sm)
+    {
+        return new HierarchyTree(
+            $sm->getServiceLocator()->get('VuFind\Config')->get('config')
+        );
+    }
+}
diff --git a/module/finc/src/finc/RecordTab/HierarchyTree.php b/module/finc/src/finc/RecordTab/HierarchyTree.php
new file mode 100644
index 00000000000..d5d3e29dba0
--- /dev/null
+++ b/module/finc/src/finc/RecordTab/HierarchyTree.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * HierarchyTree tab
+ *
+ * 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 VuFind
+ * @package  RecordTabs
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:record_tabs Wiki
+ */
+namespace finc\RecordTab;
+
+/**
+ * HierarchyTree tab
+ *
+ * @category VuFind
+ * @package  RecordTabs
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:record_tabs Wiki
+ */
+class HierarchyTree extends \VuFind\RecordTab\HierarchyTree
+{
+    /**
+     * Tree data
+     *
+     * @var array
+     */
+    protected $treeList = null;
+
+    /**
+     * Configuration
+     *
+     * @var \Zend\Config\Config
+     */
+    protected $config = null;
+
+    /**
+     * Constructor
+     *
+     * @param \Zend\Config\Config $config Configuration
+     */
+    public function __construct(\Zend\Config\Config $config)
+    {
+        $this->config = $config;
+    }
+
+    /**
+     * Is this tab active?
+     *
+     * @return bool
+     */
+    public function isActive()
+    {
+        return (
+            $this->getRecordDriver()->tryMethod('isSingleElementHierarchyRecord')
+                ? false : parent::isActive()
+        );
+    }
+}
-- 
GitLab