From 12040f24cc4e97add7a69b8f8747e2247cf70f72 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 9 Jan 2015 23:00:38 +0100 Subject: [PATCH] Add undo relations deleting callback. --- src/Netzmacht/Contao/Leaflet/Dca/Layer.php | 54 +++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Layer.php b/src/Netzmacht/Contao/Leaflet/Dca/Layer.php index 12787bb..818fc0a 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Layer.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/Layer.php @@ -13,17 +13,27 @@ namespace Netzmacht\Contao\Leaflet\Dca; use Netzmacht\Contao\DevTools\Dca\Options\OptionsBuilder; +use Netzmacht\Contao\DevTools\ServiceContainerTrait; use Netzmacht\Contao\Leaflet\Model\LayerModel; class Layer { + use ServiceContainerTrait; + private $layers; + /** + * @var \Database + */ + private $database; + public function __construct() { - $this->layers = &$GLOBALS['LEAFLET_LAYERS']; + $this->layers = &$GLOBALS['LEAFLET_LAYERS']; + $this->database = static::getService('database.connection'); \Controller::loadLanguageFile('leaflet_layer'); + } public function getVariants($dataContainer) @@ -159,6 +169,48 @@ class Layer ->getOptions(); } + + public function deleteRelations($dataContainer, $undoId) + { + if ($undoId) { + $undo = $this->database + ->prepare('SELECT * FROM tl_undo WHERE id=?') + ->limit(1) + ->execute($undoId) + ->row(); + + $result = $this->database + ->prepare('SELECT * FROM tl_leaflet_map_layer WHERE lid=?') + ->execute($dataContainer->id); + + $undo['data'] = deserialize($undo['data'], true); + + while ($result->next()) { + $undo['data']['tl_leaflet_map_layer'][] = $result->row(); + } + + $result = $this->database + ->prepare('SELECT * FROM tl_leaflet_control_layer WHERE lid=?') + ->execute($dataContainer->id); + + while ($result->next()) { + $undo['data']['tl_leaflet_control_layer'][] = $result->row(); + } + + $this->database->prepare('UPDATE tl_undo %s WHERE id=?') + ->set(array('data' => $undo['data'])) + ->execute($undo['id']); + } + + $this->database + ->prepare('DELETE FROM tl_leaflet_map_layer WHERE lid=?') + ->execute($dataContainer->id); + + $this->database + ->prepare('DELETE FROM tl_leaflet_control_layer WHERE lid=?') + ->execute($dataContainer->id); + } + /** * @param $row * @param $href