2015-01-21 14:24:22 +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-21 14:24:22 +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-21 14:24:22 +01:00
|
|
|
* @filesource
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
|
|
|
|
|
|
2017-10-11 14:27:37 +02:00
|
|
|
use Contao\Model;
|
2017-10-11 14:53:04 +02:00
|
|
|
use Netzmacht\Contao\Leaflet\Encoder\ContaoAssets;
|
2015-01-21 14:24:22 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
|
2017-10-17 18:03:42 +02:00
|
|
|
use Netzmacht\Contao\Leaflet\Mapper\Request;
|
2015-01-21 14:24:22 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Model\LayerModel;
|
2017-10-19 08:45:39 +02:00
|
|
|
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
|
2015-01-21 14:24:22 +01:00
|
|
|
use Netzmacht\JavascriptBuilder\Type\AnonymousFunction;
|
|
|
|
|
use Netzmacht\JavascriptBuilder\Type\Expression;
|
|
|
|
|
use Netzmacht\LeafletPHP\Definition;
|
|
|
|
|
use Netzmacht\LeafletPHP\Definition\Layer;
|
|
|
|
|
use Netzmacht\LeafletPHP\Plugins\MarkerCluster\MarkerClusterGroup;
|
|
|
|
|
use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class MarkerClusterLayerMapper maps the layer database model to the marker cluster definition.
|
|
|
|
|
*
|
|
|
|
|
* @package Netzmacht\Contao\Leaflet\Mapper\Layer
|
|
|
|
|
*/
|
|
|
|
|
class MarkerClusterLayerMapper extends AbstractLayerMapper
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Class of the definition being created.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2017-10-17 13:51:09 +02:00
|
|
|
protected static $definitionClass = MarkerClusterGroup::class;
|
2015-01-21 14:24:22 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Layer type.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected static $type = 'markercluster';
|
|
|
|
|
|
2016-10-06 09:02:12 +02:00
|
|
|
/**
|
|
|
|
|
* Assets manager.
|
|
|
|
|
*
|
2016-10-06 15:47:33 +02:00
|
|
|
* @var ContaoAssets
|
2016-10-06 09:02:12 +02:00
|
|
|
*/
|
2016-10-06 15:47:33 +02:00
|
|
|
private $assets;
|
2016-10-06 09:02:12 +02:00
|
|
|
|
|
|
|
|
/**
|
2017-10-19 08:45:39 +02:00
|
|
|
* Repository manager.
|
2016-10-06 09:02:12 +02:00
|
|
|
*
|
2017-10-19 08:45:39 +02:00
|
|
|
* @var RepositoryManager
|
2016-10-06 09:02:12 +02:00
|
|
|
*/
|
2017-10-19 08:45:39 +02:00
|
|
|
private $repositoryManager;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct.
|
|
|
|
|
*
|
|
|
|
|
* @param ContaoAssets $assets Assets manager.
|
|
|
|
|
* @param RepositoryManager $repositoryManager Repository manager.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(ContaoAssets $assets, RepositoryManager $repositoryManager)
|
2016-10-06 09:02:12 +02:00
|
|
|
{
|
2017-10-19 08:45:39 +02:00
|
|
|
$this->repositoryManager = $repositoryManager;
|
|
|
|
|
$this->assets = $assets;
|
2016-10-06 09:02:12 +02:00
|
|
|
|
2017-10-19 08:45:39 +02:00
|
|
|
parent::__construct();
|
2016-10-06 09:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-21 14:24:22 +01:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
protected function initialize()
|
|
|
|
|
{
|
|
|
|
|
parent::initialize();
|
|
|
|
|
|
2015-01-22 09:02:14 +01:00
|
|
|
$this->optionsBuilder
|
2015-01-21 14:24:22 +01:00
|
|
|
->addOptions('showCoverageOnHover', 'zoomToBoundsOnClick', 'spiderfyOnMaxZoom')
|
|
|
|
|
->addOption('removeOutsideVisibleBounds')
|
|
|
|
|
->addConditionalOption('maxClusterRadius')
|
|
|
|
|
->addConditionalOption('singleMarkerMode')
|
|
|
|
|
->addConditionalOption('animateAddingMarkers')
|
|
|
|
|
->addConditionalOption('disableClusteringAtZoom')
|
|
|
|
|
->addConditionalOption('spiderfyDistanceMultiplier');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
protected function build(
|
|
|
|
|
Definition $definition,
|
2017-10-11 14:27:37 +02:00
|
|
|
Model $model,
|
2015-01-21 14:24:22 +01:00
|
|
|
DefinitionMapper $mapper,
|
2017-10-11 14:27:37 +02:00
|
|
|
Request $request = null,
|
2015-01-21 14:24:22 +01:00
|
|
|
Definition $parent = null
|
|
|
|
|
) {
|
2017-10-11 14:27:37 +02:00
|
|
|
parent::build($definition, $model, $mapper, $request, $parent);
|
2015-01-21 14:24:22 +01:00
|
|
|
|
|
|
|
|
/** @var MarkerClusterGroup $definition */
|
|
|
|
|
|
|
|
|
|
if ($model->iconCreateFunction) {
|
|
|
|
|
$definition->setIconCreateFunction(new Expression($model->iconCreateFunction));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($model->polygonOptions) {
|
|
|
|
|
$definition->setPolygonOptions((array) json_decode($model->polygonOptions, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$model->disableDefaultStyle) {
|
2016-10-06 15:47:33 +02:00
|
|
|
$this->assets->addStylesheet('assets/leaflet/libs/leaflet-markercluster/MarkerCluster.Default.css');
|
2015-01-21 14:24:22 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-19 08:45:39 +02:00
|
|
|
$repository = $this->repositoryManager->getRepository(LayerModel::class);
|
|
|
|
|
$collection = $repository->findBy(
|
2017-10-17 18:11:28 +02:00
|
|
|
['pid=?', 'active=1'],
|
|
|
|
|
[$model->id],
|
|
|
|
|
['order' => 'sorting']
|
2015-01-21 14:24:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
foreach ($collection as $layerModel) {
|
|
|
|
|
$layer = $mapper->handle($layerModel);
|
|
|
|
|
|
|
|
|
|
if ($layer instanceof Layer) {
|
|
|
|
|
$definition->addLayer($layer);
|
|
|
|
|
|
|
|
|
|
if ($layer instanceof OmnivoreLayer) {
|
|
|
|
|
$callback = new AnonymousFunction();
|
|
|
|
|
$callback->addLine('layers.' . $definition->getId() . '.addLayers(this.getLayers())');
|
|
|
|
|
|
|
|
|
|
$layer->on('ready', $callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|