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

119 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-07 09:38:05 +01:00
use Netzmacht\Contao\Leaflet\Request\RequestUrl;
2015-01-06 18:49:22 +01:00
use Netzmacht\Javascript\Type\Value\Reference;
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-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}
*/
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}
*/
2015-01-06 14:55:53 +01:00
protected function doBuild(
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) {
$definition->setPointToLayer(new Reference('ContaoLeaflet.pointToLayer', 'ContaoLeaflet'));
}
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-06 18:49:22 +01:00
$feature->addFeature($mapper->handle($item)->toGeoJson());
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=?'),
array($model->id)
);
return $collection;
}
2015-01-06 14:55:53 +01:00
}