2015-01-07 17:59:56 +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-07 17:59:56 +01:00
|
|
|
* @author David Molineus <david.molineus@netzmacht.de>
|
2017-10-05 15:45:43 +02:00
|
|
|
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
|
|
|
|
|
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
|
2015-01-07 17:59:56 +01:00
|
|
|
* @filesource
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
|
|
|
|
|
|
2015-01-24 21:37:25 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Filter\Filter;
|
2015-01-07 17:59:56 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Model\VectorModel;
|
2015-01-10 15:33:46 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
|
2015-01-16 15:02:20 +01:00
|
|
|
use Netzmacht\JavascriptBuilder\Type\Expression;
|
2015-01-07 17:59:56 +01:00
|
|
|
use Netzmacht\LeafletPHP\Definition;
|
2015-01-27 14:52:20 +01:00
|
|
|
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
|
2015-01-07 17:59:56 +01:00
|
|
|
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
|
2015-01-12 10:45:05 +01:00
|
|
|
use Netzmacht\LeafletPHP\Definition\Vector;
|
2015-01-07 17:59:56 +01:00
|
|
|
|
2015-01-12 19:03:29 +01:00
|
|
|
/**
|
|
|
|
|
* Class VectorsLayerMapper maps the layer model for the Vectors layer definition.
|
|
|
|
|
*
|
|
|
|
|
* @package Netzmacht\Contao\Leaflet\Mapper\Layer
|
|
|
|
|
*/
|
2015-01-07 17:59:56 +01:00
|
|
|
class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Layer type.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected static $type = 'vectors';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
2015-01-24 21:37:25 +01:00
|
|
|
protected function getClassName(\Model $model, DefinitionMapper $mapper, Filter $filter = null)
|
2015-01-07 17:59:56 +01:00
|
|
|
{
|
|
|
|
|
if ($model->deferred) {
|
2015-01-13 14:54:23 +01:00
|
|
|
return 'Netzmacht\LeafletPHP\Plugins\Omnivore\GeoJson';
|
2015-01-07 17:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-22 11:59:19 +01:00
|
|
|
return 'Netzmacht\LeafletPHP\Definition\Group\GeoJson';
|
2015-01-07 17:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-13 14:54:23 +01:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
protected function buildConstructArguments(
|
|
|
|
|
\Model $model,
|
|
|
|
|
DefinitionMapper $mapper,
|
2015-01-24 21:37:25 +01:00
|
|
|
Filter $filter = null,
|
2015-01-13 14:54:23 +01:00
|
|
|
$elementId = null
|
|
|
|
|
) {
|
|
|
|
|
if ($model->deferred) {
|
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
|
|
if ($model->pointToLayer) {
|
|
|
|
|
$options['pointToLayer'] = new Expression($model->pointToLayer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($model->onEachFeature) {
|
|
|
|
|
$options['onEachFeature'] = new Expression($model->onEachFeature);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 00:02:17 +01:00
|
|
|
if ($model->boundsMode) {
|
|
|
|
|
$options['boundsMode'] = $model->boundsMode;
|
2015-01-21 18:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-13 14:54:23 +01:00
|
|
|
if (!empty($options)) {
|
2015-01-18 23:19:52 +01:00
|
|
|
$layer = new GeoJson($this->getElementId($model, $elementId));
|
2015-01-13 14:54:23 +01:00
|
|
|
$layer->setOptions($options);
|
|
|
|
|
|
2015-01-27 00:02:17 +01:00
|
|
|
return array(
|
|
|
|
|
$this->getElementId($model, $elementId),
|
|
|
|
|
RequestUrl::create($model->id, null, null, $filter),
|
|
|
|
|
array(),
|
|
|
|
|
$layer
|
|
|
|
|
);
|
2015-01-13 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-27 00:02:17 +01:00
|
|
|
return array(
|
|
|
|
|
$this->getElementId($model, $elementId),
|
|
|
|
|
RequestUrl::create($model->id, null, null, $filter)
|
|
|
|
|
);
|
2015-01-13 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 21:37:25 +01:00
|
|
|
return parent::buildConstructArguments($model, $mapper, $filter, $elementId);
|
2015-01-13 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-07 17:59:56 +01:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
2015-01-08 09:51:43 +01:00
|
|
|
protected function build(
|
2015-01-07 17:59:56 +01:00
|
|
|
Definition $definition,
|
|
|
|
|
\Model $model,
|
|
|
|
|
DefinitionMapper $mapper,
|
2015-01-24 21:37:25 +01:00
|
|
|
Filter $filter = null,
|
2015-01-20 16:38:23 +01:00
|
|
|
Definition $parent = null
|
2015-01-07 17:59:56 +01:00
|
|
|
) {
|
2015-01-15 12:52:00 +01:00
|
|
|
if ($definition instanceof GeoJson) {
|
2015-01-07 17:59:56 +01:00
|
|
|
$collection = $this->loadVectorModels($model);
|
2015-01-22 13:34:22 +01:00
|
|
|
|
2015-01-27 00:02:17 +01:00
|
|
|
if ($model->boundsMode) {
|
|
|
|
|
$definition->setOption('boundsMode', $model->boundsMode);
|
2015-01-22 13:34:22 +01:00
|
|
|
}
|
2015-01-07 17:59:56 +01:00
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
foreach ($collection as $item) {
|
2015-01-23 13:04:04 +01:00
|
|
|
$vector = $mapper->handle($item);
|
|
|
|
|
$feature = $mapper->convertToGeoJsonFeature($vector, $item);
|
2015-01-20 09:38:35 +01:00
|
|
|
|
2015-01-23 13:04:04 +01:00
|
|
|
if ($feature) {
|
2015-01-20 09:38:35 +01:00
|
|
|
$definition->addData($feature, true);
|
2015-01-13 14:54:23 +01:00
|
|
|
}
|
2015-01-07 17:59:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-22 13:34:22 +01:00
|
|
|
$this->addCallbacks($definition, $model);
|
2015-01-07 17:59:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-12 19:03:29 +01:00
|
|
|
* {@inheritdoc}
|
2015-01-07 17:59:56 +01:00
|
|
|
*/
|
2015-01-24 21:37:25 +01:00
|
|
|
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, Filter $filter = null)
|
2015-01-07 17:59:56 +01:00
|
|
|
{
|
2015-01-23 13:04:04 +01:00
|
|
|
$definition = new FeatureCollection();
|
2015-01-07 17:59:56 +01:00
|
|
|
$collection = $this->loadVectorModels($model);
|
|
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
foreach ($collection as $item) {
|
2015-01-23 13:04:04 +01:00
|
|
|
$vector = $mapper->handle($item);
|
|
|
|
|
$feature = $mapper->convertToGeoJsonFeature($vector, $item);
|
2015-01-20 09:38:35 +01:00
|
|
|
|
2015-01-23 13:04:04 +01:00
|
|
|
if ($feature) {
|
|
|
|
|
$definition->addFeature($feature, true);
|
2015-01-12 10:45:05 +01:00
|
|
|
}
|
2015-01-07 17:59:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 13:04:04 +01:00
|
|
|
return $definition;
|
2015-01-07 17:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-12 19:03:29 +01:00
|
|
|
* Load vector models.
|
|
|
|
|
*
|
|
|
|
|
* @param \Model $model The layer model.
|
2015-01-07 17:59:56 +01:00
|
|
|
*
|
|
|
|
|
* @return \Model\Collection|null
|
|
|
|
|
*/
|
|
|
|
|
protected function loadVectorModels(\Model $model)
|
|
|
|
|
{
|
2015-01-12 19:03:29 +01:00
|
|
|
return VectorModel::findActiveBy('pid', $model->id, array('order' => 'sorting'));
|
2015-01-07 17:59:56 +01:00
|
|
|
}
|
2015-01-22 13:34:22 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add javascript callbacks.
|
|
|
|
|
*
|
|
|
|
|
* @param GeoJson $definition The definition.
|
|
|
|
|
* @param \Model $model The database model.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function addCallbacks(GeoJson $definition, \Model $model)
|
|
|
|
|
{
|
|
|
|
|
if ($model->pointToLayer) {
|
|
|
|
|
$definition->setPointToLayer(new Expression($model->pointToLayer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($model->onEachFeature) {
|
|
|
|
|
$definition->setOnEachFeature(new Expression($model->onEachFeature));
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-07 17:59:56 +01:00
|
|
|
}
|