2020-05-30 20:39:15 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
2020-05-30 15:37:26 +02:00
|
|
|
|
|
|
|
use MapGuesser\Database\Query\Select;
|
|
|
|
use MapGuesser\Database\RawExpression;
|
2020-06-10 00:30:29 +02:00
|
|
|
use MapGuesser\Interfaces\Authentication\IUser;
|
2020-05-30 15:37:26 +02:00
|
|
|
use MapGuesser\Interfaces\Database\IResultSet;
|
2020-06-10 00:30:29 +02:00
|
|
|
use MapGuesser\Interfaces\Request\IRequest;
|
2020-05-31 20:43:14 +02:00
|
|
|
use MapGuesser\Interfaces\Response\IContent;
|
|
|
|
use MapGuesser\Response\HtmlContent;
|
2020-05-30 15:37:26 +02:00
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
class MapsController
|
2020-05-30 15:37:26 +02:00
|
|
|
{
|
2020-06-10 00:30:29 +02:00
|
|
|
private IRequest $request;
|
|
|
|
|
|
|
|
public function __construct(IRequest $request)
|
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
public function getMaps(): IContent
|
2020-05-30 15:37:26 +02:00
|
|
|
{
|
2020-07-05 15:17:28 +02:00
|
|
|
//TODO: from repository - count should be added
|
2020-05-30 15:37:26 +02:00
|
|
|
$select = new Select(\Container::$dbConnection, 'maps');
|
|
|
|
$select->columns([
|
|
|
|
['maps', 'id'],
|
|
|
|
['maps', 'name'],
|
|
|
|
['maps', 'description'],
|
|
|
|
['maps', 'bound_south_lat'],
|
|
|
|
['maps', 'bound_west_lng'],
|
|
|
|
['maps', 'bound_north_lat'],
|
|
|
|
['maps', 'bound_east_lng'],
|
2020-06-12 21:55:04 +02:00
|
|
|
['maps', 'area'],
|
2020-05-30 15:37:26 +02:00
|
|
|
new RawExpression('COUNT(places.id) AS num_places')
|
|
|
|
]);
|
|
|
|
$select->leftJoin('places', ['places', 'map_id'], '=', ['maps', 'id']);
|
2020-06-10 23:11:55 +02:00
|
|
|
$select->groupBy(['maps', 'id']);
|
2020-05-30 15:37:26 +02:00
|
|
|
$select->orderBy('name');
|
|
|
|
|
|
|
|
$result = $select->execute();
|
|
|
|
|
|
|
|
$maps = [];
|
|
|
|
while ($map = $result->fetch(IResultSet::FETCH_ASSOC)) {
|
2020-06-12 21:55:04 +02:00
|
|
|
$map['area'] = $this->formatMapAreaForHuman($map['area']);
|
2020-05-30 15:37:26 +02:00
|
|
|
|
|
|
|
$maps[] = $map;
|
|
|
|
}
|
|
|
|
|
2020-06-10 00:30:29 +02:00
|
|
|
$user = $this->request->user();
|
2020-07-05 00:58:03 +02:00
|
|
|
return new HtmlContent('maps', [
|
|
|
|
'maps' => $maps,
|
2021-05-19 17:11:49 +02:00
|
|
|
'isLoggedIn' => $user !== null,
|
2020-07-05 00:58:03 +02:00
|
|
|
'isAdmin' => $user !== null && $user->hasPermission(IUser::PERMISSION_ADMIN)
|
|
|
|
]);
|
2020-05-30 15:37:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 20:39:15 +02:00
|
|
|
private function formatMapAreaForHuman(float $area): array
|
2020-05-30 15:37:26 +02:00
|
|
|
{
|
2020-06-12 21:55:04 +02:00
|
|
|
if ($area < 0.01) {
|
2020-05-30 20:39:15 +02:00
|
|
|
$digits = 0;
|
2020-06-12 21:55:04 +02:00
|
|
|
$rounded = round($area * 1000000.0, -2);
|
2020-05-30 20:39:15 +02:00
|
|
|
$unit = 'm';
|
2020-06-12 21:55:04 +02:00
|
|
|
} elseif ($area < 0.1) {
|
2020-06-10 23:51:09 +02:00
|
|
|
$digits = 0;
|
2020-06-12 21:55:04 +02:00
|
|
|
$rounded = round($area * 1000000.0, -3);
|
2020-06-10 23:51:09 +02:00
|
|
|
$unit = 'm';
|
2020-06-12 21:55:04 +02:00
|
|
|
} elseif ($area < 1.0) {
|
2020-06-10 23:51:09 +02:00
|
|
|
$digits = 2;
|
2020-06-12 21:55:04 +02:00
|
|
|
$rounded = round($area, 2);
|
2020-06-10 23:51:09 +02:00
|
|
|
$unit = 'km';
|
2020-06-12 21:55:04 +02:00
|
|
|
} elseif ($area < 100.0) {
|
2020-05-30 20:39:15 +02:00
|
|
|
$digits = 0;
|
2020-06-12 21:55:04 +02:00
|
|
|
$rounded = round($area, 0);
|
2020-05-30 20:39:15 +02:00
|
|
|
$unit = 'km';
|
2020-06-12 21:55:04 +02:00
|
|
|
} elseif ($area < 10000.0) {
|
2020-05-30 20:39:15 +02:00
|
|
|
$digits = 0;
|
2020-06-12 21:55:04 +02:00
|
|
|
$rounded = round($area, -2);
|
2020-05-30 20:39:15 +02:00
|
|
|
$unit = 'km';
|
2020-05-30 15:37:26 +02:00
|
|
|
} else {
|
2020-05-30 20:39:15 +02:00
|
|
|
$digits = 0;
|
2020-06-12 21:55:04 +02:00
|
|
|
$rounded = round($area, -4);
|
2020-05-30 20:39:15 +02:00
|
|
|
$unit = 'km';
|
2020-05-30 15:37:26 +02:00
|
|
|
}
|
2020-05-30 20:39:15 +02:00
|
|
|
|
|
|
|
return [number_format($rounded, $digits, '.', ' '), $unit];
|
2020-05-30 15:37:26 +02:00
|
|
|
}
|
|
|
|
}
|