Rework control callbacks as a listener service.

This commit is contained in:
David Molineus
2017-10-06 13:17:49 +02:00
parent 307381ddb6
commit fde6c2b4c7
4 changed files with 58 additions and 55 deletions

View File

@@ -10,9 +10,10 @@
* @filesource * @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\Manager;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\ControlModel; use Netzmacht\Contao\Leaflet\Model\ControlModel;
@@ -23,7 +24,7 @@ use Netzmacht\Contao\Leaflet\Model\LayerModel;
* *
* @package Netzmacht\Contao\Leaflet\Dca * @package Netzmacht\Contao\Leaflet\Dca
*/ */
class ControlCallbacks extends Callbacks class ControlDcaListener extends AbstractListener
{ {
/** /**
* Name of the data container. * Name of the data container.
@@ -32,31 +33,24 @@ class ControlCallbacks extends Callbacks
*/ */
protected static $name = 'tl_leaflet_control'; protected static $name = 'tl_leaflet_control';
/**
* Helper service name.
*
* @var string
*/
protected static $serviceName = 'leaflet.dca.control-callbacks';
/** /**
* The database connection. * The database connection.
* *
* @var \Database * @var Connection
*/ */
private $database; private $connection;
/** /**
* Construct. * Construct.
* *
* @param Manager $manager Data container manager. * @param Manager $manager Data container manager.
* @param \Database $database Database connection. * @param Connection $connection Database connection.
*/ */
public function __construct(Manager $manager, \Database $database) public function __construct(Manager $manager, Connection $connection)
{ {
parent::__construct($manager); parent::__construct($manager);
$this->database = $database; $this->connection = $connection;
} }
/** /**
@@ -113,11 +107,13 @@ class ControlCallbacks extends Callbacks
*/ */
public function loadLayerRelations($value, $dataContainer) public function loadLayerRelations($value, $dataContainer)
{ {
$result = $this->database $query = 'SELECT lid As layer, mode FROM tl_leaflet_control_layer WHERE cid=:cid ORDER BY sorting';
->prepare('SELECT lid As layer, mode FROM tl_leaflet_control_layer WHERE cid=? ORDER BY sorting') $statement = $this->connection->prepare($query);
->execute($dataContainer->id); $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); $new = deserialize($layers, true);
$values = array(); $values = array();
$result = $this->database $query = 'SELECT * FROM tl_leaflet_control_layer WHERE cid=:cid order BY sorting';
->prepare('SELECT * FROM tl_leaflet_control_layer WHERE cid=? order BY sorting') $statement = $this->connection->prepare($query);
->execute($dataContainer->id); $statement->bindValue('cid', $dataContainer->id);
while ($result->next()) { while ($row = $statement->fetch()) {
$values[$result->lid] = $result->row(); $values[$row['lid']] = $row;
} }
$sorting = 0; $sorting = 0;
foreach ($new as $layer) { foreach ($new as $layer) {
if (!isset($values[$layer['layer']])) { if (!isset($values[$layer['layer']])) {
$this->database $data = [
->prepare('INSERT INTO tl_leaflet_control_layer %s') 'tstamp' => time(),
->set( 'lid' => $layer['layer'],
array( 'cid' => $dataContainer->id,
'tstamp' => time(), 'mode' => $layer['mode'],
'lid' => $layer['layer'], 'sorting' => $sorting
'cid' => $dataContainer->id, ];
'mode' => $layer['mode'],
'sorting' => $sorting
)
)
->execute();
$this->connection->insert('tl_leaflet_control_layer', $data);
$sorting += 128; $sorting += 128;
} else { } else {
$this->database $this->connection->update(
->prepare('UPDATE tl_leaflet_control_layer %s WHERE id=?') 'tl_leaflet_control_layer',
->set( [
array( 'tstamp' => time(),
'tstamp' => time(), 'sorting' => $sorting,
'sorting' => $sorting, 'mode' => $layer['mode']
'mode' => $layer['mode'] ],
) [
) 'id' => $values[$layer['layer']]['id']
->execute($values[$layer['layer']]['id']); ]
);
$sorting += 128; $sorting += 128;
unset($values[$layer['layer']]); unset($values[$layer['layer']]);
@@ -183,7 +176,11 @@ class ControlCallbacks extends Callbacks
); );
if ($ids) { 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; return null;

View File

@@ -4,3 +4,9 @@ services:
arguments: arguments:
- '@netzmacht.contao_toolkit.dca.manager' - '@netzmacht.contao_toolkit.dca.manager'
- '@database_connection' - '@database_connection'
netzmacht.contao_leaflet_maps.listeners.dca.control:
class: Netzmacht\Contao\Leaflet\Listeners\Dca\ControlDcaListener
arguments:
- '@netzmacht.contao_toolkit.dca.manager'
- '@database_connection'

View File

@@ -16,7 +16,7 @@ use Interop\Container\ContainerInterface;
use Netzmacht\Contao\Leaflet\Alias\DefaultAliasFilter; use Netzmacht\Contao\Leaflet\Alias\DefaultAliasFilter;
use Netzmacht\Contao\Leaflet\Boot; use Netzmacht\Contao\Leaflet\Boot;
use Netzmacht\Contao\Leaflet\ContaoAssets; 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\FrontendIntegration;
use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks; use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks;
use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks; use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks;
@@ -260,7 +260,7 @@ $container['leaflet.dca.layer-callbacks'] = $container->share(
$container['leaflet.dca.control-callbacks'] = $container->share( $container['leaflet.dca.control-callbacks'] = $container->share(
function ($container) { function ($container) {
return new ControlCallbacks( return new ControlDcaListener(
$container[Services::DCA_MANAGER], $container[Services::DCA_MANAGER],
$container[Services::DATABASE_CONNECTION] $container[Services::DATABASE_CONNECTION]
); );

View File

@@ -42,7 +42,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array
'flag' => 1, 'flag' => 1,
'sorting' => 2, 'sorting' => 2,
'panelLayout' => 'filter,sort;search,limit', '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 'label' => array
( (
@@ -286,10 +286,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array
'exclude' => true, 'exclude' => true,
'inputType' => 'multiColumnWizard', 'inputType' => 'multiColumnWizard',
'load_callback' => array( 'load_callback' => array(
\Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('loadLayerRelations'), ['netzmacht.contao_leaflet_maps.listeners.dca.control', 'loadLayerRelations'],
), ),
'save_callback' => array( 'save_callback' => array(
\Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('saveLayerRelations'), ['netzmacht.contao_leaflet_maps.listeners.dca.control', 'saveLayerRelations'],
), ),
'eval' => array 'eval' => array
( (
@@ -301,7 +301,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['layer'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['layer'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('getLayers'), 'options_callback' => \Netzmacht\Contao\Leaflet\Listeners\Dca\ControlDcaListener::callback('getLayers'),
'eval' => array( 'eval' => array(
'style' => 'width: 300px', 'style' => 'width: 300px',
'chosen' => true, 'chosen' => true,
@@ -388,7 +388,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['zoomControl'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['zoomControl'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', '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'], 'reference' => &$GLOBALS['TL_LANG']['tl_leaflet_control'],
'eval' => array( 'eval' => array(
'mandatory' => false, 'mandatory' => false,