From e5263a6315146ba83e88eca29ec8130a6db23e4c Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 28 Aug 2020 16:56:11 +0200 Subject: [PATCH] Copy map layer relations (Fix #89) --- CHANGELOG.md | 4 ++++ .../Resources/contao/dca/tl_leaflet_map.php | 3 +++ src/Listener/Dca/MapDcaListener.php | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47692a7..baa2fdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + + - Duplicate map layer relations ([#89](https://github.com/netzmacht/contao-leaflet-maps/issues/89)) + ## [3.1.6] - 2020-01-03 ### Fixed diff --git a/src/Bundle/Resources/contao/dca/tl_leaflet_map.php b/src/Bundle/Resources/contao/dca/tl_leaflet_map.php index 2a6c615..2d54fa5 100644 --- a/src/Bundle/Resources/contao/dca/tl_leaflet_map.php +++ b/src/Bundle/Resources/contao/dca/tl_leaflet_map.php @@ -28,6 +28,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = [ 'onsubmit_callback' => [ ['netzmacht.contao_leaflet.listeners.dca.leaflet', 'clearCache'], ], + 'oncopy_callback' => [ + ['netzmacht.contao_leaflet.listeners.dca.map', 'copyLayerRelations'], + ], ], 'list' => [ diff --git a/src/Listener/Dca/MapDcaListener.php b/src/Listener/Dca/MapDcaListener.php index 5aa45c7..c345334 100644 --- a/src/Listener/Dca/MapDcaListener.php +++ b/src/Listener/Dca/MapDcaListener.php @@ -226,4 +226,26 @@ class MapDcaListener extends AbstractListener ->asTree() ->getOptions(); } + + /** + * Copy layer relations if a map is duplicated. + * + * @param string|int $insertId Insert id of the new map. + * @param DataContainer $dataContainer Data container driver. + * + * @return void + */ + public function copyLayerRelations($insertId, DataContainer $dataContainer): void + { + $statement = $this->connection->prepare('SELECT * FROM tl_leaflet_map_layer WHERE mid=:mid order BY sorting'); + $statement->bindValue('mid', $dataContainer->id); + $statement->execute(); + + while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { + unset($row['id']); + $row['tstamp'] = time(); + $row['mid'] = $insertId; + $this->connection->insert('tl_leaflet_map_layer', $row); + } + } }