Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
PubDateVisAjax
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 8
342
0.00% covered (danger)
0.00%
0 / 1
 setConfig
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 process
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getVisFacets
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getZooming
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getFacetFields
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchParams
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 processDateFacets
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3/**
4 * PubDateVisAjax Recommendations Module
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Till Kinstler 2011.
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  Recommendations
25 * @author   Till Kinstler <kinstler@gbv.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:recommendation_modules Wiki
28 */
29
30namespace VuFind\Recommend;
31
32use function array_slice;
33
34/**
35 * PubDateVisAjax Recommendations Module
36 *
37 * This class displays a visualisation of facet values in a recommendation module
38 *
39 * @category VuFind
40 * @package  Recommendations
41 * @author   Till Kinstler <kinstler@gbv.de>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development:plugins:recommendation_modules Wiki
44 */
45class PubDateVisAjax implements RecommendInterface
46{
47    /**
48     * Raw settings string
49     *
50     * @var string
51     */
52    protected $settings;
53
54    /**
55     * Search results object
56     *
57     * @var \VuFind\Search\Base\Results
58     */
59    protected $searchObject;
60
61    /**
62     * Should we allow zooming? (String of "true" or "false")
63     *
64     * @var string
65     */
66    protected $zooming;
67
68    /**
69     * Facet fields to use
70     *
71     * @var array
72     */
73    protected $dateFacets = [];
74
75    /**
76     * Store the configuration of the recommendation module.
77     *
78     * @param string $settings Settings from searches.ini.
79     *
80     * @return void
81     */
82    public function setConfig($settings)
83    {
84        // Save the basic parameters:
85        $this->settings = $settings;
86
87        // Parse the additional settings:
88        $params = explode(':', $settings);
89        if ($params[0] == 'true' || $params[0] == 'false') {
90            $this->zooming = $params[0];
91            $this->dateFacets = array_slice($params, 1);
92        } else {
93            $this->zooming = 'false';
94            $this->dateFacets = $params;
95        }
96    }
97
98    /**
99     * Called before the Search Results object performs its main search
100     * (specifically, in response to \VuFind\Search\SearchRunner::EVENT_CONFIGURED).
101     * This method is responsible for setting search parameters needed by the
102     * recommendation module and for reading any existing search parameters that may
103     * be needed.
104     *
105     * @param \VuFind\Search\Base\Params $params  Search parameter object
106     * @param \Laminas\Stdlib\Parameters $request Parameter object representing user
107     * request.
108     *
109     * @return void
110     */
111    public function init($params, $request)
112    {
113        // No action needed.
114    }
115
116    /**
117     * Called after the Search Results object has performed its main search. This
118     * may be used to extract necessary information from the Search Results object
119     * or to perform completely unrelated processing.
120     *
121     * @param \VuFind\Search\Base\Results $results Search results object
122     *
123     * @return void
124     */
125    public function process($results)
126    {
127        $this->searchObject = $results;
128    }
129
130    /**
131     * Get visual facet details.
132     *
133     * @return array
134     */
135    public function getVisFacets()
136    {
137        // Don't bother processing if the result set is empty:
138        if ($this->searchObject->getResultTotal() <= 0) {
139            return [];
140        }
141        return $this->processDateFacets(
142            $this->searchObject->getParams()->getRawFilters()
143        );
144    }
145
146    /**
147     * Get zoom setting
148     *
149     * @return array
150     */
151    public function getZooming()
152    {
153        if (isset($this->zooming)) {
154            return $this->zooming;
155        }
156        return 'false';
157    }
158
159    /**
160     * Get facet fields
161     *
162     * @return array
163     */
164    public function getFacetFields()
165    {
166        return implode(':', $this->dateFacets);
167    }
168
169    /**
170     * Get search parameters
171     *
172     * @return string of params
173     */
174    public function getSearchParams()
175    {
176        // Get search parameters and return them minus the leading ?:
177        return substr($this->searchObject->getUrlQuery()->getParams(false), 1);
178    }
179
180    /**
181     * Support method for getVisData() -- extract details from applied filters.
182     *
183     * @param array $filters Current filter list
184     *
185     * @return array
186     */
187    protected function processDateFacets($filters)
188    {
189        $result = [];
190        foreach ($this->dateFacets as $current) {
191            $from = $to = '';
192            if (isset($filters[$current])) {
193                foreach ($filters[$current] as $filter) {
194                    if (preg_match('/\[\d+ TO \d+\]/', $filter)) {
195                        $range = explode(' TO ', trim($filter, '[]'));
196                        $from = $range[0] == '*' ? '' : $range[0];
197                        $to = $range[1] == '*' ? '' : $range[1];
198                        break;
199                    }
200                }
201            }
202            $result[$current] = [$from, $to];
203            $result[$current]['label']
204                = $this->searchObject->getParams()->getFacetLabel($current);
205        }
206        return $result;
207    }
208}