-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMapsController.php
More file actions
267 lines (241 loc) · 7.97 KB
/
MapsController.php
File metadata and controls
267 lines (241 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* Copyright © 2003-2025 The Galette Team
*
* This file is part of Galette (https://galette.eu).
*
* Galette is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Galette is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace GaletteMaps\Controllers;
use DI\Attribute\Inject;
use Galette\Controllers\AbstractPluginController;
use Galette\Entity\Adherent;
use GaletteMaps\NominatimTowns;
use GaletteMaps\Coordinates;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Analog\Analog;
/**
* Galette maps controller
*
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
class MapsController extends AbstractPluginController
{
/**
* @var array<string, mixed>
*/
#[Inject("Plugin Galette Maps")]
protected array $module_info;
/**
* Main route
*
* @param Request $request PSR Request
* @param Response $response PSR Response
*
* @return Response
*/
public function map(Request $request, Response $response): Response
{
$coords = new Coordinates();
$list = $coords->listCoords();
$params = [
'require_dialog' => true,
'page_title' => _T('Maps', 'maps'),
'module_id' => $this->getModuleId()
];
if ($list !== false) {
$params['list'] = $list;
} else {
$this->flash->addMessage(
'error_detected',
_T('Coordinates has not been loaded. Maybe plugin tables does not exists in the database?', 'maps')
);
}
// display page
$this->view->render(
$response,
$this->getTemplate('maps'),
$params
);
return $response;
}
/**
* Member localization
*
* @param Request $request PSR Request
* @param Response $response PSR Response
* @param ?int $id Member ID
*
* @return Response
*/
public function localizeMember(Request $request, Response $response, ?int $id = null): Response
{
if ($id === null) {
$id = (int)$this->login->id;
}
$deps = [
'picture' => false,
'groups' => false,
'dues' => false
];
$member = new Adherent($this->zdb, $id, $deps);
if (
$this->login->id != $id
&& !$this->login->isAdmin()
&& !$this->login->isStaff()
&& $this->login->isGroupManager()
) {
//check if requested member is part of managed groups
$groups = $member->groups;
$is_managed = false;
foreach ($groups as $g) {
if ($this->login->isGroupManager($g->getId())) {
$is_managed = true;
break;
}
}
if ($is_managed !== true) {
//requested member is not part of managed groups, fall back to logged
//in member
//FIXME: silent fallback is maybe not the best to do
$member->load($this->login->id);
}
}
$coords = new Coordinates();
$mcoords = $coords->getCoords($member->id);
$towns = false;
if (count($mcoords) === 0) {
if ($member->town != '') {
$t = new NominatimTowns($this->preferences);
$towns = $t->search(
$member->town,
$member->country
);
}
}
$params = [
'page_title' => _T('Maps', 'maps') . ' - ' . str_replace(
'%member',
$member->sfullname,
_T('%member geographic position', 'maps')
),
'member' => $member,
'require_dialog' => true,
'adh_map' => true,
'module_id' => $this->getModuleId()
];
if ($towns !== false) {
$params['towns'] = $towns;
} elseif (count($mcoords) > 0) {
$params['town'] = $mcoords;
}
if ($member->login == $this->login->login) {
$params['mymap'] = true;
}
// display page
$this->view->render(
$response,
$this->getTemplate('mymap'),
$params
);
return $response;
}
/**
* Change member localization
*
* @param Request $request PSR Request
* @param Response $response PSR Response
* @param ?int $id Member ID
*
* @return Response
*/
public function ILiveHere(Request $request, Response $response, ?int $id = null): Response
{
$error = null;
$message = null;
if ($id === null && $this->login->isSuperAdmin()) {
Analog::log(
'SuperAdmin does not live anywhere!',
Analog::INFO
);
$error = _T('Superadmin cannot be localized.', 'maps');
} elseif ($id === null) {
$member = new Adherent($this->zdb, $this->login->login);
$id = $member->id;
} elseif (
!$this->login->isSuperAdmin()
&& !$this->login->isAdmin()
&& !$this->login->isStaff()
&& $this->login->isGroupManager()
) {
$member = new Adherent($this->zdb, $id);
//check if current logged-in user can manage loaded member
$groups = $member->groups;
$can_manage = false;
foreach ($groups as $group) {
if ($this->login->isGroupManager($group->getId())) {
$can_manage = true;
break;
}
}
if ($can_manage !== true) {
Analog::log(
'Logged in member ' . $this->login->login
. ' has tried to load member #' . $id
. ' but do not manage any groups he belongs to.',
Analog::WARNING
);
$error = _T('Coordinates has not been removed :(', 'maps');
}
}
if ($error === null) {
$post = $request->getParsedBody();
$coords = new Coordinates();
if (isset($post['remove'])) {
$res = $coords->removeCoords($id);
if ($res > 0) {
$message = _T('Coordinates has been removed!', 'maps');
} else {
$error = _T('Coordinates has not been removed :(', 'maps');
}
} elseif (
isset($post['latitude'])
&& isset($post['longitude'])
) {
$res = $coords->setCoords(
$id,
(float)$post['latitude'],
(float)$post['longitude']
);
if ($res === true) {
$message = _T('New coordinates has been stored!', 'maps');
} else {
$error = _T('Coordinates has not been stored :(', 'maps');
}
} else {
$error = _T('Something went wrong :(', 'maps');
}
}
$response = $response->withHeader('Content-type', 'application/json');
$res = [
'res' => $error === null,
'message' => ($error ?? $message)
];
$body = $response->getBody();
$body->write(json_encode($res));
return $response;
}
}