Files
contao-leaflet-maps/src/Netzmacht/Contao/Leaflet/Mapper/Layer/VectorsLayerMapper.php

133 lines
3.8 KiB
PHP
Raw Normal View History

2015-01-07 17:59:56 +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\Mapper\Layer;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper;
use Netzmacht\Contao\Leaflet\Model\VectorModel;
2015-01-10 15:33:46 +01:00
use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
2015-01-08 12:52:32 +01:00
use Netzmacht\Javascript\Type\Value\Expression;
2015-01-07 17:59:56 +01:00
use Netzmacht\LeafletPHP\Definition;
2015-01-12 10:45:05 +01:00
use Netzmacht\LeafletPHP\Definition\GeoJson\ConvertsToGeoJsonFeature;
2015-01-07 17:59:56 +01:00
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
2015-01-12 10:45:05 +01:00
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature;
2015-01-07 17:59:56 +01:00
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
use Netzmacht\LeafletPHP\Definition\Group\LayerGroup;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
2015-01-12 10:45:05 +01:00
use Netzmacht\LeafletPHP\Definition\Vector;
2015-01-07 17:59:56 +01:00
use Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax;
class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
{
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Group\GeoJson';
/**
* Layer type.
*
* @var string
*/
protected static $type = 'vectors';
/**
* {@inheritdoc}
*/
protected function getClassName(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
2015-01-07 17:59:56 +01:00
{
if ($model->deferred) {
return 'Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax';
2015-01-07 17:59:56 +01:00
}
return parent::getClassName($model, $mapper, $bounds);
2015-01-07 17:59:56 +01:00
}
/**
* {@inheritdoc}
*/
protected function build(
2015-01-07 17:59:56 +01:00
Definition $definition,
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null
) {
if ($definition instanceof GeoJsonAjax) {
$definition->setUrl(RequestUrl::create($model->id));
} elseif ($definition instanceof LayerGroup) {
$collection = $this->loadVectorModels($model);
if ($collection) {
foreach ($collection as $item) {
$definition->addLayer($mapper->handle($item));
}
}
}
2015-01-12 15:11:35 +01:00
if ($definition instanceof GeoJson || $definition instanceof GeoJsonAjax) {
if ($model->pointToLayer) {
$definition->setPointToLayer(new Expression($model->pointToLayer));
}
if ($model->onEachFeature) {
$definition->setOnEachFeature(new Expression($model->onEachFeature));
}
2015-01-07 17:59:56 +01:00
}
}
/**
* @param \Model $model
* @param DefinitionMapper $mapper
* @param LatLngBounds $bounds
*
* @return mixed
*/
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$feature = new FeatureCollection();
$collection = $this->loadVectorModels($model);
if ($collection) {
foreach ($collection as $item) {
2015-01-12 10:45:05 +01:00
$vector = $mapper->handle($item);
if ($vector instanceof ConvertsToGeoJsonFeature) {
$feature->addFeature($vector->toGeoJsonFeature());
} elseif ($vector instanceof GeoJsonFeature) {
$feature->addFeature($vector);
}
2015-01-07 17:59:56 +01:00
}
}
return $feature;
}
/**
* @param \Model $model
*
* @return \Model\Collection|null
*/
protected function loadVectorModels(\Model $model)
{
$collection = VectorModel::findBy(
array('active=1', 'pid=?'),
array($model->id),
array('order' => 'sorting')
);
return $collection;
}
}