Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
2.50% covered (danger)
2.50%
1 / 40
8.33% covered (danger)
8.33%
1 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
HierarchyTree
2.50% covered (danger)
2.50%
1 / 40
8.33% covered (danger)
8.33%
1 / 12
470.60
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConfig
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isActive
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getActiveTree
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getTreeList
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 isFullHierarchyVisible
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 renderTree
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 searchActive
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getSearchLimit
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getActiveRecord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 supportsAjax
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * HierarchyTree tab
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) The National Library of Finland 2024.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  RecordTabs
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @author   Ere Maijala <ere.maijala@helsinki.fi>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org/wiki/development:plugins:record_tabs Wiki
30 */
31
32namespace VuFind\RecordTab;
33
34use function count;
35use function is_object;
36
37/**
38 * HierarchyTree tab
39 *
40 * @category VuFind
41 * @package  RecordTabs
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @author   Ere Maijala <ere.maijala@helsinki.fi>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development:plugins:record_tabs Wiki
46 */
47class HierarchyTree extends AbstractBase
48{
49    /**
50     * Tree data
51     *
52     * @var array
53     */
54    protected $treeList = null;
55
56    /**
57     * Configuration
58     *
59     * @var \Laminas\Config\Config
60     */
61    protected $config = null;
62
63    /**
64     * Constructor
65     *
66     * @param \Laminas\Config\Config $config Configuration
67     */
68    public function __construct(\Laminas\Config\Config $config)
69    {
70        $this->config = $config;
71    }
72
73    /**
74     * Get the VuFind configuration.
75     *
76     * @return \Laminas\Config\Config
77     */
78    protected function getConfig()
79    {
80        return $this->config;
81    }
82
83    /**
84     * Get the on-screen description for this tab.
85     *
86     * @return string
87     */
88    public function getDescription()
89    {
90        return 'hierarchy_tree';
91    }
92
93    /**
94     * Is this tab active?
95     *
96     * @return bool
97     */
98    public function isActive()
99    {
100        $trees = $this->getTreeList();
101        return !empty($trees);
102    }
103
104    /**
105     * Get the ID of the active tree (false if none)
106     *
107     * @return string|bool
108     */
109    public function getActiveTree()
110    {
111        $treeList = $this->getTreeList();
112        $hierarchySetting = ($request = $this->getRequest())
113            ? $request->getPost('hierarchy', $request->getQuery('hierarchy', false))
114            : false;
115        if (count($treeList) == 1 || !$hierarchySetting) {
116            $keys = array_keys($treeList);
117            return $keys[0];
118        } else {
119            return $hierarchySetting;
120        }
121    }
122
123    /**
124     * Get an array of tree data
125     *
126     * @return array
127     */
128    public function getTreeList()
129    {
130        if (null === $this->treeList) {
131            $this->treeList
132                = $this->getRecordDriver()->tryMethod('getHierarchyTrees');
133            if (null === $this->treeList) {
134                $this->treeList = [];
135            }
136        }
137        return $this->treeList;
138    }
139
140    /**
141     * Should we display the full tree, or just a partial tree?
142     *
143     * @return bool
144     */
145    public function isFullHierarchyVisible()
146    {
147        // Get hierarchy driver:
148        $recordDriver = $this->getRecordDriver();
149        $hierarchyDriver = $recordDriver->tryMethod('getHierarchyDriver');
150
151        // We need a driver to proceed:
152        if (is_object($hierarchyDriver)) {
153            // No setting, or true setting -- use default setting:
154            $settings = $hierarchyDriver->getTreeSettings();
155            if ($settings['fullHierarchyRecordView'] ?? true) {
156                return true;
157            }
158        }
159
160        // Currently displaying top of tree?  Disable partial hierarchy:
161        if ($this->getActiveTree() == $recordDriver->getUniqueId()) {
162            return true;
163        }
164
165        // Only if we got this far is it appropriate to use a partial hierarchy:
166        return false;
167    }
168
169    /**
170     * Render a hierarchy tree
171     *
172     * @param string  $id      Hierarchy ID (omit to use active tree)
173     * @param ?string $context Context for use by renderer or null for default
174     * @param array   $options Additional options (like previewElement)
175     *
176     * @return string
177     */
178    public function renderTree(string $id = null, ?string $context = null, array $options = [])
179    {
180        $id ??= $this->getActiveTree();
181        $recordDriver = $this->getRecordDriver();
182        $hierarchyDriver = $recordDriver->tryMethod('getHierarchyDriver');
183        if (is_object($hierarchyDriver)) {
184            return $hierarchyDriver->render($recordDriver, $context ?? 'Record', 'List', $id, $options);
185        }
186        return '';
187    }
188
189    /**
190     * Is tree searching active?
191     *
192     * @return bool
193     */
194    public function searchActive()
195    {
196        $config = $this->getConfig();
197        return !isset($config->Hierarchy->search) || $config->Hierarchy->search;
198    }
199
200    /**
201     * Get the tree search result limit.
202     *
203     * @return int
204     */
205    public function getSearchLimit()
206    {
207        $config = $this->getConfig();
208        return $config->Hierarchy->treeSearchLimit ?? -1;
209    }
210
211    /**
212     * Get the current active record. Returns record driver if there is an active
213     * record or null otherwise.
214     *
215     * @return ?\VuFind\RecordDriver\AbstractBase
216     */
217    public function getActiveRecord(): ?\VuFind\RecordDriver\AbstractBase
218    {
219        return null;
220    }
221
222    /**
223     * Can this tab be loaded via AJAX?
224     *
225     * @return bool
226     */
227    public function supportsAjax()
228    {
229        // No, special width adjustment needed.
230        return false;
231    }
232}