Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions lib/GaletteMaps/Controllers/MapsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class MapsController extends AbstractPluginController
#[Inject("Plugin Galette Maps")]
protected array $module_info;

#[Inject]
protected Coordinates $coordinates;

#[Inject]
protected NominatimTowns $nominatim;

/**
* Member dependencies to load; groups are loaded on demand by access checks
*
Expand Down Expand Up @@ -78,9 +84,9 @@ public function map(Request $request, Response $response): Response
];

try {
$params['list'] = (new Coordinates())->listCoords();
$params['list'] = $this->coordinates->listVisible();
} catch (\Throwable $e) {
//already logged
Analog::log('Unable to list coordinates | ' . $e->getMessage(), Analog::ERROR);
$this->flash->addMessageNow(
'error_detected',
_T('Coordinates has not been loaded. Maybe plugin tables does not exists in the database?', 'maps')
Expand Down Expand Up @@ -137,14 +143,13 @@ public function localizeMember(Request $request, Response $response, ?int $id =
}
$can_edit = $member->canEdit($this->login);

$coords = new Coordinates();
$mcoords = $coords->getCoords($member->id);
$mcoords = $this->coordinates->get($member->id);

$towns = false;
//towns are only proposed to choose a location
if ($can_edit && count($mcoords) === 0 && trim($member->town ?? '') !== '') {
if ($can_edit && $mcoords === null && trim($member->town ?? '') !== '') {
try {
$towns = (new NominatimTowns($this->preferences))->search(
$towns = $this->nominatim->search(
$member->town,
$member->country
);
Expand Down Expand Up @@ -175,7 +180,7 @@ public function localizeMember(Request $request, Response $response, ?int $id =

if ($towns !== false) {
$params['towns'] = $towns;
} elseif (count($mcoords) > 0) {
} elseif ($mcoords !== null) {
$params['town'] = $mcoords;
}

Expand Down Expand Up @@ -319,11 +324,15 @@ public function ILiveHere(Request $request, Response $response, ?int $id = null)

if ($error === null) {
$post = $request->getParsedBody();
$coords = new Coordinates();
if (isset($post['remove'])) {
if ($coords->removeCoords($id)) {
try {
$this->coordinates->remove($id);
$message = _T('Coordinates has been removed!', 'maps');
} else {
} catch (\Throwable $e) {
Analog::log(
'Unable to remove coordinates of member #' . $id . ' | ' . $e->getMessage(),
Analog::ERROR
);
$error = _T('Coordinates has not been removed :(', 'maps');
$status = 500;
}
Expand All @@ -338,27 +347,29 @@ public function ILiveHere(Request $request, Response $response, ?int $id = null)
) {
$error = _T('Invalid coordinates.', 'maps');
$status = 400;
} elseif ($coords->setCoords($id, $latitude, $longitude)) {
$message = _T('New coordinates has been stored!', 'maps');
} else {
$error = _T('Coordinates has not been stored :(', 'maps');
$status = 500;
try {
$this->coordinates->set($id, $latitude, $longitude);
$message = _T('New coordinates has been stored!', 'maps');
} catch (\Throwable $e) {
Analog::log(
'Unable to store coordinates of member #' . $id . ' | ' . $e->getMessage(),
Analog::ERROR
);
$error = _T('Coordinates has not been stored :(', 'maps');
$status = 500;
}
}
}
}

$response = $response
->withStatus($status)
->withHeader('Content-type', 'application/json');

$res = [
'res' => $error === null,
'message' => ($error ?? $message)
];

$body = $response->getBody();
$body->write(json_encode($res));

return $response;
return $this->withJson(
$response,
[
'res' => $error === null,
'message' => ($error ?? $message)
],
$status
);
}
}
Loading
Loading