Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.74% covered (success)
95.74%
45 / 47
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
NewItems
95.74% covered (success)
95.74%
45 / 47
81.82% covered (warning)
81.82%
9 / 11
25
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
 getBibIDsFromCatalog
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 getDefaultSort
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 includeFacets
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFundList
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getHiddenFilters
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 getMaxAge
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRanges
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 getResultPages
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getSolrFilter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * VuFind Action Helper - New Items Support Methods
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  Controller_Plugins
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 Main Page
28 */
29
30namespace VuFind\Controller\Plugin;
31
32use Laminas\Config\Config;
33use Laminas\Mvc\Controller\Plugin\AbstractPlugin;
34use Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger;
35
36use function array_slice;
37use function count;
38use function intval;
39use function is_string;
40
41/**
42 * Action helper to perform new items-related actions
43 *
44 * @category VuFind
45 * @package  Controller_Plugins
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org Main Page
49 */
50class NewItems extends AbstractPlugin
51{
52    /**
53     * Configuration
54     *
55     * @var Config
56     */
57    protected $config;
58
59    /**
60     * Constructor
61     *
62     * @param Config $config Configuration
63     */
64    public function __construct(Config $config)
65    {
66        $this->config = $config;
67    }
68
69    /**
70     * Figure out which bib IDs to load from the ILS.
71     *
72     * @param \VuFind\ILS\Connection     $catalog ILS connection
73     * @param \VuFind\Search\Solr\Params $params  Solr parameters
74     * @param string                     $range   Range setting
75     * @param string                     $dept    Department setting
76     * @param FlashMessenger             $flash   Flash messenger
77     *
78     * @return array
79     */
80    public function getBibIDsFromCatalog($catalog, $params, $range, $dept, $flash)
81    {
82        // The code always pulls in enough catalog results to get a fixed number
83        // of pages worth of Solr results. Note that if the Solr index is out of
84        // sync with the ILS, we may see fewer results than expected.
85        $resultPages = $this->getResultPages();
86        $perPage = $params->getLimit();
87        $newItems = $catalog->getNewItems(1, $perPage * $resultPages, $range, $dept);
88
89        // Build a list of unique IDs
90        $bibIDs = [];
91        if (isset($newItems['results'])) {
92            for ($i = 0; $i < count($newItems['results']); $i++) {
93                $bibIDs[] = $newItems['results'][$i]['id'];
94            }
95        }
96
97        // Truncate the list if it is too long:
98        $limit = $params->getQueryIDLimit();
99        if (count($bibIDs) > $limit) {
100            $bibIDs = array_slice($bibIDs, 0, $limit);
101            $flash->addMessage('too_many_new_items', 'info');
102        }
103
104        return $bibIDs;
105    }
106
107    /**
108     * Get default setting (null to use regular default).
109     *
110     * @return ?string
111     */
112    public function getDefaultSort(): ?string
113    {
114        return $this->config->default_sort ?? null;
115    }
116
117    /**
118     * Should we include facets in the new items search page?
119     *
120     * @return bool
121     */
122    public function includeFacets(): bool
123    {
124        return $this->config->include_facets ?? false;
125    }
126
127    /**
128     * Get fund list
129     *
130     * @return array
131     */
132    public function getFundList()
133    {
134        if ($this->getMethod() == 'ils') {
135            $catalog = $this->getController()->getILS();
136            return $catalog->checkCapability('getFunds')
137                ? $catalog->getFunds() : [];
138        }
139        return [];
140    }
141
142    /**
143     * Get the hidden filter settings.
144     *
145     * @return array
146     */
147    public function getHiddenFilters()
148    {
149        if (!isset($this->config->filter)) {
150            return [];
151        }
152        if (is_string($this->config->filter)) {
153            return [$this->config->filter];
154        }
155        $hiddenFilters = [];
156        foreach ($this->config->filter as $current) {
157            $hiddenFilters[] = $current;
158        }
159        return $hiddenFilters;
160    }
161
162    /**
163     * Get the maximum range setting (or return 0 for no limit).
164     *
165     * @return int
166     */
167    public function getMaxAge()
168    {
169        return max($this->getRanges());
170    }
171
172    /**
173     * Get method setting
174     *
175     * @return string
176     */
177    public function getMethod()
178    {
179        return $this->config->method ?? 'ils';
180    }
181
182    /**
183     * Get range settings
184     *
185     * @return array
186     */
187    public function getRanges()
188    {
189        // Find out if there are user configured range options; if not,
190        // default to the standard 1/5/30 days:
191        $ranges = [];
192        if (isset($this->config->ranges)) {
193            $tmp = explode(',', $this->config->ranges);
194            foreach ($tmp as $range) {
195                $range = intval($range);
196                if ($range > 0) {
197                    $ranges[] = $range;
198                }
199            }
200        }
201        if (empty($ranges)) {
202            $ranges = [1, 5, 30];
203        }
204        return $ranges;
205    }
206
207    /**
208     * Get the result pages setting.
209     *
210     * @return int
211     */
212    public function getResultPages()
213    {
214        if (isset($this->config->result_pages)) {
215            $resultPages = intval($this->config->result_pages);
216            if ($resultPages < 1) {
217                $resultPages = 10;
218            }
219        } else {
220            $resultPages = 10;
221        }
222        return $resultPages;
223    }
224
225    /**
226     * Get a Solr filter to limit to the specified number of days.
227     *
228     * @param int $range Days to search
229     *
230     * @return string
231     */
232    public function getSolrFilter($range)
233    {
234        return 'first_indexed:[NOW-' . $range . 'DAY TO NOW]';
235    }
236}