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

124 lines
3.3 KiB
PHP
Raw Normal View History

2015-01-06 14:55:53 +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\MarkerModel;
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-06 14:55:53 +01:00
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
2015-01-06 18:49:22 +01:00
use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
2015-01-06 14:55:53 +01:00
use Netzmacht\LeafletPHP\Definition\Group\LayerGroup;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
2015-01-12 10:45:05 +01:00
use Netzmacht\LeafletPHP\Definition\UI\Marker;
2015-01-06 15:43:57 +01:00
use Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax;
2015-01-06 14:55:53 +01:00
class MarkersLayerMapper 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 = 'markers';
2015-01-06 15:43:57 +01:00
/**
* {@inheritdoc}
*/
2015-01-08 12:52:32 +01:00
protected function getClassName(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
2015-01-06 15:43:57 +01:00
{
if ($model->deferred) {
2015-01-08 12:52:32 +01:00
return 'Netzmacht\LeafletPHP\Plugins\Ajax\GeoJsonAjax';
2015-01-06 15:43:57 +01:00
}
2015-01-08 12:52:32 +01:00
return parent::getClassName($model, $mapper, $bounds);
2015-01-06 15:43:57 +01:00
}
/**
* {@inheritdoc}
*/
protected function build(
2015-01-06 14:55:53 +01:00
Definition $definition,
\Model $model,
2015-01-06 18:49:22 +01:00
DefinitionMapper $mapper,
2015-01-06 14:55:53 +01:00
LatLngBounds $bounds = null
) {
2015-01-06 15:43:57 +01:00
if ($definition instanceof GeoJsonAjax) {
2015-01-07 09:38:05 +01:00
$definition->setUrl(RequestUrl::create($model->id));
2015-01-06 15:43:57 +01:00
} elseif ($definition instanceof LayerGroup) {
$collection = $this->loadMarkerModels($model);
2015-01-06 14:55:53 +01:00
if ($collection) {
foreach ($collection as $item) {
2015-01-06 18:49:22 +01:00
$definition->addLayer($mapper->handle($item));
2015-01-06 14:55:53 +01:00
}
}
}
2015-01-06 18:49:22 +01:00
if ($definition instanceof GeoJson) {
if ($model->pointToLayer) {
$definition->setPointToLayer(new Expression($model->pointToLayer));
}
2015-01-06 18:49:22 +01:00
}
2015-01-06 14:55:53 +01:00
}
/**
* @param \Model $model
* @param DefinitionMapper $mapper
* @param LatLngBounds $bounds
*
* @return mixed
*/
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
2015-01-06 15:43:57 +01:00
$feature = new FeatureCollection();
$collection = $this->loadMarkerModels($model);
2015-01-06 14:55:53 +01:00
if ($collection) {
foreach ($collection as $item) {
2015-01-12 10:45:05 +01:00
$marker = $mapper->handle($item);
if ($marker instanceof Marker) {
$feature->addFeature($marker->toGeoJsonFeature());
}
2015-01-06 14:55:53 +01:00
}
}
return $feature;
}
2015-01-06 15:43:57 +01:00
/**
* @param \Model $model
*
* @return \Model\Collection|null
*/
protected function loadMarkerModels(\Model $model)
{
$collection = MarkerModel::findBy(
array('active=1', 'pid=?'),
2015-01-08 15:29:07 +01:00
array($model->id),
array('order' => 'sorting')
2015-01-06 15:43:57 +01:00
);
return $collection;
}
2015-01-06 14:55:53 +01:00
}