mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-12-01 04:24:02 +01:00
Refactor ajax loading so that the current page is used.
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
namespace Netzmacht\Contao\Leaflet\Frontend;
|
namespace Netzmacht\Contao\Leaflet\Frontend;
|
||||||
|
|
||||||
use Netzmacht\Contao\Leaflet\MapService;
|
use Netzmacht\Contao\Leaflet\MapService;
|
||||||
|
use Netzmacht\Contao\Leaflet\Model\LayerModel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data controller handles ajax request for sub data.
|
* The data controller handles ajax request for sub data.
|
||||||
@@ -28,22 +29,26 @@ class DataController
|
|||||||
private $mapService;
|
private $mapService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The user input object.
|
* The user input data.
|
||||||
*
|
*
|
||||||
* @var \Input
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $input;
|
private $input = array(
|
||||||
|
'format' => 'geojson',
|
||||||
|
'type' => 'layer',
|
||||||
|
'id' => null
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct.
|
* Construct.
|
||||||
*
|
*
|
||||||
* @param MapService $mapService The map service.
|
* @param MapService $mapService The map service.
|
||||||
* @param \Input $input The user input object.
|
* @param array $input The user input as array.
|
||||||
*/
|
*/
|
||||||
public function __construct(MapService $mapService, \Input $input)
|
public function __construct(MapService $mapService, $input)
|
||||||
{
|
{
|
||||||
$this->mapService = $mapService;
|
$this->mapService = $mapService;
|
||||||
$this->input = $input;
|
$this->input = array_merge($this->input, $input);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,21 +60,9 @@ class DataController
|
|||||||
*/
|
*/
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
$format = $this->getInput('format', 'geojson');
|
|
||||||
$type = $this->getInput('type', 'layer');
|
|
||||||
$dataId = $this->getInput('id');
|
|
||||||
$page = \Input::get('page', true);
|
|
||||||
|
|
||||||
if ($page) {
|
|
||||||
// Fake a page request.
|
|
||||||
\Environment::set('request', base64_decode($page));
|
|
||||||
|
|
||||||
// We need the auto_item being set. So call the getPageIdFromUrl method, this does it internally.
|
|
||||||
\Frontend::getPageIdFromUrl();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
list($data, $error) = $this->loadData($type, $dataId);
|
list($data, $error) = $this->loadData($this->input['type'], $this->input['id']);
|
||||||
|
$this->encodeData($this->input['format'], $data);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
if (\Config::get('debugMode') || \Config::get('displayErrors')) {
|
if (\Config::get('debugMode') || \Config::get('displayErrors')) {
|
||||||
throw $e;
|
throw $e;
|
||||||
@@ -79,13 +72,11 @@ class DataController
|
|||||||
|
|
||||||
if ($error) {
|
if ($error) {
|
||||||
header('HTTP/1.1 500 Internal Server Error');
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
} else {
|
|
||||||
$this->encodeData($format, $data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enocode the data.
|
* Encode the data.
|
||||||
*
|
*
|
||||||
* @param string $format The requested format.
|
* @param string $format The requested format.
|
||||||
* @param mixed $data The given data.
|
* @param mixed $data The given data.
|
||||||
@@ -131,17 +122,4 @@ class DataController
|
|||||||
|
|
||||||
return array($data, $error);
|
return array($data, $error);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get an input value.
|
|
||||||
*
|
|
||||||
* @param string $name The input name.
|
|
||||||
* @param mixed $default Optional a default value if empty.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function getInput($name, $default = null)
|
|
||||||
{
|
|
||||||
return $this->input->get($name) ?: $default;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,20 @@ trait HybridTrait
|
|||||||
*/
|
*/
|
||||||
private $mapService;
|
private $mapService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user input.
|
||||||
|
*
|
||||||
|
* @var \Input
|
||||||
|
*/
|
||||||
|
private $input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Contao config.
|
||||||
|
*
|
||||||
|
* @var \Config
|
||||||
|
*/
|
||||||
|
private $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct.
|
* Construct.
|
||||||
*
|
*
|
||||||
@@ -41,6 +55,8 @@ trait HybridTrait
|
|||||||
parent::__construct($objElement, $strColumn);
|
parent::__construct($objElement, $strColumn);
|
||||||
|
|
||||||
$this->mapService = static::getService('leaflet.map.service');
|
$this->mapService = static::getService('leaflet.map.service');
|
||||||
|
$this->input = static::getService('input');
|
||||||
|
$this->config = static::getService('config');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,6 +66,9 @@ trait HybridTrait
|
|||||||
*/
|
*/
|
||||||
public function generate()
|
public function generate()
|
||||||
{
|
{
|
||||||
|
RequestUrl::setFor($this->getIdentifier());
|
||||||
|
$this->handleAjaxRequest();
|
||||||
|
|
||||||
if (TL_MODE === 'BE') {
|
if (TL_MODE === 'BE') {
|
||||||
$model = MapModel::findByPK($this->leaflet_map);
|
$model = MapModel::findByPK($this->leaflet_map);
|
||||||
|
|
||||||
@@ -107,4 +126,30 @@ trait HybridTrait
|
|||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle ajax request if leaflet parameter is given.
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
private function handleAjaxRequest()
|
||||||
|
{
|
||||||
|
$input = $this->input->get('leaflet');
|
||||||
|
|
||||||
|
// Handle ajax request.
|
||||||
|
if ($input) {
|
||||||
|
$data = (array) explode(',', base64_decode($input));
|
||||||
|
$data = array_combine(array('for', 'type', 'id', 'format'), $data);
|
||||||
|
$data = array_filter($data);
|
||||||
|
|
||||||
|
if (empty($data['for']) || $data['for'] != $this->getIdentifier()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$controller = new DataController($this->mapService, $data);
|
||||||
|
$controller->execute();
|
||||||
|
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,4 +39,9 @@ class MapElement extends \ContentElement
|
|||||||
{
|
{
|
||||||
$this->construct($objElement, $strColumn);
|
$this->construct($objElement, $strColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getIdentifier()
|
||||||
|
{
|
||||||
|
return 'ce_' . $this->id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,4 +38,9 @@ class MapModule extends \Module
|
|||||||
{
|
{
|
||||||
$this->construct($objElement, $strColumn);
|
$this->construct($objElement, $strColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getIdentifier()
|
||||||
|
{
|
||||||
|
return 'mod_' . $this->id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,13 @@ class RequestUrl
|
|||||||
{
|
{
|
||||||
const BASE = 'assets/leaflet/maps/data.php';
|
const BASE = 'assets/leaflet/maps/data.php';
|
||||||
|
|
||||||
|
private static $for;
|
||||||
|
|
||||||
|
public static function setFor($for)
|
||||||
|
{
|
||||||
|
static::$for = $for;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the request url.
|
* Create the request url.
|
||||||
*
|
*
|
||||||
@@ -33,45 +40,15 @@ class RequestUrl
|
|||||||
*/
|
*/
|
||||||
public static function create($dataId, $type = null, $format = null)
|
public static function create($dataId, $type = null, $format = null)
|
||||||
{
|
{
|
||||||
return self::createBuilder($dataId, $type, $format)->getUrl();
|
$params = array(
|
||||||
}
|
'for' => static::$for,
|
||||||
|
'type' => $type != 'layer' ? $type : null,
|
||||||
|
'id' => $dataId,
|
||||||
|
'format' => $format != 'geojson' ? $format: null
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
$param = base64_encode(implode(',', $params));
|
||||||
* Create the request builder.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*
|
|
||||||
* @return UrlBuilder
|
|
||||||
*/
|
|
||||||
public static function createBuilder($dataId, $type = null, $format = null)
|
|
||||||
{
|
|
||||||
$path = \Config::get('websitePath') . '/' . static::BASE;
|
|
||||||
$builder = new UrlBuilder();
|
|
||||||
$builder
|
|
||||||
->setPath($path)
|
|
||||||
->setQueryParameter('type', $type ?: 'layer')
|
|
||||||
->setQueryParameter('format', $format ?: 'geojson')
|
|
||||||
->setQueryParameter('id', $dataId)
|
|
||||||
->setQueryParameter('page', rawurlencode(base64_encode(self::getRequestUri())));
|
|
||||||
|
|
||||||
return $builder;
|
return \Config::get('websitePath') . '/' . \Frontend::addToUrl('leaflet=' . $param);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the request uri without leading slash.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
protected static function getRequestUri()
|
|
||||||
{
|
|
||||||
$uri = \Environment::get('requestUri');
|
|
||||||
|
|
||||||
if (strpos($uri, '/') === 0) {
|
|
||||||
return substr($uri, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $uri;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user