From 8e35dc1fef1030d007936ff6803a8a5a76d3f8f7 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 9 Jan 2015 15:24:34 +0100 Subject: [PATCH] Add reference layer. --- assets/libs/contao/contao-leaflet.js | 6 ++- module/assets/img/reference.png | Bin 0 -> 730 bytes module/assets/img/reference_1.png | Bin 0 -> 634 bytes module/config/config.php | 48 +++++++++++++++++- module/dca/tl_leaflet_layer.php | 17 +++++++ module/languages/en/leaflet.php | 18 ++++--- module/languages/en/tl_leaflet_layer.php | 4 ++ src/Netzmacht/Contao/Leaflet/Dca/Layer.php | 13 +++++ .../Leaflet/Event/BuildDefinitionEvent.php | 2 +- .../Mapper/Layer/ReferenceLayerMapper.php | 45 ++++++++++++++++ ...ctiveTrait.php => AbstractActiveModel.php} | 8 +-- .../Contao/Leaflet/Model/ControlModel.php | 2 +- .../Contao/Leaflet/Model/IconModel.php | 4 +- .../Contao/Leaflet/Model/LayerModel.php | 2 +- .../Contao/Leaflet/Model/StyleModel.php | 4 +- 15 files changed, 149 insertions(+), 24 deletions(-) create mode 100644 module/assets/img/reference.png create mode 100644 module/assets/img/reference_1.png create mode 100644 src/Netzmacht/Contao/Leaflet/Mapper/Layer/ReferenceLayerMapper.php rename src/Netzmacht/Contao/Leaflet/Model/{ActiveTrait.php => AbstractActiveModel.php} (76%) diff --git a/assets/libs/contao/contao-leaflet.js b/assets/libs/contao/contao-leaflet.js index 7ce9143..85b31d0 100644 --- a/assets/libs/contao/contao-leaflet.js +++ b/assets/libs/contao/contao-leaflet.js @@ -30,8 +30,10 @@ L.Contao = L.Class.extend({ var map = this; this.on('layeradd', function(e) { - e.layer.on('data:loading', function() { map.fire('dataloading'); }); - e.layer.on('data:loaded', function() { map.fire('dataload'); }); + if (e.layer.on) { + e.layer.on('data:loading', function() { map.fire('dataloading'); }); + e.layer.on('data:loaded', function() { map.fire('dataload'); }); + } }); }); }, diff --git a/module/assets/img/reference.png b/module/assets/img/reference.png new file mode 100644 index 0000000000000000000000000000000000000000..db3be338e830db09291f5c9e9d26e51cf9fae3b2 GIT binary patch literal 730 zcmV<00ww*4P)RCwBqQcXxyQ562}yZ61(nbBm7 zTC|D+X;a}qEecv%Lp#Yz!wFi3D?Y=UCRU;aG+^v$b@B5VIqc*i78Y{g6XKE zqa`!%uXEpzPHyVL;huZ$J>U7xcMrSpFq13lW|d(BkVcE4Y&HicIAVgk_A= z<<}TFL6ohfAdbv|8@?8-1i*ITdJd$aGPfO0h=tjk*fN6jxQu9W@cDZP9=zkcI}NUsAmn&NAKdt2ZluE5;))ErSl!J1H8ltAabY+x^BVH zZPKXPVcm456>ICyu*KTkPV9*MHMbvGRSKezspP+17{KKGw0lh?b(37B60zC_2sMqm z1beCp#D&iwzuW>WkOLRm{?5XY_u6%OE!;uK}gsh zgWS@4sJwy%6cAiCii5 zhB|Pa)j#OZ7frjul;dbKXGo+DnkXkm0s1E5Y*%2_K8*MkU}gpw8Lr7@Zcm?VOpJe* z;K!X0{l_}@2xF&C1F!q>>P-h)FDJ%6fo*`{|5w!8b7bHA;2!}709XRkqi&*{;{X5v M07*qoM6N<$f(=enXaE2J literal 0 HcmV?d00001 diff --git a/module/assets/img/reference_1.png b/module/assets/img/reference_1.png new file mode 100644 index 0000000000000000000000000000000000000000..00f1bcdd006a959bd13215092375a81adab0f7b4 GIT binary patch literal 634 zcmV-=0)_pFP)-!n6-s01%bYV!j`2naTUb;=7Vlv_lxOh}dAks@HJNdAFa2!VtY zf?aHaC<-PD%C4^QBNso=-I)~0PFybeIMtc2XU==xL-l%n?Yi!jQVL2b{4IzGL>~=fEVlfzop(S2QWq#sI;GDxS41_`<*tU(`ZU@`8VHgHD z=US!)__RnQf>0;~&N+k-uq+EwN_d_JDJ2$*1;XL5?!z4koO69ZL~tBOOCch3IvvdC zb4(_a3jqHufpZQb(iJ2U37pSoc%G*rnM?-j^%}?H@#jbCez1ZIpJ7=Rrqd}dd&D_M zDwRUN-$$iVdHbEeENXz`IADxvz-Tl=u~@|6a0mb_%R(xZf)L`_lSw6}0jt$Y_rceA zI2 'system/modules/leaflet/assets/img/leaflet.png', + 'icon' => 'system/modules/leaflet/assets/img/map.png', 'stylesheet' => 'system/modules/leaflet/assets/css/backend.css', ), 'leaflet_layer' => array @@ -67,6 +67,7 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\ProviderL $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\MarkersLayerMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\GroupLayerMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\VectorsLayerMapper'; +$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\ReferenceLayerMapper'; // Control mappers. $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\ZoomControlMapper'; @@ -122,6 +123,17 @@ $GLOBALS['LEAFLET_LAYERS'] = array ( 'children' => false, 'icon' => 'system/modules/leaflet/assets/img/tile.png', + 'label' => function ($row, $label) { + if (!empty($GLOBALS['TL_LANG']['leaflet_provider'][$row['tile_provider']][0])) { + $provider = $GLOBALS['TL_LANG']['leaflet_provider'][$row['tile_provider']][0]; + } else { + $provider = $row['tile_provider']; + } + + $label .= sprintf(' (%s)', $provider); + + return $label; + } ), 'group' => array ( @@ -133,13 +145,47 @@ $GLOBALS['LEAFLET_LAYERS'] = array 'children' => false, 'icon' => 'system/modules/leaflet/assets/img/markers.png', 'markers' => true, + 'label' => function ($row, $label) { + $count = \Netzmacht\Contao\Leaflet\Model\MarkerModel::countBy('pid', $row['id']); + $label .= sprintf( + ' (%s %s)', + $count, + $GLOBALS['TL_LANG']['tl_leaflet_layer']['countEntries'] + ); + + return $label; + } ), 'vectors' => array ( 'children' => false, 'icon' => 'system/modules/leaflet/assets/img/vectors.png', 'vectors' => true, + 'label' => function ($row, $label) { + $count = \Netzmacht\Contao\Leaflet\Model\VectorModel::countBy('pid', $row['id']); + $label .= sprintf( + ' (%s %s)', + $count, + $GLOBALS['TL_LANG']['tl_leaflet_layer']['countEntries'] + ); + + return $label; + } ), + 'reference' => array + ( + 'children' => false, + 'icon' => 'system/modules/leaflet/assets/img/reference.png', + 'label' => function ($row, $label) { + $reference = \Netzmacht\Contao\Leaflet\Model\LayerModel::findByPk($row['reference']); + + if ($reference) { + $label .= ' (' . $reference->title . ')'; + } + + return $label; + } + ) ); /* diff --git a/module/dca/tl_leaflet_layer.php b/module/dca/tl_leaflet_layer.php index 8b88a3a..a7636a6 100644 --- a/module/dca/tl_leaflet_layer.php +++ b/module/dca/tl_leaflet_layer.php @@ -136,6 +136,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'vectors extends default' => array( '+active' => array('deferred'), ), + 'reference extends default' => array( + '+title' => array('reference') + ) ), 'metasubselectpalettes' => array( @@ -294,5 +297,19 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'reference' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes'], 'sql' => "varchar(32) NOT NULL default ''" ), + 'reference' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['reference'], + 'exclude' => true, + 'inputType' => 'select', + 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'getLayers'), + 'eval' => array( + 'mandatory' => true, + 'tl_class' => 'w50', + 'chosen' => true, + 'includeBlankOption' => true, + ), + 'sql' => "int(10) unsigned NOT NULL default '0'", + ), ) ); diff --git a/module/languages/en/leaflet.php b/module/languages/en/leaflet.php index 399defd..935d0ae 100644 --- a/module/languages/en/leaflet.php +++ b/module/languages/en/leaflet.php @@ -11,14 +11,16 @@ $GLOBALS['TL_LANG']['leaflet_control']['scale'][1] = 'A simple scale contr $GLOBALS['TL_LANG']['leaflet_control']['loading'][0] = 'Loading indicator'; $GLOBALS['TL_LANG']['leaflet_control']['loading'][1] = 'Leaflet.loading is a simple loading indicator implemented as control. For more details read the Plugin documentation.'; -$GLOBALS['TL_LANG']['leaflet_layer']['provider'][0] = 'Leaflet provider'; -$GLOBALS['TL_LANG']['leaflet_layer']['provider'][1] = 'Leaflet tile provider. For more details read the plugin documentation.'; -$GLOBALS['TL_LANG']['leaflet_layer']['group'][0] = 'Layer group'; -$GLOBALS['TL_LANG']['leaflet_layer']['group'][1] = 'Layer groups combines different layers. For more details read the Leaflet documentation. '; -$GLOBALS['TL_LANG']['leaflet_layer']['markers'][0] = 'Markers'; -$GLOBALS['TL_LANG']['leaflet_layer']['markers'][1] = 'Layer containing Markers.'; -$GLOBALS['TL_LANG']['leaflet_layer']['vectors'][0] = 'Vectors'; -$GLOBALS['TL_LANG']['leaflet_layer']['vectors'][1] = 'Vectors layer containing vectors like polygons, polylines, etc.'; +$GLOBALS['TL_LANG']['leaflet_layer']['provider'][0] = 'Leaflet provider'; +$GLOBALS['TL_LANG']['leaflet_layer']['provider'][1] = 'Leaflet tile provider. For more details read the plugin documentation.'; +$GLOBALS['TL_LANG']['leaflet_layer']['group'][0] = 'Layer group'; +$GLOBALS['TL_LANG']['leaflet_layer']['group'][1] = 'Layer groups combines different layers. For more details read the Leaflet documentation. '; +$GLOBALS['TL_LANG']['leaflet_layer']['markers'][0] = 'Markers'; +$GLOBALS['TL_LANG']['leaflet_layer']['markers'][1] = 'Layer containing Markers.'; +$GLOBALS['TL_LANG']['leaflet_layer']['vectors'][0] = 'Vectors'; +$GLOBALS['TL_LANG']['leaflet_layer']['vectors'][1] = 'Vectors layer containing vectors like polygons, polylines, etc.'; +$GLOBALS['TL_LANG']['leaflet_layer']['reference'][0] = 'Reference'; +$GLOBALS['TL_LANG']['leaflet_layer']['reference'][1] = 'The reference layer is a link to another layer.'; $GLOBALS['TL_LANG']['leaflet_vector']['polyline'][0] = 'Polyline'; $GLOBALS['TL_LANG']['leaflet_vector']['polyline'][1] = 'Polyline overlay. For more details read the Leaflet documentation.'; diff --git a/module/languages/en/tl_leaflet_layer.php b/module/languages/en/tl_leaflet_layer.php index 06be532..91f1263 100644 --- a/module/languages/en/tl_leaflet_layer.php +++ b/module/languages/en/tl_leaflet_layer.php @@ -36,6 +36,8 @@ $GLOBALS['TL_LANG']['tl_leaflet_layer']['tile_provider_variant'][0] = 'Tile vari $GLOBALS['TL_LANG']['tl_leaflet_layer']['tile_provider_variant'][1] = 'Tile variant style.'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['active'][0] = 'Activate layer'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['active'][1] = 'Activate layer on the map.'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['reference'][0] = 'Reference'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['reference'][1] = 'Choose the reference layer.'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['markerCluster'][0] = 'Marker cluster'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['markerCluster'][1] = 'Choose a marker cluster layer so that markers get clustered.'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['deferred'][0] = 'Deferred loading'; @@ -47,3 +49,5 @@ $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][0] = 'Layer gro $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][1] = 'Basic layer group.
See http://leafletjs.com/reference.html#layergroup'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['feature'][0] = 'Feature group'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['feature'][1] = 'Extended layer group with events and popup support.
See http://leafletjs.com/reference.html#featuregroup'; + +$GLOBALS['TL_LANG']['tl_leaflet_layer']['countEntries'] = 'Entries'; diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Layer.php b/src/Netzmacht/Contao/Leaflet/Dca/Layer.php index 95de5b6..12787bb 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Layer.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/Layer.php @@ -57,6 +57,10 @@ class Layer $icon = \Image::getHtml($src, $alt, sprintf('title="%s"', strip_tags($alt))); + if (!empty($this->layers[$row['type']]['label'])) { + $label = $this->layers[$row['type']]['label']($row, $label); + } + return $icon . ' ' . $label; } @@ -146,6 +150,15 @@ class Layer return $this->generateButton($row, $href, $label, $title, $icon, $attributes); } + public function getLayers($dataContainer) + { + $collection = LayerModel::findBy('id !', $dataContainer->id); + + return OptionsBuilder::fromCollection($collection, 'id', 'title') + ->asTree() + ->getOptions(); + } + /** * @param $row * @param $href diff --git a/src/Netzmacht/Contao/Leaflet/Event/BuildDefinitionEvent.php b/src/Netzmacht/Contao/Leaflet/Event/BuildDefinitionEvent.php index 1ce2149..3dc8891 100644 --- a/src/Netzmacht/Contao/Leaflet/Event/BuildDefinitionEvent.php +++ b/src/Netzmacht/Contao/Leaflet/Event/BuildDefinitionEvent.php @@ -52,7 +52,7 @@ class BuildDefinitionEvent extends Event * @param \Model $model The definition model. * @param LatLngBounds $bounds Optional bounds where elements should be in. */ - public function __construct(Definition $definition, \Model $model, LatLngBounds $bounds = null) + public function __construct(Definition $definition = null, \Model $model, LatLngBounds $bounds = null) { $this->definition = $definition; $this->model = $model; diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ReferenceLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ReferenceLayerMapper.php new file mode 100644 index 0000000..d213da5 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ReferenceLayerMapper.php @@ -0,0 +1,45 @@ + + * @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\Model\LayerModel; +use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; + +/** + * Class ReferenceLayerMapper maps an reference layer to another + * + * @package Netzmacht\Contao\Leaflet\Mapper\Layer + */ +class ReferenceLayerMapper extends AbstractLayerMapper +{ + /** + * Layer type. + * + * @var string + */ + protected static $type = 'reference'; + + /** + * {@inheritdoc} + */ + public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null, $elementId = null) + { + $reference = LayerModel::findByPk($model->reference); + + if (!$reference || !$reference->active) { + return null; + } + + return $mapper->handle($reference, $bounds, $this->getElementId($model, $elementId)); + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Model/ActiveTrait.php b/src/Netzmacht/Contao/Leaflet/Model/AbstractActiveModel.php similarity index 76% rename from src/Netzmacht/Contao/Leaflet/Model/ActiveTrait.php rename to src/Netzmacht/Contao/Leaflet/Model/AbstractActiveModel.php index d4ff1d8..81c37bf 100644 --- a/src/Netzmacht/Contao/Leaflet/Model/ActiveTrait.php +++ b/src/Netzmacht/Contao/Leaflet/Model/AbstractActiveModel.php @@ -12,7 +12,7 @@ namespace Netzmacht\Contao\Leaflet\Model; -trait ActiveTrait +abstract class AbstractActiveModel extends \Model { /** * @@ -28,14 +28,14 @@ trait ActiveTrait /** * - * @param int $modelId + * @param int $value * @param array $options * * @return \Model|null */ - public static function findActiveByPid($modelId, $options = array()) + public static function findActiveBy($column, $value, $options = array()) { - return static::findBy('active=1 AND pid', $modelId, $options); + return static::findBy('active=1 AND ' . $column, $value, $options); } public static function findActivated($options = array()) diff --git a/src/Netzmacht/Contao/Leaflet/Model/ControlModel.php b/src/Netzmacht/Contao/Leaflet/Model/ControlModel.php index 54d5469..6432424 100644 --- a/src/Netzmacht/Contao/Leaflet/Model/ControlModel.php +++ b/src/Netzmacht/Contao/Leaflet/Model/ControlModel.php @@ -11,7 +11,7 @@ namespace Netzmacht\Contao\Leaflet\Model; -class ControlModel extends \Model +class ControlModel extends AbstractActiveModel { protected static $strTable = 'tl_leaflet_control'; } diff --git a/src/Netzmacht/Contao/Leaflet/Model/IconModel.php b/src/Netzmacht/Contao/Leaflet/Model/IconModel.php index e5ab092..6c725d0 100644 --- a/src/Netzmacht/Contao/Leaflet/Model/IconModel.php +++ b/src/Netzmacht/Contao/Leaflet/Model/IconModel.php @@ -21,9 +21,7 @@ namespace Netzmacht\Contao\Leaflet\Model; * @property mixed|null shadowRetinaImage * @property mixed|null shadowImage */ -class IconModel extends \Model +class IconModel extends AbstractActiveModel { - use ActiveTrait; - protected static $strTable = 'tl_leaflet_icon'; } diff --git a/src/Netzmacht/Contao/Leaflet/Model/LayerModel.php b/src/Netzmacht/Contao/Leaflet/Model/LayerModel.php index db2a6d4..4daa4da 100644 --- a/src/Netzmacht/Contao/Leaflet/Model/LayerModel.php +++ b/src/Netzmacht/Contao/Leaflet/Model/LayerModel.php @@ -12,7 +12,7 @@ namespace Netzmacht\Contao\Leaflet\Model; -class LayerModel extends \Model +class LayerModel extends AbstractActiveModel { protected static $strTable = 'tl_leaflet_layer'; diff --git a/src/Netzmacht/Contao/Leaflet/Model/StyleModel.php b/src/Netzmacht/Contao/Leaflet/Model/StyleModel.php index ec242ff..20e33c0 100644 --- a/src/Netzmacht/Contao/Leaflet/Model/StyleModel.php +++ b/src/Netzmacht/Contao/Leaflet/Model/StyleModel.php @@ -12,9 +12,7 @@ namespace Netzmacht\Contao\Leaflet\Model; -class StyleModel extends \Model +class StyleModel extends AbstractActiveModel { - use ActiveTrait; - protected static $strTable = 'tl_leaflet_style'; }