From 2c9dd0ad48667bf57d3674f1f856149b0b21fc39 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Thu, 6 Oct 2016 08:34:55 +0200 Subject: [PATCH] Refactor frontend integration using dependency injection. --- module/config/services.php | 14 +++++++ module/dca/tl_content.php | 4 +- module/dca/tl_module.php | 4 +- .../Leaflet/Dca/FrontendIntegration.php | 41 ++++++++++++++++--- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/module/config/services.php b/module/config/services.php index 3d78dec..a04a4a5 100644 --- a/module/config/services.php +++ b/module/config/services.php @@ -14,6 +14,7 @@ use Netzmacht\Contao\Leaflet\Alias\DefaultAliasFilter; use Netzmacht\Contao\Leaflet\Boot; use Netzmacht\Contao\Leaflet\ContaoAssets; use Netzmacht\Contao\Leaflet\Dca\ControlCallbacks; +use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration; use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks; use Netzmacht\Contao\Leaflet\Dca\MapCallbacks; use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices; @@ -199,6 +200,19 @@ $container['leaflet.dca.control-callbacks'] = $container->share( } ); +/** + * Callback helper class for frontend integration. + * + * @return FrontendIntegration + */ +$container['leaflet.dca.frontend-integration'] = $container->share( + function ($container) { + return new FrontendIntegration( + $container[Services::TRANSLATOR] + ); + } +); + /** * Component factory for content element. * diff --git a/module/dca/tl_content.php b/module/dca/tl_content.php index 895804d..19af579 100644 --- a/module/dca/tl_content.php +++ b/module/dca/tl_content.php @@ -22,9 +22,9 @@ $GLOBALS['TL_DCA']['tl_content']['fields']['leaflet_map'] = array( 'label' => &$GLOBALS['TL_LANG']['tl_content']['leaflet_map'], 'inputType' => 'select', 'exclude' => true, - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\FrontendIntegration', 'getMaps'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\FrontendIntegration::callback('getMaps'), 'wizard' => array( - array('Netzmacht\Contao\Leaflet\Dca\FrontendIntegration', 'getEditMapLink'), + \Netzmacht\Contao\Leaflet\Dca\FrontendIntegration::callback('getEditMapLink'), ), 'eval' => array( 'tl_class' => 'w50 wizard', diff --git a/module/dca/tl_module.php b/module/dca/tl_module.php index 3f5d0b5..19c9a45 100644 --- a/module/dca/tl_module.php +++ b/module/dca/tl_module.php @@ -22,9 +22,9 @@ $GLOBALS['TL_DCA']['tl_module']['fields']['leaflet_map'] = array( 'label' => &$GLOBALS['TL_LANG']['tl_module']['leaflet_map'], 'inputType' => 'select', 'exclude' => true, - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\FrontendIntegration', 'getMaps'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\FrontendIntegration::callback('getMaps'), 'wizard' => array( - array('Netzmacht\Contao\Leaflet\Dca\FrontendIntegration', 'getEditMapLink'), + \Netzmacht\Contao\Leaflet\Dca\FrontendIntegration::callback('getEditMapLink'), ), 'eval' => array( 'tl_class' => 'w50 wizard', diff --git a/src/Netzmacht/Contao/Leaflet/Dca/FrontendIntegration.php b/src/Netzmacht/Contao/Leaflet/Dca/FrontendIntegration.php index c69288f..368b1c5 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/FrontendIntegration.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/FrontendIntegration.php @@ -11,6 +11,8 @@ namespace Netzmacht\Contao\Leaflet\Dca; +use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator; +use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; use Netzmacht\Contao\Leaflet\Model\MapModel; @@ -21,6 +23,35 @@ use Netzmacht\Contao\Leaflet\Model\MapModel; */ class FrontendIntegration { + /** + * Translator. + * + * @var Translator + */ + private $translator; + + /** + * FrontendIntegration constructor. + * + * @param Translator $translator Translator. + */ + public function __construct(Translator $translator) + { + $this->translator = $translator; + } + + /** + * Generate the callback definition. + * + * @param string $methodName Callback method name. + * + * @return callable + */ + public static function callback($methodName) + { + return CallbackFactory::service('leaflet.dca.frontend-integration', $methodName); + } + /** * Get all leaflet maps. * @@ -39,8 +70,6 @@ class FrontendIntegration * @param \DataContainer $dataContainer The dataContainer driver. * * @return string - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function getEditMapLink($dataContainer) { @@ -53,23 +82,23 @@ class FrontendIntegration return sprintf( '%s', - 'contao/main.php?do=leaflet&table=tl_leaflet_map&act=edit&id=', + 'contao/main.php?do=leaflet_map&table=tl_leaflet_map&act=edit&id=', $dataContainer->value, \RequestToken::get(), sprintf( $pattern, - specialchars(sprintf($GLOBALS['TL_LANG']['tl_content']['editalias'][1], $dataContainer->value)), + specialchars($this->translator->translate('editalias.1', 'tl_content', [$dataContainer->value])), specialchars( str_replace( "'", "\\'", - sprintf($GLOBALS['TL_LANG']['tl_content']['editalias'][1], $dataContainer->value) + sprintf($this->translator->translate('editalias.1', 'tl_content', [$dataContainer->value])) ) ) ), \Image::getHtml( 'alias.gif', - $GLOBALS['TL_LANG']['tl_content']['editalias'][0], + $this->translator->translate('editalias.0', 'tl_content', [$dataContainer->value]), 'style="vertical-align:top"' ) );