From d54b5ddc3ead2763b2881ec285bf04fadce740ee Mon Sep 17 00:00:00 2001 From: David Molineus Date: Wed, 14 Jan 2015 17:50:23 +0100 Subject: [PATCH] Refactor ajax loading so that the current page is used. --- .../Leaflet/Frontend/DataController.php | 50 +++++------------ .../Contao/Leaflet/Frontend/HybridTrait.php | 45 ++++++++++++++++ .../Contao/Leaflet/Frontend/MapElement.php | 5 ++ .../Contao/Leaflet/Frontend/MapModule.php | 5 ++ .../Contao/Leaflet/Frontend/RequestUrl.php | 53 ++++++------------- 5 files changed, 84 insertions(+), 74 deletions(-) diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php b/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php index b825057..0ddd2c3 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php @@ -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; - } } diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php b/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php index a8cf191..9d8f11c 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php @@ -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; + } + } } diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php b/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php index b52df58..094660a 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php @@ -39,4 +39,9 @@ class MapElement extends \ContentElement { $this->construct($objElement, $strColumn); } + + protected function getIdentifier() + { + return 'ce_' . $this->id; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php b/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php index 92c7a35..05cd2f5 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php @@ -38,4 +38,9 @@ class MapModule extends \Module { $this->construct($objElement, $strColumn); } + + protected function getIdentifier() + { + return 'mod_' . $this->id; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php b/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php index d7adfff..b723aeb 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/RequestUrl.php @@ -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); } }