mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-12-04 13:58:39 +01:00
Implement boundsMode fit.
This commit is contained in:
@@ -11,8 +11,8 @@
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Frontend;
|
||||
|
||||
use Netzmacht\Contao\Leaflet\Filter\Filter;
|
||||
use Netzmacht\Contao\Leaflet\MapService;
|
||||
use Netzmacht\Contao\Leaflet\Model\LayerModel;
|
||||
|
||||
/**
|
||||
* The data controller handles ajax request for sub data.
|
||||
@@ -36,7 +36,9 @@ class DataController
|
||||
private $input = array(
|
||||
'format' => 'geojson',
|
||||
'type' => 'layer',
|
||||
'id' => null
|
||||
'id' => null,
|
||||
'filter' => null,
|
||||
'values' => null
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -61,7 +63,13 @@ class DataController
|
||||
public function execute()
|
||||
{
|
||||
try {
|
||||
list($data, $error) = $this->loadData($this->input['type'], $this->input['id']);
|
||||
if ($this->input['filter']) {
|
||||
$filter = $this->createFilter();
|
||||
} else {
|
||||
$filter = null;
|
||||
}
|
||||
|
||||
list($data, $error) = $this->loadData($this->input['type'], $this->input['id'], $filter);
|
||||
$this->encodeData($this->input['format'], $data);
|
||||
} catch (\Exception $e) {
|
||||
if (\Config::get('debugMode') || \Config::get('displayErrors')) {
|
||||
@@ -101,17 +109,18 @@ class DataController
|
||||
*
|
||||
* @param string $type The data type.
|
||||
* @param mixed $dataId The data id.
|
||||
* @param Filter $filter Optional request filter.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function loadData($type, $dataId)
|
||||
public function loadData($type, $dataId, Filter $filter = null)
|
||||
{
|
||||
$error = false;
|
||||
$data = null;
|
||||
|
||||
switch ($type) {
|
||||
case 'layer':
|
||||
$data = $this->mapService->getFeatureCollection($dataId);
|
||||
$data = $this->mapService->getFeatureCollection($dataId, $filter);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -122,4 +131,24 @@ class DataController
|
||||
|
||||
return array($data, $error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a filter.
|
||||
*
|
||||
* @return Filter
|
||||
* @throws \RuntimeException If the filter is not defined.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
*/
|
||||
private function createFilter()
|
||||
{
|
||||
if (!isset($GLOBALS['LEAFLET_FILTERS'][$this->input['filter']])) {
|
||||
throw new \RuntimeException(sprintf('Undefined filter "%s".', $this->input['filter']));
|
||||
}
|
||||
|
||||
/** @var Filter $filter */
|
||||
$filter = $GLOBALS['LEAFLET_FILTERS'][$this->input['filter']];
|
||||
|
||||
return $filter::fromRequest($this->input['values']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ trait HybridTrait
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \HttpRequestException If a bad leaflet param hash is given.
|
||||
* @throws \RuntimeException If a bad leaflet param hash is given.
|
||||
* @SuppressWarnings(ExitExpression)
|
||||
*/
|
||||
private function handleAjaxRequest()
|
||||
@@ -142,13 +142,15 @@ trait HybridTrait
|
||||
|
||||
// Handle ajax request.
|
||||
if ($input) {
|
||||
$data = explode(',', base64_decode($input));
|
||||
$data = explode(',', base64_decode($input));
|
||||
$data[] = $this->input->get('f');
|
||||
$data[] = $this->input->get('v');
|
||||
|
||||
if (count($data) != 4) {
|
||||
throw new \HttpRequestException('Bad request. Could not resolve query params');
|
||||
if (count($data) != 6) {
|
||||
throw new \RuntimeException('Bad request. Could not resolve query params');
|
||||
}
|
||||
|
||||
$data = array_combine(array('for', 'type', 'id', 'format'), $data);
|
||||
$data = array_combine(array('for', 'type', 'id', 'format', 'filter', 'values'), $data);
|
||||
$data = array_filter($data);
|
||||
|
||||
if (empty($data['for']) || $data['for'] != $this->getIdentifier()) {
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Frontend;
|
||||
|
||||
use Netzmacht\Contao\Leaflet\Filter\Filter;
|
||||
|
||||
/**
|
||||
* Class RequestUrl creates the request url.
|
||||
*
|
||||
@@ -39,6 +41,13 @@ class RequestUrl implements \JsonSerializable
|
||||
*/
|
||||
private $url;
|
||||
|
||||
/**
|
||||
* Request filter.
|
||||
*
|
||||
* @var Filter|null
|
||||
*/
|
||||
private $filter;
|
||||
|
||||
/**
|
||||
* Create the request url.
|
||||
*
|
||||
@@ -47,10 +56,11 @@ class RequestUrl implements \JsonSerializable
|
||||
* @param int $dataId The data object id.
|
||||
* @param string|null $type Object type. If empty it assumes a layer.
|
||||
* @param string|null $format Data format. If empty it assumes geojson.
|
||||
* @param Filter $filter Optional filter.
|
||||
*
|
||||
* @return RequestUrl
|
||||
*/
|
||||
public static function create($dataId, $type = null, $format = null)
|
||||
public static function create($dataId, $type = null, $format = null, Filter $filter = null)
|
||||
{
|
||||
$params = array(
|
||||
'for' => static::$for,
|
||||
@@ -59,10 +69,16 @@ class RequestUrl implements \JsonSerializable
|
||||
'format' => $format != 'geojson' ? $format : null
|
||||
);
|
||||
|
||||
$hash = base64_encode(implode(',', $params));
|
||||
$url = \Config::get('websitePath') . '/' . \Frontend::addToUrl('leaflet=' . $hash, false);
|
||||
$hash = base64_encode(implode(',', $params));
|
||||
$query = 'leaflet=' . $hash;
|
||||
|
||||
return new static($url, $hash);
|
||||
if ($filter) {
|
||||
$query .= '&f=' . $filter->getName() . '&v=' . $filter->toRequest();
|
||||
}
|
||||
|
||||
$url = \Config::get('websitePath') . '/' . \Frontend::addToUrl($query, false);
|
||||
|
||||
return new static($url, $hash, $filter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,6 +125,16 @@ class RequestUrl implements \JsonSerializable
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request filter.
|
||||
*
|
||||
* @return Filter|null
|
||||
*/
|
||||
public function getFilter()
|
||||
{
|
||||
return $this->filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to string will always return the whole url.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user