Work on service oriented listeners.

This commit is contained in:
David Molineus
2017-10-18 16:26:27 +02:00
parent dd23d386eb
commit 0716354952
9 changed files with 212 additions and 109 deletions

View File

@@ -2,7 +2,10 @@ services:
netzmacht.contao_leaflet.listeners.dca.leaflet: netzmacht.contao_leaflet.listeners.dca.leaflet:
class: Netzmacht\Contao\Leaflet\Listener\Dca\LeafletDcaListener class: Netzmacht\Contao\Leaflet\Listener\Dca\LeafletDcaListener
arguments: arguments:
- '@netzmacht.contao_toolkit.repository_manager'
- '@templating'
- '@filesystem' - '@filesystem'
- '@netzmacht.contao_toolkit.contao.system_adapter'
- '%netzmacht.contao_leaflet.cache_dir%' - '%netzmacht.contao_leaflet.cache_dir%'
netzmacht.contao_leaflet.listeners.dca.validator: netzmacht.contao_leaflet.listeners.dca.validator:
@@ -14,6 +17,7 @@ services:
netzmacht.contao_leaflet.listeners.dca.frontend_integration: netzmacht.contao_leaflet.listeners.dca.frontend_integration:
class: Netzmacht\Contao\Leaflet\Listener\Dca\FrontendIntegrationListener class: Netzmacht\Contao\Leaflet\Listener\Dca\FrontendIntegrationListener
arguments: arguments:
- '@netzmacht.contao_toolkit.repository_manager'
- '@translator' - '@translator'
netzmacht.contao_leaflet.listeners.dca.map: netzmacht.contao_leaflet.listeners.dca.map:
@@ -21,6 +25,7 @@ services:
arguments: arguments:
- '@netzmacht.contao_toolkit.dca.manager' - '@netzmacht.contao_toolkit.dca.manager'
- '@database_connection' - '@database_connection'
- '@netzmacht.contao_toolkit.repository_manager'
netzmacht.contao_leaflet.listeners.dca.control: netzmacht.contao_leaflet.listeners.dca.control:
class: Netzmacht\Contao\Leaflet\Listener\Dca\ControlDcaListener class: Netzmacht\Contao\Leaflet\Listener\Dca\ControlDcaListener
@@ -34,8 +39,10 @@ services:
arguments: arguments:
- '@netzmacht.contao_toolkit.dca.manager' - '@netzmacht.contao_toolkit.dca.manager'
- '@database_connection' - '@database_connection'
- '@netzmacht.contao_toolkit.repository_manager'
- '@translator' - '@translator'
- '@netzmacht.contao_leaflet.layer_label_renderer' - '@netzmacht.contao_leaflet.layer_label_renderer'
- '@netzmacht.contao_toolkit.contao.backend_adapter'
- '%netzmacht.contao_leaflet.layers%' - '%netzmacht.contao_leaflet.layers%'
- '%netzmacht.contao_leaflet.providers%' - '%netzmacht.contao_leaflet.providers%'
- '%netzmacht.contao_leaflet.amenities%' - '%netzmacht.contao_leaflet.amenities%'
@@ -45,11 +52,13 @@ services:
class: Netzmacht\Contao\Leaflet\Listener\Dca\MarkerDcaListener class: Netzmacht\Contao\Leaflet\Listener\Dca\MarkerDcaListener
arguments: arguments:
- '@database_connection' - '@database_connection'
- '@netzmacht.contao_toolkit.repository_manager'
netzmacht.contao_leaflet.listeners.dca.vector: netzmacht.contao_leaflet.listeners.dca.vector:
class: Netzmacht\Contao\Leaflet\Listener\Dca\VectorDcaListener class: Netzmacht\Contao\Leaflet\Listener\Dca\VectorDcaListener
arguments: arguments:
- '@netzmacht.contao_toolkit.dca.manager' - '@netzmacht.contao_toolkit.dca.manager'
- '@netzmacht.contao_toolkit.repository_manager'
- '%netzmacht.contao_leaflet.vectors%' - '%netzmacht.contao_leaflet.vectors%'
netzmacht.contao_leaflet.listeners.dca.icon: netzmacht.contao_leaflet.listeners.dca.icon:

View File

@@ -23,7 +23,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = [
], ],
], ],
'onload_callback' => [ 'onload_callback' => [
['netzmacht.contao_leaflet.listeners.dca.marker', 'initialize'], ['netzmacht.contao_leaflet.listeners.dca.leaflet', 'loadLanguageFile'],
], ],
'onsubmit_callback' => [ 'onsubmit_callback' => [
['netzmacht.contao_leaflet.listeners.dca.leaflet', 'clearCache'], ['netzmacht.contao_leaflet.listeners.dca.leaflet', 'clearCache'],

View File

@@ -16,6 +16,7 @@ use Contao\StringUtil;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Netzmacht\Contao\Leaflet\Model\ControlModel; use Netzmacht\Contao\Leaflet\Model\ControlModel;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener; use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener;
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;
@@ -48,19 +49,32 @@ class ControlDcaListener extends AbstractListener
*/ */
private $types; private $types;
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/** /**
* Construct. * Construct.
* *
* @param Manager $manager Data container manager. * @param Manager $manager Data container manager.
* @param Connection $connection Database connection. * @param Connection $connection Database connection.
* @param array $types Control types. * @param RepositoryManager $repositoryManager Repository manager.
* @param array $types Control types.
*/ */
public function __construct(Manager $manager, Connection $connection, array $types) public function __construct(
{ Manager $manager,
Connection $connection,
RepositoryManager $repositoryManager,
array $types
) {
parent::__construct($manager); parent::__construct($manager);
$this->connection = $connection; $this->connection = $connection;
$this->types = $types; $this->types = $types;
$this->repositoryManager = $repositoryManager;
} }
/** /**
@@ -96,7 +110,8 @@ class ControlDcaListener extends AbstractListener
*/ */
public function getLayers() public function getLayers()
{ {
$collection = LayerModel::findAll(); $repository = $this->repositoryManager->getRepository(LayerModel::class);
$collection = $repository->findAll();
return OptionsBuilder::fromCollection($collection, 'title') return OptionsBuilder::fromCollection($collection, 'title')
->asTree() ->asTree()
@@ -110,7 +125,8 @@ class ControlDcaListener extends AbstractListener
*/ */
public function getZoomControls() public function getZoomControls()
{ {
$collection = ControlModel::findBy('type', 'zoom', ['order' => 'title']); $repository = $this->repositoryManager->getRepository(ControlModel::class);
$collection = $repository->findBy(['type=?'], ['zoom'], ['order' => 'title']);
return OptionsBuilder::fromCollection($collection, 'title')->getOptions(); return OptionsBuilder::fromCollection($collection, 'title')->getOptions();
} }

View File

@@ -14,7 +14,11 @@ declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Listener\Dca; namespace Netzmacht\Contao\Leaflet\Listener\Dca;
use Contao\Image;
use Contao\RequestToken;
use Contao\StringUtil;
use Netzmacht\Contao\Leaflet\Model\MapModel; use Netzmacht\Contao\Leaflet\Model\MapModel;
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
use Symfony\Component\Translation\TranslatorInterface as Translator; use Symfony\Component\Translation\TranslatorInterface as Translator;
@@ -32,14 +36,23 @@ final class FrontendIntegrationListener
*/ */
private $translator; private $translator;
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/** /**
* FrontendIntegration constructor. * FrontendIntegration constructor.
* *
* @param Translator $translator Translator. * @param RepositoryManager $repositoryManager Repository manager.
* @param Translator $translator Translator.
*/ */
public function __construct(Translator $translator) public function __construct(RepositoryManager $repositoryManager, Translator $translator)
{ {
$this->translator = $translator; $this->translator = $translator;
$this->repositoryManager = $repositoryManager;
} }
/** /**
@@ -49,7 +62,8 @@ final class FrontendIntegrationListener
*/ */
public function getMaps(): array public function getMaps(): array
{ {
$collection = MapModel::findAll(); $repository = $this->repositoryManager->getRepository(MapModel::class);
$collection = $repository->findAll(['order' => 'title']);
return OptionsBuilder::fromCollection($collection, 'title')->getOptions(); return OptionsBuilder::fromCollection($collection, 'title')->getOptions();
} }
@@ -74,11 +88,13 @@ final class FrontendIntegrationListener
'<a href="%s%s&amp;popup=1&amp;rt=%s" %s>%s</a>', '<a href="%s%s&amp;popup=1&amp;rt=%s" %s>%s</a>',
'contao/main.php?do=leaflet_map&amp;table=tl_leaflet_map&amp;act=edit&amp;id=', 'contao/main.php?do=leaflet_map&amp;table=tl_leaflet_map&amp;act=edit&amp;id=',
$dataContainer->value, $dataContainer->value,
\RequestToken::get(), RequestToken::get(),
sprintf( sprintf(
$pattern, $pattern,
specialchars($this->translator->trans('editalias.1', [$dataContainer->value], 'contao_tl_content')), StringUtil::specialchars(
specialchars( $this->translator->trans('editalias.1', [$dataContainer->value], 'contao_tl_content')
),
StringUtil::specialchars(
str_replace( str_replace(
"'", "'",
"\\'", "\\'",
@@ -86,7 +102,7 @@ final class FrontendIntegrationListener
) )
) )
), ),
\Image::getHtml( Image::getHtml(
'alias.gif', 'alias.gif',
$this->translator->trans('editalias.0', [$dataContainer->value], 'contao_tl_content'), $this->translator->trans('editalias.0', [$dataContainer->value], 'contao_tl_content'),
'style="vertical-align:top"' 'style="vertical-align:top"'

View File

@@ -12,15 +12,16 @@
namespace Netzmacht\Contao\Leaflet\Listener\Dca; namespace Netzmacht\Contao\Leaflet\Listener\Dca;
use Contao\Controller; use Contao\Backend;
use Contao\CoreBundle\Framework\Adapter;
use Contao\DataContainer; use Contao\DataContainer;
use Contao\Image; use Contao\Image;
use Contao\RequestToken;
use Contao\StringUtil; use Contao\StringUtil;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\LayerLabelRenderer; use Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\LayerLabelRenderer;
use Netzmacht\Contao\Leaflet\Model\IconModel; use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener; use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener;
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;
@@ -54,6 +55,13 @@ class LayerDcaListener extends AbstractListener
*/ */
private $connection; private $connection;
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/** /**
* Tile providers configuration. * Tile providers configuration.
* *
@@ -89,23 +97,36 @@ class LayerDcaListener extends AbstractListener
*/ */
private $fileFormats; private $fileFormats;
/**
* Backend adapter.
*
* @var Backend|Adapter
*/
private $backendAdapter;
/** /**
* Construct. * Construct.
* *
* @param Manager $manager Data container manager. * @param Manager $manager Data container manager.
* @param Connection $connection Database connection. * @param Connection $connection Database connection.
* @param Translator $translator Translator. * @param RepositoryManager $repositoryManager Repository manager.
* @param LayerLabelRenderer $labelRenderer Layer label renderer. * @param Translator $translator Translator.
* @param array $layers Leaflet layer configuration. * @param LayerLabelRenderer $labelRenderer Layer label renderer.
* @param array $tileProviders Tile providers. * @param Adapter|Backend $backendAdapter Backend adapter.
* @param array $amenities OSM amenities. * @param array $layers Leaflet layer configuration.
* @param array $fileFormats File formats. * @param array $tileProviders Tile providers.
* @param array $amenities OSM amenities.
* @param array $fileFormats File formats.
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/ */
public function __construct( public function __construct(
Manager $manager, Manager $manager,
Connection $connection, Connection $connection,
RepositoryManager $repositoryManager,
Translator $translator, Translator $translator,
LayerLabelRenderer $labelRenderer, LayerLabelRenderer $labelRenderer,
$backendAdapter,
array $layers, array $layers,
array $tileProviders, array $tileProviders,
array $amenities, array $amenities,
@@ -113,15 +134,15 @@ class LayerDcaListener extends AbstractListener
) { ) {
parent::__construct($manager); parent::__construct($manager);
Controller::loadLanguageFile('leaflet_layer'); $this->connection = $connection;
$this->layers = $layers;
$this->connection = $connection; $this->tileProviders = $tileProviders;
$this->layers = $layers; $this->translator = $translator;
$this->tileProviders = $tileProviders; $this->amenities = $amenities;
$this->translator = $translator; $this->labelRenderer = $labelRenderer;
$this->amenities = $amenities; $this->fileFormats = $fileFormats;
$this->labelRenderer = $labelRenderer; $this->repositoryManager = $repositoryManager;
$this->fileFormats = $fileFormats; $this->backendAdapter = $backendAdapter;
} }
/** /**
@@ -216,7 +237,8 @@ class LayerDcaListener extends AbstractListener
) )
); );
$collection = LayerModel::findMultipleByTypes($types); $repository = $this->repositoryManager->getRepository(LayerModel::class);
$collection = $repository->findMultipleByTypes($types);
$builder = OptionsBuilder::fromCollection( $builder = OptionsBuilder::fromCollection(
$collection, $collection,
'id', 'id',
@@ -243,43 +265,42 @@ class LayerDcaListener extends AbstractListener
*/ */
public function getPasteButtons($dataContainer, $row, $table, $whatever, $children) public function getPasteButtons($dataContainer, $row, $table, $whatever, $children)
{ {
$pasteAfterUrl = \Controller::addToUrl( $pasteAfterUrl = $this->backendAdapter->addToUrl(
'act=' . $children['mode'] . '&amp;mode=1&amp;pid=' . $row['id'] 'act=' . $children['mode'] . '&amp;mode=1&amp;pid=' . $row['id']
. (!is_array($children['id']) ? '&amp;id=' . $children['id'] : '') . (!is_array($children['id']) ? '&amp;id=' . $children['id'] : '')
. '&amp;rt=' . RequestToken::get()
); );
$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($this->translator->trans('pasteafter.1', [$row['id']], 'contao_' . $table)), StringUtil::specialchars($this->translator->trans('pasteafter.1', [$row['id']], 'contao_' . $table)),
\Image::getHtml( Image::getHtml(
'pasteafter.gif', 'pasteafter.gif',
$this->translator->trans('pasteafter.1', [$row['id']], 'contao_' . $table) $this->translator->trans('pasteafter.1', [$row['id']], 'contao_' . $table)
) )
); );
if (!empty($this->layers[$row['type']]['children'])) { if (!empty($this->layers[$row['type']]['children'])) {
$pasteIntoUrl = \Controller::addToUrl( $pasteIntoUrl = $this->backendAdapter->addToUrl(
sprintf( sprintf(
'act=%s&amp;mode=2&amp;pid=%s%s', 'act=%s&amp;mode=2&amp;pid=%s%s',
$children['mode'], $children['mode'],
$row['id'], $row['id'],
!is_array($children['id']) ? '&amp;id=' . $children['id'] : '' !is_array($children['id']) ? '&amp;id=' . $children['id'] : ''
) . '&amp;rt=' . RequestToken::get() )
); );
$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($this->translator->trans('pasteinto.1', [$row['id']], 'contao_' . $table)), StringUtil::specialchars($this->translator->trans('pasteinto.1', [$row['id']], 'contao_' . $table)),
\Image::getHtml( Image::getHtml(
'pasteinto.gif', 'pasteinto.gif',
$this->translator->trans('pasteinto.1', [$row['id']], 'contao_' . $table) $this->translator->trans('pasteinto.1', [$row['id']], 'contao_' . $table)
) )
); );
} elseif ($row['id'] > 0) { } elseif ($row['id'] > 0) {
$buffer .= \Image::getHtml('pasteinto_.gif'); $buffer .= Image::getHtml('pasteinto_.gif');
} }
return $buffer; return $buffer;
@@ -348,7 +369,7 @@ class LayerDcaListener extends AbstractListener
$statement->bindValue('lid', $dataContainer->id); $statement->bindValue('lid', $dataContainer->id);
$statement->execute(); $statement->execute();
$undo['data'] = deserialize($undo['data'], true); $undo['data'] = StringUtil::deserialize($undo['data'], true);
while ($row = $statement->fetch()) { while ($row = $statement->fetch()) {
$undo['data']['tl_leaflet_map_layer'][] = $row; $undo['data']['tl_leaflet_map_layer'][] = $row;
@@ -400,7 +421,8 @@ class LayerDcaListener extends AbstractListener
*/ */
public function getLayers($dataContainer) public function getLayers($dataContainer)
{ {
$collection = LayerModel::findBy('id !', $dataContainer->id); $repository = $this->repositoryManager->getRepository(LayerModel::class);
$collection = $repository->findBy(['tl_leaflet_layer.id!=?'], [$dataContainer->id]);
return OptionsBuilder::fromCollection($collection, 'title') return OptionsBuilder::fromCollection($collection, 'title')
->asTree() ->asTree()
@@ -424,7 +446,8 @@ class LayerDcaListener extends AbstractListener
*/ */
public function getIcons() public function getIcons()
{ {
$collection = IconModel::findAll(['order' => 'title']); $repository = $this->repositoryManager->getRepository(IconModel::class);
$collection = $repository->findAll(['order' => 'title']);
$builder = OptionsBuilder::fromCollection( $builder = OptionsBuilder::fromCollection(
$collection, $collection,
function ($model) { function ($model) {

View File

@@ -12,11 +12,14 @@
namespace Netzmacht\Contao\Leaflet\Listener\Dca; namespace Netzmacht\Contao\Leaflet\Listener\Dca;
use Contao\Controller; use Contao\CoreBundle\Framework\Adapter;
use Contao\DataContainer; use Contao\DataContainer;
use Contao\System;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\LeafletPHP\Value\LatLng; use Netzmacht\LeafletPHP\Value\LatLng;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Templating\EngineInterface as TemplateEngine;
/** /**
* Class Leaflet is the base helper providing different methods. * Class Leaflet is the base helper providing different methods.
@@ -39,16 +42,48 @@ class LeafletDcaListener
*/ */
private $cacheDir; private $cacheDir;
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/**
* Template engine.
*
* @var TemplateEngine
*/
private $templateEngine;
/**
* System adapter.
*
* @var Adapter|System
*/
private $systemAdapter;
/** /**
* LeafletCallbacks constructor. * LeafletCallbacks constructor.
* *
* @param Filesystem $fileSystem File system. * @param RepositoryManager $repositoryManager Repository manager.
* @param string $cacheDir Cache dir. * @param TemplateEngine $templateEngine Template engine.
* @param Filesystem $fileSystem File system.
* @param Adapter|System $systemAdapter Contao system adapter.
* @param string $cacheDir Cache dir.
*/ */
public function __construct(Filesystem $fileSystem, string $cacheDir) public function __construct(
{ RepositoryManager $repositoryManager,
$this->fileSystem = $fileSystem; TemplateEngine $templateEngine,
$this->cacheDir = $cacheDir; Filesystem $fileSystem,
$systemAdapter,
string $cacheDir
) {
$this->repositoryManager = $repositoryManager;
$this->templateEngine = $templateEngine;
$this->fileSystem = $fileSystem;
$this->systemAdapter = $systemAdapter;
$this->cacheDir = $cacheDir;
} }
/** /**
@@ -58,7 +93,7 @@ class LeafletDcaListener
*/ */
public function loadLanguageFile() public function loadLanguageFile()
{ {
Controller::loadLanguageFile('leaflet'); $this->systemAdapter->loadLanguageFile('leaflet');
} }
/** /**
@@ -80,17 +115,16 @@ class LeafletDcaListener
*/ */
public function getGeocoder($dataContainer) public function getGeocoder($dataContainer)
{ {
$template = new \BackendTemplate('be_leaflet_geocode'); $data['field'] = 'ctrl_' . $dataContainer->field;
$template->field = 'ctrl_' . $dataContainer->field;
try { try {
$latLng = LatLng::fromString($dataContainer->value); $latLng = LatLng::fromString($dataContainer->value);
$template->marker = json_encode($latLng); $data['marker'] = json_encode($latLng);
} catch (\Exception $e) { } catch (\Exception $e) {
// LatLng throws an exception of value could not be created. Just let the value empty when. // LatLng throws an exception of value could not be created. Just let the value empty when.
} }
return $template->parse(); return $this->templateEngine->render('toolkit:be:be_leaflet_geocode.html5', $data);
} }
/** /**
@@ -101,7 +135,8 @@ class LeafletDcaListener
public function getLayers() public function getLayers()
{ {
$options = []; $options = [];
$collection = LayerModel::findBy('pid', '0', ['order' => 'title']); $repository = $this->repositoryManager->getRepository(LayerModel::class);
$collection = $repository->findBy(['pid=?'], ['0'], ['order' => 'title']);
if ($collection) { if ($collection) {
foreach ($collection as $model) { foreach ($collection as $model) {

View File

@@ -18,6 +18,7 @@ use Contao\DataContainer;
use Contao\StringUtil; use Contao\StringUtil;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener; use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener;
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;
@@ -44,17 +45,26 @@ class MapDcaListener extends AbstractListener
*/ */
private $connection; private $connection;
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/** /**
* Construct. * Construct.
* *
* @param Manager $manager Data container manager. * @param Manager $manager Data container manager.
* @param Connection $connection Database connection. * @param Connection $connection Database connection.
* @param RepositoryManager $repositoryManager Repository manager.
*/ */
public function __construct(Manager $manager, Connection $connection) public function __construct(Manager $manager, Connection $connection, RepositoryManager $repositoryManager)
{ {
parent::__construct($manager); parent::__construct($manager);
$this->connection = $connection; $this->connection = $connection;
$this->repositoryManager = $repositoryManager;
} }
/** /**
@@ -150,13 +160,12 @@ class MapDcaListener extends AbstractListener
/** /**
* Get all layers except of the current layer. * Get all layers except of the current layer.
* *
* @param DataContainer $dataContainer The dataContainer driver.
*
* @return array * @return array
*/ */
public function getLayers($dataContainer) public function getLayers()
{ {
$collection = LayerModel::findBy('id !', $dataContainer->id); $repository = $this->repositoryManager->getRepository(LayerModel::class);
$collection = $repository->findAll(['order' => 'title']);
return OptionsBuilder::fromCollection($collection, 'title') return OptionsBuilder::fromCollection($collection, 'title')
->asTree() ->asTree()

View File

@@ -16,6 +16,7 @@ use Contao\Controller;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Netzmacht\Contao\Leaflet\Model\IconModel; use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Contao\Leaflet\Model\PopupModel; use Netzmacht\Contao\Leaflet\Model\PopupModel;
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
/** /**
@@ -33,23 +34,22 @@ class MarkerDcaListener
private $connection; private $connection;
/** /**
* MarkerDcaListener constructor. * Repository manager.
* *
* @param Connection $connection Database connection. * @var RepositoryManager
*/ */
public function __construct(Connection $connection) private $repositoryManager;
{
$this->connection = $connection;
}
/** /**
* Initialize the language files. * MarkerDcaListener constructor.
* *
* @return void * @param Connection $connection Database connection.
* @param RepositoryManager $repositoryManager Repository manager.
*/ */
public function initialize() public function __construct(Connection $connection, RepositoryManager $repositoryManager)
{ {
Controller::loadLanguageFile('leaflet'); $this->connection = $connection;
$this->repositoryManager = $repositoryManager;
} }
/** /**
@@ -71,7 +71,8 @@ class MarkerDcaListener
*/ */
public function getIcons() public function getIcons()
{ {
$collection = IconModel::findAll(['order' => 'title']); $repository = $this->repositoryManager->getRepository(IconModel::class);
$collection = $repository->findAll(['order' => 'title']);
$builder = OptionsBuilder::fromCollection( $builder = OptionsBuilder::fromCollection(
$collection, $collection,
function ($model) { function ($model) {
@@ -89,7 +90,8 @@ class MarkerDcaListener
*/ */
public function getPopups() public function getPopups()
{ {
$collection = PopupModel::findAll(['order' => 'title']); $repository = $this->repositoryManager->getRepository(PopupModel::class);
$collection = $repository->findAll(['order' => 'title']);
$builder = OptionsBuilder::fromCollection($collection, 'title'); $builder = OptionsBuilder::fromCollection($collection, 'title');
return $builder->getOptions(); return $builder->getOptions();

View File

@@ -12,9 +12,9 @@
namespace Netzmacht\Contao\Leaflet\Listener\Dca; namespace Netzmacht\Contao\Leaflet\Listener\Dca;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Contao\Leaflet\Model\PopupModel; use Netzmacht\Contao\Leaflet\Model\PopupModel;
use Netzmacht\Contao\Leaflet\Model\StyleModel; use Netzmacht\Contao\Leaflet\Model\StyleModel;
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener; use Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener;
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;
@@ -40,17 +40,26 @@ class VectorDcaListener extends AbstractListener
*/ */
private $vectors; private $vectors;
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/** /**
* Construct. * Construct.
* *
* @param Manager $dcaManager Data container manager. * @param Manager $dcaManager Data container manager.
* @param array $vectors Vectors. * @param RepositoryManager $repositoryManager Repository manager.
* @param array $vectors Vectors.
*/ */
public function __construct(Manager $dcaManager, array $vectors) public function __construct(Manager $dcaManager, RepositoryManager $repositoryManager, array $vectors)
{ {
parent::__construct($dcaManager); parent::__construct($dcaManager);
$this->vectors = $vectors; $this->vectors = $vectors;
$this->repositoryManager = $repositoryManager;
} }
/** /**
@@ -86,29 +95,12 @@ class VectorDcaListener extends AbstractListener
*/ */
public function getStyles() public function getStyles()
{ {
$collection = StyleModel::findAll(['order' => 'title']); $repository = $this->repositoryManager->getRepository(StyleModel::class);
$collection = $repository->findAll(['order' => 'title']);
return OptionsBuilder::fromCollection($collection, 'title')->getOptions(); return OptionsBuilder::fromCollection($collection, 'title')->getOptions();
} }
/**
* Get all icons.
*
* @return array
*/
public function getIcons()
{
$collection = IconModel::findAll(['order' => 'title']);
$builder = OptionsBuilder::fromCollection(
$collection,
function ($model) {
return sprintf('%s [%s]', $model['title'], $model['type']);
}
);
return $builder->getOptions();
}
/** /**
* Get all popups. * Get all popups.
* *
@@ -116,7 +108,8 @@ class VectorDcaListener extends AbstractListener
*/ */
public function getPopups() public function getPopups()
{ {
$collection = PopupModel::findAll(['order' => 'title']); $repository = $this->repositoryManager->getRepository(PopupModel::class);
$collection = $repository->findAll(['order' => 'title']);
$builder = OptionsBuilder::fromCollection($collection, 'title'); $builder = OptionsBuilder::fromCollection($collection, 'title');
return $builder->getOptions(); return $builder->getOptions();