Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.96% covered (warning)
86.96%
40 / 46
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Params
86.96% covered (warning)
86.96%
40 / 46
50.00% covered (danger)
50.00%
3 / 6
16.57
0.00% covered (danger)
0.00%
0 / 1
 getBackendParameters
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
3.24
 getFacetValueRawDisplayText
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 fixPrimoFacetValue
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getFilterSettings
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
 getFilterList
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
 getFacetLabel
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2
3/**
4 * Primo Central Search Parameters
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 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  Search_Primo
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Ere Maijala <ere.maijala@helsinki.fi>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org Main Page
29 */
30
31namespace VuFind\Search\Primo;
32
33use VuFindSearch\ParamBag;
34
35use function in_array;
36
37/**
38 * Primo Central Search Parameters
39 *
40 * @category VuFind
41 * @package  Search_Primo
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 Main Page
46 */
47class Params extends \VuFind\Search\Base\Params
48{
49    /**
50     * Config sections to search for facet labels if no override configuration
51     * is set.
52     *
53     * @var array
54     */
55    protected $defaultFacetLabelSections
56        = ['Advanced_Facets', 'FacetsTop', 'Facets'];
57
58    /**
59     * Config sections to search for checkbox facet labels if no override
60     * configuration is set.
61     *
62     * @var array
63     */
64    protected $defaultFacetLabelCheckboxSections = ['CheckboxFacets'];
65
66    /**
67     * Mappings of specific Primo facet values (spelling errors and other special
68     * cases present at least in CDI)
69     *
70     * @var array
71     */
72    protected $facetValueMappings = [
73        'reference_entrys' => 'Reference Entries',
74        'newsletterarticle' => 'Newsletter Articles',
75        'archival_material_manuscripts' => 'Archival Materials / Manuscripts',
76        'magazinearticle' => 'Magazine Articles',
77    ];
78
79    /**
80     * Create search backend parameters for advanced features.
81     *
82     * @return ParamBag
83     */
84    public function getBackendParameters()
85    {
86        $backendParams = new ParamBag();
87
88        // The "relevance" sort option is a VuFind reserved word; we need to make
89        // this null in order to achieve the desired effect with Primo:
90        $sort = $this->getSort();
91        $finalSort = ($sort == 'relevance') ? null : $sort;
92        $backendParams->set('sort', $finalSort);
93        $backendParams->set('filterList', $this->getFilterSettings());
94        if ($this->getOptions()->highlightEnabled()) {
95            $backendParams->set('highlight', true);
96            $backendParams->set('highlightStart', '{{{{START_HILITE}}}}');
97            $backendParams->set('highlightEnd', '{{{{END_HILITE}}}}');
98        }
99
100        return $backendParams;
101    }
102
103    /**
104     * Get a display text for a facet field.
105     *
106     * @param string $field Facet field
107     * @param string $value Facet value
108     *
109     * @return string
110     */
111    public function getFacetValueRawDisplayText(string $field, string $value): string
112    {
113        return $this->fixPrimoFacetValue(
114            parent::getFacetValueRawDisplayText($field, $value)
115        );
116    }
117
118    /**
119     * Normalize a Primo facet value.
120     *
121     * @param string $str String to normalize
122     *
123     * @return string
124     */
125    public function fixPrimoFacetValue($str)
126    {
127        if ($replacement = $this->facetValueMappings[$str] ?? '') {
128            return $replacement;
129        }
130        return mb_convert_case(
131            preg_replace('/_/u', ' ', $str),
132            MB_CASE_TITLE,
133            'UTF-8'
134        );
135    }
136
137    /**
138     * Return the current filters as an array
139     *
140     * @return array
141     */
142    public function getFilterSettings()
143    {
144        $result = [];
145        $filterList = array_merge_recursive(
146            $this->getHiddenFilters(),
147            $this->filterList
148        );
149        foreach ($filterList as $field => $filter) {
150            $facetOp = 'AND';
151            $prefix = substr($field, 0, 1);
152            if ('~' === $prefix || '-' === $prefix) {
153                $facetOp = '~' === $prefix ? 'OR' : 'NOT';
154                $field = substr($field, 1);
155            }
156            $result[] = [
157                'field' => $field,
158                'facetOp' => $facetOp,
159                'values' => $filter,
160            ];
161        }
162        return $result;
163    }
164
165    /**
166     * Return an array structure containing information about all current filters.
167     *
168     * @param bool $excludeCheckboxFilters Should we exclude checkbox filters from
169     * the list (to be used as a complement to getCheckboxFacets()).
170     *
171     * @return array                       Field, values and translation status
172     */
173    public function getFilterList($excludeCheckboxFilters = false)
174    {
175        $result = parent::getFilterList($excludeCheckboxFilters);
176        if (isset($result['citing'])) {
177            unset($result['citing']);
178        }
179        if (isset($result['citedby'])) {
180            unset($result['citedby']);
181        }
182        return $result;
183    }
184
185    /**
186     * Get a user-friendly string to describe the provided facet field.
187     *
188     * @param string $field   Facet field name.
189     * @param string $value   Facet value.
190     * @param string $default Default field name (null for default behavior).
191     *
192     * @return string         Human-readable description of field.
193     */
194    public function getFacetLabel($field, $value = null, $default = null)
195    {
196        if (in_array($field, ['citing', 'citedby'])) {
197            return $field;
198        }
199        return parent::getFacetLabel($field, $value, $default);
200    }
201}