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

222 lines
6.2 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-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-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;
2017-10-17 18:03:42 +02:00
use Netzmacht\Contao\Leaflet\Mapper\Request;
2015-01-06 14:55:53 +01:00
use Netzmacht\Contao\Leaflet\Model\MarkerModel;
2017-10-19 08:45:39 +02:00
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\JavascriptBuilder\Type\Expression;
2015-01-06 14:55:53 +01:00
use Netzmacht\LeafletPHP\Definition;
2017-10-17 17:11:35 +02:00
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
use Netzmacht\LeafletPHP\Plugins\Omnivore\GeoJson as OmnivoreGeoJson;
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
use Symfony\Component\Routing\RouterInterface;
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';
2017-10-19 08:45:39 +02:00
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/**
* Router.
*
* @var RouterInterface
*/
private $router;
2017-10-19 08:45:39 +02:00
/**
* Construct.
*
* @param RepositoryManager $repositoryManager Repository manager.
* @param RouterInterface $router Router.
2017-10-19 08:45:39 +02:00
*/
public function __construct(RepositoryManager $repositoryManager, RouterInterface $router)
2017-10-19 08:45:39 +02:00
{
$this->repositoryManager = $repositoryManager;
$this->router = $router;
2017-10-19 08:45:39 +02:00
parent::__construct();
}
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 OmnivoreGeoJson::class;
2015-01-06 15:43:57 +01:00
}
return GeoJson::class;
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
}
return [
2015-01-27 00:02:17 +01:00
$this->getElementId($model, $elementId),
$this->generateUrl((int) $model->id, $request),
[],
$layer,
];
}
return [
2015-01-27 00:02:17 +01:00
$this->getElementId($model, $elementId),
$this->generateUrl((int) $model->id, $request),
];
}
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 \Contao\Model\Collection|null
2015-01-06 15:43:57 +01:00
*/
protected function loadMarkerModels(Model $model, Request $request = null)
2015-01-06 15:43:57 +01:00
{
2017-10-19 08:45:39 +02:00
$repository = $this->repositoryManager->getRepository(MarkerModel::class);
if ($model->boundsMode === 'fit') {
2017-10-19 08:45:39 +02:00
return $repository->findByFilter($model->id, $request->getFilter());
2015-01-27 00:02:17 +01:00
}
2017-10-19 08:45:39 +02:00
return $repository->findByFilter($model->id);
2015-01-06 15:43:57 +01:00
}
/**
* Generate the data url for a layer.
*
* @param int $layerId The layer id.
* @param Request|null $request The request.
*
* @return string
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
private function generateUrl(int $layerId, ?Request $request = null): string
{
$params = ['layerId' => $layerId];
if ($request && ($filter = $request->getFilter())) {
$params['filter'] = $filter::getName();
$params['values'] = $filter->toRequest();
}
if (isset($GLOBALS['objPage'])) {
$params['context'] = 'page';
$params['contextId'] = $GLOBALS['objPage']->id;
}
return $this->router->generate('leaflet_layer', $params, RouterInterface::ABSOLUTE_URL);
}
2015-01-06 14:55:53 +01:00
}