Files
contao-leaflet-maps/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php

177 lines
4.0 KiB
PHP
Raw Normal View History

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
*
*/
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
/**
* The user input data.
2015-01-12 19:03:29 +01:00
*
* @var array
2015-01-07 10:33:10 +01:00
*/
private $input = array(
'format' => 'geojson',
'type' => 'layer',
2015-01-27 00:02:17 +01:00
'id' => null,
'filter' => null,
'values' => null
);
/**
* Filters configuration.
*
* @var array
*/
private $filters;
2016-10-06 08:26:57 +02:00
/**
2016-10-06 08:26:57 +02:00
* Display errors.
*
* @var bool
*/
private $displayErrors;
2015-01-07 10:33:10 +01:00
2015-01-12 19:03:29 +01:00
/**
* Construct.
*
* @param MapProvider $mapProvider The map provider.
* @param array $filters Filters configuration.
* @param bool $displayErrors Display errors.
2015-01-12 19:03:29 +01:00
*/
public function __construct(MapProvider $mapProvider, array $filters, $displayErrors)
2015-01-07 10:33:10 +01: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.
*
* @param array $input The user input as array.
*
2015-01-12 19:03:29 +01:00
* @return void
*
* @throws \Exception If anything went wrong.
*/
public function execute(array $input)
2015-01-07 10:33:10 +01:00
{
$input = array_merge($this->input, $input);
2015-01-07 10:33:10 +01:00
try {
if ($input['filter']) {
$filter = $this->createFilter($input);
2015-01-27 00:02:17 +01:00
} else {
$filter = null;
}
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) {
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');
}
}
/**
* 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.
*
* @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)
*/
private function createFilter($input)
2015-01-27 00:02:17 +01: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 */
$filter = $this->filters[$input['filter']];
2015-01-27 00:02:17 +01:00
return $filter::fromRequest($input['values']);
2015-01-27 00:02:17 +01:00
}
2015-01-07 10:33:10 +01:00
}