From fde6c2b4c760d918207df8dec8fc756e058e4e3f Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 6 Oct 2017 13:17:49 +0200 Subject: [PATCH] Rework control callbacks as a listener service. --- .../Dca/ControlDcaListener.php} | 93 +++++++++---------- src/Resources/config/listeners.yml | 6 ++ src/Resources/contao/config/services.php | 4 +- .../contao/dca/tl_leaflet_control.php | 10 +- 4 files changed, 58 insertions(+), 55 deletions(-) rename src/{Dca/ControlCallbacks.php => Listeners/Dca/ControlDcaListener.php} (59%) diff --git a/src/Dca/ControlCallbacks.php b/src/Listeners/Dca/ControlDcaListener.php similarity index 59% rename from src/Dca/ControlCallbacks.php rename to src/Listeners/Dca/ControlDcaListener.php index 99c0732..9947e2e 100644 --- a/src/Dca/ControlCallbacks.php +++ b/src/Listeners/Dca/ControlDcaListener.php @@ -10,9 +10,10 @@ * @filesource */ -namespace Netzmacht\Contao\Leaflet\Dca; +namespace Netzmacht\Contao\Leaflet\Listeners\Dca; -use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks; +use Doctrine\DBAL\Connection; +use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener; use Netzmacht\Contao\Toolkit\Dca\Manager; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; use Netzmacht\Contao\Leaflet\Model\ControlModel; @@ -23,7 +24,7 @@ use Netzmacht\Contao\Leaflet\Model\LayerModel; * * @package Netzmacht\Contao\Leaflet\Dca */ -class ControlCallbacks extends Callbacks +class ControlDcaListener extends AbstractListener { /** * Name of the data container. @@ -32,31 +33,24 @@ class ControlCallbacks extends Callbacks */ protected static $name = 'tl_leaflet_control'; - /** - * Helper service name. - * - * @var string - */ - protected static $serviceName = 'leaflet.dca.control-callbacks'; - /** * The database connection. * - * @var \Database + * @var Connection */ - private $database; + private $connection; /** * Construct. * - * @param Manager $manager Data container manager. - * @param \Database $database Database connection. + * @param Manager $manager Data container manager. + * @param Connection $connection Database connection. */ - public function __construct(Manager $manager, \Database $database) + public function __construct(Manager $manager, Connection $connection) { parent::__construct($manager); - $this->database = $database; + $this->connection = $connection; } /** @@ -113,11 +107,13 @@ class ControlCallbacks extends Callbacks */ 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); + $query = 'SELECT lid As layer, mode FROM tl_leaflet_control_layer WHERE cid=:cid ORDER BY sorting'; + $statement = $this->connection->prepare($query); + $statement->bindValue('cid', $dataContainer->id); - return $result->fetchAllAssoc(); + $statement->execute(); + + return $statement->fetchAll(); } /** @@ -132,43 +128,40 @@ class ControlCallbacks extends Callbacks { $new = deserialize($layers, true); $values = array(); - $result = $this->database - ->prepare('SELECT * FROM tl_leaflet_control_layer WHERE cid=? order BY sorting') - ->execute($dataContainer->id); + $query = 'SELECT * FROM tl_leaflet_control_layer WHERE cid=:cid order BY sorting'; + $statement = $this->connection->prepare($query); + $statement->bindValue('cid', $dataContainer->id); - while ($result->next()) { - $values[$result->lid] = $result->row(); + while ($row = $statement->fetch()) { + $values[$row['lid']] = $row; } $sorting = 0; foreach ($new as $layer) { if (!isset($values[$layer['layer']])) { - $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(); + $data = [ + 'tstamp' => time(), + 'lid' => $layer['layer'], + 'cid' => $dataContainer->id, + 'mode' => $layer['mode'], + 'sorting' => $sorting + ]; + $this->connection->insert('tl_leaflet_control_layer', $data); $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']); + $this->connection->update( + 'tl_leaflet_control_layer', + [ + 'tstamp' => time(), + 'sorting' => $sorting, + 'mode' => $layer['mode'] + ], + [ + 'id' => $values[$layer['layer']]['id'] + ] + ); $sorting += 128; unset($values[$layer['layer']]); @@ -183,7 +176,11 @@ class ControlCallbacks extends Callbacks ); if ($ids) { - $this->database->query('DELETE FROM tl_leaflet_control_layer WHERE id IN(' . implode(',', $ids) . ')'); + $this->connection->executeUpdate( + 'DELETE FROM tl_leaflet_control_layer WHERE id IN(?)', + [$ids], + [Connection::PARAM_INT_ARRAY] + ); } return null; diff --git a/src/Resources/config/listeners.yml b/src/Resources/config/listeners.yml index 283abd0..721e3ae 100644 --- a/src/Resources/config/listeners.yml +++ b/src/Resources/config/listeners.yml @@ -4,3 +4,9 @@ services: arguments: - '@netzmacht.contao_toolkit.dca.manager' - '@database_connection' + + netzmacht.contao_leaflet_maps.listeners.dca.control: + class: Netzmacht\Contao\Leaflet\Listeners\Dca\ControlDcaListener + 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 ad6edbf..10c41cf 100644 --- a/src/Resources/contao/config/services.php +++ b/src/Resources/contao/config/services.php @@ -16,7 +16,7 @@ use Interop\Container\ContainerInterface; use Netzmacht\Contao\Leaflet\Alias\DefaultAliasFilter; use Netzmacht\Contao\Leaflet\Boot; use Netzmacht\Contao\Leaflet\ContaoAssets; -use Netzmacht\Contao\Leaflet\Dca\ControlCallbacks; +use Netzmacht\Contao\Leaflet\Listeners\Dca\ControlDcaListener; use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration; use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks; use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks; @@ -260,7 +260,7 @@ $container['leaflet.dca.layer-callbacks'] = $container->share( $container['leaflet.dca.control-callbacks'] = $container->share( function ($container) { - return new ControlCallbacks( + return new ControlDcaListener( $container[Services::DCA_MANAGER], $container[Services::DATABASE_CONNECTION] ); diff --git a/src/Resources/contao/dca/tl_leaflet_control.php b/src/Resources/contao/dca/tl_leaflet_control.php index be6bc71..88c10a3 100644 --- a/src/Resources/contao/dca/tl_leaflet_control.php +++ b/src/Resources/contao/dca/tl_leaflet_control.php @@ -42,7 +42,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'flag' => 1, 'sorting' => 2, 'panelLayout' => 'filter,sort;search,limit', - 'child_record_callback' => \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('generateRow'), + 'child_record_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.control', 'generateRow'], ), 'label' => array ( @@ -286,10 +286,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'exclude' => true, 'inputType' => 'multiColumnWizard', 'load_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('loadLayerRelations'), + ['netzmacht.contao_leaflet_maps.listeners.dca.control', 'loadLayerRelations'], ), 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('saveLayerRelations'), + ['netzmacht.contao_leaflet_maps.listeners.dca.control', 'saveLayerRelations'], ), 'eval' => array ( @@ -301,7 +301,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['layer'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('getLayers'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Listeners\Dca\ControlDcaListener::callback('getLayers'), 'eval' => array( 'style' => 'width: 300px', 'chosen' => true, @@ -388,7 +388,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['zoomControl'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('getZoomControls'), + 'options_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.control', 'getZoomControls'], 'reference' => &$GLOBALS['TL_LANG']['tl_leaflet_control'], 'eval' => array( 'mandatory' => false,