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\Mapper\Layer;
|
|
|
|
|
|
2017-10-11 14:27:37 +02:00
|
|
|
use Contao\Model;
|
2015-01-06 14:55:53 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Model\MarkerModel;
|
2015-01-10 15:33:46 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
|
2017-10-11 14:27:37 +02:00
|
|
|
use Netzmacht\Contao\Leaflet\Request\Request;
|
2015-01-16 15:02:20 +01:00
|
|
|
use Netzmacht\JavascriptBuilder\Type\Expression;
|
2015-01-06 14:55:53 +01:00
|
|
|
use Netzmacht\LeafletPHP\Definition;
|
2015-01-27 14:52:20 +01:00
|
|
|
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
|
2015-01-06 18:49:22 +01:00
|
|
|
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
|
2015-01-06 14:55:53 +01:00
|
|
|
|
2015-01-12 19:03:29 +01:00
|
|
|
/**
|
|
|
|
|
* Class MarkersLayerMapper maps the layer model to the markers definition.
|
|
|
|
|
*
|
|
|
|
|
* @package Netzmacht\Contao\Leaflet\Mapper\Layer
|
|
|
|
|
*/
|
2015-01-06 14:55:53 +01:00
|
|
|
class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Layer type.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected static $type = 'markers';
|
|
|
|
|
|
2015-01-06 15:43:57 +01:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
2017-10-11 14:27:37 +02:00
|
|
|
protected function getClassName(Model $model, DefinitionMapper $mapper, Request $request = null)
|
2015-01-06 15:43:57 +01:00
|
|
|
{
|
|
|
|
|
if ($model->deferred) {
|
2015-01-13 14:54:23 +01:00
|
|
|
return 'Netzmacht\LeafletPHP\Plugins\Omnivore\GeoJson';
|
2015-01-06 15:43:57 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-22 11:59:19 +01:00
|
|
|
return 'Netzmacht\LeafletPHP\Definition\Group\GeoJson';
|
2015-01-06 15:43:57 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-13 14:54:23 +01:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
protected function buildConstructArguments(
|
2017-10-11 14:27:37 +02:00
|
|
|
Model $model,
|
2015-01-13 14:54:23 +01:00
|
|
|
DefinitionMapper $mapper,
|
2017-10-11 14:27:37 +02:00
|
|
|
Request $request = null,
|
2015-01-13 14:54:23 +01:00
|
|
|
$elementId = null
|
|
|
|
|
) {
|
|
|
|
|
if ($model->deferred) {
|
2015-01-27 00:02:17 +01:00
|
|
|
if ($model->pointToLayer || $model->boundsMode) {
|
2015-01-18 23:19:52 +01:00
|
|
|
$layer = new GeoJson($this->getElementId($model, $elementId));
|
2015-01-21 20:37:53 +01:00
|
|
|
|
2015-01-22 13:34:22 +01:00
|
|
|
if ($model->pointToLayer) {
|
2015-01-21 20:37:53 +01:00
|
|
|
$layer->setPointToLayer(new Expression($model->pointToLayer));
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 00:02:17 +01:00
|
|
|
if ($model->boundsMode) {
|
|
|
|
|
$layer->setOption('boundsMode', $model->boundsMode);
|
2015-01-21 20:37:53 +01:00
|
|
|
}
|
2015-01-13 14:54:23 +01:00
|
|
|
|
2015-01-27 00:02:17 +01:00
|
|
|
return array(
|
|
|
|
|
$this->getElementId($model, $elementId),
|
2017-10-11 14:27:37 +02:00
|
|
|
RequestUrl::create($model->id, null, null, $request),
|
2015-01-27 00:02:17 +01:00
|
|
|
array(),
|
|
|
|
|
$layer
|
|
|
|
|
);
|
2015-01-13 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-27 00:02:17 +01:00
|
|
|
return array(
|
|
|
|
|
$this->getElementId($model, $elementId),
|
2017-10-11 14:27:37 +02:00
|
|
|
RequestUrl::create($model->id, null, null, $request)
|
2015-01-27 00:02:17 +01:00
|
|
|
);
|
2015-01-13 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-11 14:27:37 +02:00
|
|
|
return parent::buildConstructArguments($model, $mapper, $request, $elementId);
|
2015-01-13 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 15:43:57 +01:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
2015-01-08 09:51:43 +01:00
|
|
|
protected function build(
|
2015-01-06 14:55:53 +01:00
|
|
|
Definition $definition,
|
2017-10-11 14:27:37 +02:00
|
|
|
Model $model,
|
2015-01-06 18:49:22 +01:00
|
|
|
DefinitionMapper $mapper,
|
2017-10-11 14:27:37 +02:00
|
|
|
Request $request = null,
|
2015-01-20 16:38:23 +01:00
|
|
|
Definition $parent = null
|
2015-01-06 14:55:53 +01:00
|
|
|
) {
|
2015-01-15 12:52:00 +01:00
|
|
|
if ($definition instanceof GeoJson) {
|
2015-01-27 00:02:17 +01:00
|
|
|
if ($model->boundsMode) {
|
|
|
|
|
$definition->setOption('boundsMode', $model->boundsMode);
|
2015-01-21 20:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 15:43:57 +01:00
|
|
|
$collection = $this->loadMarkerModels($model);
|
2015-01-06 14:55:53 +01:00
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
foreach ($collection as $item) {
|
2015-01-18 23:19:52 +01:00
|
|
|
$marker = $mapper->handle($item);
|
2015-01-23 13:04:04 +01:00
|
|
|
$point = $mapper->convertToGeoJsonFeature($marker, $item);
|
2015-01-18 23:19:52 +01:00
|
|
|
|
2015-01-23 13:04:04 +01:00
|
|
|
if ($point) {
|
2015-01-27 11:43:19 +01:00
|
|
|
$definition->addData($point, true);
|
2015-01-18 23:19:52 +01:00
|
|
|
}
|
2015-01-06 14:55:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
2015-01-06 18:49:22 +01:00
|
|
|
|
2015-01-09 16:10:17 +01:00
|
|
|
if ($model->pointToLayer) {
|
|
|
|
|
$definition->setPointToLayer(new Expression($model->pointToLayer));
|
|
|
|
|
}
|
2015-01-06 18:49:22 +01:00
|
|
|
}
|
2015-01-06 14:55:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-12 19:03:29 +01:00
|
|
|
* {@inheritdoc}
|
2015-01-06 14:55:53 +01:00
|
|
|
*/
|
2017-10-11 14:27:37 +02:00
|
|
|
public function handleGeoJson(Model $model, DefinitionMapper $mapper, Request $request = null)
|
2015-01-06 14:55:53 +01:00
|
|
|
{
|
2015-01-06 15:43:57 +01:00
|
|
|
$feature = new FeatureCollection();
|
2017-10-11 14:27:37 +02:00
|
|
|
$collection = $this->loadMarkerModels($model, $request);
|
2015-01-06 14:55:53 +01:00
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
foreach ($collection as $item) {
|
2015-01-12 10:45:05 +01:00
|
|
|
$marker = $mapper->handle($item);
|
2015-01-23 13:04:04 +01:00
|
|
|
$point = $mapper->convertToGeoJsonFeature($marker, $item);
|
2015-01-12 10:45:05 +01:00
|
|
|
|
2015-01-23 13:04:04 +01:00
|
|
|
if ($point) {
|
2015-01-21 20:52:12 +01:00
|
|
|
$feature->addFeature($point);
|
2015-01-12 10:45:05 +01:00
|
|
|
}
|
2015-01-06 14:55:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $feature;
|
|
|
|
|
}
|
2015-01-06 15:43:57 +01:00
|
|
|
|
|
|
|
|
/**
|
2015-01-12 19:03:29 +01:00
|
|
|
* Load all layer markers.
|
|
|
|
|
*
|
2017-10-11 14:27:37 +02:00
|
|
|
* @param Model $model The layer model.
|
|
|
|
|
* @param Request $request Optional building request.
|
2015-01-06 15:43:57 +01:00
|
|
|
*
|
|
|
|
|
* @return \Model\Collection|null
|
|
|
|
|
*/
|
2017-10-11 14:27:37 +02:00
|
|
|
protected function loadMarkerModels(Model $model, Request $request = null)
|
2015-01-06 15:43:57 +01:00
|
|
|
{
|
2015-01-27 00:02:17 +01:00
|
|
|
if ($model->boundsMode == 'fit') {
|
2017-10-11 14:27:37 +02:00
|
|
|
return MarkerModel::findByFilter($model->id, $request->getFilter());
|
2015-01-27 00:02:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MarkerModel::findByFilter($model->id);
|
2015-01-06 15:43:57 +01:00
|
|
|
}
|
2015-01-06 14:55:53 +01:00
|
|
|
}
|