Files
contao-leaflet-maps/src/Mapper/MapMapper.php

262 lines
7.4 KiB
PHP
Raw Normal View History

2014-12-29 12:17:40 +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
2014-12-29 12:17:40 +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
2014-12-29 12:17:40 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper;
use Contao\Model;
use Contao\StringUtil;
2015-01-05 12:25:46 +01:00
use Netzmacht\Contao\Leaflet\Model\ControlModel;
2014-12-29 12:17:40 +01:00
use Netzmacht\Contao\Leaflet\Model\MapModel;
2017-10-19 08:45:39 +02:00
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
2014-12-29 12:17:40 +01:00
use Netzmacht\LeafletPHP\Definition;
2015-01-09 13:41:09 +01:00
use Netzmacht\LeafletPHP\Definition\Control;
use Netzmacht\LeafletPHP\Definition\Layer;
2014-12-29 12:17:40 +01:00
use Netzmacht\LeafletPHP\Definition\Map;
2015-01-09 13:41:09 +01:00
2015-01-12 19:03:29 +01:00
/**
* Class MapMapper maps the database map model to the leaflet definition.
*
* @package Netzmacht\Contao\Leaflet\Mapper
*/
2014-12-29 12:17:40 +01:00
class MapMapper extends AbstractMapper
{
/**
* Class of the model being build.
*
* @var string
*/
2017-10-11 14:50:14 +02:00
protected static $modelClass = MapModel::class;
2014-12-29 12:17:40 +01:00
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = Map::class;
2014-12-29 12:17:40 +01:00
2017-10-19 08:45:39 +02:00
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/**
* Construct.
*
* @param RepositoryManager $repositoryManager Repository manager.
*/
public function __construct(RepositoryManager $repositoryManager)
{
$this->repositoryManager = $repositoryManager;
parent::__construct();
}
2014-12-29 12:17:40 +01:00
/**
2015-01-12 19:03:29 +01:00
* {@inheritdoc}
2014-12-29 12:17:40 +01:00
*/
protected function initialize()
{
$this->optionsBuilder
2014-12-29 12:17:40 +01:00
->addOptions('center', 'zoom', 'zoomControl')
->addOptions('dragging', 'touchZoom', 'scrollWheelZoom', 'doubleClickZoom', 'boxZoom', 'tap', 'keyboard')
->addOptions('trackResize', 'closeOnClick', 'bounceAtZoomLimits')
->addConditionalOptions('adjustZoomExtra', ['minZoom', 'maxZoom', 'zoomSnap', 'zoomDelta'])
->addConditionalOptions('keyboard', ['keyboardPanOffset', 'keyboardZoomOffset']);
2014-12-29 12:17:40 +01:00
}
/**
2015-01-12 19:03:29 +01:00
* {@inheritdoc}
2014-12-29 12:17:40 +01:00
*/
2015-01-20 16:38:23 +01:00
protected function build(
Definition $map,
Model $model,
2015-01-20 16:38:23 +01:00
DefinitionMapper $mapper,
Request $request = null,
2015-01-20 16:38:23 +01:00
Definition $parent = null
) {
2014-12-29 12:17:40 +01:00
if ($map instanceof Map && $model instanceof MapModel) {
$this->buildCustomOptions($map, $model);
$this->buildControls($map, $model, $mapper, $request);
$this->buildLayers($map, $model, $mapper, $request);
$this->buildBoundsCalculation($map, $model);
2015-01-24 11:38:15 +01:00
$this->buildLocate($map, $model);
2014-12-29 12:17:40 +01:00
}
}
/**
2015-01-12 19:03:29 +01:00
* {@inheritdoc}
2014-12-29 12:17:40 +01:00
*/
2015-01-09 13:41:09 +01:00
protected function buildConstructArguments(
Model $model,
2015-01-09 13:41:09 +01:00
DefinitionMapper $mapper,
Request $request = null,
2015-01-09 13:41:09 +01:00
$elementId = null
) {
return [
2015-01-09 13:41:09 +01:00
$this->getElementId($model, $elementId),
$this->getElementId($model, $elementId),
];
2014-12-29 12:17:40 +01:00
}
/**
* Build map custom options.
*
* @param Map $map The map being built.
* @param MapModel $model The map model.
*
* @return void
*/
protected function buildCustomOptions(Map $map, MapModel $model)
{
if ($model->options) {
2016-10-06 11:38:52 +02:00
$options = json_decode($model->options, true);
if (is_array($options)) {
$map->setOptions($options);
}
2014-12-29 12:17:40 +01:00
}
2015-01-27 00:02:17 +01:00
$map->setOption('dynamicLoad', (bool) $model->dynamicLoad);
2014-12-29 12:17:40 +01:00
}
/**
* Build map controls.
*
* @param Map $map The map being built.
* @param MapModel $model The map model.
* @param DefinitionMapper $mapper The definition mapper.
* @param Request $request Optional building request.
2015-01-12 19:03:29 +01:00
*
* @return void
2014-12-29 12:17:40 +01:00
*/
private function buildControls(Map $map, MapModel $model, DefinitionMapper $mapper, Request $request = null)
2014-12-29 12:17:40 +01:00
{
2017-10-19 08:45:39 +02:00
$repository = $this->repositoryManager->getRepository(ControlModel::class);
$collection = $repository->findActiveBy(['pid=?'], [$model->id], ['order' => 'sorting']);
2015-01-05 12:25:46 +01:00
2015-01-09 13:41:09 +01:00
if (!$collection) {
return;
}
foreach ($collection as $control) {
$control = $mapper->handle($control, $request, null, $map);
2015-01-09 13:41:09 +01:00
if ($control instanceof Control) {
2015-01-19 12:58:16 +01:00
$control->addTo($map);
2015-01-05 12:25:46 +01:00
}
}
2014-12-29 12:17:40 +01:00
}
/**
* Build map layers.
*
* @param Map $map The map being built.
* @param MapModel $model The map model.
* @param DefinitionMapper $mapper Definition mapper.
* @param Request $request Optional building request.
2015-01-12 19:03:29 +01:00
*
* @return void
2014-12-29 12:17:40 +01:00
*/
private function buildLayers(Map $map, MapModel $model, DefinitionMapper $mapper, Request $request = null)
2014-12-29 12:17:40 +01:00
{
2015-01-09 23:48:25 +01:00
$collection = $model->findActiveLayers();
2014-12-29 16:22:16 +01:00
if ($collection) {
foreach ($collection as $layer) {
2015-01-06 14:55:53 +01:00
if (!$layer->active) {
continue;
}
$layer = $mapper->handle($layer, $request, null, $map);
2015-01-09 13:41:09 +01:00
if ($layer instanceof Layer) {
2015-01-19 12:58:16 +01:00
$layer->addTo($map);
2015-01-09 13:41:09 +01:00
}
2014-12-29 16:22:16 +01:00
}
}
2014-12-29 12:17:40 +01:00
}
/**
* Build map bounds calculations.
*
2015-01-20 17:43:41 +01:00
* @param Map $map The map being built.
* @param MapModel $model The map model.
*
* @return void
*/
private function buildBoundsCalculation(Map $map, MapModel $model)
{
$adjustBounds = StringUtil::deserialize($model->adjustBounds, true);
if (in_array('deferred', $adjustBounds)) {
$map->setOption('adjustBounds', true);
}
2016-10-11 14:32:43 +02:00
if ($model->boundsPadding) {
$value = array_map('intval', explode(',', $model->boundsPadding, 4));
if (count($value) === 4) {
$map->setOption('boundsPaddingTopLeft', [$value[0], $value[1]]);
$map->setOption('boundsPaddingBottomRight', [$value[2], $value[3]]);
} elseif (count($value) === 2) {
$map->setOption('boundsPadding', $value);
}
}
if (in_array('load', $adjustBounds)) {
$map->calculateFeatureBounds();
}
}
2015-01-24 11:38:15 +01:00
/**
* Build map bounds calculations.
*
* @param Map $map The map being built.
* @param MapModel $model The map model.
*
* @return void
*/
private function buildLocate(Map $map, MapModel $model)
{
if ($model->locate) {
$options = [];
2015-01-24 11:38:15 +01:00
$mapping = [
2015-01-24 11:38:15 +01:00
'setView' => 'locateSetView',
'watch' => 'locateWatch',
'enableHighAccuracy' => 'enableHighAccuracy',
];
2015-01-24 11:38:15 +01:00
foreach ($mapping as $option => $property) {
if ($model->$property) {
$options[$option] = (bool) $model->$property;
}
}
$mapping = [
2015-01-24 11:38:15 +01:00
'maxZoom' => 'locateMaxZoom',
'timeout' => 'locateTimeout',
'maximumAge' => 'locateMaximumAge',
];
2015-01-24 11:38:15 +01:00
foreach ($mapping as $option => $property) {
if ($model->$property) {
$options[$option] = (int) $model->$property;
}
}
$map->locate($options);
}
}
2014-12-29 12:17:40 +01:00
}