Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
GeoCoords
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 recommendationEnabled
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 getSearchUrl
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * GeoCoords 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   Leila Gonzales <lmg@agiweb.org>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use VuFind\Search\Base\Options;
33
34use function is_array;
35
36/**
37 * GeoCoords view helper
38 *
39 * @category VuFind
40 * @package  View_Helpers
41 * @author   Leila Gonzales <lmg@agiweb.org>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development Wiki
44 */
45class GeoCoords extends \Laminas\View\Helper\AbstractHelper
46{
47    /**
48     * Is Map Search enabled?
49     *
50     * @var bool
51     */
52    protected $enabled;
53
54    /**
55     * Default coordinates
56     *
57     * @var string
58     */
59    protected $coords;
60
61    /**
62     * Get geoField variable name
63     *
64     * @var string
65     */
66    protected $geoField = 'long_lat';
67
68    /**
69     * Constructor
70     *
71     * @param string $coords Default coordinates
72     */
73    public function __construct($coords)
74    {
75        $this->coords = $coords;
76    }
77
78    /**
79     * Check if the relevant recommendation module is enabled; if not, there is no
80     * point in generating a search link. Note that right now we are assuming it is
81     * set up as a default top recommendation; this may need to be made more
82     * flexible in future to account for more use cases.
83     *
84     * @param array $settings Recommendation settings
85     *
86     * @return bool
87     */
88    protected function recommendationEnabled($settings)
89    {
90        if (isset($settings['top']) && is_array($settings['top'])) {
91            foreach ($settings['top'] as $setting) {
92                $parts = explode(':', $setting);
93                if (strtolower($parts[0]) === 'mapselection') {
94                    return true;
95                }
96            }
97        }
98        return false;
99    }
100
101    /**
102     * Get search URL if geo search is enabled for the specified search class ID,
103     * false if disabled.
104     *
105     * @param Options $options Search options
106     *
107     * @return string|bool
108     */
109    public function getSearchUrl(Options $options)
110    {
111        // If the relevant module is disabled, bail out now:
112        if (!$this->recommendationEnabled($options->getRecommendationSettings())) {
113            return false;
114        }
115        $urlHelper = $this->getView()->plugin('url');
116        return $urlHelper('search-results')
117            . '?filter[]=' . urlencode($this->geoField)
118            . ':Intersects(ENVELOPE(' . urlencode($this->coords) . '))';
119    }
120}