From efb36256c7b10ce2d0786ce96e7547029fdc7e7d Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 9 Jan 2015 13:41:09 +0100 Subject: [PATCH] Rework element id generation. --- .../Contao/Leaflet/Mapper/AbstractMapper.php | 49 ++++++++++++++----- .../Control/AttributionControlMapper.php | 8 ++- .../Mapper/Control/LayersControlMapper.php | 13 +++-- .../Leaflet/Mapper/DefinitionMapper.php | 29 +++-------- .../Leaflet/Mapper/Layer/GroupLayerMapper.php | 6 ++- .../Mapper/Layer/ProviderLayerMapper.php | 16 ++++-- .../Contao/Leaflet/Mapper/MapMapper.php | 39 +++++++++------ .../Contao/Leaflet/Mapper/Mapper.php | 9 ++-- .../Leaflet/Mapper/Type/ImageIconMapper.php | 10 ++-- .../Contao/Leaflet/Mapper/UI/MarkerMapper.php | 10 ++-- .../Mapper/Vector/AbstractVectorMapper.php | 17 ++----- .../Leaflet/Mapper/Vector/RectangleMapper.php | 10 ++-- 12 files changed, 128 insertions(+), 88 deletions(-) diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/AbstractMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/AbstractMapper.php index 12f8e72..11d8e7c 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/AbstractMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/AbstractMapper.php @@ -151,9 +151,9 @@ abstract class AbstractMapper implements Mapper /** * {@inheritdoc} */ - public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null) + public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null, $elementId = null) { - $definition = $this->createInstance($model, $mapper, $bounds); + $definition = $this->createInstance($model, $mapper, $bounds, $elementId); $this->buildOptions($definition, $model); $this->buildConditionals($definition, $model); @@ -202,16 +202,21 @@ abstract class AbstractMapper implements Mapper /** * Create a new definition instance. * - * @param \Model $model The model. - * @param DefinitionMapper $mapper The definition mapper. - * @param LatLngBounds $bounds Optional bounds where elements should be in. + * @param \Model $model The model. + * @param DefinitionMapper $mapper The definition mapper. + * @param LatLngBounds $bounds Optional bounds where elements should be in. + * @param string|null $elementId Optional element id. * * @return Definition */ - protected function createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) - { + protected function createInstance( + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null, + $elementId = null + ) { $reflector = new \ReflectionClass($this->getClassName($model, $mapper, $bounds)); - $instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds)); + $instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds, $elementId)); return $instance; } @@ -222,13 +227,18 @@ abstract class AbstractMapper implements Mapper * @param \Model $model The model. * @param DefinitionMapper $mapper The definition mapper. * @param LatLngBounds $bounds Optional bounds where elements should be in. + * @param string|null $elementId Optional element id. * * @return array */ - protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) - { + protected function buildConstructArguments( + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null, + $elementId = null + ) { return array( - $model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id) + $this->getElementId($model, $elementId) ); } @@ -346,4 +356,21 @@ abstract class AbstractMapper implements Mapper { return static::$definitionClass; } + + /** + * Create element id for the model. + * + * @param \Model $model The model being passed. + * @param string|null $elementId Optional forced id. + * + * @return string + */ + protected function getElementId(\Model $model, $elementId = null) + { + if ($elementId) { + return $elementId; + } + + return $model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id); + } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Control/AttributionControlMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Control/AttributionControlMapper.php index 23327d5..6a777af 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Control/AttributionControlMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Control/AttributionControlMapper.php @@ -50,8 +50,12 @@ class AttributionControlMapper extends AbstractControlMapper /** * {@inheritdoc} */ - protected function build(Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null) - { + protected function build( + Definition $definition, + \Model $model, + DefinitionMapper $builder, + LatLngBounds $bounds = null + ) { if (!$definition instanceof Attribution) { return; } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php index eee6e1f..2080946 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php @@ -32,9 +32,16 @@ class LayersControlMapper extends AbstractControlMapper */ protected static $type = 'layers'; - protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) - { - $arguments = parent::buildConstructArguments($model, $mapper, $bounds); + /** + * {@inheritdoc} + */ + protected function buildConstructArguments( + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null, + $elementId = null + ) { + $arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId); $arguments[1] = array(); $arguments[2] = array(); diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php index bacc801..5db90ca 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php @@ -39,13 +39,6 @@ class DefinitionMapper */ private $eventDispatcher; - /** - * Map id of the current built map. - * - * @var string - */ - private $mapId; - /** * @var array */ @@ -73,21 +66,11 @@ class DefinitionMapper { $this->builders[$priority][] = $builder; - ksort($this->builders); + krsort($this->builders); return $this; } - /** - * Get the map id of the current built map. - * - * @return string - */ - public function getMapId() - { - return $this->mapId; - } - /** * Build a model. * @@ -105,15 +88,15 @@ class DefinitionMapper return $this->mapped[$hash]; } - $this->mapId = $elementId ?: ($model->alias ?: ('map_' . $model->id)); - foreach ($this->builders as $builders) { foreach($builders as $builder) { if ($builder->match($model)) { - $definition = $builder->handle($model, $this, $bounds); + $definition = $builder->handle($model, $this, $bounds, $elementId); - $event = new BuildDefinitionEvent($definition, $model, $bounds); - $this->eventDispatcher->dispatch($event::NAME, $event); + if ($definition) { + $event = new BuildDefinitionEvent($definition, $model, $bounds); + $this->eventDispatcher->dispatch($event::NAME, $event); + } $this->mapped[$hash] = $definition; diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/GroupLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/GroupLayerMapper.php index 9f8324d..e37bc45 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/GroupLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/GroupLayerMapper.php @@ -68,9 +68,11 @@ class GroupLayerMapper extends AbstractLayerMapper if ($collection) { foreach ($collection as $layerModel) { - /** @var Layer $layer */ $layer = $mapper->handle($layerModel); - $definition->addLayer($layer); + + if ($layer instanceof Layer) { + $definition->addLayer($layer); + } } } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ProviderLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ProviderLayerMapper.php index f8e534a..1c80d77 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ProviderLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ProviderLayerMapper.php @@ -65,8 +65,12 @@ class ProviderLayerMapper extends AbstractLayerMapper /** * {@inheritdoc} */ - protected function build(Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null) - { + protected function build( + Definition $definition, + \Model $model, + DefinitionMapper $builder, + LatLngBounds $bounds = null + ) { if (!empty($this->providers[$model->tile_provider]['options'])) { $this->applyOptions( $this->providers[$model->tile_provider]['options'], @@ -79,8 +83,12 @@ class ProviderLayerMapper extends AbstractLayerMapper /** * {@inheritdoc} */ - protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) - { + protected function buildConstructArguments( + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null, + $elementId = null + ) { return array( $model->alias ?: ('layer_' . $model->id), $model->tile_provider, diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php index 972ba65..f33450e 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php @@ -15,9 +15,11 @@ use Netzmacht\Contao\Leaflet\Model\ControlModel; use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\MapModel; use Netzmacht\LeafletPHP\Definition; +use Netzmacht\LeafletPHP\Definition\Control; +use Netzmacht\LeafletPHP\Definition\Layer; use Netzmacht\LeafletPHP\Definition\Map; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; -use Netzmacht\LeafletPHP\Plugins\LeafletProviders\Provider; + class MapMapper extends AbstractMapper { @@ -63,11 +65,15 @@ class MapMapper extends AbstractMapper /** * @inheritdoc */ - protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) - { + protected function buildConstructArguments( + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null, + $elementId = null + ) { return array( - $mapper->getMapId(), - $mapper->getMapId() + $this->getElementId($model, $elementId), + $this->getElementId($model, $elementId) ); } @@ -96,15 +102,16 @@ class MapMapper extends AbstractMapper */ private function buildControls(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) { - $collection = ControlModel::findBy( - array('pid=?', 'active=1'), - array($model->id), - array('order' => 'sorting') - ); + $collection = ControlModel::findActiveBy('pid', $model->id, array('order' => 'sorting')); - if ($collection) { - foreach ($collection as $control) { - $control = $mapper->handle($control, $bounds); + if (!$collection) { + return; + } + + foreach ($collection as $control) { + $control = $mapper->handle($control, $bounds); + + if ($control instanceof Control) { $map->addControl($control); } } @@ -130,9 +137,9 @@ class MapMapper extends AbstractMapper } $layer = $mapper->handle($layer, $bounds); - - /** @var Provider $layer */ - $map->addLayer($layer); + if ($layer instanceof Layer) { + $map->addLayer($layer); + } } } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Mapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Mapper.php index 4637310..5bcd748 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Mapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Mapper.php @@ -19,13 +19,14 @@ interface Mapper /** * Map model to the definition. * - * @param \Model|mixed $model The model being built. Usually a contao model. but can be anything - * @param DefinitionMapper $mapper The definition builder. - * @param LatLngBounds $bounds Optional bounds where elements should be in. + * @param \Model|mixed $model The model being built. Usually a contao model. but can be anything + * @param DefinitionMapper $mapper The definition builder. + * @param LatLngBounds $bounds Optional bounds where elements should be in. + * @param string $elementId Optional element id. * * @return Definition */ - public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null); + public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null, $elementId = null); /** * Check if mapper is responsible for the model. diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Type/ImageIconMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Type/ImageIconMapper.php index 68e7288..b0e6d19 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Type/ImageIconMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Type/ImageIconMapper.php @@ -37,9 +37,13 @@ class ImageIconMapper extends AbstractIconMapper /** * {@inheritdoc} */ - protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) - { - $arguments = parent::buildConstructArguments($model, $mapper, $bounds); + protected function buildConstructArguments( + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null, + $elementId = null + ) { + $arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId); if ($model->iconImage) { $file = \FilesModel::findByUuid($model->iconImage); diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php index a11a9ce..bc68917 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/UI/MarkerMapper.php @@ -39,9 +39,13 @@ class MarkerMapper extends AbstractMapper /** * {@inheritdoc} */ - protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) - { - $arguments = parent::buildConstructArguments($model, $mapper, $bounds); + protected function buildConstructArguments( + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null, + $elementId = null + ) { + $arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId); $arguments[] = $model->coordinates ?: null; return $arguments; diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php index e228ffd..ce4201b 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php @@ -30,20 +30,9 @@ class AbstractVectorMapper extends AbstractTypeMapper */ protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\VectorModel'; - protected function initialize() - { - parent::initialize(); - -// $this -// ->addOptions('stroke', 'weight', 'opacity', 'clickable', 'className') -// ->addConditionalOption('color') -// ->addConditionalOption('lineCap') -// ->addConditionalOption('lineJoin') -// ->addConditionalOption('dashArray') -// ->addConditionalOptions('fill', array('fill', 'fillColor', 'fillOpacity')) -// ; - } - + /** + * {@inheritdoc} + */ protected function build( Definition $definition, \Model $model, diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/RectangleMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/RectangleMapper.php index c41817f..59fca69 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/RectangleMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/RectangleMapper.php @@ -40,8 +40,12 @@ class RectangleMapper extends AbstractVectorMapper parent::initialize(); } - protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) - { + protected function buildConstructArguments( + \Model $model, + DefinitionMapper $mapper, + LatLngBounds $bounds = null, + $elementId = null + ) { $latLngs = array_map( function($latLng) { return LatLng::fromString($latLng); @@ -49,7 +53,7 @@ class RectangleMapper extends AbstractVectorMapper deserialize($model->bounds, true) ); - $arguments = parent::buildConstructArguments($model, $mapper, $bounds); + $arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId); $arguments[] = new LatLngBounds($latLngs[0], $latLngs[1]); return $arguments;