diff --git a/src/Bundle/Resources/config/config.yml b/src/Bundle/Resources/config/config.yml index 41da04e..d912d2e 100644 --- a/src/Bundle/Resources/config/config.yml +++ b/src/Bundle/Resources/config/config.yml @@ -38,6 +38,13 @@ parameters: - 'circle' - 'circleMarker' + netzmacht.contao_leaflet.file_formats: + gpx: ['gpx'] + kml: ['kml'] + wkt: ['wkt'] + geojson: ['json', 'geojson'] + topojson: ['json', 'topojson'] + # When creating a GeoJSON feature of a map object a feature.properties.model object is passed. # Define the properties you always want to set. # diff --git a/src/Bundle/Resources/config/listeners.yml b/src/Bundle/Resources/config/listeners.yml index 89ba6f8..1d1a24e 100644 --- a/src/Bundle/Resources/config/listeners.yml +++ b/src/Bundle/Resources/config/listeners.yml @@ -39,6 +39,7 @@ services: - '%netzmacht.contao_leaflet.layers%' - '%netzmacht.contao_leaflet.providers%' - '%netzmacht.contao_leaflet.amenities%' + - '%netzmacht.contao_leaflet.file_formats%' netzmacht.contao_leaflet.listeners.dca.marker: class: Netzmacht\Contao\Leaflet\Listener\Dca\MarkerDcaListener diff --git a/src/Bundle/Resources/contao/config/config.php b/src/Bundle/Resources/contao/config/config.php index a823af9..1d1fba1 100644 --- a/src/Bundle/Resources/contao/config/config.php +++ b/src/Bundle/Resources/contao/config/config.php @@ -49,6 +49,10 @@ array_insert( ] ); +if (TL_MODE === 'BE') { + $GLOBALS['TL_CSS'][] = 'bundles/netzmachtcontaoleaflet/css/backend_hacks.css'; +} + /* * Models. */ diff --git a/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php b/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php index 836b266..c180cb8 100644 --- a/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php +++ b/src/Bundle/Resources/contao/dca/tl_leaflet_layer.php @@ -219,7 +219,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [ ], 'file extends default' => [ - '+config' => ['file', 'boundsMode'], + '+config' => ['boundsMode', 'fileFormat'], '+expert' => [ 'onEachFeature', 'pointToLayer', @@ -235,6 +235,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [ 'MapBox' => ['tile_provider_key'], 'HERE' => ['tile_provider_key', 'tile_provider_code'], ], + 'fileFormat' => [ + '!' => ['file'] + ] ], 'metasubpalettes' => [ @@ -861,15 +864,34 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = [ ], 'sql' => 'mediumtext NULL', ], + 'fileFormat' => [ + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormat'], + 'exclude' => true, + 'inputType' => 'select', + 'filter' => true, + 'eval' => [ + 'mandatory' => true, + 'tl_class' => 'w50', + 'includeBlankOption' => true, + 'submitOnChange' => true, + 'chosen' => true, + 'helpwizard' => true, + ], + 'options_callback' => ['netzmacht.contao_leaflet.listeners.dca.layer', 'getFileFormats'], + 'reference' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats'], + 'sql' => "varchar(32) NOT NULL default ''", + ], 'file' => [ 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['file'], 'exclude' => true, 'inputType' => 'fileTree', + 'load_callback' => [ + ['netzmacht.contao_leaflet.listeners.dca.layer', 'prepareFileWidget'], + ], '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/tl_leaflet_layer.php b/src/Bundle/Resources/contao/languages/en/tl_leaflet_layer.php index 8f0f70d..19935ee 100644 --- a/src/Bundle/Resources/contao/languages/en/tl_leaflet_layer.php +++ b/src/Bundle/Resources/contao/languages/en/tl_leaflet_layer.php @@ -145,14 +145,27 @@ $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']['fileFormat'][0] = 'File formats'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormat'][1] = 'Choose which file format is used.'; $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']['file'][1] = 'Choose a file containing geodata. Supported formats are: %s.'; $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'; $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']['fileFormats']['gpx'][0] = 'GPX'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['gpx'][1] = 'GPS Exchange format. Read the documentation for more information.'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['kml'][0] = 'KML'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['kml'][1] = 'Keyhole Markup Language. Read the documentation for more information.'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['wkt'][0] = 'WKT'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['wkt'][1] = 'Well-known text Read the documentation for more information.'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['topojson'][0] = 'TopoJSON'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['topojson'][1] = 'TopoJSON is a topological geospatial data interchange format based on GeoJSON. Read the documentation for more information.'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['geojson'][0] = 'GeoJSON'; +$GLOBALS['TL_LANG']['tl_leaflet_layer']['fileFormats']['geojson'][1] = 'GeoJSON is a format for encoding a variety of geographic data structures. Read the documentation for more information.'; + $GLOBALS['TL_LANG']['tl_leaflet_layer']['pasteinto'][1] = 'Paste into layer %s'; $GLOBALS['TL_LANG']['tl_leaflet_layer']['pasteafter'][1] = 'Paste after layer %s'; diff --git a/src/Bundle/Resources/public/css/backend_hacks.css b/src/Bundle/Resources/public/css/backend_hacks.css new file mode 100644 index 0000000..c30f35c --- /dev/null +++ b/src/Bundle/Resources/public/css/backend_hacks.css @@ -0,0 +1,3 @@ +.tl_help_table a { + color: #77ac45 +} diff --git a/src/Listener/Dca/LayerDcaListener.php b/src/Listener/Dca/LayerDcaListener.php index 23a2d6d..1085496 100644 --- a/src/Listener/Dca/LayerDcaListener.php +++ b/src/Listener/Dca/LayerDcaListener.php @@ -13,6 +13,7 @@ namespace Netzmacht\Contao\Leaflet\Listener\Dca; use Contao\Controller; +use Contao\DataContainer; use Contao\Image; use Contao\RequestToken; use Contao\StringUtil; @@ -81,6 +82,13 @@ class LayerDcaListener extends AbstractListener */ private $labelRenderer; + /** + * File formats. + * + * @var array + */ + private $fileFormats; + /** * Construct. * @@ -91,6 +99,7 @@ class LayerDcaListener extends AbstractListener * @param array $layers Leaflet layer configuration. * @param array $tileProviders Tile providers. * @param array $amenities OSM amenities. + * @param array $fileFormats File formats. */ public function __construct( Manager $manager, @@ -99,7 +108,8 @@ class LayerDcaListener extends AbstractListener LayerLabelRenderer $labelRenderer, array $layers, array $tileProviders, - array $amenities + array $amenities, + array $fileFormats ) { parent::__construct($manager); @@ -111,6 +121,7 @@ class LayerDcaListener extends AbstractListener $this->translator = $translator; $this->amenities = $amenities; $this->labelRenderer = $labelRenderer; + $this->fileFormats = $fileFormats; } /** @@ -424,6 +435,49 @@ class LayerDcaListener extends AbstractListener return $builder->getOptions(); } + /** + * Get the file formats. + * + * @return array + */ + public function getFileFormats(): array + { + return array_keys($this->fileFormats); + } + + /** + * Prepare the file widget. + * + * @param mixed $value Given value. + * @param DataContainer $dataContainer Data container driver. + * + * @return mixed + */ + public function prepareFileWidget($value, $dataContainer) + { + if ($dataContainer->activeRecord) { + $fileFormat = $dataContainer->activeRecord->fileFormat; + + if (isset($this->fileFormats[$fileFormat])) { + $definition = $this->getDefinition(); + $definition->set( + ['fields', $dataContainer->field, 'eval', 'extensions'], + implode(',', $this->fileFormats[$fileFormat]) + ); + + $definition->set( + ['fields', $dataContainer->field, 'label', 1], + sprintf( + $definition->get(['fields', $dataContainer->field, 'label', 1]), + implode(', ', $this->fileFormats[$fileFormat]) + ) + ); + } + } + + return $value; + } + /** * Generate a button. * diff --git a/src/Mapper/Layer/FileLayerMapper.php b/src/Mapper/Layer/FileLayerMapper.php index f54d8ad..efd2508 100644 --- a/src/Mapper/Layer/FileLayerMapper.php +++ b/src/Mapper/Layer/FileLayerMapper.php @@ -22,9 +22,11 @@ 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\GeoJson as OmnivoreGeoJson; use Netzmacht\LeafletPHP\Plugins\Omnivore\Gpx; use Netzmacht\LeafletPHP\Plugins\Omnivore\Kml; use Netzmacht\LeafletPHP\Plugins\Omnivore\OmnivoreLayer; +use Netzmacht\LeafletPHP\Plugins\Omnivore\TopoJson; use Netzmacht\LeafletPHP\Plugins\Omnivore\Wkt; /** @@ -80,17 +82,25 @@ class FileLayerMapper extends AbstractLayerMapper $layerId = $this->getElementId($model, $elementId); if ($fileModel instanceof FilesModel && $fileModel->type === 'file') { - switch ($fileModel->extension) { + switch ($model->fileFormat) { case 'gpx': - $layer = new Gpx($layerId, $fileModel->path, []); + $layer = new Gpx($layerId, $fileModel->path); break; case 'kml': - $layer = new Kml($layerId, $fileModel->path, []); + $layer = new Kml($layerId, $fileModel->path); break; case 'wkt': - $layer = new Wkt($layerId, $fileModel->path, []); + $layer = new Wkt($layerId, $fileModel->path); + break; + + case 'geojson': + $layer = new OmnivoreGeoJson($layerId, $fileModel->path); + break; + + case 'topojson': + $layer = new TopoJson($layerId, $fileModel->path); break; default: