2017-10-06 15:50:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Leaflet maps for Contao CMS.
|
|
|
|
|
*
|
|
|
|
|
* @package contao-leaflet-maps
|
|
|
|
|
* @author David Molineus <david.molineus@netzmacht.de>
|
|
|
|
|
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
|
|
|
|
|
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
|
|
|
|
|
* @filesource
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Netzmacht\Contao\Leaflet\Listener;
|
|
|
|
|
|
|
|
|
|
use Netzmacht\Contao\Leaflet\ContaoAssets;
|
2017-10-09 15:36:11 +02:00
|
|
|
use Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration;
|
2017-10-06 15:50:54 +02:00
|
|
|
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Model\IconModel;
|
|
|
|
|
use Netzmacht\LeafletPHP\Assets;
|
|
|
|
|
use Netzmacht\LeafletPHP\Definition\Type\Icon;
|
|
|
|
|
use Netzmacht\LeafletPHP\Definition\Type\ImageIcon;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class LoadAssetsListener.
|
|
|
|
|
*
|
|
|
|
|
* @package Netzmacht\Contao\Leaflet\Listener
|
|
|
|
|
*/
|
|
|
|
|
class LoadAssetsListener
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Assets.
|
|
|
|
|
*
|
|
|
|
|
* @var Assets
|
|
|
|
|
*/
|
|
|
|
|
private $assets;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Definition mapper.
|
|
|
|
|
*
|
|
|
|
|
* @var DefinitionMapper
|
|
|
|
|
*/
|
|
|
|
|
private $definitionMapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Libraries.
|
|
|
|
|
*
|
2017-10-09 15:36:11 +02:00
|
|
|
* @var LibrariesConfiguration
|
2017-10-06 15:50:54 +02:00
|
|
|
*/
|
|
|
|
|
private $libraries;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* LoadAssetsListener constructor.
|
|
|
|
|
*
|
2017-10-09 15:36:11 +02:00
|
|
|
* @param Assets $assets Assets.
|
|
|
|
|
* @param DefinitionMapper $definitionMapper Definition mapper.
|
|
|
|
|
* @param LibrariesConfiguration $libraries Libraries.
|
2017-10-06 15:50:54 +02:00
|
|
|
*/
|
2017-10-09 15:36:11 +02:00
|
|
|
public function __construct(Assets $assets, DefinitionMapper $definitionMapper, LibrariesConfiguration $libraries)
|
2017-10-06 15:50:54 +02:00
|
|
|
{
|
|
|
|
|
$this->assets = $assets;
|
|
|
|
|
$this->definitionMapper = $definitionMapper;
|
2017-10-06 15:53:26 +02:00
|
|
|
$this->libraries = $libraries;
|
2017-10-06 15:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the get javascript event.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2017-10-11 11:39:23 +02:00
|
|
|
public function onGetJavascriptEvent()
|
2017-10-06 15:50:54 +02:00
|
|
|
{
|
2017-10-09 15:36:11 +02:00
|
|
|
$this->assets->addJavascript(
|
2017-10-09 16:05:34 +02:00
|
|
|
'bundles/netzmachtcontaoleaflet/js/contao-leaflet.js',
|
2017-10-09 15:36:11 +02:00
|
|
|
ContaoAssets::TYPE_FILE
|
|
|
|
|
);
|
2017-10-06 15:50:54 +02:00
|
|
|
|
|
|
|
|
$collection = IconModel::findBy('active', true);
|
|
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
$buffer = '';
|
|
|
|
|
$icons = [];
|
|
|
|
|
|
|
|
|
|
foreach ($collection as $model) {
|
|
|
|
|
/** @var ImageIcon $icon */
|
|
|
|
|
$icon = $this->definitionMapper->handle($model);
|
|
|
|
|
$icons[] = [
|
|
|
|
|
'id' => $icon->getId(),
|
|
|
|
|
'type' => lcfirst($icon->getType()),
|
|
|
|
|
'options' => $icon->getOptions(),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->loadIconsLibraries($icon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($icons) {
|
|
|
|
|
$buffer = sprintf('L.contao.loadIcons(%s);', json_encode($icons));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
|
// TODO: Cache it.
|
|
|
|
|
// codingStandardsIgnoreEnd
|
|
|
|
|
|
|
|
|
|
$file = new \File('assets/leaflet/js/icons.js');
|
|
|
|
|
$file->write($buffer);
|
|
|
|
|
$file->close();
|
|
|
|
|
|
|
|
|
|
$this->assets->addJavascript('assets/leaflet/js/icons.js', ContaoAssets::TYPE_FILE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load all libraries for an icon.
|
|
|
|
|
*
|
|
|
|
|
* @param Icon $icon Icon definition.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2017-10-11 11:39:23 +02:00
|
|
|
protected function loadIconsLibraries($icon)
|
2017-10-06 15:50:54 +02:00
|
|
|
{
|
|
|
|
|
foreach ($icon::getRequiredLibraries() as $library) {
|
|
|
|
|
if (!isset($this->libraries[$library])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$assets = $this->libraries[$library];
|
|
|
|
|
|
|
|
|
|
if (!empty($assets['css'])) {
|
|
|
|
|
list ($source, $type) = (array)$assets['css'];
|
|
|
|
|
$this->assets->addStylesheet($source, $type ?: Assets::TYPE_FILE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($assets['javascript'])) {
|
|
|
|
|
list ($source, $type) = (array)$assets['javascript'];
|
|
|
|
|
$this->assets->addJavascript($source, $type ?: Assets::TYPE_FILE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|