Files
contao-leaflet-maps/src/Listener/GeoJsonListener.php

253 lines
7.8 KiB
PHP
Raw Normal View History

<?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
* @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
* @filesource
*/
declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Listener;
2017-10-18 16:43:33 +02:00
use Contao\FilesModel;
use Contao\Model;
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
2017-10-18 16:43:33 +02:00
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\LeafletPHP\Definition as LeafletDefinition;
use Netzmacht\LeafletPHP\Definition\HasPopup;
use Netzmacht\LeafletPHP\Definition\UI\Marker;
2017-10-17 17:11:35 +02:00
use Netzmacht\LeafletPHP\Definition\Vector;
use Netzmacht\LeafletPHP\Definition\Vector\Circle;
use Netzmacht\LeafletPHP\Definition\Vector\CircleMarker;
2017-10-17 17:11:35 +02:00
use Netzmacht\LeafletPHP\Value\GeoJson\Feature;
use Netzmacht\LeafletPHP\Value\GeoJson\GeoJsonObject;
2015-01-23 14:11:43 +01:00
/**
* Class GeoJsonSubscriber provides subscribers when a definition is converted to a geo json feature.
*
* @package Netzmacht\Contao\Leaflet\Subscriber
*/
final class GeoJsonListener
{
2016-10-06 08:58:35 +02:00
/**
* Property mapping between models and features.
*
* @var array
*/
private $featureModelProperties;
2017-10-18 16:43:33 +02:00
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
2016-10-06 08:58:35 +02:00
/**
* GeoJsonSubscriber constructor.
*
2017-10-18 16:43:33 +02:00
* @param RepositoryManager $repositoryManager Repository manager.
* @param array $featureModelProperties Property mapping between models and features.
2016-10-06 08:58:35 +02:00
*/
2017-10-18 16:43:33 +02:00
public function __construct(RepositoryManager $repositoryManager, array $featureModelProperties)
2016-10-06 08:58:35 +02:00
{
2017-10-18 16:43:33 +02:00
$this->repositoryManager = $repositoryManager;
2016-10-06 08:58:35 +02:00
$this->featureModelProperties = $featureModelProperties;
}
/**
* Handle the event.
*
* @param ConvertToGeoJsonEvent $event The event.
*
* @return void
*/
public function handle(ConvertToGeoJsonEvent $event)
{
$feature = $event->getGeoJson();
$definition = $event->getDefinition();
$model = $event->getModel();
$this->addPopup($feature, $definition);
$this->enrichObjects($feature, $definition, $model);
$this->enrichCircle($feature, $definition);
$this->setModelData($feature, $model);
}
/**
* Add popup property for definitions with an popup.
*
* @param GeoJsonObject $feature The geojson feature object.
* @param LeafletDefinition $definition The definition.
*
* @return void
*/
public function addPopup(GeoJsonObject $feature, LeafletDefinition $definition)
{
if ($definition instanceof HasPopup && $feature instanceof Feature) {
if ($definition->getPopup()) {
$feature->setProperty('popup', $definition->getPopup());
}
if ($definition->getPopupContent()) {
$feature->setProperty('popupContent', $definition->getPopupContent());
}
2015-01-27 17:14:58 +01:00
if ($definition->getPopupOptions()) {
$feature->setProperty('popupOptions', $definition->getPopupOptions());
}
}
}
/**
* Enrich map object with feature data and bounds information.
2015-01-23 14:11:43 +01:00
*
* @param GeoJsonObject $feature The geojson feature object.
* @param LeafletDefinition $definition The definition.
* @param Model|object $model The data model.
2015-01-23 14:11:43 +01:00
*
* @return void
*/
public function enrichObjects(GeoJsonObject $feature, LeafletDefinition $definition, $model)
{
2017-10-06 13:47:57 +02:00
if (($definition instanceof Marker || $definition instanceof Vector)
&& $model instanceof \Model && $feature instanceof Feature) {
2015-01-23 14:11:43 +01:00
$this->setDataProperty($model, $feature);
$this->setBoundsInformation($model, $feature);
}
}
/**
* Enrich the the circle with constructor arguments.
*
* @param GeoJsonObject $feature The geojson feature object.
* @param LeafletDefinition $definition The definition.
2015-01-23 14:11:43 +01:00
*
* @return void
*/
public function enrichCircle(GeoJsonObject $feature, LeafletDefinition $definition)
{
if ($definition instanceof Circle && !$definition instanceof CircleMarker && $feature instanceof Feature) {
$feature->setProperty('arguments', [$definition->getLatLng(), $definition->getRadius()]);
}
}
/**
* Pass configured properties on an model to the properties.model key.
*
* @param GeoJsonObject $feature The geojson feature object.
* @param Model|object $model The data model.
2015-01-23 14:11:43 +01:00
*
* @return void
*/
public function setModelData(GeoJsonObject $feature, $model)
{
if (!$model instanceof \Model || !$feature instanceof Feature
2016-10-06 08:58:35 +02:00
|| empty($this->featureModelProperties[$model->getTable()])) {
return;
}
2016-10-06 08:58:35 +02:00
$mapping = $this->featureModelProperties[$model->getTable()];
$data = (array) $feature->getProperty('model');
foreach ((array) $mapping as $property) {
$value = $this->parseModelValue($model, $property);
// Important: Do not combine with line above as the property can be modified if it's an array.
$data[$property] = $value;
}
$feature->setProperty('model', $data);
}
/**
* Parse the model value based on the config.
*
* @param \Model $model The model.
* @param mixed $property The property config.
*
* @return array|mixed|null
*/
private function parseModelValue(\Model $model, &$property)
{
if (is_array($property)) {
list($property, $type) = $property;
2015-01-23 14:11:43 +01:00
$value = $model->$property;
switch ($type) {
case 'array':
case 'object':
$value = deserialize($value, true);
break;
case 'file':
2017-10-18 16:43:33 +02:00
$repository = $this->repositoryManager->getRepository(FilesModel::class);
$file = $repository->findByUuid($value);
$value = $file->path;
break;
case 'files':
2017-10-18 16:43:33 +02:00
$repository = $this->repositoryManager->getRepository(FilesModel::class);
$collection = $repository->findMultipleByUuids(deserialize($value, true));
if ($collection) {
$value = $collection->fetchEach('path');
} else {
$value = [];
}
break;
default:
$value = null;
}
} else {
$value = $model->$property;
}
return $value;
}
2015-01-23 14:11:43 +01:00
/**
* Set the bounds information.
*
* @param \Model $model The model.
* @param Feature $feature The feature.
*
* @return void
*/
protected function setBoundsInformation($model, $feature)
{
if ($model->ignoreForBounds) {
$feature->setProperty('ignoreForBounds', true);
} else {
2017-10-18 16:43:33 +02:00
$repository = $this->repositoryManager->getRepository(LayerModel::class);
$parent = $repository->find((int) $model->pid);
2015-01-23 14:11:43 +01:00
2015-01-27 11:43:19 +01:00
if ($parent && $parent->boundsMode !== 'extend') {
2015-01-23 14:11:43 +01:00
$feature->setProperty('ignoreForBounds', true);
}
}
}
/**
* Set the data property.
*
* @param \Model $model The model.
* @param Feature $feature The feature.
*
* @return void
*/
protected function setDataProperty($model, $feature)
{
if ($model->featureData) {
$feature->setProperty('data', json_decode($model->featureData, true));
}
}
}