Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetVisData
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 4
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
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
 processFacetValues
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
 handleRequest
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * "Get Visualization Data" AJAX handler
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
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  AJAX
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Chris Hallberg <crhallberg@gmail.com>
27 * @author   Till Kinstler <kinstler@gbv.de>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org/wiki/development Wiki
30 */
31
32namespace VuFind\AjaxHandler;
33
34use Laminas\Mvc\Controller\Plugin\Params;
35use Laminas\Stdlib\Parameters;
36use VuFind\Search\Solr\Results;
37use VuFind\Session\Settings as SessionSettings;
38
39/**
40 * "Get Visualization Data" AJAX handler
41 *
42 * AJAX for timeline feature (PubDateVisAjax)
43 *
44 * @category VuFind
45 * @package  AJAX
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @author   Chris Hallberg <crhallberg@gmail.com>
48 * @author   Till Kinstler <kinstler@gbv.de>
49 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
50 * @link     https://vufind.org/wiki/development Wiki
51 */
52class GetVisData extends AbstractBase
53{
54    /**
55     * Solr search results object
56     *
57     * @var Results
58     */
59    protected $results;
60
61    /**
62     * Constructor
63     *
64     * @param SessionSettings $ss      Session settings
65     * @param Results         $results Solr search results object
66     */
67    public function __construct(SessionSettings $ss, Results $results)
68    {
69        $this->sessionSettings = $ss;
70        $this->results = $results;
71    }
72
73    /**
74     * Extract details from applied filters.
75     *
76     * @param array $filters    Current filter list
77     * @param array $dateFacets Objects containing the date ranges
78     *
79     * @return array
80     */
81    protected function processDateFacets($filters, $dateFacets)
82    {
83        $result = [];
84        foreach ($dateFacets as $current) {
85            $from = $to = '';
86            if (isset($filters[$current])) {
87                foreach ($filters[$current] as $filter) {
88                    if (preg_match('/\[[\d\*]+ TO [\d\*]+\]/', $filter)) {
89                        $range = explode(' TO ', trim($filter, '[]'));
90                        $from = $range[0] == '*' ? '' : $range[0];
91                        $to = $range[1] == '*' ? '' : $range[1];
92                        break;
93                    }
94                }
95            }
96            $result[$current] = [$from, $to];
97            $result[$current]['label']
98                = $this->results->getParams()->getFacetLabel($current);
99        }
100        return $result;
101    }
102
103    /**
104     * Filter bad values from facet lists and add useful data fields.
105     *
106     * @param array $filters Current filter list
107     * @param array $fields  Processed date information from processDateFacets
108     *
109     * @return array
110     */
111    protected function processFacetValues($filters, $fields)
112    {
113        $facets = $this->results->getFullFieldFacets(array_keys($fields));
114        $retVal = [];
115        foreach ($facets as $field => $values) {
116            $filter = $filters[$field][0] ?? null;
117            $newValues = [
118                'data' => [],
119                'min' => $fields[$field][0] > 0 ? $fields[$field][0] : 0,
120                'max' => $fields[$field][1] > 0 ? $fields[$field][1] : 0,
121                'removalURL' => $this->results->getUrlQuery()
122                    ->removeFacet($field, $filter)->getParams(false),
123            ];
124            foreach ($values['data']['list'] as $current) {
125                // Only retain numeric values!
126                if (preg_match('/^[0-9]+$/', $current['value'])) {
127                    $newValues['data'][]
128                        = [$current['value'], $current['count']];
129                }
130            }
131            $retVal[$field] = $newValues;
132        }
133        return $retVal;
134    }
135
136    /**
137     * Handle a request.
138     *
139     * @param Params $params Parameter helper from controller
140     *
141     * @return array [response data, HTTP status code]
142     */
143    public function handleRequest(Params $params)
144    {
145        $this->disableSessionWrites();  // avoid session write timing bug
146        $paramsObj = $this->results->getParams();
147        $paramsObj->initFromRequest(new Parameters($params->fromQuery()));
148        foreach ($params->fromQuery('hf', []) as $hf) {
149            $paramsObj->addHiddenFilter($hf);
150        }
151        $paramsObj->getOptions()->disableHighlighting();
152        $paramsObj->getOptions()->spellcheckEnabled(false);
153        $filters = $paramsObj->getRawFilters();
154        $rawDateFacets = $params->fromQuery('facetFields');
155        $dateFacets = empty($rawDateFacets) ? [] : explode(':', $rawDateFacets);
156        $fields = $this->processDateFacets($filters, $dateFacets);
157        $facets = $this->processFacetValues($filters, $fields);
158        return $this->formatResponse(compact('facets'));
159    }
160}