Rework marker dca callbacks to a service listener.

This commit is contained in:
David Molineus
2017-10-06 13:44:11 +02:00
parent 32d47531f7
commit 6111fc3594
3 changed files with 54 additions and 23 deletions

View File

@@ -10,8 +10,10 @@
* @filesource * @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\Toolkit\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\IconModel; use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Contao\Leaflet\Model\PopupModel; use Netzmacht\Contao\Leaflet\Model\PopupModel;
@@ -21,8 +23,35 @@ use Netzmacht\Contao\Leaflet\Model\PopupModel;
* *
* @package Netzmacht\Contao\Leaflet\Dca * @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. * Generate the row label.
* *
@@ -92,10 +121,7 @@ class MarkerCallbacks
} }
} }
\Database::getInstance() $this->connection->update('tl_leaflet_marker', $combined, ['id' => $dataContainer->id]);
->prepare('UPDATE tl_leaflet_marker %s WHERE id=?')
->set($combined)
->execute($dataContainer->id);
return null; return null;
} }
@@ -112,21 +138,23 @@ class MarkerCallbacks
*/ */
public function loadCoordinates($value, $dataContainer) public function loadCoordinates($value, $dataContainer)
{ {
$result = \Database::getInstance() $query = 'SELECT latitude, longitude, altitude FROM tl_leaflet_marker WHERE id=:id';
->prepare('SELECT latitude, longitude, altitude FROM tl_leaflet_marker WHERE id=?') $statement = $this->connection->prepare($query);
->execute($dataContainer->id); $statement->bindValue('id', $dataContainer->id);
if ($result->numRows) { $statement->execute();
$buffer = $result->latitude;
if ($buffer && $result->longitude) { if ($row = $statement->fetch()) {
$buffer .= ',' . $result->longitude; $buffer = $row['latitude'];
if ($buffer && $row['longitude']) {
$buffer .= ',' . $row['longitude'];
} else { } else {
return $buffer; return $buffer;
} }
if ($buffer && $result->altitude) { if ($buffer && $row['altitude']) {
$buffer .= ',' . $result->longitude; $buffer .= ',' . $row['altitude'];
} }
return $buffer; return $buffer;

View File

@@ -21,3 +21,8 @@ services:
- '%netzmacht.contao_leaflet_maps.layers%' - '%netzmacht.contao_leaflet_maps.layers%'
- '%netzmacht.contao_leaflet_maps.providers%' - '%netzmacht.contao_leaflet_maps.providers%'
- '%netzmacht.contao_leaflet_maps.amenities%' - '%netzmacht.contao_leaflet_maps.amenities%'
netzmacht.contao_leaflet_maps.listeners.dca.marker:
class: Netzmacht\Contao\Leaflet\Listener\Dca\MarkerDcaListener
arguments:
- '@database_connection'

View File

@@ -26,9 +26,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
) )
), ),
'onload_callback' => array( 'onload_callback' => array(
function () { ['netzmacht.contao_leaflet_maps.listeners.dca.marker', 'initialize'],
\Controller::loadLanguageFile('leaflet');
}
), ),
'onsubmit_callback' => [ 'onsubmit_callback' => [
\Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'),
@@ -44,7 +42,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
'flag' => 1, 'flag' => 1,
'panelLayout' => 'sort,filter;search,limit', 'panelLayout' => 'sort,filter;search,limit',
'headerFields' => array('title', 'type'), '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 'label' => array
( (
@@ -195,10 +193,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
'inputType' => 'text', 'inputType' => 'text',
'save_callback' => array( 'save_callback' => array(
\Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates'), \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( 'load_callback' => array(
array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'loadCoordinates') array('netzmacht.contao_leaflet_maps.listeners.dca.marker', 'loadCoordinates')
), ),
'wizard' => array( 'wizard' => array(
Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder') 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'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['popup'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'getPopups'), 'options_callback' => ['netzmacht.contao_leaflet_maps.listeners.dca.marker', 'getPopups'],
'eval' => array( 'eval' => array(
'mandatory' => false, 'mandatory' => false,
'tl_class' => 'w50', 'tl_class' => 'w50',
@@ -309,7 +307,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['icon'], 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['icon'],
'exclude' => true, 'exclude' => true,
'inputType' => 'select', 'inputType' => 'select',
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'getIcons'), 'options_callback' => array('netzmacht.contao_leaflet_maps.listeners.dca.marker', 'getIcons'),
'eval' => array( 'eval' => array(
'mandatory' => true, 'mandatory' => true,
'tl_class' => 'w50', 'tl_class' => 'w50',