forked from Snck3rs/contao-leaflet-maps
Move ajax handling to the map service.
This commit is contained in:
@@ -66,7 +66,7 @@ trait HybridTrait
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$this->handleAjaxRequest();
|
||||
$this->mapService->handleAjaxRequest('map_' . $this->getIdentifier());
|
||||
|
||||
if (TL_MODE === 'BE') {
|
||||
$model = MapModel::findByPK($this->leaflet_map);
|
||||
@@ -101,10 +101,8 @@ trait HybridTrait
|
||||
protected function compile()
|
||||
{
|
||||
try {
|
||||
RequestUrl::setFor($this->getIdentifier());
|
||||
$mapId = 'map_' . ($this->cssID[0] ?: ($this->getIdentifier()));
|
||||
$mapId = 'map_' . $this->getIdentifier();
|
||||
$map = $this->mapService->generate($this->leaflet_map, null, $mapId);
|
||||
RequestUrl::setFor(null);
|
||||
|
||||
$GLOBALS['TL_BODY'][] = '<script>' . $map .'</script>';
|
||||
|
||||
@@ -127,40 +125,4 @@ trait HybridTrait
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle ajax request if leaflet parameter is given.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \RuntimeException If a bad leaflet param hash is given.
|
||||
* @SuppressWarnings(ExitExpression)
|
||||
*/
|
||||
private function handleAjaxRequest()
|
||||
{
|
||||
$input = $this->input->get('leaflet', true);
|
||||
|
||||
// Handle ajax request.
|
||||
if ($input) {
|
||||
$data = explode(',', base64_decode($input));
|
||||
$data[] = $this->input->get('f');
|
||||
$data[] = $this->input->get('v');
|
||||
|
||||
if (count($data) != 6) {
|
||||
throw new \RuntimeException('Bad request. Could not resolve query params');
|
||||
}
|
||||
|
||||
$data = array_combine(array('for', 'type', 'id', 'format', 'filter', 'values'), $data);
|
||||
$data = array_filter($data);
|
||||
|
||||
if (empty($data['for']) || $data['for'] != $this->getIdentifier()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$controller = new DataController($this->mapService, $data);
|
||||
$controller->execute();
|
||||
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ namespace Netzmacht\Contao\Leaflet;
|
||||
|
||||
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
|
||||
use Netzmacht\Contao\Leaflet\Filter\Filter;
|
||||
use Netzmacht\Contao\Leaflet\Frontend\DataController;
|
||||
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
|
||||
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
|
||||
use Netzmacht\Contao\Leaflet\Model\LayerModel;
|
||||
use Netzmacht\Contao\Leaflet\Model\MapModel;
|
||||
@@ -51,26 +53,39 @@ class MapService
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
|
||||
/**
|
||||
* The request input.
|
||||
*
|
||||
* @var \Input
|
||||
*/
|
||||
private $input;
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*
|
||||
* @param DefinitionMapper $mapper The definition mapper.
|
||||
* @param Leaflet $leaflet The Leaflet instance.
|
||||
* @param EventDispatcher $eventDispatcher The Contao event dispatcher.
|
||||
* @param \Input $input Thw request input.
|
||||
*/
|
||||
public function __construct(DefinitionMapper $mapper, Leaflet $leaflet, EventDispatcher $eventDispatcher)
|
||||
{
|
||||
public function __construct(
|
||||
DefinitionMapper $mapper,
|
||||
Leaflet $leaflet,
|
||||
EventDispatcher $eventDispatcher,
|
||||
\Input $input
|
||||
) {
|
||||
$this->mapper = $mapper;
|
||||
$this->leaflet = $leaflet;
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
$this->input = $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get map definition.
|
||||
*
|
||||
* @param MapModel|int $mapId The map database id. MapModel accepted as well.
|
||||
* @param Filter $filter Optional request filter.
|
||||
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
||||
* @param MapModel|int $mapId The map database id. MapModel accepted as well.
|
||||
* @param Filter $filter Optional request filter.
|
||||
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
@@ -78,11 +93,16 @@ class MapService
|
||||
{
|
||||
if ($mapId instanceof MapModel) {
|
||||
$model = $mapId;
|
||||
$mapId = $model->id;
|
||||
} else {
|
||||
$model = $this->getModel($mapId);
|
||||
}
|
||||
|
||||
return $this->mapper->handle($model, $filter, $elementId);
|
||||
RequestUrl::setFor($elementId ?: $mapId);
|
||||
$definition = $this->mapper->reset()->handle($model, $filter, $elementId);
|
||||
RequestUrl::setFor(null);
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,25 +129,22 @@ class MapService
|
||||
* Get map javascript.
|
||||
*
|
||||
* @param MapModel|int $mapId The map database id. MapModel accepted as well.
|
||||
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
||||
* @param Filter $filter Optional request filter.
|
||||
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
||||
* @param string $template The template being used for generating.
|
||||
* @param string $style Optional style attributes.
|
||||
*
|
||||
* @return string
|
||||
* @throws \Exception If generating went wrong.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedLocalVariables)
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function generate(
|
||||
$mapId,
|
||||
LatLngBounds $bounds = null,
|
||||
Filter $filter = null,
|
||||
$elementId = null,
|
||||
$template = 'leaflet_map_js',
|
||||
$style = ''
|
||||
) {
|
||||
$definition = $this->getDefinition($mapId, $bounds, $elementId);
|
||||
$definition = $this->getDefinition($mapId, $filter, $elementId);
|
||||
$assets = new ContaoAssets();
|
||||
$template = \Controller::getTemplate($template);
|
||||
|
||||
@@ -171,4 +188,42 @@ class MapService
|
||||
|
||||
return $this->mapper->handleGeoJson($model, $filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle ajax request.
|
||||
*
|
||||
* @param string $identifier The request identifier.
|
||||
* @param bool $exit Exit if ajax request is detected.
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function handleAjaxRequest($identifier, $exit = true)
|
||||
{
|
||||
$input = $this->input->get('leaflet', true);
|
||||
|
||||
// Handle ajax request.
|
||||
if ($input) {
|
||||
$data = explode(',', base64_decode($input));
|
||||
$data[] = $this->input->get('f');
|
||||
$data[] = $this->input->get('v');
|
||||
|
||||
if (count($data) != 6) {
|
||||
throw new \RuntimeException('Bad request. Could not resolve query params');
|
||||
}
|
||||
|
||||
$data = array_combine(array('for', 'type', 'id', 'format', 'filter', 'values'), $data);
|
||||
$data = array_filter($data);
|
||||
|
||||
if (empty($data['for']) || $data['for'] != $identifier) {
|
||||
return;
|
||||
}
|
||||
|
||||
$controller = new DataController($this, $data);
|
||||
$controller->execute();
|
||||
|
||||
if ($exit) {
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
|
||||
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
|
||||
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
|
||||
use Netzmacht\Contao\Leaflet\Filter\Filter;
|
||||
use Netzmacht\Contao\Leaflet\Model\MapModel;
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
use Netzmacht\LeafletPHP\Value\GeoJson\ConvertsToGeoJsonFeature;
|
||||
use Netzmacht\LeafletPHP\Value\GeoJson\Feature;
|
||||
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
|
||||
use Netzmacht\LeafletPHP\Value\GeoJson\GeoJsonFeature;
|
||||
use Netzmacht\LeafletPHP\Value\LatLngBounds;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
|
||||
|
||||
/**
|
||||
@@ -78,6 +78,18 @@ class DefinitionMapper
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the internal cache.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
$this->mapped = array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a model.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user