Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
29.55% covered (danger)
29.55%
13 / 44
27.27% covered (danger)
27.27%
3 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchMemory
29.55% covered (danger)
29.55%
13 / 44
27.27% covered (danger)
27.27%
3 / 11
145.25
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLastSearchLink
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
42
 getLastHiddenFilters
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLastLimit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLastSort
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getEditLink
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getLastSearchParams
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 getCurrentSearchId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCurrentSearch
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLastSearchId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLastSearch
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * View helper for remembering recent user searches/parameters.
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 * @author   Juha Luoma <juha.luoma@helsinki.fi>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31namespace VuFind\View\Helper\Root;
32
33use Laminas\View\Helper\AbstractHelper;
34use VuFind\Search\Memory;
35
36/**
37 * View helper for remembering recent user searches/parameters.
38 *
39 * @category VuFind
40 * @package  View_Helpers
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @author   Juha Luoma <juha.luoma@helsinki.fi>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development Wiki
45 */
46class SearchMemory extends AbstractHelper
47{
48    /**
49     * Search memory
50     *
51     * @var Memory
52     */
53    protected $memory;
54
55    /**
56     * Constructor
57     *
58     * @param Memory $memory Search memory
59     */
60    public function __construct(Memory $memory)
61    {
62        $this->memory = $memory;
63    }
64
65    /**
66     * If a previous search is recorded in the session, return a link to it;
67     * otherwise, return a blank string.
68     *
69     * @param string $link   Text to use as body of link
70     * @param string $prefix Text to place in front of link
71     * @param string $suffix Text to place after link
72     *
73     * @return string
74     */
75    public function getLastSearchLink($link, $prefix = '', $suffix = '')
76    {
77        if ($lastSearch = $this->getLastSearch()) {
78            $searchClassId = $lastSearch->getBackendId();
79            $params = $lastSearch->getParams();
80            // Use last settings for params that are not stored in the search:
81            foreach (['limit', 'view', 'sort'] as $setting) {
82                $value
83                    = $this->memory->retrieveLastSetting($searchClassId, $setting);
84                if ($value) {
85                    $method = 'set' . ucfirst($setting);
86                    $params->$method($value);
87                }
88            }
89
90            $urlHelper = $this->getView()->plugin('url');
91            $url = $urlHelper($lastSearch->getOptions()->getSearchAction());
92            $queryHelper = $lastSearch->getUrlQuery();
93            // Try to append page number and page size from search context parameters saved in params object
94            $searchContext = $params->getSavedSearchContextParameters();
95            if (!empty($searchContext['limit'])) {
96                $queryHelper = $queryHelper->setLimit($searchContext['limit']);
97            }
98            if (!empty($searchContext['page'])) {
99                $queryHelper = $queryHelper->setPage($searchContext['page']);
100            }
101
102            $url .= $queryHelper->getParams(false);
103
104            $escaper = $this->getView()->plugin('escapeHtml');
105            return $prefix . '<a href="' . $escaper($url) . '">' . $link . '</a>'
106                . $suffix;
107        }
108        return '';
109    }
110
111    /**
112     * Retrieve the last hidden filters used.
113     *
114     * @param string $context Context of search (usually search class ID).
115     *
116     * @return array
117     */
118    public function getLastHiddenFilters($context)
119    {
120        return $this->memory->retrieveLastSetting($context, 'hiddenFilters', []);
121    }
122
123    /**
124     * Retrieve the last limit option used.
125     *
126     * @param string $context Context of search (usually search class ID).
127     *
128     * @return string
129     */
130    public function getLastLimit($context)
131    {
132        return $this->memory->retrieveLastSetting($context, 'limit');
133    }
134
135    /**
136     * Retrieve the last sort option used.
137     *
138     * @param string $context Context of search (usually search class ID).
139     *
140     * @return string
141     */
142    public function getLastSort($context)
143    {
144        return $this->memory->retrieveLastSetting($context, 'sort');
145    }
146
147    /**
148     * Get the URL to edit the last search.
149     *
150     * @param string $searchClassId Search class
151     * @param string $action        Action to take
152     * @param mixed  $value         Value for action
153     *
154     * @return string
155     */
156    public function getEditLink($searchClassId, $action, $value)
157    {
158        $query = compact('searchClassId') + [$action => $value];
159        $url = $this->getView()->plugin('url');
160        return $url('search-editmemory', [], compact('query'));
161    }
162
163    /**
164     * Retrieve the parameters of the last search by the search class
165     *
166     * @param string $searchClassId Search class
167     *
168     * @return \VuFind\Search\Base\Params
169     */
170    public function getLastSearchParams($searchClassId)
171    {
172        $lastUrl = $this->memory->retrieveSearch();
173        $queryParams = $lastUrl ? parse_url($lastUrl, PHP_URL_QUERY) : '';
174        $request = new \Laminas\Stdlib\Parameters();
175        $request->fromString($queryParams ?? '');
176        $paramsPlugin = $this->getView()->plugin('searchParams');
177        $params = $paramsPlugin($searchClassId);
178        // Make sure the saved URL represents search results from $searchClassId;
179        // if the user jumps from search results of one backend to a record of a
180        // different backend, we don't want to display irrelevant filters. If there
181        // is a backend mismatch, don't initialize the parameter object!
182        if ($lastUrl) {
183            $expectedPath = $this->view->url($params->getOptions()->getSearchAction());
184            if (str_starts_with($lastUrl, $expectedPath)) {
185                $params->initFromRequest($request);
186            }
187        }
188        return $params;
189    }
190
191    /**
192     * Get current search id
193     *
194     * @return ?int
195     */
196    public function getCurrentSearchId(): ?int
197    {
198        return $this->memory->getCurrentSearchId();
199    }
200
201    /**
202     * Get current search
203     *
204     * @return ?\VuFind\Search\Base\Results
205     */
206    public function getCurrentSearch(): ?\VuFind\Search\Base\Results
207    {
208        return $this->memory->getCurrentSearch();
209    }
210
211    /**
212     * Get last search id
213     *
214     * @return ?int
215     */
216    public function getLastSearchId(): ?int
217    {
218        return $this->memory->getLastSearchId();
219    }
220
221    /**
222     * Get last search
223     *
224     * @return ?\VuFind\Search\Base\Results
225     */
226    public function getLastSearch(): ?\VuFind\Search\Base\Results
227    {
228        return $this->memory->getLastSearch();
229    }
230}