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

155 lines
3.6 KiB
PHP
Raw Normal View History

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
*
*/
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;
2015-01-07 10:33:10 +01:00
use Netzmacht\Contao\Leaflet\MapService;
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
{
/**
2015-01-12 19:03:29 +01:00
* The map service.
*
2015-01-07 10:33:10 +01:00
* @var MapService
*/
private $mapService;
/**
* 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
);
2015-01-07 10:33:10 +01:00
2015-01-12 19:03:29 +01:00
/**
* Construct.
*
* @param MapService $mapService The map service.
* @param array $input The user input as array.
2015-01-12 19:03:29 +01:00
*/
public function __construct(MapService $mapService, $input)
2015-01-07 10:33:10 +01:00
{
$this->mapService = $mapService;
$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);
$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');
}
}
/**
* 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':
2015-01-27 00:02:17 +01:00
$data = $this->mapService->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
}