diff --git a/assets/geojson.php b/assets/data.php similarity index 55% rename from assets/geojson.php rename to assets/data.php index 7d0159c..79bf457 100644 --- a/assets/geojson.php +++ b/assets/data.php @@ -1,11 +1,11 @@ execute(); diff --git a/src/Netzmacht/Contao/Leaflet/Controller/DataController.php b/src/Netzmacht/Contao/Leaflet/Controller/DataController.php new file mode 100644 index 0000000..47be6f3 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Controller/DataController.php @@ -0,0 +1,95 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Controller; + + +use Netzmacht\Contao\Leaflet\MapService; + +class DataController +{ + /** + * @var MapService + */ + private $mapService; + + /** + * @var \Input + */ + private $input; + + public function __construct(MapService $mapService, \Input $input) + { + $this->mapService = $mapService; + $this->input = $input; + } + + public function execute() + { + $format = $this->input->get('format') ?: 'geojson'; + $type = $this->input->get('type') ?: 'layer'; + $dataId = $this->input->get('id'); + + try { + list($data, $error) = $this->loadData($type, $dataId); + } catch (\Exception $e) { + if (\Config::get('debugMode') || \Config::get('displayErrors')) { + throw $e; + } + $error = true; + } + + if ($error) { + header('HTTP/1.1 500 Internal Server Error'); + } else { + $this->encodeData($format, $data); + } + } + + /** + * @param $format + * @param $data + */ + public function encodeData($format, $data) + { + switch ($format) { + case 'geojson': + header('Content-Type: application/json'); + echo json_encode($data, JSON_UNESCAPED_SLASHES); + break; + } + } + + /** + * @param $type + * @param $dataId + * + * @return array + */ + public function loadData($type, $dataId) + { + $error = false; + $data = null; + + switch ($type) { + case 'layer': + $data = $this->mapService->getFeatureCollection($dataId); + break; + + default: + $error = true; + + return array($data, $error); + } + + return array($data, $error); + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Controller/GeoJsonController.php b/src/Netzmacht/Contao/Leaflet/Controller/GeoJsonController.php deleted file mode 100644 index 9653761..0000000 --- a/src/Netzmacht/Contao/Leaflet/Controller/GeoJsonController.php +++ /dev/null @@ -1,47 +0,0 @@ - - * @copyright 2015 netzmacht creative David Molineus - * @license LGPL 3.0 - * @filesource - * - */ - -namespace Netzmacht\Contao\Leaflet\Controller; - - -use Netzmacht\Contao\Leaflet\MapService; - -class GeoJsonController -{ - /** - * @var MapService - */ - private $mapService; - - /** - * @var \Input - */ - private $input; - - public function __construct(MapService $mapService, \Input $input) - { - $this->mapService = $mapService; - $this->input = $input; - } - - public function execute() - { - try { - $collection = $this->mapService->getFeatureCollection(\Input::get('id')); - - header('Content-Type: application/json'); - echo json_encode($collection, JSON_UNESCAPED_SLASHES); - } - catch(\Exception $e) { - header('HTTP/1.0 403 Forbidden'); - } - } -}