2015-01-23 13:04:04 +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-23 13:04:04 +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-23 13:04:04 +01:00
|
|
|
* @filesource
|
|
|
|
|
*/
|
|
|
|
|
|
2017-10-17 17:57:40 +02:00
|
|
|
declare(strict_types=1);
|
2015-01-23 13:04:04 +01:00
|
|
|
|
2017-10-17 17:57:40 +02:00
|
|
|
namespace Netzmacht\Contao\Leaflet\Listener;
|
|
|
|
|
|
2017-10-18 16:43:33 +02:00
|
|
|
use Contao\FilesModel;
|
2017-10-17 17:57:40 +02:00
|
|
|
use Contao\Model;
|
2015-01-23 13:04:04 +01:00
|
|
|
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;
|
2017-10-17 17:57:40 +02:00
|
|
|
use Netzmacht\LeafletPHP\Definition as LeafletDefinition;
|
2015-01-23 13:04:04 +01:00
|
|
|
use Netzmacht\LeafletPHP\Definition\HasPopup;
|
|
|
|
|
use Netzmacht\LeafletPHP\Definition\UI\Marker;
|
2017-10-17 17:11:35 +02:00
|
|
|
use Netzmacht\LeafletPHP\Definition\Vector;
|
2015-01-23 13:04:04 +01:00
|
|
|
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;
|
2017-10-17 17:57:40 +02:00
|
|
|
use Netzmacht\LeafletPHP\Value\GeoJson\GeoJsonObject;
|
2015-01-23 13:04:04 +01:00
|
|
|
|
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
|
|
|
|
|
*/
|
2017-10-17 17:57:40 +02:00
|
|
|
final class GeoJsonListener
|
2015-01-23 13:04:04 +01:00
|
|
|
{
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 13:04:04 +01:00
|
|
|
/**
|
2017-10-17 17:57:40 +02:00
|
|
|
* Handle the event.
|
|
|
|
|
*
|
|
|
|
|
* @param ConvertToGeoJsonEvent $event The event.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2015-01-23 13:04:04 +01:00
|
|
|
*/
|
2017-10-17 17:57:40 +02:00
|
|
|
public function handle(ConvertToGeoJsonEvent $event)
|
2015-01-23 13:04:04 +01:00
|
|
|
{
|
2017-10-17 17:57:40 +02:00
|
|
|
$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);
|
2015-01-23 13:04:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add popup property for definitions with an popup.
|
|
|
|
|
*
|
2017-10-17 17:57:40 +02:00
|
|
|
* @param GeoJsonObject $feature The geojson feature object.
|
|
|
|
|
* @param LeafletDefinition $definition The definition.
|
2015-01-23 13:04:04 +01:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2017-10-17 17:57:40 +02:00
|
|
|
public function addPopup(GeoJsonObject $feature, LeafletDefinition $definition)
|
2015-01-23 13:04:04 +01:00
|
|
|
{
|
|
|
|
|
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());
|
|
|
|
|
}
|
2015-01-23 13:04:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-23 15:42:38 +01:00
|
|
|
* Enrich map object with feature data and bounds information.
|
2015-01-23 14:11:43 +01:00
|
|
|
*
|
2017-10-17 17:57:40 +02: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
|
2015-01-23 13:04:04 +01:00
|
|
|
*/
|
2017-10-17 17:57:40 +02:00
|
|
|
public function enrichObjects(GeoJsonObject $feature, LeafletDefinition $definition, $model)
|
2015-01-23 13:04:04 +01:00
|
|
|
{
|
2017-10-06 13:47:57 +02:00
|
|
|
if (($definition instanceof Marker || $definition instanceof Vector)
|
2015-01-23 15:42:38 +01:00
|
|
|
&& $model instanceof \Model && $feature instanceof Feature) {
|
2015-01-23 14:11:43 +01:00
|
|
|
$this->setDataProperty($model, $feature);
|
|
|
|
|
$this->setBoundsInformation($model, $feature);
|
2015-01-23 13:04:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enrich the the circle with constructor arguments.
|
|
|
|
|
*
|
2017-10-17 17:57:40 +02:00
|
|
|
* @param GeoJsonObject $feature The geojson feature object.
|
|
|
|
|
* @param LeafletDefinition $definition The definition.
|
2015-01-23 14:11:43 +01:00
|
|
|
*
|
2015-01-23 13:04:04 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2017-10-17 17:57:40 +02:00
|
|
|
public function enrichCircle(GeoJsonObject $feature, LeafletDefinition $definition)
|
2015-01-23 13:04:04 +01:00
|
|
|
{
|
|
|
|
|
if ($definition instanceof Circle && !$definition instanceof CircleMarker && $feature instanceof Feature) {
|
2017-10-17 18:11:28 +02:00
|
|
|
$feature->setProperty('arguments', [$definition->getLatLng(), $definition->getRadius()]);
|
2015-01-23 13:04:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
2015-01-23 13:58:35 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pass configured properties on an model to the properties.model key.
|
|
|
|
|
*
|
2017-10-17 17:57:40 +02:00
|
|
|
* @param GeoJsonObject $feature The geojson feature object.
|
|
|
|
|
* @param Model|object $model The data model.
|
2015-01-23 14:11:43 +01:00
|
|
|
*
|
2015-01-23 13:58:35 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2017-10-17 17:57:40 +02:00
|
|
|
public function setModelData(GeoJsonObject $feature, $model)
|
2015-01-23 13:58:35 +01:00
|
|
|
{
|
|
|
|
|
if (!$model instanceof \Model || !$feature instanceof Feature
|
2016-10-06 08:58:35 +02:00
|
|
|
|| empty($this->featureModelProperties[$model->getTable()])) {
|
2015-01-23 13:58:35 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 08:58:35 +02:00
|
|
|
$mapping = $this->featureModelProperties[$model->getTable()];
|
2015-01-23 13:58:35 +01:00
|
|
|
$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;
|
2015-01-23 13:58:35 +01:00
|
|
|
|
|
|
|
|
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;
|
2015-01-23 13:58:35 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'files':
|
2017-10-18 16:43:33 +02:00
|
|
|
$repository = $this->repositoryManager->getRepository(FilesModel::class);
|
|
|
|
|
$collection = $repository->findMultipleByUuids(deserialize($value, true));
|
2015-01-23 13:58:35 +01:00
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
$value = $collection->fetchEach('path');
|
|
|
|
|
} else {
|
2017-10-17 18:11:28 +02:00
|
|
|
$value = [];
|
2015-01-23 13:58:35 +01:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-23 13:04:04 +01:00
|
|
|
}
|