2014-12-29 12:17:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @package dev
|
|
|
|
|
* @author David Molineus <david.molineus@netzmacht.de>
|
|
|
|
|
* @copyright 2014 netzmacht creative David Molineus
|
|
|
|
|
* @license LGPL 3.0
|
|
|
|
|
* @filesource
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Netzmacht\Contao\Leaflet\Subscriber;
|
|
|
|
|
|
|
|
|
|
use ContaoCommunityAlliance\Contao\EventDispatcher\EventDispatcherInitializer;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Event\InitializeEventDispatcherEvent;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Event\InitializeLeafletBuilderEvent;
|
2015-01-06 21:30:57 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
|
|
|
|
|
use Netzmacht\Contao\Leaflet\Model\IconModel;
|
|
|
|
|
use Netzmacht\Javascript\Output;
|
|
|
|
|
use Netzmacht\LeafletPHP\Definition\Type\Icon;
|
|
|
|
|
use Netzmacht\LeafletPHP\Leaflet;
|
2014-12-29 12:17:40 +01:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class BootSubscriber
|
|
|
|
|
*
|
|
|
|
|
* @package Netzmacht\Contao\Leaflet\Subscriber
|
|
|
|
|
*/
|
|
|
|
|
class BootSubscriber implements EventSubscriberInterface
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
|
{
|
|
|
|
|
return array(
|
|
|
|
|
InitializeDefinitionMapperEvent::NAME => 'initializeDefinitionMapper',
|
|
|
|
|
InitializeEventDispatcherEvent::NAME => 'initializeEventDispatcher',
|
|
|
|
|
InitializeLeafletBuilderEvent::NAME => 'initializeLeafletBuilder',
|
2015-01-06 21:30:57 +01:00
|
|
|
GetJavascriptEvent::NAME => array(array('loadAssets'), array('loadIcons')),
|
2014-12-29 12:17:40 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create and register all configured mappers.
|
|
|
|
|
*
|
|
|
|
|
* @param InitializeDefinitionMapperEvent $event The subscribed event.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*
|
|
|
|
|
* @SuppressWarnings(PHPMD.Superglobals)
|
|
|
|
|
*/
|
|
|
|
|
public function initializeDefinitionMapper(InitializeDefinitionMapperEvent $event)
|
|
|
|
|
{
|
|
|
|
|
$mapper = $event->getDefinitionMapper();
|
|
|
|
|
|
|
|
|
|
foreach ($GLOBALS['LEAFLET_MAPPERS'] as $className) {
|
|
|
|
|
if (is_array($className)) {
|
|
|
|
|
$mapper->register(new $className[0], $className[1]);
|
|
|
|
|
} else {
|
|
|
|
|
$mapper->register(new $className());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register all leaflet encoders.
|
|
|
|
|
*
|
|
|
|
|
* @param InitializeEventDispatcherEvent $event The subscribed event.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*
|
|
|
|
|
* @SuppressWarnings(PHPMD.Superglobals)
|
|
|
|
|
*/
|
|
|
|
|
public function initializeEventDispatcher(InitializeEventDispatcherEvent $event)
|
|
|
|
|
{
|
|
|
|
|
$dispatcher = $event->getEventDispatcher();
|
|
|
|
|
$initializer = new EventDispatcherInitializer();
|
|
|
|
|
|
|
|
|
|
$initializer->addSubscribers($dispatcher, $GLOBALS['LEAFLET_ENCODERS']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register all libraries assets.
|
|
|
|
|
*
|
|
|
|
|
* @param InitializeLeafletBuilderEvent $event The subscribed event.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*
|
|
|
|
|
* @SuppressWarnings(PHPMD.Superglobals)
|
|
|
|
|
*/
|
|
|
|
|
public function initializeLeafletBuilder(InitializeLeafletBuilderEvent $event)
|
|
|
|
|
{
|
|
|
|
|
$builder = $event->getBuilder();
|
|
|
|
|
|
|
|
|
|
foreach ($GLOBALS['LEAFLET_ASSETS'] as $name => $assets) {
|
|
|
|
|
if (!empty($assets['css'])) {
|
|
|
|
|
foreach ($assets['css'] as $javascript) {
|
|
|
|
|
$builder->registerStylesheet($name, $javascript[0], $javascript[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($assets['javascript'])) {
|
|
|
|
|
foreach ($assets['javascript'] as $javascript) {
|
|
|
|
|
$builder->registerJavascript($name, $javascript[0], $javascript[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load Contao leaflet assets.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function loadAssets()
|
|
|
|
|
{
|
2015-01-07 12:59:31 +01:00
|
|
|
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/libs/contao/contao-leaflet.js|static';
|
2014-12-29 12:17:40 +01:00
|
|
|
}
|
2015-01-06 21:30:57 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load icons.
|
|
|
|
|
*
|
|
|
|
|
* @throws \Netzmacht\Javascript\Exception\EncodeValueFailed
|
|
|
|
|
*/
|
|
|
|
|
public function loadIcons()
|
|
|
|
|
{
|
|
|
|
|
$collection = IconModel::findBy('active', true);
|
|
|
|
|
|
|
|
|
|
if ($collection) {
|
|
|
|
|
/** @var DefinitionMapper $mapper */
|
2015-01-07 09:32:14 +01:00
|
|
|
$mapper = $GLOBALS['container']['leaflet.definition.mapper'];
|
|
|
|
|
$buffer = '';
|
|
|
|
|
$icons = array();
|
|
|
|
|
|
2015-01-06 21:30:57 +01:00
|
|
|
/** @var Leaflet $builder */
|
|
|
|
|
$builder = $GLOBALS['container']['leaflet.definition.builder'];
|
|
|
|
|
$encoder = $builder->getBuilder()->getEncoder();
|
|
|
|
|
|
|
|
|
|
foreach ($collection as $model) {
|
|
|
|
|
/** @var Icon $icon */
|
2015-01-07 09:32:14 +01:00
|
|
|
$icon = $mapper->handle($model);
|
|
|
|
|
$icons[] = array(
|
|
|
|
|
'id' => $icon->getId(),
|
|
|
|
|
'type' => lcfirst($icon->getType()),
|
|
|
|
|
'options' => $icon->getOptions(),
|
2015-01-06 21:30:57 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-07 09:32:14 +01:00
|
|
|
if ($icons) {
|
|
|
|
|
$buffer = sprintf('ContaoLeaflet.loadIcons(%s);', $encoder->encodeValue($icons));
|
2015-01-06 21:30:57 +01:00
|
|
|
}
|
2015-01-07 09:32:14 +01:00
|
|
|
|
|
|
|
|
$file = new \File('assets/leaflet/js/icons.js');
|
|
|
|
|
$file->write($buffer);
|
|
|
|
|
$file->close();
|
|
|
|
|
|
|
|
|
|
// TODO: Cache it.
|
|
|
|
|
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/js/icons.js|static';
|
2015-01-06 21:30:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-12-29 12:17:40 +01:00
|
|
|
}
|