Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.29% covered (warning)
68.29%
28 / 41
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
HoldingsILS
68.29% covered (warning)
68.29%
28 / 41
42.86% covered (danger)
42.86%
3 / 7
30.51
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSimpleUniqueCallNumbers
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 getUniqueCallNumbers
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
6
 isActive
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
 getTemplate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPaginator
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Holdings (ILS) tab
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  RecordTabs
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:record_tabs Wiki
28 */
29
30namespace VuFind\RecordTab;
31
32use VuFind\ILS\Connection;
33
34use function strlen;
35
36/**
37 * Holdings (ILS) tab
38 *
39 * @category VuFind
40 * @package  RecordTabs
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development:plugins:record_tabs Wiki
44 */
45class HoldingsILS extends AbstractBase
46{
47    /**
48     * ILS connection (or null if not applicable)
49     *
50     * @var Connection
51     */
52    protected $catalog;
53
54    /**
55     * Name of template to use for rendering holdings.
56     *
57     * @var string
58     */
59    protected $template;
60
61    /**
62     * Whether the holdings tab should be hidden when empty or not.
63     *
64     * @var bool
65     */
66    protected $hideWhenEmpty;
67
68    /**
69     * Constructor
70     *
71     * @param \VuFind\ILS\Connection|null $catalog       ILS connection to use to
72     * check for holdings before displaying the tab; may be set to null if no check
73     * is needed.
74     * @param string|null                 $template      Holdings template to use
75     * @param bool                        $hideWhenEmpty Whether the
76     * holdings tab should be hidden when empty or not
77     */
78    public function __construct(
79        Connection $catalog = null,
80        $template = null,
81        $hideWhenEmpty = false
82    ) {
83        $this->catalog = $catalog;
84        $this->template = $template ?? 'standard';
85        $this->hideWhenEmpty = $hideWhenEmpty;
86    }
87
88    /**
89     * Get the on-screen description for this tab.
90     *
91     * @return string
92     */
93    public function getDescription()
94    {
95        return 'Holdings';
96    }
97
98    /**
99     * Support method used in getUniqueCallNumbers for templates when full
100     * details are not supported -- extract all unique call numbers from
101     * an array of items.
102     *
103     * @param array $items Items to search through.
104     *
105     * @return array
106     */
107    protected function getSimpleUniqueCallNumbers($items)
108    {
109        $callNos = [];
110        foreach ($items as $item) {
111            if (isset($item['callnumber']) && strlen($item['callnumber']) > 0) {
112                $callNos[] = $item['callnumber'];
113            }
114        }
115        sort($callNos);
116        return array_unique($callNos);
117    }
118
119    /**
120     * Support method used by template -- extract all unique call numbers from
121     * an array of items.
122     *
123     * @param array $items       Items to search through.
124     * @param bool  $fullDetails Whether or not to return the full details about
125     *                           call numbers or only the simple legacy format.
126     *
127     * @return array
128     */
129    public function getUniqueCallNumbers($items, $fullDetails = false)
130    {
131        if (!$fullDetails) {
132            return $this->getSimpleUniqueCallNumbers($items);
133        }
134
135        $callNos = [];
136        foreach ($items as $item) {
137            if (strlen($item['callnumber'] ?? '') > 0) {
138                $prefix = $item['callnumber_prefix'] ?? '';
139                $callnumber = $item['callnumber'];
140                $display = $prefix ? $prefix . ' ' . $callnumber : $callnumber;
141                $callNos[] = compact('callnumber', 'display', 'prefix');
142            }
143        }
144
145        $unique = [];
146        foreach ($callNos as $no) {
147            $unique[$no['display']] = $no;
148        }
149        $callNosUnique = array_values($unique);
150
151        uasort(
152            $callNosUnique,
153            function ($a, $b) {
154                return $a['display'] <=> $b['display'];
155            }
156        );
157
158        return $callNosUnique;
159    }
160
161    /**
162     * Is this tab active?
163     *
164     * @return bool
165     */
166    public function isActive()
167    {
168        return ($this->catalog && $this->hideWhenEmpty)
169            ? $this->catalog->hasHoldings($this->driver->getUniqueID()) : true;
170    }
171
172    /**
173     * Get name of template for rendering holdings.
174     *
175     * @return string
176     */
177    public function getTemplate()
178    {
179        return $this->template;
180    }
181
182    /**
183     * Getting a paginator for the items list.
184     *
185     * @param int $totalItemCount Total count of items for a bib record
186     * @param int $page           Currently selected page of the items paginator
187     * @param int $itemLimit      Max. no of items per page
188     *
189     * @return \Laminas\Paginator\Paginator
190     */
191    public function getPaginator($totalItemCount, $page, $itemLimit)
192    {
193        // Return if a paginator is not needed or not supported ($itemLimit = null)
194        if (!$itemLimit || $totalItemCount <= $itemLimit) {
195            return;
196        }
197
198        // Create the paginator
199        $nullAdapter = new \Laminas\Paginator\Adapter\NullFill($totalItemCount);
200        $paginator = new \Laminas\Paginator\Paginator($nullAdapter);
201
202        // Some settings for the paginator
203        $paginator
204            ->setCurrentPageNumber($page)
205            ->setItemCountPerPage($itemLimit)
206            ->setPageRange(10);
207
208        return $paginator;
209    }
210}