Rework map callbacks as a listener.

This commit is contained in:
David Molineus
2017-10-06 12:04:39 +02:00
parent f172c42426
commit 307381ddb6
5 changed files with 175 additions and 165 deletions

View File

@@ -1,160 +0,0 @@
<?php
/**
* Leaflet maps for Contao CMS.
*
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @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();
}
}

View File

@@ -0,0 +1,164 @@
<?php
/**
* Leaflet maps for Contao CMS.
*
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @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();
}
}

View File

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

View File

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

View File

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