2014-12-29 12:17:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @package dev
|
|
|
|
|
* @author David Molineus <david.molineus@netzmacht.de>
|
|
|
|
|
* @copyright 2014 netzmacht creative David Molineus
|
|
|
|
|
* @license LGPL 3.0
|
|
|
|
|
* @filesource
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Netzmacht\Contao\Leaflet\Mapper;
|
|
|
|
|
|
2015-01-05 12:25:46 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Model\ControlModel;
|
2014-12-29 16:22:16 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Model\LayerModel;
|
2014-12-29 12:17:40 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Model\MapModel;
|
|
|
|
|
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-06 14:55:53 +01:00
|
|
|
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
2015-01-09 13:41:09 +01:00
|
|
|
|
2014-12-29 12:17:40 +01:00
|
|
|
|
|
|
|
|
class MapMapper extends AbstractMapper
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Class of the model being build.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\MapModel';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class of the definition being created.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Map';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
protected function initialize()
|
|
|
|
|
{
|
|
|
|
|
$this
|
|
|
|
|
->addOptions('center', 'zoom', 'zoomControl')
|
|
|
|
|
->addOptions('dragging', 'touchZoom', 'scrollWheelZoom', 'doubleClickZoom', 'boxZoom', 'tap', 'keyboard')
|
|
|
|
|
->addOptions('trackResize', 'closePopupOnClick', 'bounceAtZoomLimits')
|
|
|
|
|
->addConditionalOptions('adjustZoomExtra', array('minZoom', 'maxZoom'))
|
|
|
|
|
->addConditionalOptions('keyboard', array('keyboardPanOffset', 'keyboardZoomOffset'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
2015-01-08 09:51:43 +01:00
|
|
|
protected function build(Definition $map, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
|
|
|
|
if ($map instanceof Map && $model instanceof MapModel) {
|
|
|
|
|
$this->buildCustomOptions($map, $model);
|
2015-01-06 14:55:53 +01:00
|
|
|
$this->buildControls($map, $model, $builder, $bounds);
|
|
|
|
|
$this->buildLayers($map, $model, $builder, $bounds);
|
2014-12-29 12:17:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
2015-01-09 13:41:09 +01:00
|
|
|
protected function buildConstructArguments(
|
|
|
|
|
\Model $model,
|
|
|
|
|
DefinitionMapper $mapper,
|
|
|
|
|
LatLngBounds $bounds = null,
|
|
|
|
|
$elementId = null
|
|
|
|
|
) {
|
2014-12-29 12:17:40 +01:00
|
|
|
return array(
|
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) {
|
|
|
|
|
$map->setOptions(json_decode($model->options, true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build map controls.
|
|
|
|
|
*
|
|
|
|
|
* @param Map $map The map being built.
|
|
|
|
|
* @param MapModel $model The map model.
|
|
|
|
|
* @param DefinitionMapper $mapper The definition mapper.
|
2015-01-06 14:55:53 +01:00
|
|
|
* @param LatLngBounds $bounds Optional bounds.
|
2014-12-29 12:17:40 +01:00
|
|
|
*/
|
2015-01-06 14:55:53 +01:00
|
|
|
private function buildControls(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
2015-01-09 13:41:09 +01:00
|
|
|
$collection = ControlModel::findActiveBy('pid', $model->id, array('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, $bounds);
|
|
|
|
|
|
|
|
|
|
if ($control instanceof Control) {
|
2015-01-05 12:25:46 +01:00
|
|
|
$map->addControl($control);
|
|
|
|
|
}
|
|
|
|
|
}
|
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.
|
2015-01-06 14:55:53 +01:00
|
|
|
* @param LatLngBounds $bounds Optional bounds.
|
2014-12-29 12:17:40 +01:00
|
|
|
*/
|
2015-01-06 14:55:53 +01:00
|
|
|
private function buildLayers(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
2014-12-29 16:22:16 +01:00
|
|
|
$ids = deserialize($model->layers, true);
|
|
|
|
|
$collection = LayerModel::findMultipleByIds($ids);
|
|
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
foreach ($collection as $layer) {
|
2015-01-06 14:55:53 +01:00
|
|
|
if (!$layer->active) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$layer = $mapper->handle($layer, $bounds);
|
2015-01-09 13:41:09 +01:00
|
|
|
if ($layer instanceof Layer) {
|
|
|
|
|
$map->addLayer($layer);
|
|
|
|
|
}
|
2014-12-29 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-12-29 12:17:40 +01:00
|
|
|
}
|
|
|
|
|
}
|