2015-01-07 10:33:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-11 10:40:15 +02:00
|
|
|
* @package contao-leaflet-maps
|
2015-01-07 10:33:10 +01:00
|
|
|
* @author David Molineus <david.molineus@netzmacht.de>
|
2016-10-11 10:40:15 +02:00
|
|
|
* @copyright 2014-2016 netzmacht David Molineus
|
2015-01-07 10:33:10 +01:00
|
|
|
* @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
|
|
|
);
|
2016-10-06 08:14:08 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filters configuration.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $filters;
|
2016-10-06 08:26:57 +02:00
|
|
|
|
2016-10-06 08:14:08 +02:00
|
|
|
/**
|
2016-10-06 08:26:57 +02:00
|
|
|
* Display errors.
|
|
|
|
|
*
|
2016-10-06 08:14:08 +02:00
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $displayErrors;
|
2015-01-07 10:33:10 +01:00
|
|
|
|
2015-01-12 19:03:29 +01:00
|
|
|
/**
|
|
|
|
|
* Construct.
|
|
|
|
|
*
|
2016-10-06 08:14:08 +02:00
|
|
|
* @param MapProvider $mapProvider The map provider.
|
|
|
|
|
* @param array $filters Filters configuration.
|
|
|
|
|
* @param bool $displayErrors Display errors.
|
2015-01-12 19:03:29 +01:00
|
|
|
*/
|
2016-10-06 08:14:08 +02:00
|
|
|
public function __construct(MapProvider $mapProvider, array $filters, $displayErrors)
|
2015-01-07 10:33:10 +01:00
|
|
|
{
|
2016-10-06 08:14:08 +02:00
|
|
|
$this->mapProvider = $mapProvider;
|
|
|
|
|
$this->filters = $filters;
|
|
|
|
|
$this->displayErrors = $displayErrors;
|
2015-01-07 10:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-12 19:03:29 +01:00
|
|
|
/**
|
|
|
|
|
* Execute the controller and create the data response.
|
|
|
|
|
*
|
2016-10-06 08:14:08 +02:00
|
|
|
* @param array $input The user input as array.
|
|
|
|
|
*
|
2015-01-12 19:03:29 +01:00
|
|
|
* @return void
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception If anything went wrong.
|
|
|
|
|
*/
|
2016-10-06 08:14:08 +02:00
|
|
|
public function execute(array $input)
|
2015-01-07 10:33:10 +01:00
|
|
|
{
|
2016-10-06 08:14:08 +02:00
|
|
|
$input = array_merge($this->input, $input);
|
|
|
|
|
|
2015-01-07 10:33:10 +01:00
|
|
|
try {
|
2016-10-06 08:14:08 +02:00
|
|
|
if ($input['filter']) {
|
|
|
|
|
$filter = $this->createFilter($input);
|
2015-01-27 00:02:17 +01:00
|
|
|
} else {
|
|
|
|
|
$filter = null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 08:14:08 +02:00
|
|
|
list($data, $error) = $this->loadData($input['type'], $input['id'], $filter);
|
|
|
|
|
$this->encodeData($input['format'], $data);
|
2015-01-07 10:33:10 +01:00
|
|
|
} catch (\Exception $e) {
|
2016-10-06 08:14:08 +02:00
|
|
|
if ($this->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.
|
|
|
|
|
*
|
2016-10-06 08:14:08 +02:00
|
|
|
* @param array $input The user input as array.
|
|
|
|
|
*
|
2015-01-27 00:02:17 +01:00
|
|
|
* @return Filter
|
|
|
|
|
* @throws \RuntimeException If the filter is not defined.
|
|
|
|
|
*
|
|
|
|
|
* @SuppressWarnings(PHPMD.Superglobals)
|
|
|
|
|
*/
|
2016-10-06 08:14:08 +02:00
|
|
|
private function createFilter($input)
|
2015-01-27 00:02:17 +01:00
|
|
|
{
|
2016-10-06 08:14:08 +02:00
|
|
|
if (!isset($this->filters[$input['filter']])) {
|
|
|
|
|
throw new \RuntimeException(sprintf('Undefined filter "%s".', $input['filter']));
|
2015-01-27 00:02:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var Filter $filter */
|
2016-10-06 08:14:08 +02:00
|
|
|
$filter = $this->filters[$input['filter']];
|
2015-01-27 00:02:17 +01:00
|
|
|
|
2016-10-06 08:14:08 +02:00
|
|
|
return $filter::fromRequest($input['values']);
|
2015-01-27 00:02:17 +01:00
|
|
|
}
|
2015-01-07 10:33:10 +01:00
|
|
|
}
|