Files
contao-leaflet-maps/src/Mapper/Control/LoadingControlMapper.php

100 lines
2.9 KiB
PHP
Raw Normal View History

2015-01-08 11:01: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-08 11:01: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-08 11:01:22 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Control;
use Contao\Model;
2015-01-08 11:01:22 +01:00
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\ControlModel;
use Netzmacht\Contao\Leaflet\Request\Request;
2015-01-08 11:01:22 +01:00
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Control\Zoom;
use Netzmacht\LeafletPHP\Plugins\Loading\LoadingControl;
use Netzmacht\LeafletPHP\Plugins\Loading\SpinJsLoadingControl;
2015-01-12 19:03:29 +01:00
/**
* Class LoadingControlMapper maps the control model to the loading control definition.
*
* @package Netzmacht\Contao\Leaflet\Mapper\Control
*/
2015-01-08 11:01:22 +01:00
class LoadingControlMapper extends AbstractControlMapper
{
/**
* Layer type.
*
* @var string
*/
protected static $type = 'loading';
/**
* {@inheritdoc}
*/
protected function getClassName(Model $model, DefinitionMapper $mapper, Request $request = null)
2015-01-08 11:01:22 +01:00
{
if ($model->spinjs) {
return 'Netzmacht\LeafletPHP\Plugins\Loading\SpinJsLoadingControl';
}
2015-01-22 11:59:19 +01:00
return 'Netzmacht\LeafletPHP\Plugins\Loading\LoadingControl';
2015-01-08 11:01:22 +01:00
}
/**
* {@inheritdoc}
*/
protected function initialize()
{
parent::initialize();
$this->optionsBuilder->addOption('separate');
2015-01-08 11:01:22 +01:00
}
/**
* {@inheritdoc}
*/
protected function build(
Definition $definition,
Model $model,
2015-01-08 11:01:22 +01:00
DefinitionMapper $mapper,
Request $request = null,
2015-01-20 16:38:23 +01:00
Definition $parent = null
2015-01-08 11:01:22 +01:00
) {
parent::build($definition, $model, $mapper, $request);
2015-01-08 11:01:22 +01:00
if ($definition instanceof SpinJsLoadingControl && $model->spin) {
$config = json_decode($model->spin, true);
if (is_array($config)) {
$definition->setSpin($config);
}
}
2015-01-12 19:03:29 +01:00
if ($definition instanceof LoadingControl && !$definition->isSeparate() && $model->zoomControl) {
2015-01-08 11:01:22 +01:00
// Only assign if zoom control is activated and part of the map.
$control = ControlModel::findOneBy(
array('active=1', 'type=?', 'pid=?', 'id=?'),
array('zoom', $model->pid, $model->zoomControl)
);
if ($control) {
$control = $mapper->handle($control);
if ($control instanceof Zoom) {
// By default the loading control overrides the position of the zoom control. Deactivate it by
// overriding the position.
$definition->setPosition($control->getPosition());
$definition->setZoomControl($control);
}
}
}
}
}