85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php namespace MapGuesser\Controller;
|
|
|
|
use SokoWeb\Database\Query\Select;
|
|
use SokoWeb\Database\RawExpression;
|
|
use SokoWeb\Interfaces\Authentication\IUser;
|
|
use SokoWeb\Interfaces\Database\IResultSet;
|
|
use SokoWeb\Interfaces\Response\IContent;
|
|
use SokoWeb\Response\HtmlContent;
|
|
|
|
class MapsController
|
|
{
|
|
public function getMaps(): IContent
|
|
{
|
|
//TODO: from repository - count should be added
|
|
$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'],
|
|
['maps', 'area'],
|
|
['maps', 'unlisted'],
|
|
new RawExpression('COUNT(places.id) AS num_places')
|
|
]);
|
|
$select->leftJoin('places', ['places', 'map_id'], '=', ['maps', 'id']);
|
|
$select->groupBy(['maps', 'id']);
|
|
$select->orderBy('name');
|
|
|
|
$user = \Container::$request->user();
|
|
$isAdmin = $user !== null && $user->hasPermission(IUser::PERMISSION_ADMIN);
|
|
if (!$isAdmin) {
|
|
$select->where(['maps', 'unlisted'], '=', false);
|
|
}
|
|
|
|
$result = $select->execute();
|
|
|
|
$maps = [];
|
|
while ($map = $result->fetch(IResultSet::FETCH_ASSOC)) {
|
|
$map['area'] = $this->formatMapAreaForHuman($map['area']);
|
|
|
|
$maps[] = $map;
|
|
}
|
|
|
|
return new HtmlContent('maps', [
|
|
'maps' => $maps,
|
|
'isLoggedIn' => $user !== null,
|
|
'isAdmin' => $isAdmin
|
|
]);
|
|
}
|
|
|
|
private function formatMapAreaForHuman(float $area): array
|
|
{
|
|
if ($area < 0.01) {
|
|
$digits = 0;
|
|
$rounded = round($area * 1000000.0, -2);
|
|
$unit = 'm';
|
|
} elseif ($area < 0.1) {
|
|
$digits = 0;
|
|
$rounded = round($area * 1000000.0, -3);
|
|
$unit = 'm';
|
|
} elseif ($area < 1.0) {
|
|
$digits = 2;
|
|
$rounded = round($area, 2);
|
|
$unit = 'km';
|
|
} elseif ($area < 100.0) {
|
|
$digits = 0;
|
|
$rounded = round($area, 0);
|
|
$unit = 'km';
|
|
} elseif ($area < 10000.0) {
|
|
$digits = 0;
|
|
$rounded = round($area, -2);
|
|
$unit = 'km';
|
|
} else {
|
|
$digits = 0;
|
|
$rounded = round($area, -4);
|
|
$unit = 'km';
|
|
}
|
|
|
|
return [number_format($rounded, $digits, '.', ' '), $unit];
|
|
}
|
|
}
|