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

163 lines
4.6 KiB
PHP
Raw Normal View History

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-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-06 14:55:53 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
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;
use Netzmacht\Contao\Leaflet\Request\Request;
use Netzmacht\JavascriptBuilder\Type\Expression;
2015-01-06 14:55:53 +01:00
use Netzmacht\LeafletPHP\Definition;
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}
*/
protected function getClassName(Model $model, DefinitionMapper $mapper, Request $request = 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-22 11:59:19 +01:00
return 'Netzmacht\LeafletPHP\Definition\Group\GeoJson';
2015-01-06 15:43:57 +01:00
}
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(
Model $model,
DefinitionMapper $mapper,
Request $request = null,
$elementId = null
) {
if ($model->deferred) {
2015-01-27 00:02:17 +01:00
if ($model->pointToLayer || $model->boundsMode) {
$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-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-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,
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) {
$marker = $mapper->handle($item);
$point = $mapper->convertToGeoJsonFeature($marker, $item);
if ($point) {
2015-01-27 11:43:19 +01:00
$definition->addData($point, 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, Request $request = null)
2015-01-06 14:55:53 +01:00
{
2015-01-06 15:43:57 +01:00
$feature = new FeatureCollection();
$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);
$point = $mapper->convertToGeoJsonFeature($marker, $item);
2015-01-12 10:45:05 +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.
*
* @param Model $model The layer model.
* @param Request $request Optional building request.
2015-01-06 15:43:57 +01:00
*
* @return \Model\Collection|null
*/
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') {
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
}