Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractSearch
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 getContainerClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0
 renderExpandLink
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0
 renderSpellingSuggestions
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3/**
4 * Helper class for displaying search-related HTML chunks.
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  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;
31
32use Laminas\View\Helper\AbstractHelper;
33
34/**
35 * Helper class for displaying search-related HTML chunks.
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 */
43abstract class AbstractSearch extends AbstractHelper
44{
45    /**
46     * Get the CSS classes for the container holding the suggestions.
47     *
48     * @return string
49     */
50    abstract protected function getContainerClass();
51
52    /**
53     * Render an expand link.
54     *
55     * @param string                             $url  Link href
56     * @param \Laminas\View\Renderer\PhpRenderer $view View renderer object
57     *
58     * @return string
59     */
60    abstract protected function renderExpandLink($url, $view);
61
62    /**
63     * Support function to display spelling suggestions.
64     *
65     * @param string                             $msg     HTML to display at the top
66     * of the spelling section.
67     * @param \VuFind\Search\Base\Results        $results Results object
68     * @param \Laminas\View\Renderer\PhpRenderer $view    View renderer object
69     *
70     * @return string
71     */
72    public function renderSpellingSuggestions($msg, $results, $view)
73    {
74        $spellingSuggestions = $results->getSpellingSuggestions();
75        if (empty($spellingSuggestions)) {
76            return '';
77        }
78
79        $html = '<div class="spellingSuggestions ' . $this->getContainerClass() . '">';
80        $html .= '<h2>' . $msg . '</h2><ul class="terms">';
81        $normalizer = $results->getOptions()->getSpellingNormalizer();
82        foreach ($spellingSuggestions as $term => $details) {
83            $html .= '<li>' . $view->escapeHtml($term) . ' &raquo; <ul class="suggestions">';
84            foreach ($details['suggestions'] as $word => $data) {
85                $href = $results->getUrlQuery()
86                    ->replaceTerm(
87                        $term,
88                        $data['new_term'],
89                        $normalizer
90                    )->getParams();
91                $html .= '<li><a href="' . $href . '">' . $view->escapeHtml($word)
92                    . '</a>';
93                if (isset($data['expand_term']) && !empty($data['expand_term'])) {
94                    $url = $results->getUrlQuery()
95                        ->replaceTerm(
96                            $term,
97                            $data['expand_term'],
98                            $normalizer
99                        )->getParams();
100                    $html .= $this->renderExpandLink($url, $view);
101                }
102                $html .= '</li>';
103            }
104            $html .= '</ul></li>';
105        }
106        $html .= '</ul></div>';
107        return $html;
108    }
109}