From c00d08c8725f87c5c86e1931a4e656d4be1a18e3 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Mon, 5 Jan 2015 14:58:37 +0100 Subject: [PATCH] Add more controls. --- module/config/config.php | 2 +- module/dca/tl_leaflet_control.php | 31 ++++++++- module/dca/tl_leaflet_map.php | 2 +- module/languages/en/tl_leaflet_map.php | 6 +- src/Netzmacht/Contao/Leaflet/Dca/Control.php | 16 +++++ .../Control/AttributionControlMapper.php | 64 +++++++++++++++++++ .../Mapper/Control/LayersControlMapper.php | 44 +++++++++++++ .../Leaflet/Mapper/DefinitionMapper.php | 13 ++++ 8 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 src/Netzmacht/Contao/Leaflet/Mapper/Control/AttributionControlMapper.php diff --git a/module/config/config.php b/module/config/config.php index c01540a..cac6d8f 100644 --- a/module/config/config.php +++ b/module/config/config.php @@ -34,7 +34,7 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\ProviderL $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\ZoomControlMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\ScaleControlMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\LayersControlMapper'; - +$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\AttributionControlMapper'; /* * Leaflet encoders. diff --git a/module/dca/tl_leaflet_control.php b/module/dca/tl_leaflet_control.php index 89abd47..8344af9 100644 --- a/module/dca/tl_leaflet_control.php +++ b/module/dca/tl_leaflet_control.php @@ -62,6 +62,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'icon' => 'delete.gif', 'attributes' => 'onclick="if(!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\'))return false;Backend.getScrollOffset()"' ), + 'toggle' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['toggle'], + 'icon' => 'visible.gif', + 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', + 'button_callback' => \Netzmacht\Contao\DevTools\Dca::createToggleIconCallback( + 'tl_leaflet_control', + 'active' + ) + ), 'show' => array ( 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['show'], @@ -89,6 +99,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array ), 'scale extends default' => array( 'config' => array('maxWidth', 'metric', 'imperial', 'updateWhenIdle') + ), + 'attribution extends default' => array( + 'config' => array('attributions', 'prefix') ) ), @@ -225,7 +238,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'inputType' => 'select', 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Control', 'getLayers'), 'eval' => array( - 'style' => 'width: 200px', + 'style' => 'width: 300px', 'chosen' => true, 'includeBlankOption' => true ), @@ -279,5 +292,21 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'eval' => array('tl_class' => 'w50'), 'sql' => "char(1) NOT NULL default ''" ), + 'prefix' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['prefix'], + 'exclude' => true, + 'inputType' => 'text', + 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50'), + 'sql' => "varchar(255) NOT NULL default ''" + ), + 'attributions' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['attributions'], + 'exclude' => true, + 'inputType' => 'listWizard', + 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'clr', 'allowHtml' => true), + 'sql' => "mediumblob NULL" + ), ), ); diff --git a/module/dca/tl_leaflet_map.php b/module/dca/tl_leaflet_map.php index 99a87fe..8b88e6a 100644 --- a/module/dca/tl_leaflet_map.php +++ b/module/dca/tl_leaflet_map.php @@ -32,7 +32,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array ( 'layers' => array ( - 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['layers'], + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['layersBtn'], 'href' => 'table=tl_leaflet_layer', 'icon' => 'system/modules/leaflet/assets/img/layers.png', 'attributes' => 'onclick="Backend.getScrollOffset();" accesskey="e"' diff --git a/module/languages/en/tl_leaflet_map.php b/module/languages/en/tl_leaflet_map.php index 9d2bdcc..94443d3 100644 --- a/module/languages/en/tl_leaflet_map.php +++ b/module/languages/en/tl_leaflet_map.php @@ -6,8 +6,8 @@ $GLOBALS['TL_LANG']['tl_leaflet_map']['zoom_legend'] = 'Center and zoom'; $GLOBALS['TL_LANG']['tl_leaflet_map']['controls_legend'] = 'Control widgets'; $GLOBALS['TL_LANG']['tl_leaflet_map']['expert_legend'] = 'Expert settings'; -$GLOBALS['TL_LANG']['tl_leaflet_map']['layers'][0] = 'Manage layers'; -$GLOBALS['TL_LANG']['tl_leaflet_map']['layers'][1] = 'Manage leaflet layers'; +$GLOBALS['TL_LANG']['tl_leaflet_map']['layersBtn'][0] = 'Manage layers'; +$GLOBALS['TL_LANG']['tl_leaflet_map']['layersBtn'][1] = 'Manage leaflet layers'; $GLOBALS['TL_LANG']['tl_leaflet_map']['new'][0] = 'Create map'; $GLOBALS['TL_LANG']['tl_leaflet_map']['new'][1] = 'Create new map'; $GLOBALS['TL_LANG']['tl_leaflet_map']['edit'][0] = 'Edit map'; @@ -61,6 +61,8 @@ $GLOBALS['TL_LANG']['tl_leaflet_map']['options'][0] = 'Extra options' $GLOBALS['TL_LANG']['tl_leaflet_map']['options'][1] = 'Add extra map options as valid json. See http://leafletjs.com/reference.html#map-options'; $GLOBALS['TL_LANG']['tl_leaflet_map']['adjustZoomExtra'][0] = 'Adjust extra zoom settings'; $GLOBALS['TL_LANG']['tl_leaflet_map']['adjustZoomExtra'][1] = 'Enable if you want to adjust minimum and maximum zoom as well'; +$GLOBALS['TL_LANG']['tl_leaflet_map']['layers'][0] = 'Default layers'; +$GLOBALS['TL_LANG']['tl_leaflet_map']['layers'][1] = 'Inital visible layers of the map. For optional layers use the layers control.'; $GLOBALS['TL_LANG']['tl_leaflet_map']['zoomValues'][''][0] = 'Disable'; $GLOBALS['TL_LANG']['tl_leaflet_map']['zoomValues'][''][1] = 'Disable zoom function.'; diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Control.php b/src/Netzmacht/Contao/Leaflet/Dca/Control.php index eebe592..a0a9f07 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Control.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/Control.php @@ -12,6 +12,8 @@ namespace Netzmacht\Contao\Leaflet\Dca; +use Netzmacht\Contao\Leaflet\Model\LayerModel; + class Control { public function generateRow($row) @@ -22,4 +24,18 @@ class Control $row['type'] ); } + + public function getLayers() + { + $options = array(); + $collection = LayerModel::findBy('pid', '0', array('order' => 'title')); + + if ($collection) { + foreach ($collection as $model) { + $options[$model->id] = $model->title; + } + } + + return $options; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Control/AttributionControlMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Control/AttributionControlMapper.php new file mode 100644 index 0000000..7924a7f --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Control/AttributionControlMapper.php @@ -0,0 +1,64 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Mapper\Control; + +use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; +use Netzmacht\LeafletPHP\Definition; +use Netzmacht\LeafletPHP\Definition\Control\Attribution; + +/** + * AttributionControlMapper maps the the attribution database definition to the definition class. + * + * @package Netzmacht\Contao\Leaflet\Mapper\Control + */ +class AttributionControlMapper extends AbstractControlMapper +{ + /** + * Class of the definition being created. + * + * @var string + */ + protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Control\Attribution'; + + /** + * Layer type. + * + * @var string + */ + protected static $type = 'attribution'; + + /** + * {@inheritdoc} + */ + protected function initialize() + { + parent::initialize(); + + $this->addConditionalOption('prefix'); + } + + /** + * {@inheritdoc} + */ + protected function doBuild(Definition $definition, \Model $model, DefinitionMapper $builder) + { + if (!$definition instanceof Attribution) { + return; + } + + $attributions = deserialize($model->attributions, true); + + foreach ($attributions as $attribution) { + $definition->addAttribution($attribution); + } + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php index a641b1e..521f417 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php @@ -12,6 +12,9 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Control; +use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; +use Netzmacht\Contao\Leaflet\Model\LayerModel; + class LayersControlMapper extends AbstractControlMapper { /** @@ -27,4 +30,45 @@ class LayersControlMapper extends AbstractControlMapper * @var string */ protected static $type = 'layers'; + + protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper) + { + $arguments = parent::buildConstructArguments($model, $mapper); + $arguments[1] = array(); + $arguments[2] = array(); + + $definition = $this->getLayersDefinition($model); + $collection = LayerModel::findMultipleByIds(array_keys($definition)); + + if ($collection) { + foreach ($collection as $layer) { + $argument = ($definition[$layer->id] === 'overlay') ? 2 : 1; + + $arguments[$argument][] = $mapper->handle($layer); + } + } + + return $arguments; + } + + /** + * Get layers definition from the control model. + * + * @param \Model $model The control model. + * + * @return array + */ + protected function getLayersDefinition(\Model $model) + { + $layers = deserialize($model->layers, true); + $definition = array(); + + foreach ($layers as $layer) { + if ($layer['layer']) { + $definition[$layer['layer']] = $layer['mode']; + } + } + + return $definition; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php index 8667e2c..1ffcdbb 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php @@ -43,6 +43,11 @@ class DefinitionMapper */ private $mapId; + /** + * @var array + */ + private $mapped = array(); + /** * Construct. * @@ -88,6 +93,12 @@ class DefinitionMapper */ public function handle(\Model $model, $elementId = null) { + $hash = $model->getTable() . '.' . $model->{$model->getPk()}; + + if (isset($this->mapped[$hash])) { + return $this->mapped[$hash]; + } + krsort($this->builders); $this->mapId = $elementId ?: ($model->alias ?: ('map_' . $model->id)); @@ -100,6 +111,8 @@ class DefinitionMapper $event = new BuildDefinitionEvent($definition, $model); $this->eventDispatcher->dispatch($event::NAME, $event); + $this->mapped[$hash] = $definition; + return $definition; } }