Files
contao-leaflet-maps/src/Mapper/Vector/AbstractVectorMapper.php

124 lines
3.4 KiB
PHP
Raw Normal View History

2015-01-07 17:59:56 +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-07 17:59:56 +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-07 17:59:56 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Vector;
use Contao\Model;
2015-01-09 11:53:58 +01:00
use Netzmacht\Contao\Leaflet\Definition\Style;
use Netzmacht\Contao\Leaflet\Frontend\ValueFilter;
2015-01-07 17:59:56 +01:00
use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
2015-01-27 18:17:05 +01:00
use Netzmacht\Contao\Leaflet\Model\PopupModel;
2015-01-09 11:53:58 +01:00
use Netzmacht\Contao\Leaflet\Model\StyleModel;
use Netzmacht\Contao\Leaflet\Request\Request;
2015-01-07 17:59:56 +01:00
use Netzmacht\LeafletPHP\Definition;
2015-01-09 11:53:58 +01:00
use Netzmacht\LeafletPHP\Definition\HasPopup;
2015-01-27 18:17:05 +01:00
use Netzmacht\LeafletPHP\Definition\UI\Popup;
2015-01-09 11:53:58 +01:00
use Netzmacht\LeafletPHP\Definition\Vector\Path;
2015-01-07 17:59:56 +01:00
2015-01-12 19:03:29 +01:00
/**
* Class AbstractVectorMapper is the base class for the vector model definition mapping.
*
* @package Netzmacht\Contao\Leaflet\Mapper\Vector
*/
2015-01-07 17:59:56 +01:00
class AbstractVectorMapper extends AbstractTypeMapper
{
/**
* Class of the model being build.
*
* @var string
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\VectorModel';
/**
* Frontend filter.
*
* @var ValueFilter
*/
protected $valueFilter;
/**
* Construct.
*
* @param ValueFilter $valueFilter Frontend filter.
*/
public function __construct(ValueFilter $valueFilter)
{
parent::__construct();
$this->valueFilter = $valueFilter;
}
2015-01-09 13:41:09 +01:00
/**
* {@inheritdoc}
*/
protected function build(
2015-01-07 17:59:56 +01:00
Definition $definition,
Model $model,
2015-01-09 11:53:58 +01:00
DefinitionMapper $mapper,
Request $request = null,
2015-01-20 16:38:23 +01:00
Definition $parent = null
2015-01-07 17:59:56 +01:00
) {
parent::build($definition, $model, $mapper, $request);
2015-01-07 17:59:56 +01:00
2015-01-09 11:53:58 +01:00
if ($definition instanceof Path && $model->style) {
2016-10-05 09:45:31 +02:00
$styleModel = StyleModel::findActiveByPK($model->style);
2015-01-09 11:53:58 +01:00
if ($styleModel) {
$style = $mapper->handle($styleModel);
if ($style instanceof Style) {
$style->apply($definition);
}
}
}
$this->buildPopup($definition, $model, $mapper, $request);
2015-01-27 18:21:05 +01:00
}
/**
* Build the popup.
*
* @param Definition $definition The definition.
* @param Model $model The model.
2015-01-27 18:21:05 +01:00
* @param DefinitionMapper $mapper The definition mapper.
* @param Request $request Optional building request.
2015-01-27 18:21:05 +01:00
*
* @return void
*/
2015-01-27 19:10:14 +01:00
protected function buildPopup(
Definition $definition,
Model $model,
2015-01-27 19:10:14 +01:00
DefinitionMapper $mapper,
Request $request = null
2015-01-27 19:10:14 +01:00
) {
2015-01-09 11:53:58 +01:00
if ($definition instanceof HasPopup && $model->addPopup) {
2015-01-27 18:17:05 +01:00
$popup = null;
$content = $this->valueFilter->filter($model->popupContent);
2015-01-27 18:17:05 +01:00
if ($model->popup) {
$popupModel = PopupModel::findActiveByPK($model->popup);
if ($popupModel) {
$popup = $mapper->handle($popupModel, $request, null, $definition);
2015-01-27 18:17:05 +01:00
}
}
if ($popup instanceof Popup) {
$definition->bindPopup($content, $popup->getOptions());
} else {
$definition->bindPopup($content);
}
2015-01-07 17:59:56 +01:00
}
}
}