From 6111fc359458c89a9e9c404114d17eeb643d01d8 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 6 Oct 2017 13:44:11 +0200 Subject: [PATCH] Rework marker dca callbacks to a service listener. --- .../Dca/MarkerDcaListener.php} | 58 ++++++++++++++----- src/Resources/config/listeners.yml | 5 ++ .../contao/dca/tl_leaflet_marker.php | 14 ++--- 3 files changed, 54 insertions(+), 23 deletions(-) rename src/{Dca/MarkerCallbacks.php => Listener/Dca/MarkerDcaListener.php} (68%) diff --git a/src/Dca/MarkerCallbacks.php b/src/Listener/Dca/MarkerDcaListener.php similarity index 68% rename from src/Dca/MarkerCallbacks.php rename to src/Listener/Dca/MarkerDcaListener.php index 933234c..6d3716c 100644 --- a/src/Dca/MarkerCallbacks.php +++ b/src/Listener/Dca/MarkerDcaListener.php @@ -10,8 +10,10 @@ * @filesource */ -namespace Netzmacht\Contao\Leaflet\Dca; +namespace Netzmacht\Contao\Leaflet\Listener\Dca; +use Contao\Controller; +use Doctrine\DBAL\Connection; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; use Netzmacht\Contao\Leaflet\Model\IconModel; use Netzmacht\Contao\Leaflet\Model\PopupModel; @@ -21,8 +23,35 @@ use Netzmacht\Contao\Leaflet\Model\PopupModel; * * @package Netzmacht\Contao\Leaflet\Dca */ -class MarkerCallbacks +class MarkerDcaListener { + /** + * Database connection. + * + * @var Connection + */ + private $connection; + + /** + * MarkerDcaListener constructor. + * + * @param Connection $connection Database connection. + */ + public function __construct(Connection $connection) + { + $this->connection = $connection; + } + + /** + * Initialize the language files. + * + * @return void + */ + public function initialize(): void + { + Controller::loadLanguageFile('leaflet'); + } + /** * Generate the row label. * @@ -92,10 +121,7 @@ class MarkerCallbacks } } - \Database::getInstance() - ->prepare('UPDATE tl_leaflet_marker %s WHERE id=?') - ->set($combined) - ->execute($dataContainer->id); + $this->connection->update('tl_leaflet_marker', $combined, ['id' => $dataContainer->id]); return null; } @@ -112,21 +138,23 @@ class MarkerCallbacks */ public function loadCoordinates($value, $dataContainer) { - $result = \Database::getInstance() - ->prepare('SELECT latitude, longitude, altitude FROM tl_leaflet_marker WHERE id=?') - ->execute($dataContainer->id); + $query = 'SELECT latitude, longitude, altitude FROM tl_leaflet_marker WHERE id=:id'; + $statement = $this->connection->prepare($query); + $statement->bindValue('id', $dataContainer->id); - if ($result->numRows) { - $buffer = $result->latitude; + $statement->execute(); - if ($buffer && $result->longitude) { - $buffer .= ',' . $result->longitude; + if ($row = $statement->fetch()) { + $buffer = $row['latitude']; + + if ($buffer && $row['longitude']) { + $buffer .= ',' . $row['longitude']; } else { return $buffer; } - if ($buffer && $result->altitude) { - $buffer .= ',' . $result->longitude; + if ($buffer && $row['altitude']) { + $buffer .= ',' . $row['altitude']; } return $buffer; diff --git a/src/Resources/config/listeners.yml b/src/Resources/config/listeners.yml index a38b719..7ece7ff 100644 --- a/src/Resources/config/listeners.yml +++ b/src/Resources/config/listeners.yml @@ -21,3 +21,8 @@ services: - '%netzmacht.contao_leaflet_maps.layers%' - '%netzmacht.contao_leaflet_maps.providers%' - '%netzmacht.contao_leaflet_maps.amenities%' + + netzmacht.contao_leaflet_maps.listeners.dca.marker: + class: Netzmacht\Contao\Leaflet\Listener\Dca\MarkerDcaListener + arguments: + - '@database_connection' diff --git a/src/Resources/contao/dca/tl_leaflet_marker.php b/src/Resources/contao/dca/tl_leaflet_marker.php index eda2571..cc8c3ba 100644 --- a/src/Resources/contao/dca/tl_leaflet_marker.php +++ b/src/Resources/contao/dca/tl_leaflet_marker.php @@ -26,9 +26,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array ) ), 'onload_callback' => array( - function () { - \Controller::loadLanguageFile('leaflet'); - } + ['netzmacht.contao_leaflet_maps.listeners.dca.marker', 'initialize'], ), 'onsubmit_callback' => [ \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), @@ -44,7 +42,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'flag' => 1, 'panelLayout' => 'sort,filter;search,limit', 'headerFields' => array('title', 'type'), - 'child_record_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'generateRow'), + 'child_record_callback' => array('netzmacht.contao_leaflet_maps.listeners.dca.marker\'', 'generateRow'), ), 'label' => array ( @@ -195,10 +193,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'inputType' => 'text', 'save_callback' => array( \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates'), - array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'saveCoordinates') + array('netzmacht.contao_leaflet_maps.listeners.dca.marker', 'saveCoordinates') ), 'load_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'loadCoordinates') + array('netzmacht.contao_leaflet_maps.listeners.dca.marker', 'loadCoordinates') ), 'wizard' => array( Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder') @@ -278,7 +276,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['popup'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'getPopups'), + 'options_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.marker', 'getPopups'], 'eval' => array( 'mandatory' => false, 'tl_class' => 'w50', @@ -309,7 +307,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['icon'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'getIcons'), + 'options_callback' => array('netzmacht.contao_leaflet_maps.listeners.dca.marker', 'getIcons'), 'eval' => array( 'mandatory' => true, 'tl_class' => 'w50',