Files
contao-leaflet-maps/src/Mapper/Layer/OverpassLayerMapper.php

164 lines
4.2 KiB
PHP
Raw Normal View History

2016-11-09 10:27:18 +01:00
<?php
/**
2017-10-05 15:45:43 +02:00
* Leaflet maps for Contao CMS.
*
* @package contao-leaflet-maps
2016-11-09 10:27:18 +01:00
* @author David Molineus <david.molineus@netzmacht.de>
2017-10-11 15:00:48 +02:00
* @copyright 2014-2017 netzmacht David Molineus. All rights reserved.
2017-10-05 15:45:43 +02:00
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
2016-11-09 10:27:18 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
use Contao\Model;
use Contao\StringUtil;
use Netzmacht\Contao\Leaflet\Definition\Layer\OverpassLayer;
2016-11-09 10:27:18 +01:00
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
2017-10-17 18:03:42 +02:00
use Netzmacht\Contao\Leaflet\Mapper\Request;
2016-11-14 11:35:09 +01:00
use Netzmacht\Contao\Leaflet\Model\IconModel;
2017-10-19 08:45:39 +02:00
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
2016-11-09 10:27:18 +01:00
use Netzmacht\JavascriptBuilder\Type\Expression;
use Netzmacht\LeafletPHP\Definition;
/**
2016-11-15 14:01:48 +01:00
* Class OverpassLayerMapper.
2016-11-09 10:27:18 +01:00
*
* @package Netzmacht\Contao\Leaflet\Mapper\Layer
*/
class OverpassLayerMapper extends AbstractLayerMapper
{
/**
* The definition type.
*
* @var string
*/
protected static $type = 'overpass';
2016-11-09 11:23:26 +01:00
/**
* The definition class.
*
* @var string
*/
2017-10-11 14:50:14 +02:00
protected static $definitionClass = OverpassLayer::class;
2016-11-09 11:23:26 +01:00
2017-10-19 08:45:39 +02:00
/**
* Repository manager.
*
* @var RepositoryManager
*/
private $repositoryManager;
/**
* Construct.
*
* @param RepositoryManager $repositoryManager Repository manager.
*/
public function __construct(RepositoryManager $repositoryManager)
{
$this->repositoryManager = $repositoryManager;
parent::__construct();
}
2016-11-09 10:27:18 +01:00
/**
* {@inheritdoc}
*/
protected function initialize()
{
parent::initialize();
$this->optionsBuilder
2016-11-09 11:23:26 +01:00
->addOption('query', 'overpassQuery')
->addOption('minZoom')
->addOption('boundsMode')
2016-11-09 11:23:26 +01:00
->addOption('overpassEndpoint', 'endpoint');
2016-11-09 10:27:18 +01:00
}
/**
* {@inheritdoc}
*/
protected function build(
Definition $definition,
Model $model,
2016-11-09 10:27:18 +01:00
DefinitionMapper $mapper,
Request $request = null,
2016-11-09 10:27:18 +01:00
Definition $parent = null
) {
if (!$definition instanceof OverpassLayer) {
return;
}
2016-11-14 11:35:09 +01:00
$amenityIconsMap = $this->buildAmenityIconsMap($model);
$definition->setOption('amenityIcons', $amenityIconsMap);
if ($model->pointToLayer) {
$definition->setPointToLayer(new Expression($model->pointToLayer));
}
2016-11-09 10:27:18 +01:00
if ($model->onEachFeature) {
$definition->setOnEachFeature(new Expression($model->onEachFeature));
2016-11-09 10:27:18 +01:00
}
2016-11-14 11:50:20 +01:00
if ($model->overpassPopup) {
$definition->setOption('overpassPopup', new Expression($model->overpassPopup));
}
2016-11-09 10:27:18 +01:00
}
2016-11-14 11:35:09 +01:00
/**
* Build the amenity icons map.
*
* @param Model $model Definition model.
*
* @return array
*/
protected function buildAmenityIconsMap(Model $model)
{
2016-11-15 14:01:48 +01:00
$amenityIconsMap = $this->filterAmenityIconsConfig($model->amenityIcons);
2016-11-14 11:35:09 +01:00
if ($amenityIconsMap) {
2017-10-19 08:45:39 +02:00
$repository = $this->repositoryManager->getRepository(IconModel::class);
$collection = $repository->findMultipleByIds(array_unique($amenityIconsMap));
2016-11-15 14:01:48 +01:00
$icons = [];
2016-11-14 11:35:09 +01:00
if ($collection) {
foreach ($collection as $iconModel) {
$icons[$iconModel->id] = $iconModel->alias ?: $iconModel->id;
}
foreach ($amenityIconsMap as $amenity => $iconId) {
if (isset($icons[$iconId])) {
$amenityIconsMap[$amenity] = $icons[$iconId];
}
}
}
}
return $amenityIconsMap;
}
2016-11-15 14:01:48 +01:00
/**
* Filter the amenity icons config.
*
* @param mixed $amenityIconsConfig Raw config from the db.
*
* @return array
*/
private function filterAmenityIconsConfig($amenityIconsConfig)
{
$amenityIconsConfig = StringUtil::deserialize($amenityIconsConfig, true);
2016-11-15 14:01:48 +01:00
$amenityIconsMap = [];
foreach ($amenityIconsConfig as $config) {
if (!$config['amenity'] || !$config['icon']) {
continue;
}
$amenityIconsMap[$config['amenity']] = $config['icon'];
}
return $amenityIconsMap;
}
2016-11-09 10:27:18 +01:00
}