Files
contao-leaflet-maps/src/Model/MarkerModel.php

137 lines
3.8 KiB
PHP
Raw Normal View History

2015-01-06 14:55:53 +01:00
<?php
/**
2017-10-05 15:45:43 +02:00
* Leaflet maps for Contao CMS.
*
2016-10-11 10:40:15 +02:00
* @package contao-leaflet-maps
2015-01-06 14:55:53 +01:00
* @author David Molineus <david.molineus@netzmacht.de>
2017-10-11 15:00:48 +02:00
* @copyright 2014-2017 netzmacht David Molineus. All rights reserved.
2017-10-05 15:45:43 +02:00
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
2015-01-06 14:55:53 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Model;
use Contao\Model\Collection;
2015-01-27 00:02:17 +01:00
use Netzmacht\Contao\Leaflet\Filter\BboxFilter;
use Netzmacht\Contao\Leaflet\Filter\DistanceFilter;
2015-01-27 00:02:17 +01:00
use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\LeafletPHP\Value\LatLngBounds;
use function var_dump;
2015-01-27 00:02:17 +01:00
2015-01-12 19:03:29 +01:00
/**
* Class MarkerModel for the tl_leaflet_marker table.
*
* @package Netzmacht\Contao\Leaflet\Model
*/
2015-01-12 09:32:21 +01:00
class MarkerModel extends AbstractActiveModel
2015-01-06 14:55:53 +01:00
{
2015-01-12 19:03:29 +01:00
/**
* Model table.
*
* @var string
*/
2015-01-06 14:55:53 +01:00
protected static $strTable = 'tl_leaflet_marker';
2015-01-27 00:02:17 +01:00
/**
2015-01-27 17:58:03 +01:00
* Find by a filter.
*
* @param int $pid The parent id.
* @param Filter|null $filter The filter.
2015-01-27 00:02:17 +01:00
*
* @return Collection|null
2015-01-27 00:02:17 +01:00
*/
public static function findByFilter($pid, ?Filter $filter = null)
2015-01-27 00:02:17 +01:00
{
if (!$filter) {
2018-12-07 11:22:16 +01:00
$table = static::getTable();
return static::findBy(
[
$table . '.active=1',
$table . '.pid=?',
$table . '.latitude>0',
$table . '.longitude>0',
],
[$pid],
['order' => 'sorting']
);
2015-01-27 00:02:17 +01:00
}
switch (true) {
case $filter instanceof BboxFilter:
2015-01-27 00:02:17 +01:00
return static::findByBBoxFilter($pid, $filter);
case $filter instanceof DistanceFilter:
return static::findByDistanceFilter($pid, $filter);
2015-01-27 00:02:17 +01:00
default:
return null;
}
}
/**
2015-01-27 17:58:03 +01:00
* Find by the bbox filter.
*
* @param int $pid The layer id.
* @param BboxFilter $filter The bbox filter.
2015-01-27 00:02:17 +01:00
*
* @return Collection|null|MarkerModel[]
2015-01-27 00:02:17 +01:00
*/
public static function findByBBoxFilter($pid, BboxFilter $filter)
{
$table = static::getTable();
$columns = [
$table . '.active=1',
$table . '.pid=?',
$table . '.latitude > ? AND ' . $table . '.latitude < ?',
$table . '.longitude > ? AND ' . $table . '.longitude < ?',
];
2015-01-27 00:02:17 +01:00
/** @var LatLngBounds $bounds */
$bounds = $filter->getValues()['bounds'];
$values = [
2015-01-27 00:02:17 +01:00
$pid,
$bounds->getSouthWest()->getLatitude(),
$bounds->getNorthEast()->getLatitude(),
$bounds->getSouthWest()->getLongitude(),
$bounds->getNorthEast()->getLongitude(),
];
2015-01-27 00:02:17 +01:00
return static::findBy($columns, $values, ['order' => $table . '.sorting']);
}
/**
* Find marker by distance filter.
*
* @param int $pid The layer id.
* @param DistanceFilter $filter THe distance filter.
*
* @return Collection|null|MarkerModel[]
*/
public static function findByDistanceFilter($pid, DistanceFilter $filter): ?Collection
{
$table = static::getTable();
$query = <<<SQL
round(
sqrt(
power( 2 * pi() / 360 * (? - {$table}.latitude) * 6371,2)
+ power( 2 * pi() / 360 * (? - {$table}.longitude) * 6371 * COS( 2 * pi() / 360 * (? + {$table}.latitude) * 0.5 ),2)
)
) <= ?
SQL;
$center = $filter->getCenter();
$latitude = $center->getLatitude();
$longitude = $center->getLongitude();
$values = [$pid, $latitude, $longitude, $latitude, $filter->getRadius()];
$columns = [
$table . '.active=1',
$table . '.pid=?',
$query
];
return static::findBy($columns, $values, ['order' => $table . '.sorting']);
2015-01-27 00:02:17 +01:00
}
2015-01-06 14:55:53 +01:00
}