diff --git a/composer.json b/composer.json index 507d83b..c1962f9 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "contao/core-bundle": "~4.4", "netzmacht/contao-leaflet-libraries": "^1.0", "netzmacht/php-javascript-builder": "^1.0", - "netzmacht/php-leaflet": "^1.0.1", + "netzmacht/php-leaflet": "^1.0.2", "netzmacht/contao-toolkit": "^3.0@dev", "contao-community-alliance/meta-palettes": "^1.5", "menatwork/contao-multicolumnwizard": "^3.2", diff --git a/src/Bundle/Resources/config/layers.yml b/src/Bundle/Resources/config/layers.yml index 4716389..9350a9f 100644 --- a/src/Bundle/Resources/config/layers.yml +++ b/src/Bundle/Resources/config/layers.yml @@ -45,6 +45,10 @@ parameters: extend: true fit: true + file: + children: false + icon: 'bundles/netzmachtcontaoleaflet/img/file.png' + services: _defaults: public: false diff --git a/src/Bundle/Resources/config/mappers.yml b/src/Bundle/Resources/config/mappers.yml index 9a50e51..6aa5fe8 100644 --- a/src/Bundle/Resources/config/mappers.yml +++ b/src/Bundle/Resources/config/mappers.yml @@ -58,6 +58,10 @@ services: tags: - { name: netzmacht.contao_leflet_maps.mapper } + netzmacht.contao_leaflet.mapper.file_layer: + class: Netzmacht\Contao\Leaflet\Mapper\Layer\FileLayerMapper + tags: + - { name: netzmacht.contao_leaflet.mapper } # Control mappers netzmacht.contao_leaflet.mapper.zoom_control: diff --git a/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php b/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php index 9e77edb..4aa8745 100644 --- a/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php +++ b/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php @@ -217,6 +217,14 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [ 'pointToLayer', ], ], + + 'file extends default' => [ + '+config' => ['file'], + '+expert' => [ + 'onEachFeature', + 'pointToLayer', + ], + ], ], 'metasubselectpalettes' => [ @@ -853,5 +861,18 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [ ], 'sql' => 'mediumtext NULL', ], + 'file' => [ + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['file'], + 'exclude' => true, + 'inputType' => 'fileTree', + 'eval' => [ + 'filesOnly' => true, + 'fieldType' => 'radio', + 'mandatory' => true, + 'extensions' => 'gpx,kml,wkt', + 'tl_class' => 'clr', + ], + 'sql' => 'binary(16) NULL', + ], ], ]; diff --git a/src/Bundle/Resources/contao/languages/en/leaflet.php b/src/Bundle/Resources/contao/languages/en/leaflet.php index bd79aa6..a268eec 100644 --- a/src/Bundle/Resources/contao/languages/en/leaflet.php +++ b/src/Bundle/Resources/contao/languages/en/leaflet.php @@ -39,6 +39,8 @@ $GLOBALS['TL_LANG']['leaflet_layer']['tile'][0] = 'Tile layer'; $GLOBALS['TL_LANG']['leaflet_layer']['tile'][1] = 'Tile layer with full config options.'; $GLOBALS['TL_LANG']['leaflet_layer']['overpass'][0] = 'Overpass API'; $GLOBALS['TL_LANG']['leaflet_layer']['overpass'][1] = 'Overpass API data layer.'; +$GLOBALS['TL_LANG']['leaflet_layer']['file'][0] = 'File'; +$GLOBALS['TL_LANG']['leaflet_layer']['file'][1] = 'Geo data from a file (gpx,kml,wkt).'; $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/src/Bundle/Resources/contao/languages/en/tl_leaflet_layer.php b/src/Bundle/Resources/contao/languages/en/tl_leaflet_layer.php index 9c5ddc7..8f0f70d 100644 --- a/src/Bundle/Resources/contao/languages/en/tl_leaflet_layer.php +++ b/src/Bundle/Resources/contao/languages/en/tl_leaflet_layer.php @@ -145,6 +145,8 @@ $GLOBALS['TL_LANG']['tl_leaflet_layer']['amenity'][0] = 'Amen $GLOBALS['TL_LANG']['tl_leaflet_layer']['amenity'][1] = 'OSM amenity.'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['amenityIcon'][1] = 'Icon'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['amenityIcon'][0] = 'Icon style'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['file'][0] = 'File'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['file'][1] = 'Choose a file containing geodata. Supported formats are gpx,kml and wkt.'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][0] = 'Layer group'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['groupTypes']['layer'][1] = 'Basic layer group.
See http://leafletjs.com/reference.html#layergroup'; diff --git a/src/Bundle/Resources/public/img/file.png b/src/Bundle/Resources/public/img/file.png new file mode 100644 index 0000000..7a7227e Binary files /dev/null and b/src/Bundle/Resources/public/img/file.png differ diff --git a/src/Bundle/Resources/public/img/file_1.png b/src/Bundle/Resources/public/img/file_1.png new file mode 100644 index 0000000..502668d Binary files /dev/null and b/src/Bundle/Resources/public/img/file_1.png differ diff --git a/src/Mapper/Layer/FileLayerMapper.php b/src/Mapper/Layer/FileLayerMapper.php new file mode 100644 index 0000000..f58e764 --- /dev/null +++ b/src/Mapper/Layer/FileLayerMapper.php @@ -0,0 +1,136 @@ + + * @copyright 2014-2017 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE + * @filesource + */ + +declare(strict_types=1); + +namespace Netzmacht\Contao\Leaflet\Mapper\Layer; + +use Contao\FilesModel; +use Contao\Model; +use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; +use Netzmacht\Contao\Leaflet\Request\Request; +use Netzmacht\JavascriptBuilder\Type\Expression; +use Netzmacht\LeafletPHP\Definition; +use Netzmacht\LeafletPHP\Definition\Group\FeatureGroup; +use Netzmacht\LeafletPHP\Definition\Group\GeoJson; +use Netzmacht\LeafletPHP\Plugins\Omnivore\Gpx; +use Netzmacht\LeafletPHP\Plugins\Omnivore\Kml; +use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer; +use Netzmacht\LeafletPHP\Plugins\Omnivore\Wkt; + +/** + * Class FileLayerMapper. + * + * @package Netzmacht\Contao\Leaflet\Mapper\Layer + */ +class FileLayerMapper extends AbstractLayerMapper +{ + /** + * The definition type. + * + * @var string + */ + protected static $type = 'file'; + + /** + * Class of the model being build. + * + * @var string + */ + protected static $definitionClass = FeatureGroup::class; + + /** + * {@inheritDoc} + */ + public function handle( + $model, + DefinitionMapper $mapper, + Request $request = null, + $elementId = null, + Definition $parent = null + ) { + $fileModel = FilesModel::findByPk($model->file); + $definition = $this->createInstance($model, $mapper, $request, $elementId, $fileModel); + + $this->optionsBuilder->build($definition, $model); + $this->build($definition, $model, $mapper, $request, $parent); + + return $definition; + } + + /** + * {@inheritDoc} + */ + protected function createInstance( + Model $model, + DefinitionMapper $mapper, + Request $request = null, + $elementId = null, + FilesModel $fileModel = null + ) { + $layerId = $this->getElementId($model, $elementId); + + if ($fileModel instanceof FilesModel && $fileModel->type === 'file') { + switch ($fileModel->extension) { + case 'gpx': + $layer = new Gpx($layerId, $fileModel->path, []); + break; + + case 'kml': + $layer = new Kml($layerId, $fileModel->path, []); + break; + + case 'wkt': + $layer = new Wkt($layerId, $fileModel->path, []); + break; + + default: + return parent::createInstance($model, $mapper, $request, $elementId); + } + + $customId = $layerId . '_data'; + $customLayer = new GeoJson($customId); + + $layer->setCustomLayer($customLayer); + + return $layer; + } + + return parent::createInstance($model, $mapper, $request, $elementId); + } + + /** + * {@inheritDoc} + */ + protected function build( + Definition $definition, + Model $model, + DefinitionMapper $mapper, + Request $request = null, + Definition $parent = null + ) { + if (!$definition instanceof OmnivoreLayer) { + return; + } + + $customLayer = $definition->getCustomLayer(); + if ($customLayer instanceof GeoJson) { + if ($model->pointToLayer) { + $customLayer->setPointToLayer(new Expression($model->pointToLayer)); + } + + if ($model->onEachFeature) { + $customLayer->setOnEachFeature(new Expression($model->onEachFeature)); + } + } + } +} diff --git a/src/Subscriber/EncoderSubscriber.php b/src/Subscriber/EncoderSubscriber.php index 259c1d3..bf0aae6 100644 --- a/src/Subscriber/EncoderSubscriber.php +++ b/src/Subscriber/EncoderSubscriber.php @@ -13,11 +13,10 @@ namespace Netzmacht\Contao\Leaflet\Subscriber; use Netzmacht\Contao\Leaflet\Frontend\RequestUrl; -use Netzmacht\JavascriptBuilder\Encoder; -use Netzmacht\JavascriptBuilder\Flags; -use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeValueEvent; -use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent; use Netzmacht\JavascriptBuilder\Exception\EncodeValueFailed; +use Netzmacht\JavascriptBuilder\Flags; +use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent; +use Netzmacht\JavascriptBuilder\Symfony\Event\EncodeValueEvent; use Netzmacht\LeafletPHP\Definition\Type\Icon; use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer; use Symfony\Component\EventDispatcher\EventSubscriberInterface;