Add icon support.

This commit is contained in:
David Molineus
2015-01-06 21:30:57 +01:00
parent 3608f7cd48
commit adc8de55ae
11 changed files with 367 additions and 136 deletions

View File

@@ -0,0 +1,25 @@
<?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\Type;
use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper;
class AbstractIconMapper extends AbstractTypeMapper
{
/**
* Class of the model being build.
*
* @var string
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\IconModel';
}

View File

@@ -0,0 +1,132 @@
<?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\Type;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
class ImageIconMapper extends AbstractIconMapper
{
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Type\Icon';
/**
* Layer type.
*
* @var string
*/
protected static $type = 'image';
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$arguments = parent::buildConstructArguments($model, $mapper, $bounds);
if ($model->iconImage) {
$file = \FilesModel::findByUuid($model->iconImage);
if ($file) {
$arguments[] = $file->path;
}
}
return $arguments;
}
protected function doBuild(
Definition $definition,
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null
) {
if ($definition instanceof Icon) {
$this->addIcon($definition, $model);
$this->addShadow($definition, $model);
}
}
/**
* @param Icon $definition
* @param IconModel $model
*/
private function addIcon(Icon $definition, IconModel $model)
{
if ($model->iconImage) {
$file = \FilesModel::findByUuid($model->iconImage);
if ($file) {
$definition->setIconUrl($file->path);
$file = new \File($file->path);
$definition->setIconSize(array($file->width, $file->height));
if (!$model->iconAnchor) {
$definition->setIconAnchor(array($file->width / 2, $file->height));
}
if (!$model->popupAnchor) {
$definition->setPopupAnchor(array(0, 10 - $file->height));
}
}
}
if ($model->iconAnchor) {
$definition->setIconAnchor(array_map('intval', explode(',', $model->iconAnchor)));
}
if ($model->iconRetinaImage) {
$file = \FilesModel::findByUuid($model->iconRetinaImage);
if ($file) {
$definition->setIconRetinaUrl($file->path);
}
}
}
private function addShadow(Icon $definition, $model)
{
if ($model->shadowImage) {
$file = \FilesModel::findByUuid($model->shadowImage);
if ($file) {
$definition->setShadowUrl($file->path);
$file = new \File($file->path);
$definition->setShadowSize(array($file->width, $file->height));
if (!$model->shadowAnchor) {
$definition->setShadowAnchor(array($file->width / 2, $file->height));
}
}
}
if ($model->shadowAnchor) {
$definition->setShadowAnchor(array_map('intval', explode(',', $model->shadowAnchor)));
}
if ($model->shadowRetinaImage) {
$file = \FilesModel::findByUuid($model->shadowRetinaImage);
if ($file) {
$definition->setShadowRetinaUrl($file->path);
}
}
}
}

View File

@@ -14,7 +14,9 @@ namespace Netzmacht\Contao\Leaflet\Mapper\UI;
use Netzmacht\Contao\Leaflet\Mapper\AbstractMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Definition\UI\Marker;
@@ -69,6 +71,20 @@ class MarkerMapper extends AbstractMapper
if ($model->addPopup) {
$definition->bindPopup($model->popupContent);
}
if ($model->customIcon) {
$iconModel = IconModel::findBy(
array('id=?', 'active=1'),
array($model->icon),
array('return' => 'Model')
);
if ($iconModel) {
/** @var Icon $icon */
$icon = $builder->handle($iconModel);
$definition->setIcon($icon);
}
}
}
}
}