2015-01-07 10:33:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @package dev
|
|
|
|
|
* @author David Molineus <david.molineus@netzmacht.de>
|
|
|
|
|
* @copyright 2015 netzmacht creative David Molineus
|
|
|
|
|
* @license LGPL 3.0
|
|
|
|
|
* @filesource
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2015-01-12 17:05:32 +01:00
|
|
|
namespace Netzmacht\Contao\Leaflet\Frontend;
|
2015-01-07 10:33:10 +01:00
|
|
|
|
2015-01-27 00:02:17 +01:00
|
|
|
use Netzmacht\Contao\Leaflet\Filter\Filter;
|
2016-10-05 14:18:15 +02:00
|
|
|
use Netzmacht\Contao\Leaflet\MapProvider;
|
2015-01-07 10:33:10 +01:00
|
|
|
|
2015-01-12 19:03:29 +01:00
|
|
|
/**
|
|
|
|
|
* The data controller handles ajax request for sub data.
|
|
|
|
|
*
|
|
|
|
|
* @package Netzmacht\Contao\Leaflet\Frontend
|
|
|
|
|
*/
|
2015-01-07 10:33:10 +01:00
|
|
|
class DataController
|
|
|
|
|
{
|
|
|
|
|
/**
|
2016-10-05 14:18:15 +02:00
|
|
|
* The map provider.
|
2015-01-12 19:03:29 +01:00
|
|
|
*
|
2016-10-05 14:18:15 +02:00
|
|
|
* @var MapProvider
|
2015-01-07 10:33:10 +01:00
|
|
|
*/
|
2016-10-05 14:18:15 +02:00
|
|
|
private $mapProvider;
|
2015-01-07 10:33:10 +01:00
|
|
|
|
|
|
|
|
/**
|
2015-01-14 17:50:23 +01:00
|
|
|
* The user input data.
|
2015-01-12 19:03:29 +01:00
|
|
|
*
|
2015-01-14 17:50:23 +01:00
|
|
|
* @var array
|
2015-01-07 10:33:10 +01:00
|
|
|
*/
|
2015-01-14 17:50:23 +01:00
|
|
|
private $input = array(
|
|
|
|
|
'format' => 'geojson',
|
|
|
|
|
'type' => 'layer',
|
2015-01-27 00:02:17 +01:00
|
|
|
'id' => null,
|
|
|
|
|
'filter' => null,
|
|
|
|
|
'values' => null
|
2015-01-14 17:50:23 +01:00
|
|
|
);
|
2015-01-07 10:33:10 +01:00
|
|
|
|
2015-01-12 19:03:29 +01:00
|
|
|
/**
|
|
|
|
|
* Construct.
|
|
|
|
|
*
|
2016-10-05 14:18:15 +02:00
|
|
|
* @param MapProvider $mapProvider The map provider.
|
|
|
|
|
* @param array $input The user input as array.
|
2015-01-12 19:03:29 +01:00
|
|
|
*/
|
2016-10-05 14:18:15 +02:00
|
|
|
public function __construct(MapProvider $mapProvider, $input)
|
2015-01-07 10:33:10 +01:00
|
|
|
{
|
2016-10-05 14:18:15 +02:00
|
|
|
$this->mapProvider = $mapProvider;
|
|
|
|
|
$this->input = array_merge($this->input, $input);
|
2015-01-07 10:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-12 19:03:29 +01:00
|
|
|
/**
|
|
|
|
|
* Execute the controller and create the data response.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception If anything went wrong.
|
|
|
|
|
*/
|
2015-01-07 10:33:10 +01:00
|
|
|
public function execute()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2015-01-27 00:02:17 +01:00
|
|
|
if ($this->input['filter']) {
|
|
|
|
|
$filter = $this->createFilter();
|
|
|
|
|
} else {
|
|
|
|
|
$filter = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list($data, $error) = $this->loadData($this->input['type'], $this->input['id'], $filter);
|
2015-01-14 17:50:23 +01:00
|
|
|
$this->encodeData($this->input['format'], $data);
|
2015-01-07 10:33:10 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
if (\Config::get('debugMode') || \Config::get('displayErrors')) {
|
2015-01-15 15:30:16 +01:00
|
|
|
throw $e;
|
2015-01-07 10:33:10 +01:00
|
|
|
}
|
|
|
|
|
$error = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-14 17:50:23 +01:00
|
|
|
* Encode the data.
|
2015-01-12 19:03:29 +01:00
|
|
|
*
|
|
|
|
|
* @param string $format The requested format.
|
|
|
|
|
* @param mixed $data The given data.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2015-01-07 10:33:10 +01:00
|
|
|
*/
|
|
|
|
|
public function encodeData($format, $data)
|
|
|
|
|
{
|
|
|
|
|
switch ($format) {
|
|
|
|
|
case 'geojson':
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
|
break;
|
2015-01-12 19:03:29 +01:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// Blame the code sniffer.
|
2015-01-07 10:33:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-12 19:03:29 +01:00
|
|
|
* Load the data.
|
|
|
|
|
*
|
|
|
|
|
* @param string $type The data type.
|
|
|
|
|
* @param mixed $dataId The data id.
|
2015-01-27 00:02:17 +01:00
|
|
|
* @param Filter $filter Optional request filter.
|
2015-01-07 10:33:10 +01:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2015-01-27 00:02:17 +01:00
|
|
|
public function loadData($type, $dataId, Filter $filter = null)
|
2015-01-07 10:33:10 +01:00
|
|
|
{
|
|
|
|
|
$error = false;
|
|
|
|
|
$data = null;
|
|
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
|
case 'layer':
|
2016-10-05 14:18:15 +02:00
|
|
|
$data = $this->mapProvider->getFeatureCollection($dataId, $filter);
|
2015-01-07 10:33:10 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
$error = true;
|
|
|
|
|
|
|
|
|
|
return array($data, $error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array($data, $error);
|
|
|
|
|
}
|
2015-01-27 00:02:17 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a filter.
|
|
|
|
|
*
|
|
|
|
|
* @return Filter
|
|
|
|
|
* @throws \RuntimeException If the filter is not defined.
|
|
|
|
|
*
|
|
|
|
|
* @SuppressWarnings(PHPMD.Superglobals)
|
|
|
|
|
*/
|
|
|
|
|
private function createFilter()
|
|
|
|
|
{
|
|
|
|
|
if (!isset($GLOBALS['LEAFLET_FILTERS'][$this->input['filter']])) {
|
|
|
|
|
throw new \RuntimeException(sprintf('Undefined filter "%s".', $this->input['filter']));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var Filter $filter */
|
|
|
|
|
$filter = $GLOBALS['LEAFLET_FILTERS'][$this->input['filter']];
|
|
|
|
|
|
|
|
|
|
return $filter::fromRequest($this->input['values']);
|
|
|
|
|
}
|
2015-01-07 10:33:10 +01:00
|
|
|
}
|