Refactor LayerCallbacks using dependency injection.

This commit is contained in:
David Molineus
2016-10-06 08:26:45 +02:00
parent ea359e8b79
commit 4068f62df1
3 changed files with 44 additions and 25 deletions

View File

@@ -178,7 +178,9 @@ $container['leaflet.dca.layer-callbacks'] = $container->share(
return new LayerCallbacks( return new LayerCallbacks(
$container[Services::DCA_MANAGER], $container[Services::DCA_MANAGER],
$container[Services::DATABASE_CONNECTION], $container[Services::DATABASE_CONNECTION],
$GLOBALS['LEAFLET_LAYERS'] $container[Services::TRANSLATOR],
$GLOBALS['LEAFLET_LAYERS'],
$GLOBALS['LEAFLET_TILE_PROVIDERS']
); );
} }
); );

View File

@@ -124,4 +124,7 @@ $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][1] = 'Basic lay
$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['feature'][0] = 'Feature group'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['feature'][0] = 'Feature group';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['feature'][1] = 'Extended layer group with events and popup support. <br>See <a href="http://leafletjs.com/reference.html#featuregroup" target="_blank">http://leafletjs.com/reference.html#featuregroup</a>'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['feature'][1] = 'Extended layer group with events and popup support. <br>See <a href="http://leafletjs.com/reference.html#featuregroup" target="_blank">http://leafletjs.com/reference.html#featuregroup</a>';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['pasteinto'][1] = 'Paste into layer %s';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['pasteafter'][1] = 'Paste after layer %s';
$GLOBALS['TL_LANG']['tl_leaflet_layer']['countEntries'] = 'Entries'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['countEntries'] = 'Entries';

View File

@@ -11,6 +11,7 @@
namespace Netzmacht\Contao\Leaflet\Dca; namespace Netzmacht\Contao\Leaflet\Dca;
use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator;
use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks; use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks;
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;
@@ -50,25 +51,46 @@ class LayerCallbacks extends Callbacks
* @var \Database * @var \Database
*/ */
private $database; private $database;
/**
* Tile providers configuration.
*
* @var array
*/
private $tileProviders;
/**
* Translator.
*
* @var Translator
*/
private $translator;
/** /**
* Construct. * Construct.
* *
* @param Manager $manager Data container manager. * @param Manager $manager Data container manager.
* @param \Database $database Database connection. * @param \Database $database Database connection.
* @param array $layers Leaflet layer configuration. * @param Translator $translator Translator.
* * @param array $layers Leaflet layer configuration.
* @SuppressWarnings(PHPMD.Superglobals) * @param array $tileProviders Tile providers.
*/ */
public function __construct(Manager $manager, \Database $database, array $layers) public function __construct(
{ Manager $manager,
\Database $database,
Translator $translator,
array $layers,
array $tileProviders
) {
parent::__construct($manager); parent::__construct($manager);
$this->layers = $layers; $this->database = $database;
$this->database = $database; $this->layers = $layers;
$this->tileProviders = $tileProviders;
\Controller::loadLanguageFile('leaflet_layer'); \Controller::loadLanguageFile('leaflet_layer');
$this->translator = $translator;
} }
/** /**
@@ -77,16 +99,14 @@ class LayerCallbacks extends Callbacks
* @param \DataContainer $dataContainer The dataContainer driver. * @param \DataContainer $dataContainer The dataContainer driver.
* *
* @return array * @return array
*
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
public function getVariants($dataContainer) public function getVariants($dataContainer)
{ {
if ($dataContainer->activeRecord if ($dataContainer->activeRecord
&& $dataContainer->activeRecord->tile_provider && $dataContainer->activeRecord->tile_provider
&& !empty($GLOBALS['LEAFLET_TILE_PROVIDERS'][$dataContainer->activeRecord->tile_provider]['variants']) && !empty($this->tileProviders[$dataContainer->activeRecord->tile_provider]['variants'])
) { ) {
return $GLOBALS['LEAFLET_TILE_PROVIDERS'][$dataContainer->activeRecord->tile_provider]['variants']; return $this->tileProviders[$dataContainer->activeRecord->tile_provider]['variants'];
} }
return array(); return array();
@@ -99,15 +119,9 @@ class LayerCallbacks extends Callbacks
* @param string $label Current row label. * @param string $label Current row label.
* *
* @return string * @return string
*
* @SuppressWarnings(PHPMD.Superglobals)
*/ */
public function generateRow($row, $label) public function generateRow($row, $label)
{ {
$alt = empty($GLOBALS['TL_LANG']['leaflet_layer'][$row['type']][0])
? $row['type']
: $GLOBALS['TL_LANG']['leaflet_layer'][$row['type']][0];
if (!empty($this->layers[$row['type']]['icon'])) { if (!empty($this->layers[$row['type']]['icon'])) {
$src = $this->layers[$row['type']]['icon']; $src = $this->layers[$row['type']]['icon'];
@@ -119,6 +133,7 @@ class LayerCallbacks extends Callbacks
$src = preg_replace('/(\.[^\.]+)$/', '_1$1', $src); $src = preg_replace('/(\.[^\.]+)$/', '_1$1', $src);
} }
$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"', strip_tags($alt)));
if (!empty($this->layers[$row['type']]['label'])) { if (!empty($this->layers[$row['type']]['label'])) {
@@ -167,7 +182,6 @@ class LayerCallbacks extends Callbacks
* *
* @return string * @return string
* *
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/ */
public function getPasteButtons($dataContainer, $row, $table, $whatever, $children) public function getPasteButtons($dataContainer, $row, $table, $whatever, $children)
@@ -180,10 +194,10 @@ class LayerCallbacks extends Callbacks
$buffer = sprintf( $buffer = sprintf(
'<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a> ', '<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a> ',
$pasteAfterUrl, $pasteAfterUrl,
specialchars(sprintf($GLOBALS['TL_LANG'][$table]['pasteafter'][1], $row['id'])), specialchars($this->translator->translate('pasteafter.1', $table, [$row['id']])),
\Image::getHtml( \Image::getHtml(
'pasteafter.gif', 'pasteafter.gif',
sprintf($GLOBALS['TL_LANG'][$table]['pasteafter'][1], $row['id']) $this->translator->translate('pasteafter.1', $table, [$row['id']])
) )
); );
@@ -200,10 +214,10 @@ class LayerCallbacks extends Callbacks
$buffer .= sprintf( $buffer .= sprintf(
'<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a> ', '<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a> ',
$pasteIntoUrl, $pasteIntoUrl,
specialchars(sprintf($GLOBALS['TL_LANG'][$table]['pasteinto'][1], $row['id'])), specialchars($this->translator->translate('pasteinto.1', $table, [$row['id']])),
\Image::getHtml( \Image::getHtml(
'pasteinto.gif', 'pasteinto.gif',
sprintf($GLOBALS['TL_LANG'][$table]['pasteinto'][1], $row['id']) $this->translator->translate('pasteinto.1', $table, [$row['id']])
) )
); );