Files
contao-leaflet-maps/src/Mapper/Layer/VectorsLayerMapper.php

183 lines
5.1 KiB
PHP
Raw Normal View History

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-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-07 17:59:56 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
use Contao\Model;
use Contao\Model\Collection;
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;
use Netzmacht\Contao\Leaflet\Request\Request;
use Netzmacht\JavascriptBuilder\Type\Expression;
2015-01-07 17:59:56 +01:00
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
2015-01-07 17:59:56 +01:00
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
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}
*/
protected function getClassName(Model $model, DefinitionMapper $mapper, Request $request = null)
2015-01-07 17:59:56 +01:00
{
if ($model->deferred) {
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
}
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(
Model $model,
DefinitionMapper $mapper,
Request $request = null,
$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;
}
if (!empty($options)) {
$layer = new GeoJson($this->getElementId($model, $elementId));
$layer->setOptions($options);
2015-01-27 00:02:17 +01:00
return array(
$this->getElementId($model, $elementId),
RequestUrl::create($model->id, null, null, $request),
2015-01-27 00:02:17 +01:00
array(),
$layer
);
}
2015-01-27 00:02:17 +01:00
return array(
$this->getElementId($model, $elementId),
RequestUrl::create($model->id, null, null, $request)
2015-01-27 00:02:17 +01:00
);
}
return parent::buildConstructArguments($model, $mapper, $request, $elementId);
}
2015-01-07 17:59:56 +01:00
/**
* {@inheritdoc}
*/
protected function build(
2015-01-07 17:59:56 +01:00
Definition $definition,
Model $model,
2015-01-07 17:59:56 +01:00
DefinitionMapper $mapper,
Request $request = 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) {
$vector = $mapper->handle($item);
$feature = $mapper->convertToGeoJsonFeature($vector, $item);
if ($feature) {
$definition->addData($feature, true);
}
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
*/
public function handleGeoJson(Model $model, DefinitionMapper $mapper, Request $request = null)
2015-01-07 17:59:56 +01:00
{
$definition = new FeatureCollection();
2015-01-07 17:59:56 +01:00
$collection = $this->loadVectorModels($model);
if ($collection) {
foreach ($collection as $item) {
$vector = $mapper->handle($item);
$feature = $mapper->convertToGeoJsonFeature($vector, $item);
if ($feature) {
$definition->addFeature($feature, true);
2015-01-12 10:45:05 +01:00
}
2015-01-07 17:59:56 +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 Collection|null
2015-01-07 17:59:56 +01:00
*/
protected function loadVectorModels(Model $model)
2015-01-07 17:59:56 +01:00
{
return VectorModel::findActiveBy('pid', $model->id, ['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
}