From f5badd14c560806156492cd4780cb9ae2267f937 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 6 Oct 2017 10:07:19 +0200 Subject: [PATCH] Move layers configuration. --- .../Label/Layer/AbstractLabelRenderer.php | 38 +++++++++ .../Renderer/Label/Layer/ChainRenderer.php | 70 ++++++++++++++++ .../Label/Layer/LayerLabelRenderer.php | 45 +++++++++++ .../Label/Layer/MarkersLabelRenderer.php | 49 ++++++++++++ .../Label/Layer/OverpassLabelRenderer.php | 46 +++++++++++ .../Label/Layer/ProviderLabelRenderer.php | 52 ++++++++++++ .../Label/Layer/ReferenceLabelRenderer.php | 48 +++++++++++ .../Label/Layer/VectorsLabelRenderer.php | 49 ++++++++++++ .../NetzmachtContaoLeafletExtension.php | 1 + src/Resources/config/layers.yml | 80 +++++++++++++++++++ 10 files changed, 478 insertions(+) create mode 100644 src/Backend/Renderer/Label/Layer/AbstractLabelRenderer.php create mode 100644 src/Backend/Renderer/Label/Layer/ChainRenderer.php create mode 100644 src/Backend/Renderer/Label/Layer/LayerLabelRenderer.php create mode 100644 src/Backend/Renderer/Label/Layer/MarkersLabelRenderer.php create mode 100644 src/Backend/Renderer/Label/Layer/OverpassLabelRenderer.php create mode 100644 src/Backend/Renderer/Label/Layer/ProviderLabelRenderer.php create mode 100644 src/Backend/Renderer/Label/Layer/ReferenceLabelRenderer.php create mode 100644 src/Backend/Renderer/Label/Layer/VectorsLabelRenderer.php create mode 100644 src/Resources/config/layers.yml diff --git a/src/Backend/Renderer/Label/Layer/AbstractLabelRenderer.php b/src/Backend/Renderer/Label/Layer/AbstractLabelRenderer.php new file mode 100644 index 0000000..7b18795 --- /dev/null +++ b/src/Backend/Renderer/Label/Layer/AbstractLabelRenderer.php @@ -0,0 +1,38 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; + +/** + * Class AbstractLabelRenderer. + * + * @package Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer + */ +abstract class AbstractLabelRenderer implements LayerLabelRenderer +{ + /** + * {@inheritdoc} + */ + public function supports(array $row): bool + { + return $row['type'] === $this->getLayerType(); + } + + /** + * Get the supported layer type. + * + * @return string + */ + abstract protected function getLayerType(): string; +} diff --git a/src/Backend/Renderer/Label/Layer/ChainRenderer.php b/src/Backend/Renderer/Label/Layer/ChainRenderer.php new file mode 100644 index 0000000..3a8963d --- /dev/null +++ b/src/Backend/Renderer/Label/Layer/ChainRenderer.php @@ -0,0 +1,70 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; + +use Symfony\Component\Translation\TranslatorInterface as Translator; + +/** + * Class ChainRenderer. + * + * @package Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer + */ +final class ChainRenderer implements LayerLabelRenderer +{ + /** + * List of layer label renderer. + * + * @var array|LayerLabelRenderer[] + */ + private $renderer; + + /** + * ChainRenderer constructor. + * + * @param array|LayerLabelRenderer[] $renderer List of layer label renderer. + */ + public function __construct($renderer) + { + $this->renderer = $renderer; + } + + /** + * {@inheritDoc} + */ + public function supports(array $row): bool + { + foreach ($this->renderer as $renderer) { + if ($renderer->supports($row)) { + return true; + } + } + + return false; + } + + /** + * {@inheritDoc} + */ + public function render(array $row, string $label, Translator $translator): string + { + foreach ($this->renderer as $renderer) { + if ($renderer->supports($row)) { + return $renderer->render($row, $label, $translator); + } + } + + return $label; + } +} diff --git a/src/Backend/Renderer/Label/Layer/LayerLabelRenderer.php b/src/Backend/Renderer/Label/Layer/LayerLabelRenderer.php new file mode 100644 index 0000000..dbefd3d --- /dev/null +++ b/src/Backend/Renderer/Label/Layer/LayerLabelRenderer.php @@ -0,0 +1,45 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; + +use Symfony\Component\Translation\TranslatorInterface as Translator; + +/** + * Interface LayerLabelRenderer. + * + * @package Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer + */ +interface LayerLabelRenderer +{ + /** + * Check if current row is supported. + * + * @param array $row Current row. + * + * @return bool + */ + public function supports(array $row): bool; + + /** + * Render the backend label of an layer. + * + * @param array $row Current row. + * @param string $label Default label. + * @param Translator $translator The translator. + * + * @return string + */ + public function render(array $row, string $label, Translator $translator): string; +} diff --git a/src/Backend/Renderer/Label/Layer/MarkersLabelRenderer.php b/src/Backend/Renderer/Label/Layer/MarkersLabelRenderer.php new file mode 100644 index 0000000..20b6cd7 --- /dev/null +++ b/src/Backend/Renderer/Label/Layer/MarkersLabelRenderer.php @@ -0,0 +1,49 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; + +use Netzmacht\Contao\Leaflet\Model\MarkerModel; +use Symfony\Component\Translation\TranslatorInterface as Translator; + +/** + * Class MarkersLabelRenderer. + * + * @package Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer + */ +final class MarkersLabelRenderer extends AbstractLabelRenderer +{ + /** + * {@inheritdoc} + */ + protected function getLayerType(): string + { + return 'markers'; + } + + /** + * {@inheritdoc} + */ + public function render(array $row, string $label, Translator $translator): string + { + $count = MarkerModel::countBy('pid', $row['id']); + $label .= sprintf( + ' (%s %s)', + $count, + $translator->trans('countEntries', [], 'contao_tl_leaflet_layer') + ); + + return $label; + } +} diff --git a/src/Backend/Renderer/Label/Layer/OverpassLabelRenderer.php b/src/Backend/Renderer/Label/Layer/OverpassLabelRenderer.php new file mode 100644 index 0000000..93542b5 --- /dev/null +++ b/src/Backend/Renderer/Label/Layer/OverpassLabelRenderer.php @@ -0,0 +1,46 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; + +use Contao\StringUtil; +use Symfony\Component\Translation\TranslatorInterface as Translator; + +/** + * Class OverpassLabelRenderer. + * + * @package Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer + */ +final class OverpassLabelRenderer extends AbstractLabelRenderer +{ + /** + * {@inheritdoc} + */ + protected function getLayerType(): string + { + return 'vectors'; + } + + /** + * {@inheritdoc} + */ + public function render(array $row, string $label, Translator $translator): string + { + if ($row['overpassQuery']) { + $label .= ' ' . StringUtil::substr($row['overpassQuery'], 50) . ''; + } + + return $label; + } +} diff --git a/src/Backend/Renderer/Label/Layer/ProviderLabelRenderer.php b/src/Backend/Renderer/Label/Layer/ProviderLabelRenderer.php new file mode 100644 index 0000000..7d6b08a --- /dev/null +++ b/src/Backend/Renderer/Label/Layer/ProviderLabelRenderer.php @@ -0,0 +1,52 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; + +use Symfony\Component\Translation\TranslatorInterface as Translator; + +/** + * Backend label renderer for provider layer. + * + * @package Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer + */ +final class ProviderLabelRenderer extends AbstractLabelRenderer +{ + /** + * {@inheritdoc} + */ + protected function getLayerType(): string + { + return 'provider'; + } + + /** + * {@inheritdoc} + */ + public function render(array $row, string $label, Translator $translator): string + { + $langKey = 'leaflet_provider.' . $row['tile_provider'] . '.0'; + $translated = $translator->trans($langKey, [], 'contao_leaflet'); + + if ($translated !== $langKey) { + $provider = $translated; + } else { + $provider = $row['tile_provider']; + } + + $label .= sprintf(' (%s)', $provider); + + return $label; + } +} diff --git a/src/Backend/Renderer/Label/Layer/ReferenceLabelRenderer.php b/src/Backend/Renderer/Label/Layer/ReferenceLabelRenderer.php new file mode 100644 index 0000000..7968928 --- /dev/null +++ b/src/Backend/Renderer/Label/Layer/ReferenceLabelRenderer.php @@ -0,0 +1,48 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; + +use Netzmacht\Contao\Leaflet\Model\LayerModel; +use Symfony\Component\Translation\TranslatorInterface as Translator; + +/** + * Class ReferenceLabelRenderer. + * + * @package Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer + */ +final class ReferenceLabelRenderer extends AbstractLabelRenderer +{ + /** + * {@inheritdoc} + */ + protected function getLayerType(): string + { + return 'vectors'; + } + + /** + * {@inheritdoc} + */ + public function render(array $row, string $label, Translator $translator): string + { + $reference = LayerModel::findByPk($row['reference']); + + if ($reference) { + $label .= ' (' . $reference->title . ')'; + } + + return $label; + } +} diff --git a/src/Backend/Renderer/Label/Layer/VectorsLabelRenderer.php b/src/Backend/Renderer/Label/Layer/VectorsLabelRenderer.php new file mode 100644 index 0000000..f91ce66 --- /dev/null +++ b/src/Backend/Renderer/Label/Layer/VectorsLabelRenderer.php @@ -0,0 +1,49 @@ + + * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; + +use Netzmacht\Contao\Leaflet\Model\VectorModel; +use Symfony\Component\Translation\TranslatorInterface as Translator; + +/** + * Class VectorsLabelRenderer. + * + * @package Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer + */ +final class VectorsLabelRenderer extends AbstractLabelRenderer +{ + /** + * {@inheritdoc} + */ + protected function getLayerType(): string + { + return 'vectors'; + } + + /** + * {@inheritdoc} + */ + public function render(array $row, string $label, Translator $translator): string + { + $count = VectorModel::countBy('pid', $row['id']); + $label .= sprintf( + ' (%s %s)', + $count, + $translator->trans('countEntries', [], 'contao_tl_leaflet_layer') + ); + + return $label; + } +} diff --git a/src/DependencyInjection/NetzmachtContaoLeafletExtension.php b/src/DependencyInjection/NetzmachtContaoLeafletExtension.php index 076c91e..0735618 100644 --- a/src/DependencyInjection/NetzmachtContaoLeafletExtension.php +++ b/src/DependencyInjection/NetzmachtContaoLeafletExtension.php @@ -47,5 +47,6 @@ class NetzmachtContaoLeafletExtension extends Extension $loader->load('filters.yml'); $loader->load('mappers.yml'); $loader->load('encoders.yml'); + $loader->load('layers.yml'); } } diff --git a/src/Resources/config/layers.yml b/src/Resources/config/layers.yml new file mode 100644 index 0000000..6026489 --- /dev/null +++ b/src/Resources/config/layers.yml @@ -0,0 +1,80 @@ +# Leaflet layer types. +# +# The type is used for the database driven definitions. +parameters: + netzmacht.contao_leaflet_maps.layers: + provider: + children: false + icon: 'bundles/netzmachtcontaoleaflet/img/provider.png' + + group: + children: true + icon: 'bundles/netzmachtcontaoleaflet/img/group.png' + + markers: + children: false + icon: 'bundles/netzmachtcontaoleaflet/img/markers.png' + markers: true + boundsMode: + extend: true + fit: 'deferred' + + vectors: + children: false + icon: 'bundles/netzmachtcontaoleaflet/img/vectors.png' + vectors: true + boundsMode: + extend: true + + reference: + children: false + icon: 'bundles/netzmachtcontaoleaflet/img/reference.png' + + markercluster: + children: true + icon: 'bundles/netzmachtcontaoleaflet/img/markercluster.png' + + tile: + children: false + icon: 'bundles/netzmachtcontaoleaflet/img/tile.png' + + overpass: + children: false + icon: 'bundles/netzmachtcontaoleaflet/img/overpass.png' + boundsMode: + extend: true + fit: true + +services: + _defaults: + public: false + + netzmacht.contao_leaflet_maps.layer_label_renderer: + class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\ChainRenderer + arguments: + - [] + + netzmacht.contao_leaflet_maps.layer_label_renderer.markers: + class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\MarkersLabelRenderer + tags: + - { name: 'netzmacht.contao_leaflet_maps.layer_label_renderer' } + + netzmacht.contao_leaflet_maps.layer_label_renderer.overpass: + class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\OverpassLabelRenderer + tags: + - { name: 'netzmacht.contao_leaflet_maps.layer_label_renderer' } + + netzmacht.contao_leaflet_maps.layer_label_renderer.provider: + class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\ProviderLabelRenderer + tags: + - { name: 'netzmacht.contao_leaflet_maps.layer_label_renderer' } + + netzmacht.contao_leaflet_maps.layer_label_renderer.reference: + class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\ReferenceLabelRenderer + tags: + - { name: 'netzmacht.contao_leaflet_maps.layer_label_renderer' } + + netzmacht.contao_leaflet_maps.layer_label_renderer.vectors: + class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\VectorsLabelRenderer + tags: + - { name: 'netzmacht.contao_leaflet_maps.layer_label_renderer' }