Files
contao-leaflet-maps/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php

156 lines
4.4 KiB
PHP
Raw Normal View History

2015-01-06 14:55:53 +01:00
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
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;
use Netzmacht\JavascriptBuilder\Type\Expression;
2015-01-06 14:55:53 +01:00
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
2015-01-06 18:49:22 +01:00
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
2015-01-06 14:55:53 +01:00
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
2015-01-12 10:45:05 +01:00
use Netzmacht\LeafletPHP\Definition\UI\Marker;
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
{
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Group\GeoJson';
/**
* Layer type.
*
* @var string
*/
protected static $type = 'markers';
2015-01-06 15:43:57 +01:00
/**
* {@inheritdoc}
*/
2015-01-08 12:52:32 +01:00
protected function getClassName(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
2015-01-06 15:43:57 +01:00
{
if ($model->deferred) {
return 'Netzmacht\LeafletPHP\Plugins\Omnivore\GeoJson';
2015-01-06 15:43:57 +01:00
}
2015-01-08 12:52:32 +01:00
return parent::getClassName($model, $mapper, $bounds);
2015-01-06 15:43:57 +01:00
}
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
if ($model->deferred) {
if ($model->pointToLayer || $model->affectBounds) {
$layer = new GeoJson($this->getElementId($model, $elementId));
2015-01-21 20:37:53 +01:00
if ($model->pointToLayer ) {
$layer->setPointToLayer(new Expression($model->pointToLayer));
}
if ($model->affectBounds) {
$layer->setOption('affectBounds', (bool) $model->affectBounds);
}
return array($this->getElementId($model, $elementId), RequestUrl::create($model->id), array(), $layer);
}
return array($this->getElementId($model, $elementId), RequestUrl::create($model->id));
}
return parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
}
2015-01-06 15:43:57 +01:00
/**
* {@inheritdoc}
*/
protected function build(
2015-01-06 14:55:53 +01:00
Definition $definition,
\Model $model,
2015-01-06 18:49:22 +01:00
DefinitionMapper $mapper,
2015-01-20 16:38:23 +01:00
LatLngBounds $bounds = null,
Definition $parent = null
2015-01-06 14:55:53 +01:00
) {
$definition->setOption('affectBounds', (bool) $model->affectBounds);
2015-01-15 12:52:00 +01:00
if ($definition instanceof GeoJson) {
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) {
$marker = $mapper->handle($item);
if ($marker instanceof Marker) {
$feature = $marker->toGeoJsonFeature();
$feature->setProperty('ignoreForBounds', ($item->ignoreForBounds));
$definition->addData($feature, true);
}
2015-01-06 14:55:53 +01:00
}
}
2015-01-06 18:49:22 +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
*/
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
2015-01-06 15:43:57 +01:00
$feature = new FeatureCollection();
$collection = $this->loadMarkerModels($model);
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);
if ($marker instanceof Marker) {
$feature->addFeature($marker->toGeoJsonFeature());
}
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.
*
* @param \Model $model The layer model.
2015-01-06 15:43:57 +01:00
*
* @return \Model\Collection|null
*/
protected function loadMarkerModels(\Model $model)
{
2015-01-12 19:03:29 +01:00
return MarkerModel::findActiveBy('pid', $model->id, array('order' => 'sorting'));
2015-01-06 15:43:57 +01:00
}
2015-01-06 14:55:53 +01:00
}