From 307381ddb6403ae181777d8e734d1062bd2425f0 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 6 Oct 2017 12:04:39 +0200 Subject: [PATCH] Rework map callbacks as a listener. --- src/Dca/MapCallbacks.php | 160 ------------------- src/Listeners/Dca/MapDcaListener.php | 164 ++++++++++++++++++++ src/Resources/config/listeners.yml | 6 + src/Resources/contao/config/services.php | 4 +- src/Resources/contao/dca/tl_leaflet_map.php | 6 +- 5 files changed, 175 insertions(+), 165 deletions(-) delete mode 100644 src/Dca/MapCallbacks.php create mode 100644 src/Listeners/Dca/MapDcaListener.php diff --git a/src/Dca/MapCallbacks.php b/src/Dca/MapCallbacks.php deleted file mode 100644 index c599748..0000000 --- a/src/Dca/MapCallbacks.php +++ /dev/null @@ -1,160 +0,0 @@ - - * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. - * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE - * @filesource - */ - -namespace Netzmacht\Contao\Leaflet\Dca; - -use Netzmacht\Contao\Leaflet\Model\LayerModel; -use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks; -use Netzmacht\Contao\Toolkit\Dca\Manager; -use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; - -/** - * Class Map is the helper class for the tl_leaflet_map dca. - * - * @package Netzmacht\Contao\Leaflet\Dca - */ -class MapCallbacks extends Callbacks -{ - /** - * Name of the data container. - * - * @var string - */ - protected static $name = 'tl_leaflet_map'; - - /** - * Helper service name. - * - * @var string - */ - protected static $serviceName = 'leaflet.dca.map-callbacks'; - - /** - * The database connection. - * - * @var \Database - */ - private $database; - - /** - * Construct. - * - * @param Manager $manager Data container manager. - * @param \Database $database Database connection. - */ - public function __construct(Manager $manager, \Database $database) - { - parent::__construct($manager); - - $this->database = $database; - } - - /** - * Load layer relations. - * - * @param mixed $value The actual value. - * @param \DataContainer $dataContainer The data container driver. - * - * @return array - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - 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'); - } - - /** - * Save layer relations. - * - * @param mixed $layerId The layer id values. - * @param \DataContainer $dataContainer The dataContainer driver. - * - * @return null - */ - 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; - } - - /** - * Get all layers except of the current layer. - * - * @param \DataContainer $dataContainer The dataContainer driver. - * - * @return array - */ - public function getLayers($dataContainer) - { - $collection = LayerModel::findBy('id !', $dataContainer->id); - - return OptionsBuilder::fromCollection($collection, 'title') - ->asTree() - ->getOptions(); - } -} diff --git a/src/Listeners/Dca/MapDcaListener.php b/src/Listeners/Dca/MapDcaListener.php new file mode 100644 index 0000000..a09776f --- /dev/null +++ b/src/Listeners/Dca/MapDcaListener.php @@ -0,0 +1,164 @@ + + * @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\Listeners\Dca; + +use Contao\DataContainer; +use Doctrine\DBAL\Connection; +use Netzmacht\Contao\Leaflet\Model\LayerModel; +use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener; +use Netzmacht\Contao\Toolkit\Dca\Manager; +use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; +use PDO; + +/** + * Class Map is the helper class for the tl_leaflet_map dca. + * + * @package Netzmacht\Contao\Leaflet\Dca + */ +class MapDcaListener extends AbstractListener +{ + /** + * Name of the data container. + * + * @var string + */ + protected static $name = 'tl_leaflet_map'; + + /** + * The database connection. + * + * @var Connection + */ + private $connection; + + /** + * Construct. + * + * @param Manager $manager Data container manager. + * @param Connection $connection Database connection. + */ + public function __construct(Manager $manager, Connection $connection) + { + parent::__construct($manager); + + $this->connection = $connection; + } + + /** + * Load layer relations. + * + * @param mixed $value The actual value. + * @param DataContainer $dataContainer The data container driver. + * + * @return array + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function loadLayerRelations($value, $dataContainer): array + { + $statement = $this->connection->prepare('SELECT lid FROM tl_leaflet_map_layer WHERE mid=:mid ORDER BY sorting'); + $statement->bindValue('mid', $dataContainer->id); + + if ($statement->execute()) { + return $statement->fetchAll(PDO::FETCH_ASSOC, PDO::FETCH_COLUMN, 0); + } + + return []; + } + + /** + * Save layer relations. + * + * @param mixed $layerId The layer id values. + * @param DataContainer $dataContainer The dataContainer driver. + * + * @return null + */ + public function saveLayerRelations($layerId, $dataContainer) + { + $new = deserialize($layerId, true); + $values = array(); + $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()) { + $values[$row['lid']] = $row; + } + + $sorting = 0; + + foreach ($new as $layerId) { + if (!isset($values[$layerId])) { + $data = [ + 'tstamp' => time(), + 'lid' => $layerId, + 'mid' => $dataContainer->id, + 'sorting' => $sorting + ]; + + $this->connection->insert('tl_leaflet_map_layer', $data); + $sorting += 128; + } else { + if ($values[$layerId]['sorting'] <= ($sorting - 128) + || $values[$layerId]['sorting'] >= ($sorting + 128) + ) { + $this->connection->update( + 'tl_leaflet_map_layer', + ['tstamp' => time(), 'sorting' => $sorting], + ['id' => $values[$layerId]['id']] + ); + } + + $sorting += 128; + unset($values[$layerId]); + } + } + + $ids = array_map( + function ($item) { + return $item['id']; + }, + $values + ); + + if ($ids) { + $this->connection->executeUpdate( + 'DELETE FROM tl_leaflet_map_layer WHERE id IN(?)', + [$ids], + [Connection::PARAM_INT_ARRAY] + ); + } + + return null; + } + + /** + * Get all layers except of the current layer. + * + * @param DataContainer $dataContainer The dataContainer driver. + * + * @return array + */ + public function getLayers($dataContainer) + { + $collection = LayerModel::findBy('id !', $dataContainer->id); + + return OptionsBuilder::fromCollection($collection, 'title') + ->asTree() + ->getOptions(); + } +} diff --git a/src/Resources/config/listeners.yml b/src/Resources/config/listeners.yml index e69de29..283abd0 100644 --- a/src/Resources/config/listeners.yml +++ b/src/Resources/config/listeners.yml @@ -0,0 +1,6 @@ +services: + netzmacht.contao_leaflet_maps.listeners.dca.map: + class: Netzmacht\Contao\Leaflet\Listeners\Dca\MapDcaListener + arguments: + - '@netzmacht.contao_toolkit.dca.manager' + - '@database_connection' diff --git a/src/Resources/contao/config/services.php b/src/Resources/contao/config/services.php index 1680757..ad6edbf 100644 --- a/src/Resources/contao/config/services.php +++ b/src/Resources/contao/config/services.php @@ -20,7 +20,7 @@ use Netzmacht\Contao\Leaflet\Dca\ControlCallbacks; use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration; use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks; use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks; -use Netzmacht\Contao\Leaflet\Dca\MapCallbacks; +use Netzmacht\Contao\Leaflet\Listeners\Dca\MapDcaListener; use Netzmacht\Contao\Leaflet\Dca\Validator; use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks; use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices; @@ -230,7 +230,7 @@ $container[LeafletServices::PARENT_ALIAS_GENERATOR] = $container->share( $container['leaflet.dca.map-callbacks'] = $container->share( function ($container) { - return new MapCallbacks( + return new MapDcaListener( $container[Services::DCA_MANAGER], $container[Services::DATABASE_CONNECTION] ); diff --git a/src/Resources/contao/dca/tl_leaflet_map.php b/src/Resources/contao/dca/tl_leaflet_map.php index 17b4193..edf1964 100644 --- a/src/Resources/contao/dca/tl_leaflet_map.php +++ b/src/Resources/contao/dca/tl_leaflet_map.php @@ -209,10 +209,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'exclude' => true, 'inputType' => 'multiColumnWizard', 'load_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\MapCallbacks::callback('loadLayerRelations'), + ['netzmacht.contao_leaflet_maps.listeners.dca.map', 'loadLayerRelations'], ), 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\MapCallbacks::callback('saveLayerRelations'), + ['netzmacht.contao_leaflet_maps.listeners.dca.map', 'saveLayerRelations'], ), 'eval' => array( 'multiple' => true, @@ -223,7 +223,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['reference'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\MapCallbacks::callback('getLayers'), + 'options_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.map', 'getLayers'], 'eval' => array( 'mandatory' => true, 'tl_class' => 'w50',