2014-12-29 12:17:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-05 15:45:43 +02:00
|
|
|
* Leaflet maps for Contao CMS.
|
|
|
|
|
*
|
2016-10-11 10:40:15 +02:00
|
|
|
* @package contao-leaflet-maps
|
2014-12-29 12:17:40 +01:00
|
|
|
* @author David Molineus <david.molineus@netzmacht.de>
|
2017-10-11 15:00:48 +02:00
|
|
|
* @copyright 2014-2017 netzmacht David Molineus. All rights reserved.
|
2017-10-05 15:45:43 +02:00
|
|
|
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
|
2014-12-29 12:17:40 +01:00
|
|
|
* @filesource
|
|
|
|
|
*/
|
|
|
|
|
|
2017-10-17 17:48:14 +02:00
|
|
|
namespace Netzmacht\Contao\Leaflet\Encoder\Subscriber;
|
2014-12-29 12:17:40 +01:00
|
|
|
|
2015-01-15 12:45:23 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
|
2017-10-17 16:22:24 +02:00
|
|
|
use Netzmacht\JavascriptBuilder\Exception\EncodeValueFailed;
|
2015-01-28 10:33:00 +01:00
|
|
|
use Netzmacht\JavascriptBuilder\Flags;
|
2015-01-16 15:02:20 +01:00
|
|
|
use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent;
|
2017-10-17 16:22:24 +02:00
|
|
|
use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeValueEvent;
|
2015-01-12 15:11:35 +01:00
|
|
|
use Netzmacht\LeafletPHP\Definition\Type\Icon;
|
2015-01-13 14:54:23 +01:00
|
|
|
use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer;
|
2014-12-29 12:17:40 +01:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class EncoderSubscriber subscribes to the internal encoding event dispatcher.
|
|
|
|
|
*
|
|
|
|
|
* @package Netzmacht\Contao\Leaflet\Subscriber
|
|
|
|
|
*/
|
|
|
|
|
class EncoderSubscriber implements EventSubscriberInterface
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
|
{
|
2017-10-17 18:03:49 +02:00
|
|
|
return [
|
2017-10-17 18:11:28 +02:00
|
|
|
EncodeValueEvent::NAME => [
|
2017-10-17 18:03:49 +02:00
|
|
|
['encodeIcons', 1000],
|
|
|
|
|
['loadLayer', 100],
|
|
|
|
|
],
|
|
|
|
|
EncodeReferenceEvent::NAME => ['referenceIcon', 100],
|
|
|
|
|
];
|
2014-12-29 12:17:40 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-15 15:30:16 +01:00
|
|
|
/**
|
|
|
|
|
* Create icon reference to the contao leaflet icon registry.
|
|
|
|
|
*
|
2015-01-16 15:02:20 +01:00
|
|
|
* @param EncodeReferenceEvent $event The subscribed event.
|
2015-01-15 15:30:16 +01:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2015-01-16 15:02:20 +01:00
|
|
|
public function referenceIcon(EncodeReferenceEvent $event)
|
2015-01-15 01:10:36 +01:00
|
|
|
{
|
|
|
|
|
$value = $event->getObject();
|
|
|
|
|
|
|
|
|
|
if ($value instanceof Icon) {
|
2015-01-15 13:01:58 +01:00
|
|
|
$event->setReference('L.contao.getIcon(\'' . $value->getId() . '\')');
|
2015-01-15 01:10:36 +01:00
|
|
|
$event->stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-12 19:03:29 +01:00
|
|
|
/**
|
2015-01-15 13:01:58 +01:00
|
|
|
* Force that icons are encoded as reference to the L.contao icon registry.
|
2015-01-12 19:03:29 +01:00
|
|
|
*
|
|
|
|
|
* @param EncodeValueEvent $event The subscribed event.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2015-01-12 15:11:35 +01:00
|
|
|
public function encodeIcons(EncodeValueEvent $event)
|
|
|
|
|
{
|
|
|
|
|
$value = $event->getValue();
|
|
|
|
|
|
|
|
|
|
if ($value instanceof Icon) {
|
2015-01-15 15:30:16 +01:00
|
|
|
// Do not encode the icon, as it is generated in an separate icon file.
|
2015-01-15 01:10:36 +01:00
|
|
|
$event->setSuccessful();
|
2015-01-15 18:07:27 +01:00
|
|
|
$event->stopPropagation();
|
2015-01-12 15:11:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
2015-01-13 14:54:23 +01:00
|
|
|
|
2015-01-15 15:30:16 +01:00
|
|
|
/**
|
|
|
|
|
* Encode OmnivoreLayers so that the internal used contao.loadLayer method is used.
|
|
|
|
|
*
|
|
|
|
|
* @param EncodeValueEvent $event The subscribed event.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws EncodeValueFailed If encoding failed.
|
|
|
|
|
*/
|
2015-01-13 14:54:23 +01:00
|
|
|
public function loadLayer(EncodeValueEvent $event)
|
|
|
|
|
{
|
2015-01-18 23:38:44 +01:00
|
|
|
$value = $event->getValue();
|
|
|
|
|
$encoder = $event->getEncoder();
|
2017-10-17 17:43:12 +02:00
|
|
|
$template = 'L.contao.%s(%s, %s, %s, %s, map);';
|
|
|
|
|
$method = 'loadFile';
|
2015-01-13 14:54:23 +01:00
|
|
|
|
2015-01-14 16:47:06 +01:00
|
|
|
if ($value instanceof OmnivoreLayer) {
|
2015-01-15 12:45:23 +01:00
|
|
|
$url = $value->getUrl();
|
|
|
|
|
|
|
|
|
|
if ($url instanceof RequestUrl) {
|
2017-10-17 18:03:49 +02:00
|
|
|
$url = $url->getHash();
|
2017-10-17 17:43:12 +02:00
|
|
|
$method = 'load';
|
2015-01-15 12:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-18 13:14:32 +01:00
|
|
|
if ($value->getCustomLayer()) {
|
|
|
|
|
$ref = $encoder->encodeReference($value->getCustomLayer());
|
|
|
|
|
} else {
|
|
|
|
|
$template = $encoder->encodeReference($value) . ' = ' . $template;
|
|
|
|
|
$ref = 'null';
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-13 14:54:23 +01:00
|
|
|
$event->addLine(
|
|
|
|
|
sprintf(
|
2015-01-18 13:14:32 +01:00
|
|
|
$template,
|
2017-10-17 17:43:12 +02:00
|
|
|
$method,
|
2015-01-15 12:45:23 +01:00
|
|
|
$encoder->encodeValue($url),
|
2015-01-13 14:54:23 +01:00
|
|
|
$encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))),
|
2015-01-15 13:01:58 +01:00
|
|
|
$encoder->encodeArray($value->getOptions(), JSON_FORCE_OBJECT),
|
2015-01-18 13:14:32 +01:00
|
|
|
$ref
|
2015-01-13 14:54:23 +01:00
|
|
|
)
|
|
|
|
|
);
|
2015-01-15 01:10:36 +01:00
|
|
|
|
|
|
|
|
foreach ($value->getMethodCalls() as $call) {
|
2015-01-28 10:33:00 +01:00
|
|
|
$event->addLine($call->encode($encoder, Flags::CLOSE_STATEMENT));
|
2015-01-15 01:10:36 +01:00
|
|
|
}
|
2015-01-13 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-12-29 12:17:40 +01:00
|
|
|
}
|