Rename data controller.

This commit is contained in:
David Molineus
2015-01-07 10:33:10 +01:00
parent 27a01112c3
commit 37c3c56b98
3 changed files with 97 additions and 49 deletions

View File

@@ -1,11 +1,11 @@
<?php
use Netzmacht\Contao\Leaflet\Controller\GeoJsonController;
use Netzmacht\Contao\Leaflet\Controller\DataController;
define('TL_MODE', 'FE');
require(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME']))) . '/system/initialize.php');
$container = $GLOBALS['container'];
$controller = new GeoJsonController($container['leaflet.map.service'], $container['input']);
$controller = new DataController($container['leaflet.map.service'], $container['input']);
$controller->execute();

View File

@@ -0,0 +1,95 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @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);
}
}

View File

@@ -1,47 +0,0 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @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');
}
}
}