Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AlphaBrowse
92.86% covered (success)
92.86%
13 / 14
66.67% covered (warning)
66.67%
2 / 3
6.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getUrl
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
 escapeForSolr
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * AlphaBrowse view helper
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  View_Helpers
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 Wiki
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Laminas\View\Helper\Url;
33
34/**
35 * AlphaBrowse view helper
36 *
37 * @category VuFind
38 * @package  View_Helpers
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class AlphaBrowse extends \Laminas\View\Helper\AbstractHelper
44{
45    /**
46     * URL helper
47     *
48     * @var Url
49     */
50    protected $url;
51
52    /**
53     * Additional configuration options.
54     *
55     * @var array
56     */
57    protected $options;
58
59    /**
60     * Constructor
61     *
62     * @param Url   $helper  URL helper
63     * @param array $options Additional configuration options
64     */
65    public function __construct(Url $helper, array $options = [])
66    {
67        $this->url = $helper;
68        $this->options = $options;
69    }
70
71    /**
72     * Get link to browse results (or null if no valid URL available)
73     *
74     * @param string $source AlphaBrowse index currently being used
75     * @param array  $item   Item to link to
76     *
77     * @return string
78     */
79    public function getUrl($source, $item)
80    {
81        if ($item['count'] <= 0) {
82            return null;
83        }
84
85        $query = [
86            'type' => ucwords($source) . 'Browse',
87            'lookfor' => $this->escapeForSolr($item['heading']),
88        ];
89        if ($this->options['bypass_default_filters'] ?? true) {
90            $query['dfApplied'] = 1;
91        }
92        if ($item['count'] == 1) {
93            $query['jumpto'] = 1;
94        }
95        return ($this->url)('search-results', [], ['query' => $query]);
96    }
97
98    /**
99     * Escape a string for inclusion in a Solr query.
100     *
101     * @param string $str String to escape
102     *
103     * @return string
104     */
105    protected function escapeForSolr($str)
106    {
107        return '"' . addcslashes($str, '"') . '"';
108    }
109}