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

View File

@@ -10,3 +10,14 @@ services:
arguments:
- '@netzmacht.contao_toolkit.dca.manager'
- '@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\Boot;
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\LayerCallbacks;
use Netzmacht\Contao\Leaflet\Listener\Dca\LayerDcaListener;
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\VectorCallbacks;
use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices;
@@ -243,7 +243,7 @@ $container['leaflet.dca.map-callbacks'] = $container->share(
$container['leaflet.dca.layer-callbacks'] = $container->share(
function ($container) {
return new LayerCallbacks(
return new LayerDcaListener(
$container[Services::DCA_MANAGER],
$container[Services::DATABASE_CONNECTION],
$container[Services::TRANSLATOR],

View File

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