Files
contao-leaflet-maps/src/Mapper/UI/MarkerMapper.php

139 lines
3.8 KiB
PHP
Raw Normal View History

2015-01-06 18:49:22 +01:00
<?php
/**
2017-10-05 15:45:43 +02:00
* Leaflet maps for Contao CMS.
*
2016-10-11 10:40:15 +02:00
* @package contao-leaflet-maps
2015-01-06 18:49:22 +01:00
* @author David Molineus <david.molineus@netzmacht.de>
2017-10-05 15:45:43 +02:00
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
2015-01-06 18:49:22 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper\UI;
use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Frontend\ValueFilter;
2015-01-06 18:49:22 +01:00
use Netzmacht\Contao\Leaflet\Mapper\AbstractMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
2015-01-06 21:30:57 +01:00
use Netzmacht\Contao\Leaflet\Model\IconModel;
2015-01-27 17:14:58 +01:00
use Netzmacht\Contao\Leaflet\Model\PopupModel;
2015-01-06 18:49:22 +01:00
use Netzmacht\LeafletPHP\Definition;
2015-01-08 15:21:45 +01:00
use Netzmacht\LeafletPHP\Definition\Type\ImageIcon;
2015-01-06 18:49:22 +01:00
use Netzmacht\LeafletPHP\Definition\UI\Marker;
2015-01-27 17:14:58 +01:00
use Netzmacht\LeafletPHP\Definition\UI\Popup;
2015-01-06 18:49:22 +01:00
2015-01-12 19:03:29 +01:00
/**
* Class MarkerMapper maps the marker model to the marker definition.
*
* @package Netzmacht\Contao\Leaflet\Mapper\UI
*/
2015-01-06 18:49:22 +01:00
class MarkerMapper extends AbstractMapper
{
/**
* Class of the model being build.
*
* @var string
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\MarkerModel';
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\UI\Marker';
/**
* Frontend filter.
*
* @var ValueFilter
*/
protected $valueFilter;
/**
* Construct.
*
* @param ValueFilter $valueFilter Frontend filter.
*/
public function __construct(ValueFilter $valueFilter)
{
parent::__construct();
$this->valueFilter = $valueFilter;
}
2015-01-06 18:49:22 +01:00
/**
* {@inheritdoc}
*/
2015-01-09 13:41:09 +01:00
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
Filter $filter = null,
2015-01-09 13:41:09 +01:00
$elementId = null
) {
$arguments = parent::buildConstructArguments($model, $mapper, $filter, $elementId);
2015-01-27 17:14:58 +01:00
$arguments[] = array($model->latitude, $model->longitude, $model->altitude ?: null) ?: null;
2015-01-06 18:49:22 +01:00
return $arguments;
}
/**
* {@inheritdoc}
*/
protected function initialize()
{
$this->optionsBuilder
2015-01-06 18:49:22 +01:00
->addConditionalOption('tooltip', 'title', 'tooltip')
->addConditionalOption('alt')
2015-01-08 16:27:55 +01:00
->addConditionalOption('zIndexOffset')
2015-01-06 18:49:22 +01:00
->addOptions('clickable', 'keyboard', 'draggable');
}
/**
* {@inheritdoc}
*/
protected function build(
2015-01-06 18:49:22 +01:00
Definition $definition,
\Model $model,
2015-01-20 16:38:23 +01:00
DefinitionMapper $mapper,
Filter $filter = null,
2015-01-20 16:38:23 +01:00
Definition $parent = null
2015-01-06 18:49:22 +01:00
) {
if ($definition instanceof Marker) {
if ($model->addPopup) {
2015-01-27 17:14:58 +01:00
$popup = null;
$content = $this->valueFilter->filter($model->popupContent);
2015-01-27 17:14:58 +01:00
if ($model->popup) {
$popupModel = PopupModel::findActiveByPK($model->popup);
if ($popupModel) {
$popup = $mapper->handle($popupModel, $filter, null, $definition);
}
}
if ($popup instanceof Popup) {
$definition->bindPopup($content, $popup->getOptions());
} else {
$definition->bindPopup($content);
}
2015-01-06 18:49:22 +01:00
}
2015-01-06 21:30:57 +01:00
if ($model->customIcon) {
$iconModel = IconModel::findBy(
array('id=?', 'active=1'),
array($model->icon),
array('return' => 'Model')
);
if ($iconModel) {
2015-01-08 15:21:45 +01:00
/** @var ImageIcon $icon */
2015-01-20 16:38:23 +01:00
$icon = $mapper->handle($iconModel);
2015-01-06 21:30:57 +01:00
$definition->setIcon($icon);
}
}
2015-01-06 18:49:22 +01:00
}
}
}