Rework layer callbacks to an listener service.

This commit is contained in:
David Molineus
2017-10-06 13:33:19 +02:00
parent e67b152225
commit 598adb54b1
4 changed files with 66 additions and 52 deletions

View File

@@ -12,6 +12,11 @@
namespace Netzmacht\Contao\Leaflet\Listener\Dca; namespace Netzmacht\Contao\Leaflet\Listener\Dca;
use Contao\Controller;
use Contao\Image;
use Contao\StringUtil;
use Doctrine\DBAL\Connection;
use Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\LayerLabelRenderer;
use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener; 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;
@@ -42,9 +47,9 @@ class LayerDcaListener extends AbstractListener
/** /**
* The database connection. * The database connection.
* *
* @var \Database * @var Connection
*/ */
private $database; private $connection;
/** /**
* Tile providers configuration. * Tile providers configuration.
@@ -67,33 +72,43 @@ class LayerDcaListener extends AbstractListener
*/ */
private $amenities; private $amenities;
/**
* Layer label renderer.
*
* @var LayerLabelRenderer
*/
private $labelRenderer;
/** /**
* Construct. * Construct.
* *
* @param Manager $manager Data container manager. * @param Manager $manager Data container manager.
* @param \Database $database Database connection. * @param Connection $connection Database connection.
* @param Translator $translator Translator. * @param Translator $translator Translator.
* @param LayerLabelRenderer $labelRenderer Layer label renderer.
* @param array $layers Leaflet layer configuration. * @param array $layers Leaflet layer configuration.
* @param array $tileProviders Tile providers. * @param array $tileProviders Tile providers.
* @param array $amenities OSM amenities. * @param array $amenities OSM amenities.
*/ */
public function __construct( public function __construct(
Manager $manager, Manager $manager,
\Database $database, Connection $connection,
Translator $translator, Translator $translator,
LayerLabelRenderer $labelRenderer,
array $layers, array $layers,
array $tileProviders, array $tileProviders,
array $amenities array $amenities
) { ) {
parent::__construct($manager); parent::__construct($manager);
\Controller::loadLanguageFile('leaflet_layer'); Controller::loadLanguageFile('leaflet_layer');
$this->database = $database; $this->connection = $connection;
$this->layers = $layers; $this->layers = $layers;
$this->tileProviders = $tileProviders; $this->tileProviders = $tileProviders;
$this->translator = $translator; $this->translator = $translator;
$this->amenities = $amenities; $this->amenities = $amenities;
$this->labelRenderer = $labelRenderer;
} }
/** /**
@@ -136,11 +151,8 @@ class LayerDcaListener extends AbstractListener
} }
$alt = $this->getFormatter()->formatValue('type', $row['type']); $alt = $this->getFormatter()->formatValue('type', $row['type']);
$icon = \Image::getHtml($src, $alt, sprintf('title="%s"', strip_tags($alt))); $icon = Image::getHtml($src, $alt, sprintf('title="%s"', StringUtil::specialchars(strip_tags($alt))));
$label = $this->labelRenderer->render($row, $label, $this->translator);
if (!empty($this->layers[$row['type']]['label'])) {
$label = $this->layers[$row['type']]['label']($row, $label);
}
return $icon . ' ' . $label; return $icon . ' ' . $label;
} }
@@ -282,13 +294,13 @@ class LayerDcaListener extends AbstractListener
public function deleteRelations($dataContainer, $undoId) public function deleteRelations($dataContainer, $undoId)
{ {
if ($undoId) { if ($undoId) {
$undo = $this->database $statement = $this->connection->prepare('SELECT * FROM tl_undo WHERE id=:id LIMIT 0,1');
->prepare('SELECT * FROM tl_undo WHERE id=?') $statement->bindValue('id', $undoId);
->limit(1) $statement->execute();
->execute($undoId)
->row();
$result = $this->database $undo = $statement->fetch();
$result = $this->connection
->prepare('SELECT * FROM tl_leaflet_map_layer WHERE lid=?') ->prepare('SELECT * FROM tl_leaflet_map_layer WHERE lid=?')
->execute($dataContainer->id); ->execute($dataContainer->id);
@@ -298,26 +310,17 @@ class LayerDcaListener extends AbstractListener
$undo['data']['tl_leaflet_map_layer'][] = $result->row(); $undo['data']['tl_leaflet_map_layer'][] = $result->row();
} }
$result = $this->database $statement = $this->connection->prepare('SELECT * FROM tl_leaflet_control_layer WHERE lid=:lid');
->prepare('SELECT * FROM tl_leaflet_control_layer WHERE lid=?') $statement->bindValue('lid', $dataContainer->id);
->execute($dataContainer->id); $statement->execute();
while ($result->next()) { $undo['data']['tl_leaflet_control_layer'] = $statement->fetchAll();
$undo['data']['tl_leaflet_control_layer'][] = $result->row();
$this->connection->update('tl_undo', ['data' => $undo['data']], ['id' => $undo['id']]);
} }
$this->database->prepare('UPDATE tl_undo %s WHERE id=?') $this->connection->delete('tl_leaflet_map_layer', ['lid' => $dataContainer->id]);
->set(array('data' => $undo['data'])) $this->connection->delete('tl_leaflet_control_layer', ['lid' => $dataContainer->id]);
->execute($undo['id']);
}
$this->database
->prepare('DELETE FROM tl_leaflet_map_layer WHERE lid=?')
->execute($dataContainer->id);
$this->database
->prepare('DELETE FROM tl_leaflet_control_layer WHERE lid=?')
->execute($dataContainer->id);
} }
/** /**

View File

@@ -10,3 +10,14 @@ services:
arguments: arguments:
- '@netzmacht.contao_toolkit.dca.manager' - '@netzmacht.contao_toolkit.dca.manager'
- '@database_connection' - '@database_connection'
netzmacht.contao_leaflet_maps.listeners.dca.layer:
class: Netzmacht\Contao\Leaflet\Listener\Dca\LayerDcaListener
arguments:
- '@netzmacht.contao_toolkit.dca.manager'
- '@database_connection'
- '@translator'
- '@netzmacht.contao_leaflet_maps.layer_label_renderer'
- '%netzmacht.contao_leaflet_maps.layers%'
- '%netzmacht.contao_leaflet_maps.providers%'
- '%netzmacht.contao_leaflet_maps.amenities%'

View File

@@ -16,11 +16,11 @@ 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\Listeners\Dca\ControlDcaListener; use Netzmacht\Contao\Leaflet\Listener\Dca\ControlDcaListener;
use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration; use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration;
use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks; use Netzmacht\Contao\Leaflet\Listener\Dca\LayerDcaListener;
use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks; use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks;
use Netzmacht\Contao\Leaflet\Listeners\Dca\MapDcaListener; use Netzmacht\Contao\Leaflet\Listener\Dca\MapDcaListener;
use Netzmacht\Contao\Leaflet\Dca\Validator; use Netzmacht\Contao\Leaflet\Dca\Validator;
use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks; use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks;
use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices; use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices;
@@ -243,7 +243,7 @@ $container['leaflet.dca.map-callbacks'] = $container->share(
$container['leaflet.dca.layer-callbacks'] = $container->share( $container['leaflet.dca.layer-callbacks'] = $container->share(
function ($container) { function ($container) {
return new LayerCallbacks( return new LayerDcaListener(
$container[Services::DCA_MANAGER], $container[Services::DCA_MANAGER],
$container[Services::DATABASE_CONNECTION], $container[Services::DATABASE_CONNECTION],
$container[Services::TRANSLATOR], $container[Services::TRANSLATOR],

View File

@@ -16,7 +16,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
'enableVersioning' => true, 'enableVersioning' => true,
'ctable' => ['tl_leaflet_vector', 'tl_leaflet_marker'], 'ctable' => ['tl_leaflet_vector', 'tl_leaflet_marker'],
'ondelete_callback' => [ 'ondelete_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('deleteRelations'), ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'deleteRelations'],
], ],
'sql' => [ 'sql' => [
'keys' => [ 'keys' => [
@@ -41,12 +41,12 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
'flag' => 1, 'flag' => 1,
'icon' => 'system/modules/leaflet/assets/img/layers.png', 'icon' => 'system/modules/leaflet/assets/img/layers.png',
'panelLayout' => 'filter;search,limit', 'panelLayout' => 'filter;search,limit',
'paste_button_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getPasteButtons'), 'paste_button_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'getPasteButtons'],
], ],
'label' => [ 'label' => [
'fields' => ['title'], 'fields' => ['title'],
'format' => '%s', 'format' => '%s',
'label_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('generateRow'), 'label_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'generateRow'],
], ],
'global_operations' => [ 'global_operations' => [
'styles' => [ 'styles' => [
@@ -79,13 +79,13 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['markers'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['markers'],
'href' => 'table=tl_leaflet_marker', 'href' => 'table=tl_leaflet_marker',
'icon' => 'edit.gif', 'icon' => 'edit.gif',
'button_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('generateMarkersButton'), 'button_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'generateMarkersButton'],
], ],
'vectors' => [ 'vectors' => [
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['vectors'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['vectors'],
'href' => 'table=tl_leaflet_vector', 'href' => 'table=tl_leaflet_vector',
'icon' => 'edit.gif', 'icon' => 'edit.gif',
'button_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('generateVectorsButton'), 'button_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'generateVectorsButton'],
], ],
'edit' => [ 'edit' => [
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['edit'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['edit'],
@@ -321,7 +321,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
'submitOnChange' => true, 'submitOnChange' => true,
'chosen' => false, 'chosen' => false,
], ],
'options_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getVariants'), 'options_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'getVariants'],
'sql' => "varchar(32) NOT NULL default ''", 'sql' => "varchar(32) NOT NULL default ''",
], ],
'tile_provider_key' => [ 'tile_provider_key' => [
@@ -365,7 +365,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['reference'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['reference'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getLayers'), 'options_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'getLayers'],
'eval' => [ 'eval' => [
'mandatory' => true, 'mandatory' => true,
'tl_class' => 'w50', 'tl_class' => 'w50',
@@ -525,7 +525,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['boundsMode'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['boundsMode'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getBoundsModes'), 'options_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'getBoundsModes'],
'eval' => ['tl_class' => 'w50', 'includeBlankOption' => true], 'eval' => ['tl_class' => 'w50', 'includeBlankOption' => true],
'sql' => "varchar(6) NOT NULL default ''", 'sql' => "varchar(6) NOT NULL default ''",
], ],
@@ -810,7 +810,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['amenity'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['amenity'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getAmenities'), 'options_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.layer', 'getAmenities'],
'eval' => [ 'eval' => [
'mandatory' => true, 'mandatory' => true,
'tl_class' => 'w50', 'tl_class' => 'w50',