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
*/
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;

View File

@@ -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'

View File

@@ -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]
);

View File

@@ -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,