Refactor ajax loading so that the current page is used.

This commit is contained in:
David Molineus
2015-01-14 17:50:23 +01:00
parent cbf2c53ee0
commit d54b5ddc3e
5 changed files with 84 additions and 74 deletions

View File

@@ -12,6 +12,7 @@
namespace Netzmacht\Contao\Leaflet\Frontend;
use Netzmacht\Contao\Leaflet\MapService;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
/**
* The data controller handles ajax request for sub data.
@@ -28,22 +29,26 @@ class DataController
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.
*
* @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->input = $input;
$this->input = array_merge($this->input, $input);
}
/**
@@ -55,21 +60,9 @@ class DataController
*/
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 {
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) {
if (\Config::get('debugMode') || \Config::get('displayErrors')) {
throw $e;
@@ -79,13 +72,11 @@ class DataController
if ($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 mixed $data The given data.
@@ -131,17 +122,4 @@ class DataController
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;
}
}

View File

@@ -28,6 +28,20 @@ trait HybridTrait
*/
private $mapService;
/**
* The user input.
*
* @var \Input
*/
private $input;
/**
* The Contao config.
*
* @var \Config
*/
private $config;
/**
* Construct.
*
@@ -41,6 +55,8 @@ trait HybridTrait
parent::__construct($objElement, $strColumn);
$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()
{
RequestUrl::setFor($this->getIdentifier());
$this->handleAjaxRequest();
if (TL_MODE === 'BE') {
$model = MapModel::findByPK($this->leaflet_map);
@@ -107,4 +126,30 @@ trait HybridTrait
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;
}
}
}

View File

@@ -39,4 +39,9 @@ class MapElement extends \ContentElement
{
$this->construct($objElement, $strColumn);
}
protected function getIdentifier()
{
return 'ce_' . $this->id;
}
}

View File

@@ -38,4 +38,9 @@ class MapModule extends \Module
{
$this->construct($objElement, $strColumn);
}
protected function getIdentifier()
{
return 'mod_' . $this->id;
}
}

View File

@@ -22,6 +22,13 @@ class RequestUrl
{
const BASE = 'assets/leaflet/maps/data.php';
private static $for;
public static function setFor($for)
{
static::$for = $for;
}
/**
* Create the request url.
*
@@ -33,45 +40,15 @@ class RequestUrl
*/
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
);
/**
* 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())));
$param = base64_encode(implode(',', $params));
return $builder;
}
/**
* 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;
return \Config::get('websitePath') . '/' . \Frontend::addToUrl('leaflet=' . $param);
}
}