forked from Snck3rs/contao-leaflet-maps
Work on vectors integration.
This commit is contained in:
122
src/Netzmacht/Contao/Leaflet/Mapper/Layer/VectorsLayerMapper.php
Normal file
122
src/Netzmacht/Contao/Leaflet/Mapper/Layer/VectorsLayerMapper.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?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\MarkerModel;
|
||||
use Netzmacht\Contao\Leaflet\Model\VectorModel;
|
||||
use Netzmacht\Contao\Leaflet\Request\RequestUrl;
|
||||
use Netzmacht\Javascript\Type\Value\Reference;
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
||||
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
|
||||
use Netzmacht\LeafletPHP\Definition\Group\LayerGroup;
|
||||
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
||||
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 createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
|
||||
{
|
||||
if ($model->deferred) {
|
||||
$reflector = new \ReflectionClass('Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax');
|
||||
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds));
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
return parent::createInstance($model, $mapper, $bounds);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doBuild(
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($definition instanceof GeoJson) {
|
||||
$definition->setOnEachFeature(new Reference('ContaoLeaflet.onEachFeature', 'ContaoLeaflet'));
|
||||
$definition->setPointToLayer(new Reference('ContaoLeaflet.pointToLayer', 'ContaoLeaflet'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
$feature->addFeature($mapper->handle($item)->toGeoJson());
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user