From ff2c3fb8c30a9516acb550e0b516392940918699 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 9 Jan 2015 22:33:57 +0100 Subject: [PATCH] Store layer relations in extra table. --- module/dca/tl_leaflet_control.php | 6 ++ module/dca/tl_leaflet_control_layer.php | 43 +++++++++ module/dca/tl_leaflet_map.php | 8 +- module/dca/tl_leaflet_map_layer.php | 39 ++++++++ module/dca/tl_leaflet_vector.php | 4 +- module/languages/en/tl_leaflet_vector.php | 2 + src/Netzmacht/Contao/Leaflet/Dca/Control.php | 83 ++++++++++++++++ src/Netzmacht/Contao/Leaflet/Dca/Map.php | 95 +++++++++++++++++++ .../Mapper/Control/LayersControlMapper.php | 28 +----- .../Contao/Leaflet/Mapper/MapMapper.php | 3 +- .../Contao/Leaflet/Model/ControlModel.php | 13 +++ .../Contao/Leaflet/Model/MapModel.php | 12 +++ 12 files changed, 307 insertions(+), 29 deletions(-) create mode 100644 module/dca/tl_leaflet_control_layer.php create mode 100644 module/dca/tl_leaflet_map_layer.php create mode 100644 src/Netzmacht/Contao/Leaflet/Dca/Map.php diff --git a/module/dca/tl_leaflet_control.php b/module/dca/tl_leaflet_control.php index 932f315..8fc90d3 100644 --- a/module/dca/tl_leaflet_control.php +++ b/module/dca/tl_leaflet_control.php @@ -255,6 +255,12 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['layers'], 'exclude' => true, 'inputType' => 'multiColumnWizard', + 'load_callback' => array( + array('Netzmacht\Contao\Leaflet\Dca\Control', 'loadLayerRelations'), + ), + 'save_callback' => array( + array('Netzmacht\Contao\Leaflet\Dca\Control', 'saveLayerRelations'), + ), 'eval' => array ( 'tl_class' => 'clr', diff --git a/module/dca/tl_leaflet_control_layer.php b/module/dca/tl_leaflet_control_layer.php new file mode 100644 index 0000000..858d27a --- /dev/null +++ b/module/dca/tl_leaflet_control_layer.php @@ -0,0 +1,43 @@ + array( + 'dataContainer' => 'Table', + 'sql' => array + ( + 'keys' => array + ( + 'id' => 'primary', + 'cid,lid' => 'unique', + ) + ) + ), + + 'fields' => array + ( + 'id' => array + ( + 'sql' => "int(10) unsigned NOT NULL auto_increment" + ), + 'tstamp' => array + ( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'sorting' => array( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'cid' => array + ( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'lid' => array + ( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'mode' => array + ( + 'sql' => "varchar(16) NOT NULL default ''" + ) + ) +); diff --git a/module/dca/tl_leaflet_map.php b/module/dca/tl_leaflet_map.php index 4b5854c..1914f1f 100644 --- a/module/dca/tl_leaflet_map.php +++ b/module/dca/tl_leaflet_map.php @@ -163,9 +163,15 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'exclude' => true, 'inputType' => 'checkboxWizard', 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getLayers'), - 'default' => '', + 'load_callback' => array( + array('Netzmacht\Contao\Leaflet\Dca\Map', 'loadLayerRelations'), + ), + 'save_callback' => array( + array('Netzmacht\Contao\Leaflet\Dca\Map', 'saveLayerRelations'), + ), 'eval' => array( 'multiple' => true, + 'doNotSaveEmpty' => true, ), 'sql' => "mediumblob NULL" ), diff --git a/module/dca/tl_leaflet_map_layer.php b/module/dca/tl_leaflet_map_layer.php new file mode 100644 index 0000000..87ba6d6 --- /dev/null +++ b/module/dca/tl_leaflet_map_layer.php @@ -0,0 +1,39 @@ + array( + 'dataContainer' => 'Table', + 'sql' => array + ( + 'keys' => array + ( + 'id' => 'primary', + 'mid,lid' => 'unique', + ) + ) + ), + + 'fields' => array + ( + 'id' => array + ( + 'sql' => "int(10) unsigned NOT NULL auto_increment" + ), + 'tstamp' => array + ( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'sorting' => array( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'mid' => array + ( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + 'lid' => array + ( + 'sql' => "int(10) unsigned NOT NULL default '0'" + ), + ) +); diff --git a/module/dca/tl_leaflet_vector.php b/module/dca/tl_leaflet_vector.php index 59fecaf..12286bb 100644 --- a/module/dca/tl_leaflet_vector.php +++ b/module/dca/tl_leaflet_vector.php @@ -37,9 +37,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array ), 'global_operations' => array ( - 'style' => array + 'styles' => array ( - 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_vector']['style'], + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_vector']['styles'], 'href' => 'table=tl_leaflet_style', 'icon' => 'system/modules/leaflet/assets/img/style.png', 'attributes' => 'onclick="Backend.getScrollOffset();"' diff --git a/module/languages/en/tl_leaflet_vector.php b/module/languages/en/tl_leaflet_vector.php index 9f05f5d..6463d20 100644 --- a/module/languages/en/tl_leaflet_vector.php +++ b/module/languages/en/tl_leaflet_vector.php @@ -20,6 +20,8 @@ $GLOBALS['TL_LANG']['tl_leaflet_vector']['cut'][0] = 'Move vector'; $GLOBALS['TL_LANG']['tl_leaflet_vector']['cut'][1] = 'Move vector ID %s'; $GLOBALS['TL_LANG']['tl_leaflet_vector']['toggle'][0] = 'Toggle activation'; $GLOBALS['TL_LANG']['tl_leaflet_vector']['toggle'][1] = 'Toggle vector ID %s activation'; +$GLOBALS['TL_LANG']['tl_leaflet_vector']['styles'][0] = 'Manage styles'; +$GLOBALS['TL_LANG']['tl_leaflet_vector']['styles'][1] = 'Manage vector styles'; $GLOBALS['TL_LANG']['tl_leaflet_vector']['title'][0] = 'Title'; $GLOBALS['TL_LANG']['tl_leaflet_vector']['title'][1] = 'Title of the vector.'; diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Control.php b/src/Netzmacht/Contao/Leaflet/Dca/Control.php index 5077e2c..b448ca8 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Control.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/Control.php @@ -13,11 +13,24 @@ namespace Netzmacht\Contao\Leaflet\Dca; use Netzmacht\Contao\DevTools\Dca\Options\OptionsBuilder; +use Netzmacht\Contao\DevTools\ServiceContainerTrait; use Netzmacht\Contao\Leaflet\Model\ControlModel; use Netzmacht\Contao\Leaflet\Model\LayerModel; class Control { + use ServiceContainerTrait; + + /** + * @var \Database + */ + private $database; + + public function __construct() + { + $this->database = static::getService('database.connection'); + } + public function generateRow($row) { return sprintf( @@ -47,4 +60,74 @@ class Control return OptionsBuilder::fromCollection($collection, 'id', 'title')->getOptions(); } + + public function loadLayerRelations($value, $dataContainer) + { + $result = $this->database + ->prepare('SELECT lid As layer, mode FROM tl_leaflet_control_layer WHERE cid=? ORDER BY sorting') + ->execute($dataContainer->id); + + return $result->fetchAllAssoc(); + } + + public function saveLayerRelations($layers, $dataContainer) + { + $new = deserialize($layers, true); + $values = array(); + $result = $this->database + ->prepare('SELECT * FROM tl_leaflet_control_layer WHERE cid=? order BY sorting') + ->execute($dataContainer->id); + + while ($result->next()) { + $values[$result->lid] = $result->row(); + } + + $sorting = 0; + + foreach ($new as $layer) { + if (!isset($values[$layer['layer']]) || $values[$layer['layer']]['mode'] != $layer['mode']) { + $this->database + ->prepare('INSERT INTO tl_leaflet_control_layer %s') + ->set( + array( + 'tstamp' => time(), + 'lid' => $layer['layer'], + 'cid' => $dataContainer->id, + 'mode' => $layer['mode'], + 'sorting' => $sorting + ) + ) + ->execute(); + + $sorting += 128; + } else { + $this->database + ->prepare('UPDATE tl_leaflet_control_layer %s WHERE id=?') + ->set( + array( + 'tstamp' => time(), + 'sorting' => $sorting, + 'mode' => $layer['mode'] + ) + ) + ->execute($values[$layer['layer']]['id']); + + $sorting += 128; + unset ($values[$layer['layer']]); + } + } + + $ids = array_map( + function ($item) { + return $item['id']; + }, + $values + ); + + if ($ids) { + $this->database->query('DELETE FROM tl_leaflet_control_layer WHERE id IN(' . implode(',', $ids) . ')'); + } + + return null; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Map.php b/src/Netzmacht/Contao/Leaflet/Dca/Map.php new file mode 100644 index 0000000..4cc7607 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Dca/Map.php @@ -0,0 +1,95 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Dca; + + +use Netzmacht\Contao\DevTools\ServiceContainerTrait; + +class Map +{ + use ServiceContainerTrait; + + /** + * @var \Database + */ + private $database; + + public function __construct() + { + $this->database = static::getService('database.connection'); + } + + public function loadLayerRelations($value, $dataContainer) + { + $result = $this->database + ->prepare('SELECT lid FROM tl_leaflet_map_layer WHERE mid=? ORDER BY sorting') + ->execute($dataContainer->id); + + return $result->fetchEach('lid'); + } + + public function saveLayerRelations($layerId, $dataContainer) + { + $new = deserialize($layerId, true); + $values = array(); + $result = $this->database + ->prepare('SELECT * FROM tl_leaflet_map_layer WHERE mid=? order BY sorting') + ->execute($dataContainer->id); + + while ($result->next()) { + $values[$result->lid] = $result->row(); + } + + $sorting = 0; + + foreach ($new as $layerId) { + if (!isset($values[$layerId])) { + $this->database + ->prepare('INSERT INTO tl_leaflet_map_layer %s') + ->set( + array( + 'tstamp' => time(), + 'lid' => $layerId, + 'mid' => $dataContainer->id, + 'sorting' => $sorting + ) + ) + ->execute(); + + $sorting += 128; + } else { + if ($values[$layerId]['sorting'] <= ($sorting - 128) || $values[$layerId]['sorting'] >= ($sorting + 128)) { + $this->database + ->prepare('UPDATE tl_leaflet_map_layer %s WHERE id=?') + ->set(array('tstamp' => time(), 'sorting' => $sorting)) + ->execute($values[$layerId]['id']); + } + + $sorting += 128; + unset ($values[$layerId]); + } + } + + $ids = array_map( + function ($item) { + return $item['id']; + }, + $values + ); + + if ($ids) { + $this->database->query('DELETE FROM tl_leaflet_map_layer WHERE id IN(' . implode(',', $ids) . ')'); + } + + return null; + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php index 2080946..4d0902c 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Control/LayersControlMapper.php @@ -13,6 +13,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Control; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; +use Netzmacht\Contao\Leaflet\Model\ControlModel; use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; @@ -45,12 +46,12 @@ class LayersControlMapper extends AbstractControlMapper $arguments[1] = array(); $arguments[2] = array(); - $definition = $this->getLayersDefinition($model); - $collection = LayerModel::findMultipleByIds(array_keys($definition)); + /** @var ControlModel $model */ + $collection = $model->findLayers(); if ($collection) { foreach ($collection as $layer) { - $argument = ($definition[$layer->id] === 'overlay') ? 2 : 1; + $argument = ($layer->controlMode === 'overlay') ? 2 : 1; $arguments[$argument][] = $mapper->handle($layer, $bounds); } @@ -58,25 +59,4 @@ class LayersControlMapper extends AbstractControlMapper return $arguments; } - - /** - * Get layers definition from the control model. - * - * @param \Model $model The control model. - * - * @return array - */ - protected function getLayersDefinition(\Model $model) - { - $layers = deserialize($model->layers, true); - $definition = array(); - - foreach ($layers as $layer) { - if ($layer['layer']) { - $definition[$layer['layer']] = $layer['mode']; - } - } - - return $definition; - } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php index de06f17..5dbb167 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php @@ -127,8 +127,7 @@ class MapMapper extends AbstractMapper */ private function buildLayers(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) { - $ids = deserialize($model->layers, true); - $collection = LayerModel::findMultipleByIds($ids); + $collection = $model->findLayers(); if ($collection) { foreach ($collection as $layer) { diff --git a/src/Netzmacht/Contao/Leaflet/Model/ControlModel.php b/src/Netzmacht/Contao/Leaflet/Model/ControlModel.php index 6432424..87c0d2d 100644 --- a/src/Netzmacht/Contao/Leaflet/Model/ControlModel.php +++ b/src/Netzmacht/Contao/Leaflet/Model/ControlModel.php @@ -14,4 +14,17 @@ namespace Netzmacht\Contao\Leaflet\Model; class ControlModel extends AbstractActiveModel { protected static $strTable = 'tl_leaflet_control'; + + /** + * @return \Model\Collection + */ + public function findLayers() + { + $query = 'SELECT l.*, c.mode as controlMode FROM tl_leaflet_layer l LEFT JOIN tl_leaflet_control_layer c ON l.id = c.lid WHERE c.cid=?'; + $result = \Database::getInstance() + ->prepare($query) + ->execute($this->id); + + return \Model\Collection::createFromDbResult($result, 'tl_leaflet_layer'); + } } diff --git a/src/Netzmacht/Contao/Leaflet/Model/MapModel.php b/src/Netzmacht/Contao/Leaflet/Model/MapModel.php index 105a35d..359c0f7 100644 --- a/src/Netzmacht/Contao/Leaflet/Model/MapModel.php +++ b/src/Netzmacht/Contao/Leaflet/Model/MapModel.php @@ -16,4 +16,16 @@ class MapModel extends \Model { protected static $strTable = 'tl_leaflet_map'; + /** + * @return \Model\Collection + */ + public function findLayers() + { + $query = 'SELECT l.* FROM tl_leaflet_layer l LEFT JOIN tl_leaflet_map_layer m ON l.id = m.lid WHERE m.mid=?'; + $result = \Database::getInstance() + ->prepare($query) + ->execute($this->id); + + return \Model\Collection::createFromDbResult($result, 'tl_leaflet_layer'); + } }