xref: /webtrees/app/Module/BingMaps.php (revision bd51cb5d7e4bcd204833cb85b18909a70c2bff49)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Validator;
25use Psr\Http\Message\ResponseInterface;
26use Psr\Http\Message\ServerRequestInterface;
27
28use function redirect;
29
30/**
31 * Class BingMaps - use maps within webtrees
32 */
33class BingMaps extends AbstractModule implements ModuleConfigInterface, ModuleMapProviderInterface
34{
35    use ModuleConfigTrait;
36    use ModuleMapProviderTrait;
37
38    /**
39     * Name of the map provider.
40     *
41     * @return string
42     */
43    public function description(): string
44    {
45        $link = '<a href="https://www.bing.com/maps" dir="ltr">www.bing.com/maps</a>';
46
47        // I18N: %s is a link/URL
48        return I18N::translate('Create maps using %s.', $link);
49    }
50
51    /**
52     * Should this module be enabled when it is first installed?
53     *
54     * @return bool
55     */
56    public function isEnabledByDefault(): bool
57    {
58        return false;
59    }
60
61    /**
62     * @return ResponseInterface
63     */
64    public function getAdminAction(): ResponseInterface
65    {
66        $this->layout = 'layouts/administration';
67
68        $api_key = $this->getPreference('api_key');
69
70        return $this->viewResponse('modules/bing-maps/config', [
71            'api_key' => $api_key,
72            'title'   => $this->title(),
73        ]);
74    }
75
76    /**
77     * Name of the map provider.
78     *
79     * @return string
80     */
81    public function title(): string
82    {
83        return I18N::translate('Bing™ maps');
84    }
85
86    /**
87     * @param ServerRequestInterface $request
88     *
89     * @return ResponseInterface
90     */
91    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
92    {
93        $api_key = Validator::parsedBody($request)->string('api_key');
94
95        $this->setPreference('api_key', $api_key);
96
97        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
98
99        return redirect($this->getConfigLink());
100    }
101
102    /**
103     * Parameters to create a TileLayer in LeafletJs.
104     *
105     * @return array<object>
106     */
107    public function leafletJsTileLayers(): array
108    {
109        $api_key = $this->getPreference('api_key');
110
111        return [
112            (object) [
113                'bingMapsKey' => $api_key,
114                'default'     => false,
115                'imagerySet'  => 'Aerial',
116                'label'       => 'Aerial',
117            ],
118            (object) [
119                'bingMapsKey' => $api_key,
120                'default'     => false,
121                'imagerySet'  => 'CanvasDark',
122                'label'       => 'Dark',
123            ],
124            (object) [
125                'bingMapsKey' => $api_key,
126                'default'     => false,
127                'imagerySet'  => 'CanvasGray',
128                'label'       => 'Grey',
129            ],
130            (object) [
131                'bingMapsKey' => $api_key,
132                'default'     => true,
133                'imagerySet'  => 'CanvasLight',
134                'label'       => 'Light',
135            ],
136            (object) [
137                'bingMapsKey' => $api_key,
138                'default'     => false,
139                'imagerySet'  => 'Road',
140                'label'       => 'Road',
141            ],
142        ];
143    }
144}
145