Implement amenity icons mapping.

This commit is contained in:
David Molineus
2016-11-14 11:35:09 +01:00
parent c61e2d9cc9
commit f462a11c1d
7 changed files with 344 additions and 14 deletions

View File

@@ -66,6 +66,17 @@ class LayerCallbacks extends Callbacks
*/
private $translator;
/**
* OSM amenities.
*
* @var array
*/
private $amenities;
/**
* @var Manager
*/
private $manager;
/**
* Construct.
*
@@ -74,23 +85,26 @@ class LayerCallbacks extends Callbacks
* @param Translator $translator Translator.
* @param array $layers Leaflet layer configuration.
* @param array $tileProviders Tile providers.
* @param array $amenities OSM amenities
*/
public function __construct(
Manager $manager,
\Database $database,
Translator $translator,
array $layers,
array $tileProviders
array $tileProviders,
array $amenities
) {
parent::__construct($manager);
\Controller::loadLanguageFile('leaflet_layer');
$this->database = $database;
$this->layers = $layers;
$this->tileProviders = $tileProviders;
\Controller::loadLanguageFile('leaflet_layer');
$this->translator = $translator;
$this->manager = $manager;
$this->translator = $translator;
$this->amenities = $amenities;
}
/**
@@ -359,6 +373,16 @@ class LayerCallbacks extends Callbacks
->getOptions();
}
/**
* Get all know osm amenities as options.
*
* @return array
*/
public function getAmenities()
{
return $this->amenities;
}
/**
* Generate a button.
*

View File

@@ -14,6 +14,7 @@ use Model;
use Netzmacht\Contao\Leaflet\Definition\Layer\OverpassLayer;
use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\JavascriptBuilder\Type\Expression;
use Netzmacht\LeafletPHP\Definition;
@@ -66,6 +67,9 @@ class OverpassLayerMapper extends AbstractLayerMapper
return;
}
$amenityIconsMap = $this->buildAmenityIconsMap($model);
$definition->setOption('amenityIcons', $amenityIconsMap);
if ($model->pointToLayer) {
$definition->setPointToLayer(new Expression($model->pointToLayer));
}
@@ -74,4 +78,43 @@ class OverpassLayerMapper extends AbstractLayerMapper
$definition->setOnEachFeature(new Expression($model->onEachFeature));
}
}
/**
* Build the amenity icons map.
*
* @param Model $model Definition model.
*
* @return array
*/
protected function buildAmenityIconsMap(Model $model)
{
$amenityIconsConfig = deserialize($model->amenityIcons, true);
$amenityIconsMap = [];
foreach ($amenityIconsConfig as $config) {
if (!$config['amenity'] || !$config['icon']) {
continue;
}
$amenityIconsMap[$config['amenity']] = $config['icon'];
}
if ($amenityIconsMap) {
$collection = IconModel::findMultipleByIds(array_unique($amenityIconsMap));
$icons = [];
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;
}
}