From b47e6ccb8a0e5aec3b8e4af8801d4aac40a65b01 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Wed, 7 Jan 2015 12:13:26 +0100 Subject: [PATCH] Add layer group support. --- module/config/config.php | 13 +-- module/dca/tl_leaflet_layer.php | 19 +++++ module/languages/en/tl_leaflet_layer.php | 7 ++ .../Leaflet/Mapper/Layer/GroupLayerMapper.php | 80 +++++++++++++++++++ 4 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 src/Netzmacht/Contao/Leaflet/Mapper/Layer/GroupLayerMapper.php diff --git a/module/config/config.php b/module/config/config.php index da37800..3c0a9fb 100644 --- a/module/config/config.php +++ b/module/config/config.php @@ -41,6 +41,7 @@ $GLOBALS['LEAFLET_MAPPERS'] = array(); $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\MapMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\ProviderLayerMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\MarkersLayerMapper'; +$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\GroupLayerMapper'; $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'; @@ -76,11 +77,6 @@ $GLOBALS['LEAFLET_ENCODERS'][] = 'Netzmacht\Contao\Leaflet\Subscriber\EncoderSub */ $GLOBALS['LEAFLET_LAYERS'] = array ( - 'default' => array - ( - 'children' => true, - 'icon' => '', - ), 'provider' => array ( 'children' => false, @@ -91,13 +87,6 @@ $GLOBALS['LEAFLET_LAYERS'] = array 'children' => true, 'icon' => 'system/modules/leaflet/assets/img/group.png', ), - 'elements' => array - ( - 'children' => true, - 'filter' => 'whitelist', - 'layers' => array('markers', 'vectors'), - 'icon' => 'system/modules/leaflet/assets/img/map.png', - ), 'markers' => array ( 'children' => false, diff --git a/module/dca/tl_leaflet_layer.php b/module/dca/tl_leaflet_layer.php index f7f37f9..5c9f22f 100644 --- a/module/dca/tl_leaflet_layer.php +++ b/module/dca/tl_leaflet_layer.php @@ -127,6 +127,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array '+title' => array('markerCluster'), '+active' => array('deferred') ), + 'group extends default' => array( + '+title' => array('groupType'), + ) , ), 'metasubselectpalettes' => array( @@ -263,5 +266,21 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'eval' => array('tl_class' => 'w50', 'submitOnChange' => false), 'sql' => "char(1) NOT NULL default ''" ), + 'groupType' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupType'], + 'exclude' => true, + 'inputType' => 'select', + 'eval' => array( + 'mandatory' => true, + 'tl_class' => 'w50', + 'submitOnChange' => true, + 'helpwizard' => true, + ), + 'default' => 'layer', + 'options' => array('layer', 'feature'), + 'reference' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes'], + 'sql' => "varchar(32) NOT NULL default ''" + ), ) ); diff --git a/module/languages/en/tl_leaflet_layer.php b/module/languages/en/tl_leaflet_layer.php index f8074e0..d06deb3 100644 --- a/module/languages/en/tl_leaflet_layer.php +++ b/module/languages/en/tl_leaflet_layer.php @@ -38,3 +38,10 @@ $GLOBALS['TL_LANG']['tl_leaflet_layer']['markerCluster'][0] = 'Marker cl $GLOBALS['TL_LANG']['tl_leaflet_layer']['markerCluster'][1] = 'Choose a marker cluster layer so that markers get clustered.'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['deferred'][0] = 'Deferred loading'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['deferred'][1] = 'Load data of the layer deferred using ajax.'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupType'][0] = 'Group type'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupType'][1] = 'Choose a layer group type.'; + +$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][0] = 'Layer group'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][1] = 'Basic layer group.
See http://leafletjs.com/reference.html#layergroup'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['feature'][0] = 'Feature group'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['feature'][1] = 'Extended layer group with events and popup support.
See http://leafletjs.com/reference.html#featuregroup'; diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/GroupLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/GroupLayerMapper.php new file mode 100644 index 0000000..342037b --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/GroupLayerMapper.php @@ -0,0 +1,80 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Mapper\Layer; + + +use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; +use Netzmacht\Contao\Leaflet\Model\LayerModel; +use Netzmacht\LeafletPHP\Definition; +use Netzmacht\LeafletPHP\Definition\Group\LayerGroup; +use Netzmacht\LeafletPHP\Definition\Layer; +use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; + +class GroupLayerMapper extends AbstractLayerMapper +{ + /** + * Class of the definition being created. + * + * @var string + */ + protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Group\LayerGroup'; + + /** + * Layer type. + * + * @var string + */ + protected static $type = 'group'; + + /** + * {@inheritdoc} + */ + protected function createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) + { + $class = $model->groupType === 'feature' + ? 'Netzmacht\LeafletPHP\Definition\Group\FeatureGroup' + : static::$definitionClass; + + $reflector = new \ReflectionClass($class); + $instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds)); + + return $instance; + } + + /** + * {@inheritdoc} + */ + protected function doBuild( + Definition $definition, + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null + ) { + if (!$definition instanceof LayerGroup) { + return; + } + + $collection = LayerModel::findBy( + array('pid=?', 'active=1'), + array($model->id), + array('order' => 'sorting') + ); + + if ($collection) { + foreach ($collection as $layerModel) { + /** @var Layer $layer */ + $layer = $mapper->handle($layerModel); + $definition->addLayer($layer); + } + } + } +}