Passed
Pull Request — main (#5234)
by David
20:53
created

HereMaps::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\FlashMessages;
24
use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\Validator;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
30
use function e;
31
use function redirect;
32
33
/**
34
 * Class HereMaps - use maps within webtrees
35
 */
36
class HereMaps extends AbstractModule implements ModuleConfigInterface, ModuleMapProviderInterface
37
{
38
    use ModuleConfigTrait;
39
    use ModuleMapProviderTrait;
40
41
    // ISO-639-1 language schema
42
    private const array LANGUAGE_CODES = ["ar","as","az","be","bg","bn","bs","ca","cs","cy","da","de","el","en","es","et","eu","fi","fo","fr","ga","gl","gn","gu","he","hi","hr","hu","hy","id","is","it","ja","ka","kk","km","kn","ko","ky","lt","lv","mk","ml","mr","ms","mt","my","nl","no","or","pa","pl","pt","ro","ru","sk","sl","sq","sr","sv","ta","te","th","tr","uk","uz","vi","zh","zh-Hant"];
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 42 at column 24
Loading history...
43
44
    /**
45
     * Name of the map provider.
46
     *
47
     * @return string
48
     */
49
    public function description(): string
50
    {
51
        $link = '<a href="https://www.here.com" dir="ltr">www.here.com</a>';
52
53
        // I18N: %s is a link/URL
54
        return I18N::translate('Create maps using %s.', $link);
55
    }
56
57
    public function isEnabledByDefault(): bool
58
    {
59
        return false;
60
    }
61
62
    /**
63
     * @return ResponseInterface
64
     */
65
    public function getAdminAction(): ResponseInterface
66
    {
67
        $this->layout = 'layouts/administration';
68
69
        $api_key = $this->getPreference('api_key');
70
71
        return $this->viewResponse('modules/here-maps/config', [
72
            'api_key' => $api_key,
73
            'title'   => $this->title(),
74
        ]);
75
    }
76
77
    /**
78
     * Name of the map provider.
79
     *
80
     * @return string
81
     */
82
    public function title(): string
83
    {
84
        return /* I18N: https://wego.here.com */ I18N::translate('Here maps');
85
    }
86
87
    /**
88
     * @param ServerRequestInterface $request
89
     *
90
     * @return ResponseInterface
91
     */
92
    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
93
    {
94
        $api_key = Validator::parsedBody($request)->string('api_key');
95
96
        $this->setPreference('api_key', $api_key);
97
98
        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
99
100
        return redirect($this->getConfigLink());
101
    }
102
103
    /**
104
     * Parameters to create a TileLayer in LeafletJs.
105
     *
106
     * @return array<object>
107
     */
108
    public function leafletJsTileLayers(): array
109
    {
110
        $api_key = $this->getPreference('api_key');
111
112
        if ($api_key === '') {
113
            $message = I18N::translate('This service requires an API key.');
114
115
            if (Auth::isAdmin()) {
116
                $message = '<a href="' . e($this->getConfigLink()) . '">' . $message . '</a>';
117
            }
118
119
            throw new HttpServerErrorException($message);
120
        }
121
122
        $tag = I18N::languageTag();
123
        $lang2 = 'en';
124
        foreach (self::LANGUAGE_CODES as $code) {
125
            if (str_contains($tag, $code)) {
126
                $lang2 = $code;
127
                break;
128
            }
129
        }
130
131
        return [
132
            (object) [
133
                'apiKey'      => $api_key,
134
                'attribution' => '&copy;2024 HERE Technologies',
135
                'label'       => 'Day',
136
                'maxZoom'     => 18,
137
                'minZoom'     => 2,
138
		        'lang2'       => $lang2,
139
                'url'         => "https://maps.hereapi.com/v3/base/mc/{z}/{x}/{y}/jpeg?size=256&style={variant}&lang=en&lang2={lang2}&apiKey={apiKey}",
140
                'variant'     => 'explore.day',
141
                'localName'   => 'HEREDay',
142
            ],
143
            (object) [
144
                'apiKey'      => $api_key,
145
                'attribution' => '&copy;2024 HERE Technologies',
146
                'label'       => 'Satellite Day',
147
                'maxZoom'     => 18,
148
                'minZoom'     => 2,
149
		        'lang2'       => $lang2,
150
                'url'         => "https://maps.hereapi.com/v3/base/mc/{z}/{x}/{y}/jpeg?size=256&style={variant}&lang=en&lang2={lang2}&apiKey={apiKey}",
151
                'variant'     => 'explore.satellite.day',
152
                'localName'   => 'HEREDSatelliteDay',
153
            ],
154
            (object) [
155
                'apiKey'      => $api_key,
156
                'attribution' => '&copy;2024 HERE Technologies',
157
                'label'       => 'Night',
158
                'maxZoom'     => 18,
159
                'minZoom'     => 2,
160
		        'lang2'       => $lang2,
161
                'url'         => "https://maps.hereapi.com/v3/base/mc/{z}/{x}/{y}/jpeg?size=256&style={variant}&lang=en&lang2={lang2}&apiKey={apiKey}",
162
                'variant'     => 'explore.night',
163
                'localName'   => 'HERENight',
164
            ],
165
            (object) [
166
                'apiKey'      => $api_key,
167
                'attribution' => '&copy;2024 HERE Technologies',
168
                'label'       => 'Terrain',
169
                'maxZoom'     => 18,
170
                'minZoom'     => 2,
171
		        'lang2'       => $lang2,
172
                'url'         => "https://maps.hereapi.com/v3/base/mc/{z}/{x}/{y}/jpeg?size=256&style={variant}&lang=en&lang2={lang2}&apiKey={apiKey}",
173
                'variant'     => 'topo.day',
174
                'localName'   => 'HERETerrain',
175
            ],
176
        ];
177
    }
178
}
179