Files
contao-leaflet-maps/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php

179 lines
5.0 KiB
PHP
Raw Normal View History

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
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
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\MapModel';
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Map';
/**
2015-01-12 19:03:29 +01:00
* {@inheritdoc}
2014-12-29 12:17:40 +01:00
*/
protected function initialize()
{
$this
->addOptions('center', 'zoom', 'zoomControl')
->addOptions('dragging', 'touchZoom', 'scrollWheelZoom', 'doubleClickZoom', 'boxZoom', 'tap', 'keyboard')
->addOptions('trackResize', 'closeOnClick', 'bounceAtZoomLimits')
2014-12-29 12:17:40 +01:00
->addConditionalOptions('adjustZoomExtra', array('minZoom', 'maxZoom'))
->addConditionalOptions('keyboard', array('keyboardPanOffset', 'keyboardZoomOffset'));
}
/**
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,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
Definition $parent = null
) {
2014-12-29 12:17:40 +01:00
if ($map instanceof Map && $model instanceof MapModel) {
$this->buildCustomOptions($map, $model);
2015-01-20 16:38:23 +01:00
$this->buildControls($map, $model, $mapper, $bounds);
$this->buildLayers($map, $model, $mapper, $bounds);
$this->buildBoundsCalculation($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,
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.
2015-01-12 19:03:29 +01:00
*
* @return void
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) {
2015-01-20 17:29:10 +01:00
$control = $mapper->handle($control, $bounds, 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.
2015-01-06 14:55:53 +01:00
* @param LatLngBounds $bounds Optional bounds.
2015-01-12 19:03:29 +01:00
*
* @return void
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
{
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;
}
2015-01-20 17:29:10 +01:00
$layer = $mapper->handle($layer, $bounds, 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.
*
* @param Map $map The map being built.
* @param MapModel $model The map model.
*/
private function buildBoundsCalculation(Map $map, MapModel $model)
{
$adjustBounds = deserialize($model->adjustBounds, true);
if (in_array('deferred', $adjustBounds)) {
$map->setOption('adjustBounds', true);
}
if (in_array('load', $adjustBounds)) {
$map->calculateFeatureBounds();
}
}
2014-12-29 12:17:40 +01:00
}