diff --git a/.travis.yml b/.travis.yml index d353487..a983487 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,21 +1,14 @@ language: php php: - - 5.4 - - 5.5 - - 5.6 + - "5.4" + - "5.5" + - "5.6" + - "7.0" + - "nightly" env: - - CONTAO_VERSION=~3.2.0 - - CONTAO_VERSION=~3.3.0 - - CONTAO_VERSION=~3.4.0 - - CONTAO_VERSION=~3.5.0 - -# Exclude impossible Contao Version combinations. -matrix: - exclude: - - php: 5.3 - env: CONTAO_VERSION=~3.5.0 + - CONTAO_VERSION=~3.5.1 sudo: false @@ -26,6 +19,11 @@ install: script: ant -keep-going +matrix: + allow_failures: + - php: "7.0" + - php: "nightly" + # Hack to make things work again - we can not use a shallow repository. git: depth: 2147483647 diff --git a/assets/maps/contao-leaflet.js b/assets/maps/contao-leaflet.js index 1196484..a9bf4c5 100644 --- a/assets/maps/contao-leaflet.js +++ b/assets/maps/contao-leaflet.js @@ -1 +1 @@ -L.Contao=L.Class.extend({includes:L.Mixin.Events,statics:{ATTRIBUTION:' | netzmacht creative'},maps:{},icons:{},initialize:function(){L.Icon.Default.imagePath="assets/leaflet/libs/leaflet/images",this.setGeoJsonListeners(L.GeoJSON)},addMap:function(t,e){return this.maps[t]=e,this.fire("map:added",{id:t,map:e}),this},getMap:function(t){return"undefined"==typeof this.maps[t]?null:this.maps[t]},addIcon:function(t,e){return this.icons[t]=e,this.fire("icon:added",{id:t,icon:e}),this},loadIcons:function(t){for(var e=0;er&&(i[i.length]=[n,t].join("=")),t=document.location.pathname+"?"+i.join("&")}return e&&e.options.dynamicLoad&&(o=e.getBounds(),t+="&f=bbox&v=",t+=o.getSouth()+","+o.getWest(),t+=","+o.getNorth()+","+o.getEast()),t}}),L.contao=new L.Contao,L.Control.Attribution.addInitHook(function(){this.options.prefix+=L.Contao.ATTRIBUTION}),L.Control.Attribution.include({setPrefix:function(t){return-1===t.indexOf(L.Contao.ATTRIBUTION)&&(t+=L.Contao.ATTRIBUTION),this.options.prefix=t,this._update(),this}}),L.GeoJSON.include({refreshData:function(t){var e=L.geoJson(),o=this;e.on("ready",function(){var t,e=o.getLayers();for(t=0;tnetzmacht'},maps:{},icons:{},initialize:function(){L.Icon.Default.imagePath="assets/leaflet/libs/leaflet/images/",this.setGeoJsonListeners(L.GeoJSON)},addMap:function(t,e){return this.maps[t]=e,this.fire("map:added",{id:t,map:e}),this},getMap:function(t){return"undefined"==typeof this.maps[t]?null:this.maps[t]},addIcon:function(t,e){return this.icons[t]=e,this.fire("icon:added",{id:t,icon:e}),this},loadIcons:function(t){for(var e=0;enetzmacht creative' + ATTRIBUTION: ' | netzmacht' }, /** @@ -29,7 +29,7 @@ L.Contao = L.Class.extend({ * Initialize Contao leaflet integration. */ initialize: function () { - L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images'; + L.Icon.Default.imagePath = 'assets/leaflet/libs/leaflet/images/'; this.setGeoJsonListeners(L.GeoJSON); }, diff --git a/composer.json b/composer.json index 824fa59..d01387b 100644 --- a/composer.json +++ b/composer.json @@ -19,23 +19,23 @@ }, "require":{ "php":">=5.4", - "contao/core":">=3.2,<3.6-dev", - "contao-community-alliance/dependency-container":"~1.0", - "contao-community-alliance/event-dispatcher":"~1.0", - "netzmacht/contao-leaflet-libraries": "~0.7", - "netzmacht/php-javascript-builder": "~1.0", - "netzmacht/php-leaflet": "~0.7", - "netzmacht/contao-toolkit":"~1.0", - "contao-community-alliance/meta-palettes": "~1.5", - "menatwork/contao-multicolumnwizard": "~3.2", - "doctrine/cache": "~1.0" + "contao/core":"^3.5", + "contao-community-alliance/dependency-container":"^1.0", + "contao-community-alliance/event-dispatcher":"^1.0", + "netzmacht/contao-leaflet-libraries": "^1.0", + "netzmacht/php-javascript-builder": "^1.0", + "netzmacht/php-leaflet": "^1.0", + "netzmacht/contao-toolkit":"^2.0", + "contao-community-alliance/meta-palettes": "^1.5", + "menatwork/contao-multicolumnwizard": "^3.2", + "doctrine/cache": "^1.0" }, "require-dev":{ - "netzmacht/contao-build-tools": "~1.1" + "netzmacht/contao-build-tools": "^1.1" }, "autoload": { "psr-0": { - "Netzmacht\\Contao\\Leaflet": "src/" + "Netzmacht\\Contao\\Leaflet\\": "src/" } }, "config": { diff --git a/gulpfile.js b/gulpfile.js index bfc2b00..cd55cd9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -9,8 +9,8 @@ var paths = { dest: 'assets/maps' }; -gulp.task('clear', function(cb) { - del([paths.dest + '/' + paths.minified], cb); +gulp.task('clear', function() { + del([paths.dest + '/' + paths.minified]); }); gulp.task('scripts', ['clear'], function() { diff --git a/module/assets/img/logo.png b/module/assets/img/logo.png index e143873..7de9611 100644 Binary files a/module/assets/img/logo.png and b/module/assets/img/logo.png differ diff --git a/module/config/autoload.ini b/module/config/autoload.ini new file mode 100644 index 0000000..2524a49 --- /dev/null +++ b/module/config/autoload.ini @@ -0,0 +1 @@ +requires[] = 'toolkit' diff --git a/module/config/config.php b/module/config/config.php index adda96d..d2acc49 100644 --- a/module/config/config.php +++ b/module/config/config.php @@ -56,13 +56,13 @@ array_insert( /* * Content elements. */ -$GLOBALS['TL_CTE']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\MapElement'; +$GLOBALS['TL_CTE']['includes']['leaflet'] = 'Netzmacht\Contao\Toolkit\Component\ContentElement\ContentElementDecorator'; /* * Frontend modules */ -$GLOBALS['FE_MOD']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\MapModule'; +$GLOBALS['FE_MOD']['includes']['leaflet'] = 'Netzmacht\Contao\Toolkit\Component\Module\ModuleDecorator'; /* @@ -88,12 +88,21 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\MapMapper'; // Layer mappers. $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\TileLayerMapper'; -$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\ProviderLayerMapper'; +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new \Netzmacht\Contao\Leaflet\Mapper\Layer\ProviderLayerMapper( + $GLOBALS['LEAFLET_TILE_PROVIDERS'] + ); +}; + $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'; -$GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Layer\MarkerClusterLayerMapper'; +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new \Netzmacht\Contao\Leaflet\Mapper\Layer\MarkerClusterLayerMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::MAP_ASSETS] + ); +}; // Control mappers. $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\ZoomControlMapper'; @@ -104,26 +113,40 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\Loading $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Control\FullscreenControlMapper'; // Vector mappers. -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) { - return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolylineMapper($container->getFrontendValueFilter()); +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolylineMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] + ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) { - return new Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolylineMapper($container->getFrontendValueFilter()); +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolylineMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] + ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) { - return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolygonMapper($container->getFrontendValueFilter()); +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new Netzmacht\Contao\Leaflet\Mapper\Vector\PolygonMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] + ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) { - return new Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolygonMapper($container->getFrontendValueFilter()); +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new Netzmacht\Contao\Leaflet\Mapper\Vector\MultiPolygonMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] + ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) { - return new Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMapper($container->getFrontendValueFilter()); +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] + ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) { - return new Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMarkerMapper($container->getFrontendValueFilter()); +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new Netzmacht\Contao\Leaflet\Mapper\Vector\CircleMarkerMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] + ); }; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) { - return new Netzmacht\Contao\Leaflet\Mapper\Vector\RectangleMapper($container->getFrontendValueFilter()); +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new Netzmacht\Contao\Leaflet\Mapper\Vector\RectangleMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] + ); }; // Miscellaneous mappers. @@ -131,8 +154,10 @@ $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\UI\PopupMapper' $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Type\ImageIconMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Type\DivIconMapper'; $GLOBALS['LEAFLET_MAPPERS'][] = 'Netzmacht\Contao\Leaflet\Mapper\Style\FixedStyleMapper'; -$GLOBALS['LEAFLET_MAPPERS'][] = function (\Netzmacht\Contao\Leaflet\ServiceContainer $container) { - return new \Netzmacht\Contao\Leaflet\Mapper\UI\MarkerMapper($container->getFrontendValueFilter()); +$GLOBALS['LEAFLET_MAPPERS'][] = function () { + return new \Netzmacht\Contao\Leaflet\Mapper\UI\MarkerMapper( + $GLOBALS['container'][\Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::FRONTEND_VALUE_FILTER] + ); }; /* diff --git a/module/config/event_subscribers.php b/module/config/event_subscribers.php index e432193..346868c 100644 --- a/module/config/event_subscribers.php +++ b/module/config/event_subscribers.php @@ -9,7 +9,7 @@ * */ return array( - 'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber', + $GLOBALS['container']['leaflet.boot.subscriber'], 'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber', - 'Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber', + $GLOBALS['container']['leaflet.subscriber.geo-json'], ); diff --git a/module/config/services.php b/module/config/services.php index fe95276..b3e0dc5 100644 --- a/module/config/services.php +++ b/module/config/services.php @@ -9,15 +9,34 @@ * */ +use Doctrine\Common\Cache\ArrayCache; +use Doctrine\Common\Cache\FilesystemCache; +use Interop\Container\ContainerInterface; +use Netzmacht\Contao\Leaflet\Alias\DefaultAliasFilter; use Netzmacht\Contao\Leaflet\Boot; use Netzmacht\Contao\Leaflet\ContaoAssets; -use Netzmacht\Contao\Leaflet\Frontend\Helper\InsertTagReplacer; +use Netzmacht\Contao\Leaflet\Dca\ControlCallbacks; +use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration; +use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks; +use Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks; +use Netzmacht\Contao\Leaflet\Dca\MapCallbacks; +use Netzmacht\Contao\Leaflet\Dca\Validator; +use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks; +use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices; +use Netzmacht\Contao\Leaflet\Frontend\MapElement; +use Netzmacht\Contao\Leaflet\Frontend\MapModule; use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; -use Netzmacht\Contao\Leaflet\MapService; -use Netzmacht\Contao\Leaflet\ServiceContainer; +use Netzmacht\Contao\Leaflet\MapProvider; +use Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber; +use Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\ExistingAliasFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SlugifyFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\SuffixFilter; +use Netzmacht\Contao\Toolkit\Data\Alias\FilterBasedAliasGenerator; +use Netzmacht\Contao\Toolkit\Data\Alias\Validator\UniqueDatabaseValueValidator; +use Netzmacht\Contao\Toolkit\DependencyInjection\Services; use Netzmacht\JavascriptBuilder\Builder; -use Netzmacht\JavascriptBuilder\Encoder; use Netzmacht\JavascriptBuilder\Encoder\ChainEncoder; use Netzmacht\JavascriptBuilder\Encoder\JavascriptEncoder; use Netzmacht\JavascriptBuilder\Encoder\MultipleObjectsEncoder; @@ -31,40 +50,52 @@ use Symfony\Component\EventDispatcher\EventDispatcher; global $container; /* - * Leaflet map service is a simply api entry to to get the leaflet map from the database. + * Leaflet map provider is a simply api entry to to get the leaflet map from the database. */ -$container['leaflet.map.service'] = $container->share(function ($container) { - return new MapService( - $container['leaflet.definition.mapper'], - $container['leaflet.definition.builder'], - $container['event-dispatcher'], - $container['input'], - $container['leaflet.map.assets'] +$container[LeafletServices::MAP_PROVIDER] = $container->share(function ($container) { + return new MapProvider( + $container[LeafletServices::DEFINITION_MAPPER], + $container[LeafletServices::DEFINITION_BUILDER], + $container[Services::EVENT_DISPATCHER], + $container[Services::INPUT], + $container[LeafletServices::MAP_ASSETS], + $container[LeafletServices::CACHE], + $GLOBALS['LEAFLET_FILTERS'], + \Config::get('debugMode') || \Config::get('displayErrors') ); }); /* * Contao assets handler. Loads Leaflet assets as contao (static) assets. */ -$container['leaflet.map.assets'] = $container->share(function () { - return new ContaoAssets(); +$container[LeafletServices::MAP_ASSETS] = $container->share(function ($container) { + return new ContaoAssets($container[Services::ASSETS_MANAGER]); }); /* * The leaflet boot. */ -$container['leaflet.boot'] = $container->share(function ($container) { - return new Boot($container['event-dispatcher']); +$container[LeafletServices::BOOT] = $container->share(function ($container) { + return new Boot($container[Services::EVENT_DISPATCHER]); +}); + +$container['leaflet.boot.subscriber'] = $container->share(function ($container) { + return new BootSubscriber( + $container[LeafletServices::MAP_ASSETS], + $GLOBALS['LEAFLET_MAPPERS'], + $GLOBALS['LEAFLET_ENCODERS'], + $GLOBALS['LEAFLET_LIBRARIES'] + ); }); /* * The definition mapper. */ -$container['leaflet.definition.mapper'] = $container->share(function ($container) { +$container[LeafletServices::DEFINITION_MAPPER] = $container->share(function ($container) { /** @var Boot $boot */ - $boot = $container['leaflet.boot']; - $mapper = new DefinitionMapper($container['event-dispatcher']); + $boot = $container[LeafletServices::BOOT]; + $mapper = new DefinitionMapper($container[Services::EVENT_DISPATCHER]); return $boot->initializeDefinitionMapper($mapper); }); @@ -73,9 +104,9 @@ $container['leaflet.definition.mapper'] = $container->share(function ($container /* * The local event dispatcher is used for the leaflet javascript encoding system. */ -$container['leaflet.definition.builder.event-dispatcher'] = $container->share(function ($container) { +$container[LeafletServices::DEFINITION_BUILDER_EVENT_DISPATCHER] = $container->share(function ($container) { /** @var Boot $boot */ - $boot = $container['leaflet.boot']; + $boot = $container[LeafletServices::BOOT]; $dispatcher = new EventDispatcher(); return $boot->initializeEventDispatcher($dispatcher); @@ -84,8 +115,8 @@ $container['leaflet.definition.builder.event-dispatcher'] = $container->share(fu /* * The javascript encoder factory being used for building the map javascript. */ -$container['leaflet.definition.builder.encoder-factory'] = function ($container) { - $dispatcher = $container['leaflet.definition.builder.event-dispatcher']; +$container[LeafletServices::DEFINITION_ENCODER_FACTORY] = function ($container) { + $dispatcher = $container[LeafletServices::DEFINITION_BUILDER_EVENT_DISPATCHER]; return function (Output $output) use ($dispatcher) { $encoder = new ChainEncoder(); @@ -101,11 +132,11 @@ $container['leaflet.definition.builder.encoder-factory'] = function ($container) /* * The leaflet builder transforms the definition to javascript. */ -$container['leaflet.definition.builder'] = $container->share(function($container) { +$container[LeafletServices::DEFINITION_BUILDER] = $container->share(function($container) { /** @var Boot $boot */ - $boot = $container['leaflet.boot']; - $dispatcher = $container['leaflet.definition.builder.event-dispatcher']; - $factory = $container['leaflet.definition.builder.encoder-factory']; + $boot = $container[LeafletServices::BOOT]; + $dispatcher = $container[LeafletServices::DEFINITION_BUILDER_EVENT_DISPATCHER]; + $factory = $container[LeafletServices::DEFINITION_ENCODER_FACTORY]; $builder = new Builder($factory); $leaflet = new Leaflet($builder, $dispatcher, array(), JSON_UNESCAPED_SLASHES ^ Flags::BUILD_STACK); @@ -113,10 +144,217 @@ $container['leaflet.definition.builder'] = $container->share(function($container return $boot->initializeLeafletBuilder($leaflet); }); -$container['leaflet.frontend.value-filter'] = $container->share(function($container) { - return new ValueFilter($container['toolkit.insert-tag-replacer']); +$container[LeafletServices::FRONTEND_VALUE_FILTER] = $container->share(function($container) { + return new ValueFilter($container[Services::INSERT_TAG_REPLACER]); }); -$container['leaflet.service-container'] = $container->share(function($container) { - return new ServiceContainer($container); +/** + * Internal used leaflet cache. + * + * @var Cache + */ +$container[LeafletServices::CACHE] = $container->share( + function ($container) { + if ($container[Services::PRODUCTION_MODE]) { + return new FilesystemCache(TL_ROOT . '/system/cache/leaflet'); + } else { + return new ArrayCache(); + } + } +); + +/** + * Leaflet alias generator. + * + * @return \Netzmacht\Contao\Toolkit\Data\Alias\AliasGenerator + */ +$container[LeafletServices::ALIAS_GENERATOR] = $container->share( + function ($container) { + return function ($dataContainerName, $aliasField, $fields) use ($container) { + $filters = [ + new ExistingAliasFilter(), + new SlugifyFilter($fields), + new DefaultAliasFilter($dataContainerName), + new SuffixFilter(), + ]; + + $validator = new UniqueDatabaseValueValidator( + $container[Services::DATABASE_CONNECTION], + $dataContainerName, + $aliasField + ); + + return new FilterBasedAliasGenerator($filters, $validator, $dataContainerName, $aliasField, '_'); + }; + } +); + +/** + * Leaflet alias generator. + * + * @return \Netzmacht\Contao\Toolkit\Data\Alias\AliasGenerator + */ +$container[LeafletServices::PARENT_ALIAS_GENERATOR] = $container->share( + function ($container) { + return function ($dataContainerName, $aliasField, $fields) use ($container) { + $filters = [ + new ExistingAliasFilter(), + new SlugifyFilter($fields), + new DefaultAliasFilter($dataContainerName), + new SuffixFilter(), + ]; + + $validator = new UniqueDatabaseValueValidator( + $container[Services::DATABASE_CONNECTION], + $dataContainerName, + $aliasField, + ['pid'] + ); + + return new FilterBasedAliasGenerator($filters, $validator, $dataContainerName, $aliasField, '_'); + }; + } +); + +/** + * Callback helper class for tl_leaflet_map. + * + * @return MapCallbacks + */ +$container['leaflet.dca.map-callbacks'] = $container->share( + function ($container) { + return new MapCallbacks( + $container[Services::DCA_MANAGER], + $container[Services::DATABASE_CONNECTION] + ); + } +); + +/** + * Callback helper class for tl_leaflet_layer. + * + * @return LayerCallbacks + */ +$container['leaflet.dca.layer-callbacks'] = $container->share( + function ($container) { + return new LayerCallbacks( + $container[Services::DCA_MANAGER], + $container[Services::DATABASE_CONNECTION], + $container[Services::TRANSLATOR], + $GLOBALS['LEAFLET_LAYERS'], + $GLOBALS['LEAFLET_TILE_PROVIDERS'] + ); + } +); + +/** + * Callback helper class for tl_leaflet_control. + * + * @return ControlCallbacks + */ +$container['leaflet.dca.control-callbacks'] = $container->share( + function ($container) { + return new ControlCallbacks( + $container[Services::DCA_MANAGER], + $container[Services::DATABASE_CONNECTION] + ); + } +); + +/** + * Callback helper class for tl_leaflet_control. + * + * @return ControlCallbacks + */ +$container['leaflet.dca.vector-callbacks'] = $container->share( + function ($container) { + return new VectorCallbacks($container[Services::DCA_MANAGER]); + } +); + +/** + * Callback helper class for frontend integration. + * + * @return FrontendIntegration + */ +$container['leaflet.dca.frontend-integration'] = $container->share( + function ($container) { + return new FrontendIntegration( + $container[Services::TRANSLATOR] + ); + } +); + +/** + * Common callback helpers. + * + * @return LeafletCallbacks + */ +$container['leaflet.dca.common'] = $container->share( + function ($container) { + return new LeafletCallbacks( + $container[Services::FILE_SYSTEM] + ); + } +); + +/** + * Validator helper class. + * + * @return Validator + */ +$container['leaflet.dca.validator'] = $container->share( + function ($container) { + return new Validator( + $container[Services::TRANSLATOR] + ); + } +); + +/** + * Component factory for content element. + * + * @param ContentModel $model Content model. + * @param string $column Template section. + * @param ContainerInterface $container Container. + * + * @return MapElement + */ +$container[Services::CONTENT_ELEMENTS_MAP]['leaflet'] = function ($model, $column, ContainerInterface $container) { + return new MapElement( + $model, + $container->get(Services::TEMPLATE_FACTORY), + $container->get(Services::TRANSLATOR), + $container->get(LeafletServices::MAP_PROVIDER), + $container->get(Services::INPUT), + $container->get(Services::CONFIG), + $column + ); +}; + +/** + * Component factory for frontend module. + * + * @param ModuleModel $model Module model. + * @param string $column Template section. + * @param ContainerInterface $container Container. + * + * @return MapModule + */ +$container[Services::MODULES_MAP]['leaflet'] = function ($model, $column, ContainerInterface $container) { + return new MapModule( + $model, + $container->get(Services::TEMPLATE_FACTORY), + $container->get(Services::TRANSLATOR), + $container->get(LeafletServices::MAP_PROVIDER), + $container->get(Services::INPUT), + $container->get(Services::CONFIG), + $column + ); +}; + +$container['leaflet.subscriber.geo-json'] = $container->share(function () { + return new GeoJsonSubscriber( + $GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES'] + ); }); diff --git a/module/dca/tl_content.php b/module/dca/tl_content.php index ddf8d6c..19af579 100644 --- a/module/dca/tl_content.php +++ b/module/dca/tl_content.php @@ -22,9 +22,9 @@ $GLOBALS['TL_DCA']['tl_content']['fields']['leaflet_map'] = array( 'label' => &$GLOBALS['TL_LANG']['tl_content']['leaflet_map'], 'inputType' => 'select', 'exclude' => true, - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\FrontendIntegration', 'getMaps'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\FrontendIntegration::callback('getMaps'), 'wizard' => array( - array('Netzmacht\Contao\Leaflet\Dca\FrontendIntegration', 'getEditMapLink'), + \Netzmacht\Contao\Leaflet\Dca\FrontendIntegration::callback('getEditMapLink'), ), 'eval' => array( 'tl_class' => 'w50 wizard', @@ -70,7 +70,7 @@ $GLOBALS['TL_DCA']['tl_content']['fields']['leaflet_template'] = array( 'label' => &$GLOBALS['TL_LANG']['tl_content']['leaflet_template'], 'inputType' => 'select', 'exclude' => true, - 'options_callback' => \Netzmacht\Contao\Toolkit\Dca::createGetTemplatesCallback('leaflet_map_js'), + 'options_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::getTemplates('leaflet_map_js'), 'eval' => array( 'tl_class' => 'w50', 'chosen' => true, diff --git a/module/dca/tl_leaflet_control.php b/module/dca/tl_leaflet_control.php index 3bc5278..6267f7d 100644 --- a/module/dca/tl_leaflet_control.php +++ b/module/dca/tl_leaflet_control.php @@ -22,9 +22,11 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array ( 'id' => 'primary', 'pid' => 'index', - 'alias' => 'unique', ) - ) + ), + 'onsubmit_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), // List @@ -38,7 +40,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'flag' => 1, 'sorting' => 2, 'panelLayout' => 'filter,sort;search,limit', - 'child_record_callback' => array('Netzmacht\Contao\Leaflet\Dca\Control', 'generateRow'), + 'child_record_callback' => \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('generateRow'), ), 'label' => array ( @@ -81,10 +83,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['toggle'], 'icon' => 'visible.gif', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', - 'button_callback' => \Netzmacht\Contao\Toolkit\Dca::createToggleIconCallback( - 'tl_leaflet_control', - 'active' - ) + 'button_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::stateButton('tl_leaflet_control', 'active') ), 'show' => array ( @@ -166,10 +165,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'inputType' => 'text', 'search' => true, 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\Helper::createGenerateAliasCallback('tl_leaflet_control', 'title'), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::aliasGenerator( + 'tl_leaflet_control', + 'alias', + ['title'], + \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::PARENT_ALIAS_GENERATOR + ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), - 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), - 'sql' => "varchar(255) NOT NULL default ''" + 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'nullIfEmpty' => true), + 'sql' => "varchar(255) NULL" ), 'type' => array ( @@ -209,7 +214,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'inputType' => 'checkbox', 'filter' => true, 'eval' => array('tl_class' => 'w50'), - 'sql' => "char(1) NOT NULL default ''" + 'sql' => "char(1) NOT NULL default ''", + 'save_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'zoomInText' => array ( @@ -267,10 +275,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'exclude' => true, 'inputType' => 'multiColumnWizard', 'load_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Control', 'loadLayerRelations'), + \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('loadLayerRelations'), ), 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Control', 'saveLayerRelations'), + \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('saveLayerRelations'), ), 'eval' => array ( @@ -282,7 +290,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['layer'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'getLayers'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('getLayers'), 'eval' => array( 'style' => 'width: 300px', 'chosen' => true, @@ -369,7 +377,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_control']['zoomControl'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Control', 'getZoomControls'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\ControlCallbacks::callback('getZoomControls'), 'reference' => &$GLOBALS['TL_LANG']['tl_leaflet_control'], 'eval' => array( 'mandatory' => false, diff --git a/module/dca/tl_leaflet_icon.php b/module/dca/tl_leaflet_icon.php index 677888b..0f9af6b 100644 --- a/module/dca/tl_leaflet_icon.php +++ b/module/dca/tl_leaflet_icon.php @@ -21,7 +21,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array 'id' => 'primary', 'alias' => 'unique', ) - ) + ), + 'onsubmit_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'list' => array @@ -96,7 +99,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_icon']['toggle'], 'icon' => 'visible.gif', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', - 'button_callback' => \Netzmacht\Contao\Toolkit\Dca::createToggleIconCallback( + 'button_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::stateButton( 'tl_leaflet_icon', 'active' ) @@ -174,10 +177,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\Helper::createGenerateAliasCallback('tl_leaflet_icon', 'title'), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::aliasGenerator( + 'tl_leaflet_icon', + 'alias', + ['title'], + \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR + ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), - 'sql' => "varchar(255) NOT NULL default ''" + 'sql' => "varchar(255) NULL" ), 'type' => array ( @@ -201,7 +210,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array 'exclude' => true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50'), - 'sql' => "char(1) NOT NULL default ''" + 'sql' => "char(1) NOT NULL default ''", + 'save_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'iconImage' => array ( @@ -265,7 +277,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates') ), 'eval' => array( 'maxlength' => 255, @@ -280,7 +292,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates') ), 'eval' => array( 'maxlength' => 255, @@ -295,7 +307,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates') ), 'eval' => array( 'maxlength' => 255, diff --git a/module/dca/tl_leaflet_layer.php b/module/dca/tl_leaflet_layer.php index ed526fd..7663298 100644 --- a/module/dca/tl_leaflet_layer.php +++ b/module/dca/tl_leaflet_layer.php @@ -16,7 +16,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'enableVersioning' => true, 'ctable' => array('tl_leaflet_vector', 'tl_leaflet_marker'), 'ondelete_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Layer', 'deleteRelations'), + \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('deleteRelations'), ), 'sql' => array ( @@ -31,7 +31,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array function() { \Controller::loadLanguageFile('leaflet'); } - ) + ), + 'onsubmit_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'list' => array ( @@ -42,13 +45,13 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'flag' => 1, 'icon' => 'system/modules/leaflet/assets/img/layers.png', 'panelLayout' => 'filter;search,limit', - 'paste_button_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'getPasteButtons'), + 'paste_button_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getPasteButtons'), ), 'label' => array ( 'fields' => array('title'), 'format' => '%s', - 'label_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'generateRow') + 'label_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('generateRow') ), 'global_operations' => array ( @@ -88,14 +91,14 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['markers'], 'href' => 'table=tl_leaflet_marker', 'icon' => 'edit.gif', - 'button_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'generateMarkersButton'), + 'button_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('generateMarkersButton') ), 'vectors' => array ( 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['vectors'], 'href' => 'table=tl_leaflet_vector', 'icon' => 'edit.gif', - 'button_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'generateVectorsButton'), + 'button_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('generateVectorsButton'), ), 'edit' => array ( @@ -128,10 +131,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['toggle'], 'icon' => 'visible.gif', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', - 'button_callback' => \Netzmacht\Contao\Toolkit\Dca::createToggleIconCallback( + 'button_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::stateButton( 'tl_leaflet_layer', 'active' - ) + ), ), 'show' => array ( @@ -150,7 +153,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'default' => array( 'title' => array('title', 'alias', 'type'), 'config' => array(), - 'expert' => array(':hide'), + 'expert' => array(':hide', 'cache'), 'active' => array('active'), ), 'markers extends default' => array( @@ -224,7 +227,8 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array ), 'metasubpalettes' => array( - 'spiderfyOnMaxZoom' => array('spiderfyDistanceMultiplier') + 'spiderfyOnMaxZoom' => array('spiderfyDistanceMultiplier'), + 'cache' => array('cacheLifeTime') ), 'fields' => array @@ -261,10 +265,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'inputType' => 'text', 'search' => true, 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\Helper::createGenerateAliasCallback('tl_leaflet_layer', 'title'), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::aliasGenerator( + 'tl_leaflet_layer', + 'alias', + ['title'], + \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR + ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), - 'sql' => "varchar(255) NOT NULL default ''" + 'sql' => "varchar(255) NULL" ), 'type' => array ( @@ -291,7 +301,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'inputType' => 'checkbox', 'filter' => true, 'eval' => array('tl_class' => 'w50'), - 'sql' => "char(1) NOT NULL default ''" + 'sql' => "char(1) NOT NULL default ''", + 'save_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'tile_provider' => array( 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['tile_provider'], @@ -317,7 +330,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'submitOnChange' => true, 'chosen' => false, ), - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'getVariants'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getVariants'), 'sql' => "varchar(32) NOT NULL default ''" ), 'tile_provider_key' => array @@ -366,7 +379,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['reference'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'getLayers'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getLayers'), 'eval' => array( 'mandatory' => true, 'tl_class' => 'w50', @@ -462,7 +475,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['disableClusteringAtZoom'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getZoomLevels'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'), 'default' => '', 'eval' => array( 'maxlength' => 4, @@ -542,7 +555,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['boundsMode'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'getBoundsModes'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\LayerCallbacks::callback('getBoundsModes'), 'eval' => array('tl_class' => 'w50', 'includeBlankOption' => true), 'sql' => "varchar(6) NOT NULL default ''" ), @@ -560,7 +573,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['minZoom'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getZoomLevels'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'), 'eval' => array( 'maxlength' => 4, 'rgxp' => 'digit', @@ -575,7 +588,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['maxZoom'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getZoomLevels'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'), 'eval' => array( 'maxlength' => 4, 'rgxp' => 'digit', @@ -590,7 +603,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['maxNativeZoom'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getZoomLevels'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'), 'eval' => array( 'maxlength' => 4, 'rgxp' => 'digit', @@ -751,5 +764,23 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array ), 'sql' => "mediumblob NULL" ), + 'cache' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['cache'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'default' => false, + 'eval' => array('tl_class' => 'w50 m12', 'submitOnChange' => true), + 'sql' => "char(1) NOT NULL default ''" + ), + 'cacheLifeTime' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_layer']['cacheLifeTime'], + 'exclude' => true, + 'inputType' => 'text', + 'default' => null, + 'eval' => array('maxlength' => 5, 'rgxp' => 'digit', 'tl_class' => 'w50', 'nullIfEmpty' => true), + 'sql' => "int(9) NOT NULL default '0'" + ), ) ); diff --git a/module/dca/tl_leaflet_map.php b/module/dca/tl_leaflet_map.php index 11430aa..8221698 100644 --- a/module/dca/tl_leaflet_map.php +++ b/module/dca/tl_leaflet_map.php @@ -27,7 +27,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array function() { \Controller::loadLanguageFile('leaflet'); } - ) + ), + 'onsubmit_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'list' => array @@ -114,6 +117,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array ), 'expert' => array( 'options', + 'cache', ) ), ), @@ -135,6 +139,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'locateMaximumAge', 'enableHighAccuracy' ), + 'cache' => array( + 'cacheLifeTime' + ) ), 'fields' => array @@ -163,10 +170,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'inputType' => 'text', 'search' => true, 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\Helper::createGenerateAliasCallback('tl_leaflet_map', 'title'), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::aliasGenerator( + 'tl_leaflet_map', + 'alias', + ['title'], + \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR + ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias') ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), - 'sql' => "varchar(255) NOT NULL default ''" + 'sql' => "varchar(255) NULL" ), 'center' => array ( @@ -174,10 +187,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates'), ), 'wizard' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getGeocoder') + Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder') ), 'eval' => array( 'maxlength' => 255, @@ -192,10 +205,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'exclude' => true, 'inputType' => 'multiColumnWizard', 'load_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Map', 'loadLayerRelations'), + \Netzmacht\Contao\Leaflet\Dca\MapCallbacks::callback('loadLayerRelations'), ), 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Map', 'saveLayerRelations'), + \Netzmacht\Contao\Leaflet\Dca\MapCallbacks::callback('saveLayerRelations'), ), 'eval' => array( 'multiple' => true, @@ -206,7 +219,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['reference'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Layer', 'getLayers'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\MapCallbacks::callback('getLayers'), 'eval' => array( 'mandatory' => true, 'tl_class' => 'w50', @@ -226,7 +239,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['zoom'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getZoomLevels'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'), 'default' => '', 'eval' => array( 'maxlength' => 4, @@ -251,7 +264,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['minZoom'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getZoomLevels'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'), 'eval' => array( 'maxlength' => 4, 'rgxp' => 'digit', @@ -266,7 +279,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['maxZoom'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getZoomLevels'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'), 'eval' => array( 'maxlength' => 4, 'rgxp' => 'digit', @@ -497,7 +510,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['locateMaxZoom'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getZoomLevels'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getZoomLevels'), 'eval' => array( 'maxlength' => 4, 'rgxp' => 'digit', @@ -507,5 +520,23 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array ), 'sql' => "int(4) NULL" ), + 'cache' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['cache'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'default' => false, + 'eval' => array('tl_class' => 'w50 m12', 'submitOnChange' => true), + 'sql' => "char(1) NOT NULL default ''" + ), + 'cacheLifeTime' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['cacheLifeTime'], + 'exclude' => true, + 'inputType' => 'text', + 'default' => null, + 'eval' => array('maxlength' => 5, 'rgxp' => 'digit', 'tl_class' => 'w50', 'nullIfEmpty' => true), + 'sql' => "int(9) NOT NULL default '0'" + ), ), ); diff --git a/module/dca/tl_leaflet_marker.php b/module/dca/tl_leaflet_marker.php index beab48d..4aab6d3 100644 --- a/module/dca/tl_leaflet_marker.php +++ b/module/dca/tl_leaflet_marker.php @@ -28,7 +28,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array function() { \Controller::loadLanguageFile('leaflet'); } - ) + ), + 'onsubmit_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'list' => array @@ -40,7 +43,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'flag' => 1, 'panelLayout' => 'sort,filter;search,limit', 'headerFields' => array('title', 'type'), - 'child_record_callback' => array('Netzmacht\Contao\Leaflet\Dca\Marker', 'generateRow'), + 'child_record_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'generateRow'), ), 'label' => array ( @@ -97,7 +100,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['toggle'], 'icon' => 'visible.gif', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', - 'button_callback' => \Netzmacht\Contao\Toolkit\Dca::createToggleIconCallback( + 'button_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::stateButton( 'tl_leaflet_marker', 'active' ) @@ -172,10 +175,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'inputType' => 'text', 'search' => true, 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\Helper::createGenerateAliasCallback('tl_leaflet_marker', 'title'), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::aliasGenerator( + 'tl_leaflet_marker', + 'alias', + ['title'], + \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR + ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), - 'sql' => "varchar(255) NOT NULL default ''" + 'sql' => "varchar(255) NULL" ), 'coordinates' => array ( @@ -183,14 +192,14 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate'), - array('Netzmacht\Contao\Leaflet\Dca\Marker', 'saveCoordinates') + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates'), + array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'saveCoordinates') ), 'load_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Marker', 'loadCoordinates') + array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'loadCoordinates') ), 'wizard' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getGeocoder') + Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder') ), 'eval' => array( 'maxlength' => 255, @@ -232,7 +241,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'flag' => 12, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50'), - 'sql' => "char(1) NOT NULL default ''" + 'sql' => "char(1) NOT NULL default ''", + 'save_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'tooltip' => array ( @@ -264,7 +276,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['popup'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Marker', 'getPopups'), + 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'getPopups'), 'eval' => array( 'mandatory' => false, 'tl_class' => 'w50', @@ -295,7 +307,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['icon'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Marker', 'getIcons'), + 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'getIcons'), 'eval' => array( 'mandatory' => true, 'tl_class' => 'w50', diff --git a/module/dca/tl_leaflet_popup.php b/module/dca/tl_leaflet_popup.php index d6e35aa..f8d89a7 100644 --- a/module/dca/tl_leaflet_popup.php +++ b/module/dca/tl_leaflet_popup.php @@ -21,7 +21,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array 'id' => 'primary', 'alias' => 'unique', ) - ) + ), + 'onsubmit_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'list' => array @@ -96,7 +99,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_popup']['toggle'], 'icon' => 'visible.gif', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', - 'button_callback' => \Netzmacht\Contao\Toolkit\Dca::createToggleIconCallback( + 'button_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::stateButton( 'tl_leaflet_popup', 'active' ) @@ -160,10 +163,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\Helper::createGenerateAliasCallback('tl_leaflet_popup', 'title'), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::aliasGenerator( + 'tl_leaflet_popup', + 'alias', + ['title'], + \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR + ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), - 'sql' => "varchar(255) NOT NULL default ''" + 'sql' => "varchar(255) NULL" ), 'maxWidth' => array ( @@ -225,7 +234,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates') ), 'eval' => array( 'maxlength' => 255, @@ -284,7 +293,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array 'search' => false, 'flag' => 12, 'eval' => array('tl_class' => 'w50'), - 'sql' => "char(1) NOT NULL default ''" + 'sql' => "char(1) NOT NULL default ''", + 'save_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), ), ); diff --git a/module/dca/tl_leaflet_style.php b/module/dca/tl_leaflet_style.php index f3ea5d9..349017e 100644 --- a/module/dca/tl_leaflet_style.php +++ b/module/dca/tl_leaflet_style.php @@ -20,7 +20,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array 'id' => 'primary', 'alias' => 'unique', ) - ) + ), + 'onsubmit_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'list' => array @@ -95,7 +98,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_style']['toggle'], 'icon' => 'visible.gif', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', - 'button_callback' => \Netzmacht\Contao\Toolkit\Dca::createToggleIconCallback( + 'button_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::stateButton( 'tl_leaflet_style', 'active' ) @@ -153,10 +156,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\Helper::createGenerateAliasCallback('tl_leaflet_style', 'title'), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::aliasGenerator( + 'tl_leaflet_style', + 'alias', + ['title'], + \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR + ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), - 'sql' => "varchar(255) NOT NULL default ''" + 'sql' => "varchar(255) NULL" ), 'type' => array ( @@ -189,7 +198,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array 'exclude' => true, 'inputType' => 'text', 'wizard' => array( - \Netzmacht\Contao\Toolkit\Dca::createColorPickerCallback(), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::colorPicker() ), 'eval' => array( 'tl_class' => 'w50 wizard clr', @@ -230,7 +239,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array 'exclude' => true, 'inputType' => 'text', 'wizard' => array( - \Netzmacht\Contao\Toolkit\Dca::createColorPickerCallback(), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::colorPicker() ), 'eval' => array( 'tl_class' => 'clr w50 wizard', @@ -286,7 +295,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array 'search' => false, 'flag' => 12, 'eval' => array('tl_class' => 'w50'), - 'sql' => "char(1) NOT NULL default ''" + 'sql' => "char(1) NOT NULL default ''", + 'save_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), ), ); diff --git a/module/dca/tl_leaflet_vector.php b/module/dca/tl_leaflet_vector.php index dcf6d4a..09c60e3 100644 --- a/module/dca/tl_leaflet_vector.php +++ b/module/dca/tl_leaflet_vector.php @@ -28,7 +28,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array function() { \Controller::loadLanguageFile('leaflet'); } - ) + ), + 'onsubmit_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'list' => array @@ -40,7 +43,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'flag' => 1, 'panelLayout' => 'sort,filter;search,limit', 'headerFields' => array('title', 'type'), - 'child_record_callback' => array('Netzmacht\Contao\Leaflet\Dca\Vector', 'generateRow'), + 'child_record_callback' => Netzmacht\Contao\Leaflet\Dca\VectorCallbacks::callback('generateRow'), ), 'label' => array ( @@ -104,7 +107,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_vector']['toggle'], 'icon' => 'visible.gif', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', - 'button_callback' => \Netzmacht\Contao\Toolkit\Dca::createToggleIconCallback( + 'button_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::stateButton( 'tl_leaflet_vector', 'active' ) @@ -198,10 +201,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'inputType' => 'text', 'search' => true, 'save_callback' => array( - \Netzmacht\Contao\Leaflet\Dca\Helper::createGenerateAliasCallback('tl_leaflet_vector', 'title'), + \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::aliasGenerator( + 'tl_leaflet_vector', + 'alias', + ['title'], + \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR + ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), - 'sql' => "varchar(255) NOT NULL default ''" + 'sql' => "varchar(255) NULL" ), 'type' => array ( @@ -234,7 +243,10 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'search' => false, 'flag' => 12, 'eval' => array('tl_class' => 'w50'), - 'sql' => "char(1) NOT NULL default ''" + 'sql' => "char(1) NOT NULL default ''", + 'save_callback' => [ + \Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('clearCache'), + ], ), 'addPopup' => array ( @@ -250,7 +262,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_vector']['popup'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Marker', 'getPopups'), + 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\MarkerCallbacks', 'getPopups'), 'eval' => array( 'mandatory' => false, 'tl_class' => 'w50', @@ -273,7 +285,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_vector']['style'], 'exclude' => true, 'inputType' => 'select', - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Vector', 'getStyles'), + 'options_callback' => Netzmacht\Contao\Leaflet\Dca\VectorCallbacks::callback('getStyles'), 'eval' => array( 'mandatory' => false, 'tl_class' => 'w50', @@ -305,15 +317,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'exclude' => true, 'inputType' => 'text', 'save_callback' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'validateCoordinate') + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateCoordinates') ), 'wizard' => array( - array('Netzmacht\Contao\Leaflet\Dca\Leaflet', 'getGeocoder') + Netzmacht\Contao\Leaflet\Dca\LeafletCallbacks::callback('getGeocoder') ), 'eval' => array( 'maxlength' => 255, 'tl_class' => 'long clr', 'nullIfEmpty' => true, + 'mandatory' => true, ), 'sql' => "varchar(255) NULL" ), @@ -332,6 +345,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'inputType' => 'textarea', 'search' => false, 'eval' => array('mandatory' => true, 'alwaysSave' => true), + 'save_callback' => array( + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateMultipleCoordinates') + ), 'sql' => "longblob NULL" ), 'multiData' => array @@ -354,6 +370,9 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array ) ) ), + 'save_callback' => array( + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateMultipleCoordinateSets') + ), 'sql' => "longblob NULL" ), 'bounds' => array @@ -361,8 +380,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array 'label' => &$GLOBALS['TL_LANG']['tl_leaflet_vector']['bounds'], 'exclude' => true, 'inputType' => 'text', - 'save_callback' => array( - ), + 'save_callback' => array(), 'eval' => array( 'maxlength' => 255, 'multiple'=>true, diff --git a/module/dca/tl_module.php b/module/dca/tl_module.php index 3b64b03..19c9a45 100644 --- a/module/dca/tl_module.php +++ b/module/dca/tl_module.php @@ -22,9 +22,9 @@ $GLOBALS['TL_DCA']['tl_module']['fields']['leaflet_map'] = array( 'label' => &$GLOBALS['TL_LANG']['tl_module']['leaflet_map'], 'inputType' => 'select', 'exclude' => true, - 'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\FrontendIntegration', 'getMaps'), + 'options_callback' => \Netzmacht\Contao\Leaflet\Dca\FrontendIntegration::callback('getMaps'), 'wizard' => array( - array('Netzmacht\Contao\Leaflet\Dca\FrontendIntegration', 'getEditMapLink'), + \Netzmacht\Contao\Leaflet\Dca\FrontendIntegration::callback('getEditMapLink'), ), 'eval' => array( 'tl_class' => 'w50 wizard', @@ -70,7 +70,7 @@ $GLOBALS['TL_DCA']['tl_module']['fields']['leaflet_template'] = array( 'label' => &$GLOBALS['TL_LANG']['tl_module']['leaflet_template'], 'inputType' => 'select', 'exclude' => true, - 'options_callback' => \Netzmacht\Contao\Toolkit\Dca::createGetTemplatesCallback('leaflet_map_js'), + 'options_callback' => \Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory::getTemplates('leaflet_map_js'), 'eval' => array( 'tl_class' => 'w50', 'chosen' => true, diff --git a/module/languages/de/leaflet.php b/module/languages/de/leaflet.php index 1a77ea6..428fcc3 100644 --- a/module/languages/de/leaflet.php +++ b/module/languages/de/leaflet.php @@ -8,10 +8,9 @@ * * @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL * - * last-updated: 2015-02-11T08:57:52+01:00 + * last-updated: 2016-10-04T13:07:35+02:00 */ - $GLOBALS['TL_LANG']['leaflet']['applyPosition'] = 'Position anwenden'; $GLOBALS['TL_LANG']['leaflet_control']['attribution']['0'] = 'Copyright-Leiste'; $GLOBALS['TL_LANG']['leaflet_control']['fullscreen']['0'] = 'Fullscreen-Kontrollelement'; @@ -21,15 +20,21 @@ $GLOBALS['TL_LANG']['leaflet_control']['layers']['1'] = 'Das Layer-Kontrol $GLOBALS['TL_LANG']['leaflet_control']['loading']['0'] = 'Lade-Indikator'; $GLOBALS['TL_LANG']['leaflet_control']['loading']['1'] = 'Leaflet.loading ist ein einfacher Ladeindikator als Kontrollelement. Für weitere Details lesen Sie den Artikel in der Plugin-Dokumentation.'; $GLOBALS['TL_LANG']['leaflet_control']['scale']['0'] = 'Maßstabs-Kontrollelement'; +$GLOBALS['TL_LANG']['leaflet_control']['scale']['1'] = 'Einfaches Maßstabs-Kontrollelement, das den aktuellen Maßstab der Kartenmitte anzeigt. Lesen Sie für mehr Details die Leaflet-Dokumentation.'; $GLOBALS['TL_LANG']['leaflet_control']['zoom']['0'] = 'Zoom-Kontrollelement'; $GLOBALS['TL_LANG']['leaflet_control']['zoom']['1'] = 'Diese Komponente ermöglicht eine Kontrolle des Zoomverhaltens. Für weitere Details lesen Sie die Leaflet-Documentation.'; $GLOBALS['TL_LANG']['leaflet_layer']['group']['0'] = 'Layer-Gruppe'; +$GLOBALS['TL_LANG']['leaflet_layer']['group']['1'] = 'Layergruppen, die aus mehreren unterschiedlichen Layern zusammengesetzt sind. Lesen Sie für die Details die Leaflet-Dokumentation. '; $GLOBALS['TL_LANG']['leaflet_layer']['markercluster']['0'] = 'Marker-Cluster'; $GLOBALS['TL_LANG']['leaflet_layer']['markercluster']['1'] = 'Markercluster-Layer, der auf einem based on Leaflet.markercluster This software ships with an visible attribution to netzmacht creative in the map attribution control. If you want to remove this attribution, you have to purchase a commercial license. - For more details visit the project website. + For more details visit the project website.

>

- netzmacht creative
+ netzmacht
David Molineus

@@ -51,12 +51,12 @@

There are many ways you can con­tribute. Translations, docu­mentation or code contributions are welcome.

-

Contribute

+

Contribute

Fund a feature

Contao Leaflet is not feature complete. Have a look at the list of planned feature and support them.

-

Planned features

+

Planned features

@@ -64,12 +64,12 @@

Individual development

If you have special requirements you can hire me for an individual development.

-

Contact

+

Contact

Commercial license

If you want to remove the attribution in the attribution control, you can purchase a commercial license.

-

Purchase license

+

Purchase license

diff --git a/module/templates/be_leaflet_geocode.html5 b/module/templates/be_leaflet_geocode.html5 index ecdd8cd..842d1f5 100644 --- a/module/templates/be_leaflet_geocode.html5 +++ b/module/templates/be_leaflet_geocode.html5 @@ -4,7 +4,7 @@ -
+
'; ?> diff --git a/module/templates/mod_leaflet_map.html5 b/module/templates/mod_leaflet_map.html5 index e88bc5b..e0138dd 100644 --- a/module/templates/mod_leaflet_map.html5 +++ b/module/templates/mod_leaflet_map.html5 @@ -6,12 +6,7 @@
-map): ?> - - - +' . $this->javascript .''; ?> diff --git a/package.json b/package.json new file mode 100644 index 0000000..96a0656 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "contao-leaflet-maps", + "version": "1.0.0", + "description": "Leaflet maps for Contao", + "main": "gulpfile.js", + "dependencies": { + "del": "^2.2.2", + "gulp": "^3.9.1", + "gulp-concat": "^2.6.0", + "gulp-uglify": "^2.0.0" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/netzmacht/contao-leaflet-maps.git" + }, + "keywords": [ + "Contao", + "Leaflet", + "Maps" + ], + "author": "netzmacht David Molineus", + "license": "LGPL-3.0", + "bugs": { + "url": "https://github.com/netzmacht/contao-leaflet-maps/issues" + }, + "homepage": "https://github.com/netzmacht/contao-leaflet-maps#readme" +} diff --git a/src/Netzmacht/Contao/Leaflet/Alias/DefaultAliasFilter.php b/src/Netzmacht/Contao/Leaflet/Alias/DefaultAliasFilter.php new file mode 100644 index 0000000..3fe9e70 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Alias/DefaultAliasFilter.php @@ -0,0 +1,61 @@ + + * @copyright 2016 netzmacht David Molineus. All rights reserved. + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Alias; + +use Netzmacht\Contao\Toolkit\Data\Alias\Filter\AbstractFilter; + +/** + * Class DefaultAliasFilter creates an prefix of the alias. + * + * @package Netzmacht\Contao\Leaflet\Alias + */ +class DefaultAliasFilter extends AbstractFilter +{ + /** + * Alias prefix. + * + * @var string + */ + private $prefix; + + /** + * DefaultAliasFilter constructor. + * + * @param string $dataContainerName Data container name. + * @param int $combine Combine strategy. + */ + public function __construct($dataContainerName, $combine = self::COMBINE_REPLACE) + { + parent::__construct(true, $combine); + + $this->prefix = str_replace('tl_leaflet_', '', $dataContainerName); + } + + /** + * {@inheritdoc} + */ + public function repeatUntilValid() + { + return false; + } + + /** + * {@inheritdoc} + */ + public function apply($model, $value, $separator) + { + if (!$value) { + return $this->prefix . $separator . $model->id; + } + + return $value; + } +} diff --git a/src/Netzmacht/Contao/Leaflet/ContaoAssets.php b/src/Netzmacht/Contao/Leaflet/ContaoAssets.php index bb46e29..4827f7a 100644 --- a/src/Netzmacht/Contao/Leaflet/ContaoAssets.php +++ b/src/Netzmacht/Contao/Leaflet/ContaoAssets.php @@ -11,6 +11,7 @@ namespace Netzmacht\Contao\Leaflet; +use Netzmacht\Contao\Toolkit\View\Assets\AssetsManager; use Netzmacht\LeafletPHP\Assets; /** @@ -27,6 +28,34 @@ class ContaoAssets implements Assets */ private $map; + /** + * Assets manager. + * + * @var AssetsManager + */ + private $assetsManager; + + /** + * Cached assets. + * + * @var array + */ + private $cache = [ + 'stylesheets' => [], + 'javascripts' => [], + 'map' => [] + ]; + + /** + * ContaoAssets constructor. + * + * @param AssetsManager $assetsManager Contao assets manager. + */ + public function __construct(AssetsManager $assetsManager) + { + $this->assetsManager = $assetsManager; + } + /** * {@inheritdoc} * @@ -34,20 +63,16 @@ class ContaoAssets implements Assets */ public function addJavascript($script, $type = self::TYPE_SOURCE) { + $this->cache['javascripts'][] = [$script, $type]; + switch ($type) { case static::TYPE_SOURCE: $GLOBALS['TL_HEAD'][] = sprintf('', $script); break; case static::TYPE_FILE: - if (!\Config::get('debugMode') && TL_MODE === 'FE') { - $script .= '|static'; - } - - // no break - default: - $GLOBALS['TL_JAVASCRIPT'][] = $script; + $this->assetsManager->addJavascript($script); } } @@ -58,19 +83,16 @@ class ContaoAssets implements Assets */ public function addStylesheet($stylesheet, $type = self::TYPE_FILE) { + $this->cache['stylesheets'][] = [$stylesheet, $type]; + switch ($type) { case static::TYPE_SOURCE: $GLOBALS['TL_HEAD'][] = sprintf('', $stylesheet); break; case static::TYPE_FILE: - if (!\Config::get('debugMode')) { - $stylesheet .= '|all|static'; - } - // no break - default: - $GLOBALS['TL_CSS'][] = $stylesheet; + $this->assetsManager->addStylesheet($stylesheet); } } @@ -87,8 +109,39 @@ class ContaoAssets implements Assets */ public function setMap($map) { - $this->map = $map; + $this->cache['map'] = $map; + $this->map = $map; return $this; } + + /** + * Export to array. + * + * @return array + */ + public function toArray() + { + return $this->cache; + } + + /** + * From array. + * + * @param array $cache Cache. + * + * @return void + */ + public function fromArray(array $cache) + { + foreach ($cache['javascripts'] as $javascript) { + $this->addJavascript($javascript[0], $javascript[1]); + } + + foreach ($cache['stylesheets'] as $stylesheet) { + $this->addStylesheet($stylesheet[0], $stylesheet[1]); + } + + $this->map = $cache['map']; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Control.php b/src/Netzmacht/Contao/Leaflet/Dca/ControlCallbacks.php similarity index 81% rename from src/Netzmacht/Contao/Leaflet/Dca/Control.php rename to src/Netzmacht/Contao/Leaflet/Dca/ControlCallbacks.php index 28da0b3..a4c52cf 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Control.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/ControlCallbacks.php @@ -11,8 +11,9 @@ namespace Netzmacht\Contao\Leaflet\Dca; +use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks; +use Netzmacht\Contao\Toolkit\Dca\Manager; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; -use Netzmacht\Contao\Toolkit\ServiceContainerTrait; use Netzmacht\Contao\Leaflet\Model\ControlModel; use Netzmacht\Contao\Leaflet\Model\LayerModel; @@ -21,9 +22,21 @@ use Netzmacht\Contao\Leaflet\Model\LayerModel; * * @package Netzmacht\Contao\Leaflet\Dca */ -class Control +class ControlCallbacks extends Callbacks { - use ServiceContainerTrait; + /** + * Name of the data container. + * + * @var string + */ + protected static $name = 'tl_leaflet_control'; + + /** + * Helper service name. + * + * @var string + */ + protected static $serviceName = 'leaflet.dca.control-callbacks'; /** * The database connection. @@ -34,10 +47,15 @@ class Control /** * Construct. + * + * @param Manager $manager Data container manager. + * @param \Database $database Database connection. */ - public function __construct() + public function __construct(Manager $manager, \Database $database) { - $this->database = static::getServiceContainer()->getDatabaseConnection(); + parent::__construct($manager); + + $this->database = $database; } /** @@ -63,16 +81,11 @@ class Control */ public function getLayers() { - $options = array(); - $collection = LayerModel::findBy('pid', '0', array('order' => 'title')); + $collection = LayerModel::findAll(); - if ($collection) { - foreach ($collection as $model) { - $options[$model->id] = $model->title; - } - } - - return $options; + return OptionsBuilder::fromCollection($collection, 'title') + ->asTree() + ->getOptions(); } /** @@ -84,7 +97,7 @@ class Control { $collection = ControlModel::findBy('type', 'zoom', array('order' => 'title')); - return OptionsBuilder::fromCollection($collection, 'id', 'title')->getOptions(); + return OptionsBuilder::fromCollection($collection, 'title')->getOptions(); } /** diff --git a/src/Netzmacht/Contao/Leaflet/Dca/FrontendIntegration.php b/src/Netzmacht/Contao/Leaflet/Dca/FrontendIntegration.php index 1bf1ded..368b1c5 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/FrontendIntegration.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/FrontendIntegration.php @@ -11,6 +11,8 @@ namespace Netzmacht\Contao\Leaflet\Dca; +use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator; +use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; use Netzmacht\Contao\Leaflet\Model\MapModel; @@ -21,6 +23,35 @@ use Netzmacht\Contao\Leaflet\Model\MapModel; */ class FrontendIntegration { + /** + * Translator. + * + * @var Translator + */ + private $translator; + + /** + * FrontendIntegration constructor. + * + * @param Translator $translator Translator. + */ + public function __construct(Translator $translator) + { + $this->translator = $translator; + } + + /** + * Generate the callback definition. + * + * @param string $methodName Callback method name. + * + * @return callable + */ + public static function callback($methodName) + { + return CallbackFactory::service('leaflet.dca.frontend-integration', $methodName); + } + /** * Get all leaflet maps. * @@ -30,7 +61,7 @@ class FrontendIntegration { $collection = MapModel::findAll(); - return OptionsBuilder::fromCollection($collection, 'id', 'title')->getOptions(); + return OptionsBuilder::fromCollection($collection, 'title')->getOptions(); } /** @@ -39,8 +70,6 @@ class FrontendIntegration * @param \DataContainer $dataContainer The dataContainer driver. * * @return string - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function getEditMapLink($dataContainer) { @@ -53,23 +82,23 @@ class FrontendIntegration return sprintf( '%s', - 'contao/main.php?do=leaflet&table=tl_leaflet_map&act=edit&id=', + 'contao/main.php?do=leaflet_map&table=tl_leaflet_map&act=edit&id=', $dataContainer->value, \RequestToken::get(), sprintf( $pattern, - specialchars(sprintf($GLOBALS['TL_LANG']['tl_content']['editalias'][1], $dataContainer->value)), + specialchars($this->translator->translate('editalias.1', 'tl_content', [$dataContainer->value])), specialchars( str_replace( "'", "\\'", - sprintf($GLOBALS['TL_LANG']['tl_content']['editalias'][1], $dataContainer->value) + sprintf($this->translator->translate('editalias.1', 'tl_content', [$dataContainer->value])) ) ) ), \Image::getHtml( 'alias.gif', - $GLOBALS['TL_LANG']['tl_content']['editalias'][0], + $this->translator->translate('editalias.0', 'tl_content', [$dataContainer->value]), 'style="vertical-align:top"' ) ); diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Helper.php b/src/Netzmacht/Contao/Leaflet/Dca/Helper.php deleted file mode 100644 index 5d933e4..0000000 --- a/src/Netzmacht/Contao/Leaflet/Dca/Helper.php +++ /dev/null @@ -1,43 +0,0 @@ - - * @copyright 2015 netzmacht creative David Molineus - * @license LGPL 3.0 - * @filesource - * - */ - -namespace Netzmacht\Contao\Leaflet\Dca; - -use Netzmacht\Contao\Toolkit\Dca; -use Netzmacht\Contao\Toolkit\Dca\Callback\GenerateAliasCallback; - -/** - * Helper class for dca functions. - * - * @package Netzmacht\Contao\Leaflet\Dca - */ -class Helper -{ - /** - * Generate an alias callback which creates a valid javascript var name. - * - * @param string $table The table. - * @param string $column The value column. - * - * @return GenerateAliasCallback - */ - public static function createGenerateAliasCallback($table, $column) - { - $callback = Dca::createGenerateAliasCallback($table, $column); - $callback->getGenerator()->addFilter( - function ($value) { - return str_replace('-', '_', $value); - } - ); - - return $callback; - } -} diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Layer.php b/src/Netzmacht/Contao/Leaflet/Dca/LayerCallbacks.php similarity index 81% rename from src/Netzmacht/Contao/Leaflet/Dca/Layer.php rename to src/Netzmacht/Contao/Leaflet/Dca/LayerCallbacks.php index 0ce8782..54006df 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Layer.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/LayerCallbacks.php @@ -11,8 +11,10 @@ namespace Netzmacht\Contao\Leaflet\Dca; +use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator; +use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks; +use Netzmacht\Contao\Toolkit\Dca\Manager; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; -use Netzmacht\Contao\Toolkit\ServiceContainerTrait; use Netzmacht\Contao\Leaflet\Model\LayerModel; /** @@ -20,9 +22,21 @@ use Netzmacht\Contao\Leaflet\Model\LayerModel; * * @package Netzmacht\Contao\Leaflet\Dca */ -class Layer +class LayerCallbacks extends Callbacks { - use ServiceContainerTrait; + /** + * Name of the data container. + * + * @var string + */ + protected static $name = 'tl_leaflet_layer'; + + /** + * Helper service name. + * + * @var string + */ + protected static $serviceName = 'leaflet.dca.layer-callbacks'; /** * Layers definition. @@ -37,19 +51,46 @@ class Layer * @var \Database */ private $database; + + /** + * Tile providers configuration. + * + * @var array + */ + private $tileProviders; + + /** + * Translator. + * + * @var Translator + */ + private $translator; /** * Construct. * - * @SuppressWarnings(PHPMD.Superglobals) + * @param Manager $manager Data container manager. + * @param \Database $database Database connection. + * @param Translator $translator Translator. + * @param array $layers Leaflet layer configuration. + * @param array $tileProviders Tile providers. */ - public function __construct() - { - $this->layers = &$GLOBALS['LEAFLET_LAYERS']; - $this->database = static::getServiceContainer()->getDatabaseConnection(); + public function __construct( + Manager $manager, + \Database $database, + Translator $translator, + array $layers, + array $tileProviders + ) { + parent::__construct($manager); + + $this->database = $database; + $this->layers = $layers; + $this->tileProviders = $tileProviders; \Controller::loadLanguageFile('leaflet_layer'); + $this->translator = $translator; } /** @@ -58,16 +99,14 @@ class Layer * @param \DataContainer $dataContainer The dataContainer driver. * * @return array - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function getVariants($dataContainer) { if ($dataContainer->activeRecord && $dataContainer->activeRecord->tile_provider - && !empty($GLOBALS['LEAFLET_TILE_PROVIDERS'][$dataContainer->activeRecord->tile_provider]['variants']) + && !empty($this->tileProviders[$dataContainer->activeRecord->tile_provider]['variants']) ) { - return $GLOBALS['LEAFLET_TILE_PROVIDERS'][$dataContainer->activeRecord->tile_provider]['variants']; + return $this->tileProviders[$dataContainer->activeRecord->tile_provider]['variants']; } return array(); @@ -80,15 +119,9 @@ class Layer * @param string $label Current row label. * * @return string - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function generateRow($row, $label) { - $alt = empty($GLOBALS['TL_LANG']['leaflet_layer'][$row['type']][0]) - ? $row['type'] - : $GLOBALS['TL_LANG']['leaflet_layer'][$row['type']][0]; - if (!empty($this->layers[$row['type']]['icon'])) { $src = $this->layers[$row['type']]['icon']; @@ -100,6 +133,7 @@ class Layer $src = preg_replace('/(\.[^\.]+)$/', '_1$1', $src); } + $alt = $this->getFormatter()->formatValue('type', $row['type']); $icon = \Image::getHtml($src, $alt, sprintf('title="%s"', strip_tags($alt))); if (!empty($this->layers[$row['type']]['label'])) { @@ -148,7 +182,6 @@ class Layer * * @return string * - * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function getPasteButtons($dataContainer, $row, $table, $whatever, $children) @@ -161,10 +194,10 @@ class Layer $buffer = sprintf( '%s ', $pasteAfterUrl, - specialchars(sprintf($GLOBALS['TL_LANG'][$table]['pasteafter'][1], $row['id'])), + specialchars($this->translator->translate('pasteafter.1', $table, [$row['id']])), \Image::getHtml( 'pasteafter.gif', - sprintf($GLOBALS['TL_LANG'][$table]['pasteafter'][1], $row['id']) + $this->translator->translate('pasteafter.1', $table, [$row['id']]) ) ); @@ -181,10 +214,10 @@ class Layer $buffer .= sprintf( '%s ', $pasteIntoUrl, - specialchars(sprintf($GLOBALS['TL_LANG'][$table]['pasteinto'][1], $row['id'])), + specialchars($this->translator->translate('pasteinto.1', $table, [$row['id']])), \Image::getHtml( 'pasteinto.gif', - sprintf($GLOBALS['TL_LANG'][$table]['pasteinto'][1], $row['id']) + $this->translator->translate('pasteinto.1', $table, [$row['id']]) ) ); @@ -237,22 +270,6 @@ class Layer return $this->generateButton($row, $href, $label, $title, $icon, $attributes); } - /** - * Get all layers except of the current layer. - * - * @param \DataContainer $dataContainer The dataContainer driver. - * - * @return array - */ - public function getLayers($dataContainer) - { - $collection = LayerModel::findBy('id !', $dataContainer->id); - - return OptionsBuilder::fromCollection($collection, 'id', 'title') - ->asTree() - ->getOptions(); - } - /** * Delete the relations when the layer is deleted. * @@ -326,6 +343,22 @@ class Layer return $options; } + /** + * Get all layers except of the current layer. + * + * @param \DataContainer $dataContainer The dataContainer driver. + * + * @return array + */ + public function getLayers($dataContainer) + { + $collection = LayerModel::findBy('id !', $dataContainer->id); + + return OptionsBuilder::fromCollection($collection, 'title') + ->asTree() + ->getOptions(); + } + /** * Generate a button. * diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Leaflet.php b/src/Netzmacht/Contao/Leaflet/Dca/LeafletCallbacks.php similarity index 66% rename from src/Netzmacht/Contao/Leaflet/Dca/Leaflet.php rename to src/Netzmacht/Contao/Leaflet/Dca/LeafletCallbacks.php index 730ffc1..e9c5564 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Leaflet.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/LeafletCallbacks.php @@ -11,10 +11,8 @@ namespace Netzmacht\Contao\Leaflet\Dca; -use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; -use Netzmacht\Contao\Leaflet\Mapper\MapMapper; use Netzmacht\Contao\Leaflet\Model\LayerModel; -use Netzmacht\Contao\Leaflet\Model\MapModel; +use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory; use Netzmacht\LeafletPHP\Value\LatLng; /** @@ -22,24 +20,35 @@ use Netzmacht\LeafletPHP\Value\LatLng; * * @package Netzmacht\Contao\Leaflet\Dca */ -class Leaflet +class LeafletCallbacks { /** - * Validate a coordinate. + * File system. * - * @param mixed $value The given value. - * - * @return mixed + * @var \Files */ - public function validateCoordinate($value) + private $fileSystem; + + /** + * LeafletCallbacks constructor. + * + * @param \Files $fileSystem File system. + */ + public function __construct(\Files $fileSystem) { - if (!empty($value)) { - // Validate by creating latlng object. Throws an exception. + $this->fileSystem = $fileSystem; + } - LatLng::fromString($value); - } - - return $value; + /** + * Generate the callback definition. + * + * @param string $methodName Callback method name. + * + * @return callable + */ + public static function callback($methodName) + { + return CallbackFactory::service('leaflet.dca.common', $methodName); } /** @@ -93,4 +102,18 @@ class Leaflet return $options; } + + /** + * Clear the leaflet cache. + * + * @param mixed $value Value when used as save_callback. + * + * @return mixed + */ + public function clearCache($value = null) + { + $this->fileSystem->rrdir('system/cache/leaflet', true); + + return $value; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Map.php b/src/Netzmacht/Contao/Leaflet/Dca/MapCallbacks.php similarity index 72% rename from src/Netzmacht/Contao/Leaflet/Dca/Map.php rename to src/Netzmacht/Contao/Leaflet/Dca/MapCallbacks.php index 83ab048..215bfb7 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Map.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/MapCallbacks.php @@ -11,16 +11,31 @@ namespace Netzmacht\Contao\Leaflet\Dca; -use Netzmacht\Contao\Toolkit\ServiceContainerTrait; +use Netzmacht\Contao\Leaflet\Model\LayerModel; +use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks; +use Netzmacht\Contao\Toolkit\Dca\Manager; +use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; /** * Class Map is the helper class for the tl_leaflet_map dca. * * @package Netzmacht\Contao\Leaflet\Dca */ -class Map +class MapCallbacks extends Callbacks { - use ServiceContainerTrait; + /** + * Name of the data container. + * + * @var string + */ + protected static $name = 'tl_leaflet_map'; + + /** + * Helper service name. + * + * @var string + */ + protected static $serviceName = 'leaflet.dca.map-callbacks'; /** * The database connection. @@ -31,10 +46,15 @@ class Map /** * Construct. + * + * @param Manager $manager Data container manager. + * @param \Database $database Database connection. */ - public function __construct() + public function __construct(Manager $manager, \Database $database) { - $this->database = static::getServiceContainer()->getDatabaseConnection(); + parent::__construct($manager); + + $this->database = $database; } /** @@ -120,4 +140,20 @@ class Map return null; } + + /** + * Get all layers except of the current layer. + * + * @param \DataContainer $dataContainer The dataContainer driver. + * + * @return array + */ + public function getLayers($dataContainer) + { + $collection = LayerModel::findBy('id !', $dataContainer->id); + + return OptionsBuilder::fromCollection($collection, 'title') + ->asTree() + ->getOptions(); + } } diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Marker.php b/src/Netzmacht/Contao/Leaflet/Dca/MarkerCallbacks.php similarity index 98% rename from src/Netzmacht/Contao/Leaflet/Dca/Marker.php rename to src/Netzmacht/Contao/Leaflet/Dca/MarkerCallbacks.php index b5e697c..6df9083 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Marker.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/MarkerCallbacks.php @@ -20,7 +20,7 @@ use Netzmacht\Contao\Leaflet\Model\PopupModel; * * @package Netzmacht\Contao\Leaflet\Dca */ -class Marker +class MarkerCallbacks { /** * Generate the row label. @@ -44,7 +44,6 @@ class Marker $collection = IconModel::findAll(array('order' => 'title')); $builder = OptionsBuilder::fromCollection( $collection, - 'id', function ($model) { return sprintf('%s [%s]', $model['title'], $model['type']); } @@ -61,7 +60,7 @@ class Marker public function getPopups() { $collection = PopupModel::findAll(array('order' => 'title')); - $builder = OptionsBuilder::fromCollection($collection, 'id', 'title'); + $builder = OptionsBuilder::fromCollection($collection, 'title'); return $builder->getOptions(); } diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Validator.php b/src/Netzmacht/Contao/Leaflet/Dca/Validator.php new file mode 100644 index 0000000..5da9c57 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Dca/Validator.php @@ -0,0 +1,133 @@ + + * @copyright 2016 netzmacht David Molineus. All rights reserved. + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Dca; + +use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator; +use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory; +use Netzmacht\LeafletPHP\Value\LatLng; + +/** + * Class Validator. + * + * @package Netzmacht\Contao\Leaflet\Dca + */ +class Validator +{ + /** + * Translator. + * + * @var Translator; + */ + private $translator; + + /** + * Validator constructor. + * + * @param Translator $translator Translator. + */ + public function __construct(Translator $translator) + { + $this->translator = $translator; + } + + /** + * Generate the callback definition. + * + * @param string $methodName Callback method name. + * + * @return callable + */ + public static function callback($methodName) + { + return CallbackFactory::service('leaflet.dca.validator', $methodName); + } + + /** + * Validate coordinates. + * + * @param mixed $value Given value. + * + * @return mixed + * @throws \InvalidArgumentException When invalid coordinates give. + */ + public function validateCoordinates($value) + { + try { + LatLng::fromString($value); + } catch (\Exception $e) { + throw new \InvalidArgumentException( + $this->translator->translate('invalidCoordinates', 'leaflet', [$value]), + 0, + $e + ); + } + + return $value; + } + + /** + * Validate multiple coordinates. + * + * @param mixed $values Given value. + * + * @return mixed + */ + public function validateMultipleCoordinates($values) + { + if (!is_array($values)) { + $lines = explode("\n", $values); + } else { + $lines = $values; + } + + foreach ($lines as $coordinate) { + $this->validateCoordinates($coordinate); + } + + return $values; + } + + /** + * Validate multiple coordinate sets. + * + * @param mixed $values Given value. + * + * @return mixed + */ + public function validateMultipleCoordinateSets($values) + { + $sets = deserialize($values, true); + foreach ($sets as $lines) { + $this->validateMultipleCoordinates($lines); + } + + return $values; + } + + /** + * Validate an alias. + * + * @param string $value Given value. + * + * @return string + * @throws \InvalidArgumentException When invalid value given. + */ + public function validateAlias($value) + { + if (preg_match('/^[A-Za-z_]+[A-Za-z0-9_]+$/', $value) !== 1) { + throw new \InvalidArgumentException( + $this->translator->translate('invalidAlias', 'leaflet') + ); + } + + return $value; + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Vector.php b/src/Netzmacht/Contao/Leaflet/Dca/VectorCallbacks.php similarity index 56% rename from src/Netzmacht/Contao/Leaflet/Dca/Vector.php rename to src/Netzmacht/Contao/Leaflet/Dca/VectorCallbacks.php index adeb619..c38b141 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Vector.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/VectorCallbacks.php @@ -11,6 +11,7 @@ namespace Netzmacht\Contao\Leaflet\Dca; +use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks; use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder; use Netzmacht\Contao\Leaflet\Model\StyleModel; @@ -19,8 +20,22 @@ use Netzmacht\Contao\Leaflet\Model\StyleModel; * * @package Netzmacht\Contao\Leaflet\Dca */ -class Vector +class VectorCallbacks extends Callbacks { + /** + * Name of the data container. + * + * @var string + */ + protected static $name = 'tl_leaflet_vector'; + + /** + * Helper service name. + * + * @var string + */ + protected static $serviceName = 'leaflet.dca.vector-callbacks'; + /** * Generate the row label. * @@ -30,7 +45,11 @@ class Vector */ public function generateRow($row) { - return sprintf('%s [%s]', $row['title'], $row['type']); + return sprintf( + '%s (%s)', + $row['title'], + $this->getFormatter()->formatValue('type', $row['type']) + ); } /** @@ -42,6 +61,6 @@ class Vector { $collection = StyleModel::findAll(array('order' => 'title')); - return OptionsBuilder::fromCollection($collection, 'id', 'title')->getOptions(); + return OptionsBuilder::fromCollection($collection, 'title')->getOptions(); } } diff --git a/src/Netzmacht/Contao/Leaflet/DependencyInjection/LeafletServices.php b/src/Netzmacht/Contao/Leaflet/DependencyInjection/LeafletServices.php new file mode 100644 index 0000000..4d3b2a6 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/DependencyInjection/LeafletServices.php @@ -0,0 +1,106 @@ + + * @copyright 2016 netzmacht David Molineus. All rights reserved. + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\DependencyInjection; + +use Doctrine\Common\Cache\Cache; +use Netzmacht\Contao\Leaflet\Boot; +use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; +use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; +use Netzmacht\Contao\Leaflet\MapProvider; +use Netzmacht\Contao\Toolkit\Data\Alias\AliasGenerator; +use Netzmacht\LeafletPHP\Assets; +use Netzmacht\LeafletPHP\Leaflet; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; + +/** + * Class LeafletServices describes services provided by the leaflet package. + * + * @package Netzmacht\Contao\Leaflet\DependencyInjection + */ +class LeafletServices +{ + /** + * Service name for the alias generator which creates valid js aliases. + * + * @return AliasGenerator + */ + const ALIAS_GENERATOR = 'leaflet.alias-generator.default'; + + /** + * Service name of the boot handler. + * + * @return Boot + */ + const BOOT = 'leaflet.boot'; + + /** + * Leaflet cache + * + * @return Cache + */ + const CACHE = 'leaflet.cache'; + + /** + * Service name of the definition builder. + * + * @return Leaflet + */ + const DEFINITION_BUILDER = 'leaflet.definition.builder'; + + /** + * Service name of the encoder factory used inside of the definition builder. + * + * @return \callable + */ + const DEFINITION_ENCODER_FACTORY = 'leaflet.definition.builder.encoder-factory'; + + /** + * Service name of the internal used event dispatcher of the definition builder. + * + * @return EventDispatcherInterface + */ + const DEFINITION_BUILDER_EVENT_DISPATCHER = 'leaflet.definition.builder.event-dispatcher'; + + /** + * Service name of the definition mapper. + * + * @return DefinitionMapper + */ + const DEFINITION_MAPPER = 'leaflet.definition.mapper'; + + /** + * Service name of the leaflet map provider. + * + * @return MapProvider + */ + const MAP_PROVIDER = 'leaflet.map.provider'; + + /** + * Service name of the map assets handler. + * + * @return Assets + */ + const MAP_ASSETS = 'leaflet.map.assets'; + + /** + * Service name of the frontend value filter. + * + * @return ValueFilter + */ + const FRONTEND_VALUE_FILTER = 'leaflet.frontend.value-filter'; + + /** + * Service name for the alias generator uses for rows being unique in the pid. + * + * @return AliasGenerator + */ + const PARENT_ALIAS_GENERATOR = 'leaflet.alias-generator.parent'; +} diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/AbstractMapHybrid.php b/src/Netzmacht/Contao/Leaflet/Frontend/AbstractMapHybrid.php new file mode 100644 index 0000000..ad04a26 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Frontend/AbstractMapHybrid.php @@ -0,0 +1,150 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Frontend; + +use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator; +use Database\Result; +use Model\Collection; +use Netzmacht\Contao\Leaflet\MapProvider; +use Netzmacht\Contao\Leaflet\Model\MapModel; +use Netzmacht\Contao\Toolkit\Component\Hybrid\AbstractHybrid; +use Netzmacht\Contao\Toolkit\View\Template\TemplateFactory; + +/** + * Class HybridTrait provides method required by the frontend module and content element the same time. + * + * @package Netzmacht\Contao\Leaflet\Frontend + */ +abstract class AbstractMapHybrid extends AbstractHybrid +{ + /** + * The map provider. + * + * @var MapProvider + */ + private $mapProvider; + + /** + * The user input. + * + * @var \Input + */ + private $input; + + /** + * The Contao config. + * + * @var \Config + */ + private $config; + + /** + * HybridTrait constructor. + * + * @param Result|\Model|Collection $model Component model. + * @param TemplateFactory $templateFactory Template factory. + * @param Translator $translator Translator. + * @param MapProvider $mapProvider Map provider. + * @param \Input $input Request Input. + * @param \Config $config Config. + * @param string $column Column in which the element appears. + */ + public function __construct( + $model, + TemplateFactory $templateFactory, + Translator $translator, + MapProvider $mapProvider, + \Input $input, + \Config $config, + $column = null + ) { + parent::__construct($model, $templateFactory, $translator, $column); + + $this->mapProvider = $mapProvider; + $this->input = $input; + $this->config = $config; + } + + /** + * Do the frontend integration generation. + * + * @return string + */ + public function generate() + { + $this->mapProvider->handleAjaxRequest($this->getIdentifier()); + + if (TL_MODE === 'BE') { + $model = MapModel::findByPk($this->get('leaflet_map')); + + $template = $this->getTemplateFactory()->createBackendTemplate('be_wildcard'); + + if ($model) { + $href = 'contao/main.php?do=leaflet&table=tl_leaflet_map&act=edit&id=' . $model->id; + + $template->set('wildcard', '### LEAFLET MAP ' . $model->title . ' ###'); + $template->set('title', $this->get('headline')); + $template->set('id', $model->id); + $template->set('link', $model->title); + $template->set('href', $href); + } + + return $template->parse(); + } + + return parent::generate(); + } + + /** + * Do the frontend integration compiling. + * + * @return void + * + * @throws \Exception If the map could not be created. + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + protected function compile() + { + try { + $template = $this->get('leaflet_template') ?: 'leaflet_map_js'; + $mapId = $this->getIdentifier(); + $map = $this->mapProvider->generate($this->get('leaflet_map'), null, $mapId, $template); + + $this->template->set('javascript', $map); + $this->template->set('mapId', $mapId); + + $style = ''; + $height = deserialize($this->get('leaflet_height'), true); + $width = deserialize($this->get('leaflet_width'), true); + + if (!empty($width['value'])) { + $style .= 'width:' . $width['value'] . $width['unit'] . ';'; + } + + if (!empty($height['value'])) { + $style .= 'height:' . $height['value'] . $height['unit'] . ';'; + } + + $this->template->set('mapStyle', $style); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get the component identifier which is used as unique name. + * + * @return string + */ + abstract protected function getIdentifier(); +} diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php b/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php index 1b9d67b..20b3d45 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/DataController.php @@ -12,7 +12,7 @@ namespace Netzmacht\Contao\Leaflet\Frontend; use Netzmacht\Contao\Leaflet\Filter\Filter; -use Netzmacht\Contao\Leaflet\MapService; +use Netzmacht\Contao\Leaflet\MapProvider; /** * The data controller handles ajax request for sub data. @@ -22,11 +22,11 @@ use Netzmacht\Contao\Leaflet\MapService; class DataController { /** - * The map service. + * The map provider. * - * @var MapService + * @var MapProvider */ - private $mapService; + private $mapProvider; /** * The user input data. @@ -40,39 +40,59 @@ class DataController 'filter' => null, 'values' => null ); + + /** + * Filters configuration. + * + * @var array + */ + private $filters; + + /** + * Display errors. + * + * @var bool + */ + private $displayErrors; /** * Construct. * - * @param MapService $mapService The map service. - * @param array $input The user input as array. + * @param MapProvider $mapProvider The map provider. + * @param array $filters Filters configuration. + * @param bool $displayErrors Display errors. */ - public function __construct(MapService $mapService, $input) + public function __construct(MapProvider $mapProvider, array $filters, $displayErrors) { - $this->mapService = $mapService; - $this->input = array_merge($this->input, $input); + $this->mapProvider = $mapProvider; + $this->filters = $filters; + $this->displayErrors = $displayErrors; } /** * Execute the controller and create the data response. * + * @param array $input The user input as array. + * * @return void * * @throws \Exception If anything went wrong. */ - public function execute() + public function execute(array $input) { + $input = array_merge($this->input, $input); + try { - if ($this->input['filter']) { - $filter = $this->createFilter(); + if ($input['filter']) { + $filter = $this->createFilter($input); } else { $filter = null; } - list($data, $error) = $this->loadData($this->input['type'], $this->input['id'], $filter); - $this->encodeData($this->input['format'], $data); + list($data, $error) = $this->loadData($input['type'], $input['id'], $filter); + $this->encodeData($input['format'], $data); } catch (\Exception $e) { - if (\Config::get('debugMode') || \Config::get('displayErrors')) { + if ($this->displayErrors) { throw $e; } $error = true; @@ -120,7 +140,7 @@ class DataController switch ($type) { case 'layer': - $data = $this->mapService->getFeatureCollection($dataId, $filter); + $data = $this->mapProvider->getFeatureCollection($dataId, $filter); break; default: @@ -135,20 +155,22 @@ class DataController /** * Create a filter. * + * @param array $input The user input as array. + * * @return Filter * @throws \RuntimeException If the filter is not defined. * * @SuppressWarnings(PHPMD.Superglobals) */ - private function createFilter() + private function createFilter($input) { - if (!isset($GLOBALS['LEAFLET_FILTERS'][$this->input['filter']])) { - throw new \RuntimeException(sprintf('Undefined filter "%s".', $this->input['filter'])); + if (!isset($this->filters[$input['filter']])) { + throw new \RuntimeException(sprintf('Undefined filter "%s".', $input['filter'])); } /** @var Filter $filter */ - $filter = $GLOBALS['LEAFLET_FILTERS'][$this->input['filter']]; + $filter = $this->filters[$input['filter']]; - return $filter::fromRequest($this->input['values']); + return $filter::fromRequest($input['values']); } } diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php b/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php deleted file mode 100644 index d163004..0000000 --- a/src/Netzmacht/Contao/Leaflet/Frontend/HybridTrait.php +++ /dev/null @@ -1,129 +0,0 @@ - - * @copyright 2015 netzmacht creative David Molineus - * @license LGPL 3.0 - * @filesource - * - */ - -namespace Netzmacht\Contao\Leaflet\Frontend; - -use Netzmacht\Contao\Leaflet\MapService; -use Netzmacht\Contao\Leaflet\Model\MapModel; - -/** - * Class HybridTrait provides method required by the frontend module and content element the same time. - * - * @package Netzmacht\Contao\Leaflet\Frontend - */ -trait HybridTrait -{ - /** - * The map service. - * - * @var MapService - */ - private $mapService; - - /** - * The user input. - * - * @var \Input - */ - private $input; - - /** - * The Contao config. - * - * @var \Config - */ - private $config; - - /** - * Construct. - * - * @param \ContentModel $objElement Content element model. - * @param string $strColumn Layout column. - * - * @return void - */ - protected function construct($objElement, $strColumn = 'main') - { - parent::__construct($objElement, $strColumn); - - $this->mapService = static::getService('leaflet.map.service'); - $this->input = static::getService('input'); - $this->config = static::getService('config'); - } - - /** - * Do the frontend integration generation. - * - * @return string - */ - public function generate() - { - $this->mapService->handleAjaxRequest($this->getIdentifier()); - - if (TL_MODE === 'BE') { - $model = MapModel::findByPK($this->leaflet_map); - - $template = new \BackendTemplate('be_wildcard'); - - if ($model) { - $href = 'contao/main.php?do=leaflet&table=tl_leaflet_map&act=edit&id=' . $model->id; - - $template->wildcard = '### LEAFLET MAP ' . $model->title . ' ###'; - $template->title = $this->headline; - $template->id = $model->id; - $template->link = $model->title; - $template->href = $href; - } - - return $template->parse(); - } - - return parent::generate(); - } - - /** - * Do the frontend integration compiling. - * - * @return void - * - * @throws \Exception If the map could not be created. - * - * @SuppressWarnings(PHPMD.Superglobals) - */ - protected function compile() - { - try { - $template = $this->leaflet_template ?: 'leaflet_map_js'; - $mapId = $this->getIdentifier(); - $map = $this->mapService->generate($this->leaflet_map, null, $mapId, $template); - - $GLOBALS['TL_BODY'][] = ''; - - $this->Template->mapId = $mapId; - - $style = ''; - $height = deserialize($this->leaflet_height, true); - $width = deserialize($this->leaflet_width, true); - - if (!empty($width['value'])) { - $style .= 'width:' . $width['value'] . $width['unit'] . ';'; - } - - if (!empty($height['value'])) { - $style .= 'height:' . $height['value'] . $height['unit'] . ';'; - } - - $this->Template->mapStyle = $style; - } catch (\Exception $e) { - throw $e; - } - } -} diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/InsertTag/LeafletInsertTagParser.php b/src/Netzmacht/Contao/Leaflet/Frontend/InsertTag/LeafletInsertTagParser.php index 61f140f..8448e00 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/InsertTag/LeafletInsertTagParser.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/InsertTag/LeafletInsertTagParser.php @@ -11,7 +11,7 @@ namespace Netzmacht\Contao\Leaflet\Frontend\InsertTag; -use Netzmacht\Contao\Leaflet\MapService; +use Netzmacht\Contao\Leaflet\MapProvider; use Netzmacht\Contao\Toolkit\InsertTag\Parser; /** @@ -31,9 +31,9 @@ class LeafletInsertTagParser implements Parser /** * The map service. * - * @var MapService + * @var MapProvider */ - private $mapService; + private $mapProvider; /** * Debug mode. @@ -45,13 +45,13 @@ class LeafletInsertTagParser implements Parser /** * LeafletInsertTagParser constructor. * - * @param MapService $mapService Map service. - * @param bool $debugMode Debug mode. + * @param MapProvider $mapProvider Map provider. + * @param bool $debugMode Debug mode. */ - public function __construct(MapService $mapService, $debugMode) + public function __construct(MapProvider $mapProvider, $debugMode) { - $this->mapService = $mapService; - $this->debugMode = $debugMode; + $this->mapProvider = $mapProvider; + $this->debugMode = $debugMode; } /** @@ -101,7 +101,7 @@ class LeafletInsertTagParser implements Parser private function generateMap($mapId, $template, $style) { try { - return $this->mapService->generate($mapId, null, $mapId, $template, $style); + return $this->mapProvider->generate($mapId, null, $mapId, $template, $style); } catch (\Exception $e) { if ($this->debugMode) { throw $e; diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php b/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php index 826cbc2..3ebaf40 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php @@ -11,34 +11,19 @@ namespace Netzmacht\Contao\Leaflet\Frontend; -use ContentElement; -use Netzmacht\Contao\Toolkit\ServiceContainerTrait; -use Netzmacht\Contao\Leaflet\MapService; - /** * The content element for the leaflet map. * * @property int leaflet_map */ -class MapElement extends \ContentElement +class MapElement extends AbstractMapHybrid { - use ServiceContainerTrait; - use HybridTrait; - /** * Template name. * * @var string */ - protected $strTemplate = 'ce_leaflet_map'; - - /** - * {@inheritdoc} - */ - public function __construct($objElement, $strColumn = 'main') - { - $this->construct($objElement, $strColumn); - } + protected $templateName = 'ce_leaflet_map'; /** * Get the identifier. @@ -47,14 +32,14 @@ class MapElement extends \ContentElement */ protected function getIdentifier() { - if ($this->leaflet_mapId) { - return $this->leaflet_mapId; + if ($this->get('leaflet_mapId')) { + return $this->get('leaflet_mapId'); } - if ($this->cssID[0]) { - return 'map_' . $this->cssID[0]; + if ($this->get('cssID')[0]) { + return 'map_' . $this->get('cssID')[0]; } - return 'map_ce_' . $this->id; + return 'map_ce_' . $this->get('id'); } } diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php b/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php index 1e376ee..ad58ba1 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php @@ -11,33 +11,19 @@ namespace Netzmacht\Contao\Leaflet\Frontend; -use Netzmacht\Contao\Toolkit\ServiceContainerTrait; -use Netzmacht\Contao\Leaflet\MapService; - /** * The frontend module for the Leaflet map. * * @package Netzmacht\Contao\Leaflet\Frontend */ -class MapModule extends \Module +class MapModule extends AbstractMapHybrid { - use ServiceContainerTrait; - use HybridTrait; - /** * Template name. * * @var string */ - protected $strTemplate = 'mod_leaflet_map'; - - /** - * {@inheritdoc} - */ - public function __construct($objElement, $strColumn = 'main') - { - $this->construct($objElement, $strColumn); - } + protected $templateName = 'mod_leaflet_map'; /** * Get the identifier. @@ -46,14 +32,14 @@ class MapModule extends \Module */ protected function getIdentifier() { - if ($this->leaflet_mapId) { - return $this->leaflet_mapId; + if ($this->get('leaflet_mapId')) { + return $this->get('leaflet_mapId'); } - if ($this->cssID[0]) { - return 'map_' . $this->cssID[0]; + if ($this->get('cssID')[0]) { + return 'map_' . $this->get('cssID')[0]; } - return 'map_mod_' . $this->id; + return 'map_mod_' . $this->get('id'); } } diff --git a/src/Netzmacht/Contao/Leaflet/MapService.php b/src/Netzmacht/Contao/Leaflet/MapProvider.php similarity index 61% rename from src/Netzmacht/Contao/Leaflet/MapService.php rename to src/Netzmacht/Contao/Leaflet/MapProvider.php index e82d535..4e14c6c 100644 --- a/src/Netzmacht/Contao/Leaflet/MapService.php +++ b/src/Netzmacht/Contao/Leaflet/MapProvider.php @@ -11,6 +11,7 @@ namespace Netzmacht\Contao\Leaflet; +use Doctrine\Common\Cache\Cache; use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent; use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Frontend\DataController; @@ -18,18 +19,17 @@ use Netzmacht\Contao\Leaflet\Frontend\RequestUrl; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\MapModel; -use Netzmacht\LeafletPHP\Assets; use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection; use Netzmacht\LeafletPHP\Definition\Map; use Netzmacht\LeafletPHP\Leaflet; use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher; /** - * Class MapService. + * Class MapProvider. * * @package Netzmacht\Contao\Leaflet */ -class MapService +class MapProvider { /** * The definition mapper. @@ -62,10 +62,31 @@ class MapService /** * Map assets collector. * - * @var Assets + * @var ContaoAssets */ private $assets; + /** + * Request filters configuration. + * + * @var array + */ + private $filters; + + /** + * Display errors setting. + * + * @var bool + */ + private $displayErrors; + + /** + * Cache. + * + * @var Cache + */ + private $cache; + /** * Construct. * @@ -73,20 +94,29 @@ class MapService * @param Leaflet $leaflet The Leaflet instance. * @param EventDispatcher $eventDispatcher The Contao event dispatcher. * @param \Input $input Thw request input. - * @param Assets $assets Assets handler. + * @param ContaoAssets $assets Assets handler. + * @param Cache $cache Cache. + * @param array $filters Request filters configuration. + * @param bool $displayErrors Display errors setting. */ public function __construct( DefinitionMapper $mapper, Leaflet $leaflet, EventDispatcher $eventDispatcher, \Input $input, - Assets $assets + ContaoAssets $assets, + Cache $cache, + array $filters, + $displayErrors ) { $this->mapper = $mapper; $this->leaflet = $leaflet; $this->eventDispatcher = $eventDispatcher; $this->input = $input; $this->assets = $assets; + $this->filters = $filters; + $this->displayErrors = $displayErrors; + $this->cache = $cache; } /** @@ -145,9 +175,6 @@ class MapService * * @return string * @throws \Exception If generating went wrong. - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function generate( $mapId, @@ -156,23 +183,38 @@ class MapService $template = 'leaflet_map_js', $style = '' ) { - $definition = $this->getDefinition($mapId, $filter, $elementId); - $template = \Controller::getTemplate($template); + if ($mapId instanceof MapModel) { + $model = $mapId; + $mapId = $mapId->id; + } else { + $model = $this->getModel($mapId); + } - // @codingStandardsIgnoreStart - Set for the template. - $javascript = $this->leaflet->build($definition, $this->assets); - $mapId = $definition->getId(); - // @codingStandardsIgnoreEnd + if ($model->cache) { + $cacheKey = $this->getCacheKey($mapId, $filter, $elementId, $template, $style); - ob_start(); - include $template; - $content = ob_get_contents(); - ob_end_clean(); + if ($this->cache->contains($cacheKey)) { + $cached = $this->cache->fetch($cacheKey); + $this->assets->fromArray($cached['assets']); - $event = new GetJavascriptEvent($definition, $content); - $this->eventDispatcher->dispatch($event::NAME, $event); + return $cached['javascript']; + } + } - return $event->getJavascript(); + $buffer = $this->doGenerate($mapId, $filter, $elementId, $template, $model, $style); + + if ($model->cache) { + $this->cache->save( + $cacheKey, + [ + 'assets' => $this->assets->toArray(), + 'javascript' => $buffer + ], + (int) $model->cacheLifeTime + ); + } + + return $buffer; } /** @@ -197,7 +239,23 @@ class MapService throw new \InvalidArgumentException(sprintf('Could not find layer "%s"', $layerId)); } - return $this->mapper->handleGeoJson($model, $filter); + if (!$model->cache) { + return $this->mapper->handleGeoJson($model, $filter); + } + + $cacheKey = 'feature_layer_' . $model->id; + if ($filter) { + $cacheKey .= '.filter_' . md5($filter->toRequest()); + } + + if ($this->cache->contains($cacheKey)) { + return $this->cache->fetch($cacheKey); + } + + $collection = $this->mapper->handleGeoJson($model, $filter); + $this->cache->save($cacheKey, $collection, $model->cacheLifeTime); + + return $collection; } /** @@ -232,12 +290,83 @@ class MapService return; } - $controller = new DataController($this, $data); - $controller->execute(); + $controller = new DataController($this, $this->filters, $this->displayErrors); + $controller->execute($data); if ($exit) { exit; } } } + + /** + * Get the cache key. + * + * @param int $mapId The map database id. + * @param Filter|null $filter Optional request filter. + * @param string $elementId Optional element id. If none given the mapId or alias is used. + * @param string $template The template being used for generating. + * @param string $style Optional style attributes. + * + * @return string + */ + protected function getCacheKey($mapId, $filter, $elementId, $template, $style) + { + $cacheKey = 'map_' . $mapId; + + if ($filter) { + $cacheKey .= '.filter_' . md5($filter->toRequest()); + } + + if ($elementId) { + $cacheKey .= '.element_' . $elementId; + } + + $cacheKey .= '.template_' . $template; + + if ($style) { + $cacheKey .= '.style_' . md5($style); + + return $cacheKey; + } + + return $cacheKey; + } + + /** + * Do the generating of the map. + * + * @param MapModel $model Map model. + * @param Filter|null $filter Optional request filter. + * @param string $elementId Optional element id. If none given the mapId or alias is used. + * @param string $template The template being used for generating. + * @param string $style Optional style attributes. + * + * @return string + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + */ + protected function doGenerate($model, $filter, $elementId, $template, $style) + { + $definition = $this->getDefinition($model, $filter, $elementId); + $template = \Controller::getTemplate($template); + + // @codingStandardsIgnoreStart - Set for the template. + $javascript = $this->leaflet->build($definition, $this->assets); + $mapId = $definition->getId(); + // @codingStandardsIgnoreEnd + + ob_start(); + include $template; + $content = ob_get_contents(); + ob_end_clean(); + + $event = new GetJavascriptEvent($definition, $content); + $this->eventDispatcher->dispatch($event::NAME, $event); + + $buffer = $event->getJavascript(); + + return $buffer; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkerClusterLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkerClusterLayerMapper.php index 2024c81..cf36b8f 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkerClusterLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkerClusterLayerMapper.php @@ -11,9 +11,11 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Layer; +use Netzmacht\Contao\Leaflet\ContaoAssets; use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Model\LayerModel; +use Netzmacht\Contao\Toolkit\View\Assets\AssetsManager; use Netzmacht\JavascriptBuilder\Type\AnonymousFunction; use Netzmacht\JavascriptBuilder\Type\Expression; use Netzmacht\LeafletPHP\Definition; @@ -42,6 +44,25 @@ class MarkerClusterLayerMapper extends AbstractLayerMapper */ protected static $type = 'markercluster'; + /** + * Assets manager. + * + * @var ContaoAssets + */ + private $assets; + + /** + * MarkerClusterLayerMapper constructor. + * + * @param ContaoAssets $assets Assets manager. + */ + public function __construct(ContaoAssets $assets) + { + parent::__construct(); + + $this->assets = $assets; + } + /** * {@inheritdoc} */ @@ -61,7 +82,6 @@ class MarkerClusterLayerMapper extends AbstractLayerMapper /** * {@inheritdoc} - * @SuppressWarnings(PHPMD.Superglobals) */ protected function build( Definition $definition, @@ -83,7 +103,7 @@ class MarkerClusterLayerMapper extends AbstractLayerMapper } if (!$model->disableDefaultStyle) { - $GLOBALS['TL_CSS'][] = 'assets/leaflet/libs/leaflet-markercluster/MarkerCluster.Default.css||static'; + $this->assets->addStylesheet('assets/leaflet/libs/leaflet-markercluster/MarkerCluster.Default.css'); } $collection = LayerModel::findBy( diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ProviderLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ProviderLayerMapper.php index ee45ed2..9652ff4 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ProviderLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/ProviderLayerMapper.php @@ -40,11 +40,11 @@ class ProviderLayerMapper extends AbstractLayerMapper /** * Construct. * - * @SuppressWarnings(PHPMD.Superglobals) + * @param array $providers Registered providers. */ - public function __construct() + public function __construct(array $providers) { - $this->providers = &$GLOBALS['LEAFLET_TILE_PROVIDERS']; + $this->providers = $providers; parent::__construct(); } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php index 1741e40..e4d1bcb 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php @@ -14,6 +14,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper; use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Model\ControlModel; use Netzmacht\Contao\Leaflet\Model\MapModel; +use Netzmacht\JavascriptBuilder\Type\Expression; use Netzmacht\LeafletPHP\Definition; use Netzmacht\LeafletPHP\Definition\Control; use Netzmacht\LeafletPHP\Definition\Layer; @@ -98,7 +99,11 @@ class MapMapper extends AbstractMapper protected function buildCustomOptions(Map $map, MapModel $model) { if ($model->options) { - $map->setOptions(json_decode($model->options, true)); + $options = json_decode($model->options, true); + + if (is_array($options)) { + $map->setOptions($options); + } } $map->setOption('dynamicLoad', (bool) $model->dynamicLoad); diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/OptionsBuilder.php b/src/Netzmacht/Contao/Leaflet/Mapper/OptionsBuilder.php index 511f214..1d5458f 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/OptionsBuilder.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/OptionsBuilder.php @@ -12,6 +12,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper; use Netzmacht\LeafletPHP\Definition; +use Netzmacht\LeafletPHP\Definition\HasOptions; /** * Class OptionsBuilder handles the option mapping between the database model and the definition. @@ -236,7 +237,7 @@ class OptionsBuilder if (method_exists($definition, $setter)) { $definition->$setter($value); - } else { + } elseif ($definition instanceof HasOptions) { $definition->setOption($option, $value); } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php index 6eb04e8..513834f 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/AbstractVectorMapper.php @@ -69,7 +69,7 @@ class AbstractVectorMapper extends AbstractTypeMapper parent::build($definition, $model, $mapper, $filter); if ($definition instanceof Path && $model->style) { - $styleModel = StyleModel::findActiveByPk($model->style); + $styleModel = StyleModel::findActiveByPK($model->style); if ($styleModel) { $style = $mapper->handle($styleModel); diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/CircleMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/CircleMapper.php index 0c28633..cb39ba7 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/CircleMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/CircleMapper.php @@ -14,6 +14,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Vector; use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\LeafletPHP\Definition; +use Netzmacht\LeafletPHP\Definition\Vector\CircleMarker; use Netzmacht\LeafletPHP\Value\LatLng; use Netzmacht\LeafletPHP\Definition\Vector\Circle; @@ -60,7 +61,7 @@ class CircleMapper extends AbstractVectorMapper ) { parent::build($definition, $model, $mapper, $filter); - if ($definition instanceof Circle) { + if ($definition instanceof CircleMarker) { $definition->setLatLng(LatLng::fromString($model->coordinates)); } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/CircleMarkerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/CircleMarkerMapper.php index 01d0718..61ad12f 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/CircleMarkerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/CircleMarkerMapper.php @@ -11,8 +11,6 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Vector; -use Netzmacht\LeafletPHP\Definition; - /** * Class CircleMarkerMapper maps the database model to the circle marker definition. * diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/MultiPolygonMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/MultiPolygonMapper.php index 8f3e2a2..72fb405 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/MultiPolygonMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/MultiPolygonMapper.php @@ -14,7 +14,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Vector; use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\LeafletPHP\Definition; -use Netzmacht\LeafletPHP\Definition\Vector\MultiPolygon; +use Netzmacht\LeafletPHP\Definition\Vector\Polygon; /** * Class MultiPolygonMapper maps the multi polygon database model to its definition. @@ -28,7 +28,7 @@ class MultiPolygonMapper extends MultiPolylineMapper * * @var string */ - protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Vector\MultiPolygon'; + protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Vector\Polygon'; /** * Layer type. @@ -49,7 +49,7 @@ class MultiPolygonMapper extends MultiPolylineMapper ) { parent::build($definition, $model, $mapper, $filter); - if ($definition instanceof MultiPolygon) { + if ($definition instanceof Polygon) { $this->createLatLngs($definition, $model); } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/MultiPolylineMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/MultiPolylineMapper.php index a744b7a..7ecbc82 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Vector/MultiPolylineMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Vector/MultiPolylineMapper.php @@ -14,8 +14,8 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Vector; use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\LeafletPHP\Definition; +use Netzmacht\LeafletPHP\Definition\Vector\Polyline; use Netzmacht\LeafletPHP\Value\LatLng; -use Netzmacht\LeafletPHP\Definition\Vector\MultiPolyline; /** * Class MultiPolylineMapper maps the databse model it the multi polyline definition. @@ -29,7 +29,7 @@ class MultiPolylineMapper extends AbstractVectorMapper * * @var string */ - protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Vector\MultiPolyline'; + protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Vector\Polyline'; /** * Layer type. @@ -50,7 +50,7 @@ class MultiPolylineMapper extends AbstractVectorMapper ) { parent::build($definition, $model, $mapper, $filter); - if ($definition instanceof MultiPolyline) { + if ($definition instanceof Polyline) { $this->createLatLngs($definition, $model); } } @@ -58,24 +58,22 @@ class MultiPolylineMapper extends AbstractVectorMapper /** * Create lat lngs for the definition. * - * @param MultiPolyline $definition The multi polyline. - * @param \Model $model The definition model. + * @param Polyline $definition The multi polyline. + * @param \Model $model The definition model. * * @return void */ - protected function createLatLngs(MultiPolyline $definition, \Model $model) + protected function createLatLngs(Polyline $definition, \Model $model) { - $latLngs = array(); - - foreach (deserialize($model->multiData, true) as $data) { - $latLngs[] = array_map( + foreach (deserialize($model->multiData, true) as $ring => $data) { + $latLngs = array_map( function ($row) { return LatLng::fromString($row); }, explode("\n", $data) ); - } - $definition->setLatLngs($latLngs); + $definition->addLatLngs($latLngs, $ring); + } } } diff --git a/src/Netzmacht/Contao/Leaflet/Model/MapModel.php b/src/Netzmacht/Contao/Leaflet/Model/MapModel.php index a3795f1..a5b4288 100644 --- a/src/Netzmacht/Contao/Leaflet/Model/MapModel.php +++ b/src/Netzmacht/Contao/Leaflet/Model/MapModel.php @@ -17,6 +17,8 @@ use Model\Collection; * Class MapModel for the tl_leaflet_map table. * * @property mixed|null locate + * @property mixed|null title + * * @package Netzmacht\Contao\Leaflet\Model */ class MapModel extends \Model diff --git a/src/Netzmacht/Contao/Leaflet/ServiceContainer.php b/src/Netzmacht/Contao/Leaflet/ServiceContainer.php deleted file mode 100644 index 4396991..0000000 --- a/src/Netzmacht/Contao/Leaflet/ServiceContainer.php +++ /dev/null @@ -1,61 +0,0 @@ - - * @copyright 2015 netzmacht creative David Molineus - * @license LGPL 3.0 - * @filesource - * - */ - -namespace Netzmacht\Contao\Leaflet; - -use Netzmacht\Contao\Leaflet\Frontend\ValueFilter; - -/** - * Class ServiceContainer provides the services which are available for Contao leaflet. - * - * @package Netzmacht\Contao\Leaflet - */ -class ServiceContainer -{ - /** - * The global service container. - * - * @var \Pimple - */ - private $container; - - /** - * Construct. - * - * @param \Pimple $container The global service container. - */ - public function __construct(\Pimple $container) - { - $this->container = $container; - } - - /** - * Get the value filter service. - * - * @return ValueFilter - */ - public function getFrontendValueFilter() - { - return $this->getService('leaflet.frontend.value-filter'); - } - - /** - * Get a service from the container. - * - * @param string $name The service name. - * - * @return mixed - */ - public function getService($name) - { - return $this->container[$name]; - } -} diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php index 48bdd36..5809307 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/BootSubscriber.php @@ -12,6 +12,8 @@ namespace Netzmacht\Contao\Leaflet\Subscriber; use ContaoCommunityAlliance\Contao\EventDispatcher\EventDispatcherInitializer; +use Netzmacht\Contao\Leaflet\ContaoAssets; +use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices; use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent; use Netzmacht\Contao\Leaflet\Event\InitializeDefinitionMapperEvent; use Netzmacht\Contao\Leaflet\Event\InitializeEventDispatcherEvent; @@ -20,7 +22,9 @@ use Netzmacht\Contao\Leaflet\Frontend\InsertTag\LeafletInsertTagParser; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\Mapper; use Netzmacht\Contao\Leaflet\Model\IconModel; -use Netzmacht\Contao\Toolkit\Event\InitializeSystemEvent; +use Netzmacht\Contao\Toolkit\Boot\Event\InitializeSystemEvent; +use Netzmacht\Contao\Toolkit\DependencyInjection\Services; +use Netzmacht\Contao\Toolkit\View\Assets\AssetsManager; use Netzmacht\LeafletPHP\Assets; use Netzmacht\LeafletPHP\Definition\Type\ImageIcon; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -32,6 +36,61 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; */ class BootSubscriber implements EventSubscriberInterface { + /** + * Leaflet mapper configuration. + * + * @var array + */ + private $mappers; + + /** + * Leaflet encoder configuration. + * + * @var array + */ + private $encoders; + + /** + * Leaflet libraries configuration. + * + * @var array + */ + private $libraries; + + /** + * Assets manager. + * + * @var ContaoAssets + */ + private $assets; + + /** + * Definition mapper. + * + * @var DefinitionMapper + */ + private $definitionMapper; + + /** + * BootSubscriber constructor. + * + * @param ContaoAssets $assets Leaflet assets manager. + * @param array $mappers Leaflet mapper configuration. + * @param array $encoders Leaflet encoder configuration. + * @param array $libraries Leaflet libraries configuration. + */ + public function __construct( + ContaoAssets $assets, + array $mappers, + array $encoders, + array $libraries + ) { + $this->assets = $assets; + $this->mappers = $mappers; + $this->encoders = $encoders; + $this->libraries = $libraries; + } + /** * {@inheritdoc} */ @@ -55,12 +114,12 @@ class BootSubscriber implements EventSubscriberInterface */ public function initializeInsertTagParser(InitializeSystemEvent $event) { - $container = $event->getServiceContainer(); - $debugMode = $container->getConfig()->get('debugMode'); - $mapService = $container->getService('leaflet.map.service'); - $parser = new LeafletInsertTagParser($mapService, $debugMode); + $container = $event->getContainer(); + $debugMode = $container->get(Services::CONFIG)->get('debugMode'); + $mapProvider = $container->get(LeafletServices::MAP_PROVIDER); + $parser = new LeafletInsertTagParser($mapProvider, $debugMode); - $container->getInsertTagReplacer()->registerParser($parser); + $container->get(Services::INSERT_TAG_REPLACER)->registerParser($parser); } /** @@ -69,14 +128,13 @@ class BootSubscriber implements EventSubscriberInterface * @param InitializeDefinitionMapperEvent $event The subscribed event. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function initializeDefinitionMapper(InitializeDefinitionMapperEvent $event) { - $mapper = $event->getDefinitionMapper(); + $mapper = $event->getDefinitionMapper(); + $this->definitionMapper = $mapper; - foreach ($GLOBALS['LEAFLET_MAPPERS'] as $className) { + foreach ($this->mappers as $className) { if (is_array($className)) { $mapper->register($this->createMapper($className[0]), $className[1]); } else { @@ -91,15 +149,13 @@ class BootSubscriber implements EventSubscriberInterface * @param InitializeEventDispatcherEvent $event The subscribed event. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function initializeEventDispatcher(InitializeEventDispatcherEvent $event) { $dispatcher = $event->getEventDispatcher(); $initializer = new EventDispatcherInitializer(); - $initializer->addSubscribers($dispatcher, $GLOBALS['LEAFLET_ENCODERS']); + $initializer->addSubscribers($dispatcher, $this->encoders); } /** @@ -108,14 +164,12 @@ class BootSubscriber implements EventSubscriberInterface * @param InitializeLeafletBuilderEvent $event The subscribed event. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function initializeLeafletBuilder(InitializeLeafletBuilderEvent $event) { $builder = $event->getBuilder(); - foreach ($GLOBALS['LEAFLET_LIBRARIES'] as $name => $assets) { + foreach ($this->libraries as $name => $assets) { if (!empty($assets['css'])) { list ($source, $type) = (array) $assets['css']; $builder->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE); @@ -132,34 +186,32 @@ class BootSubscriber implements EventSubscriberInterface * Load Contao leaflet assets. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function loadAssets() { - $GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/maps/contao-leaflet.js' . $this->staticFlag(); + $this->assets->addJavascript('assets/leaflet/maps/contao-leaflet.js', ContaoAssets::TYPE_FILE); } /** * Load icons. * * @return void - * - * @SuppressWarnings(PHPMD.Superglobals) */ public function loadIcons() { + if (!$this->definitionMapper) { + return; + } + $collection = IconModel::findBy('active', true); if ($collection) { - /** @var DefinitionMapper $mapper */ - $mapper = $GLOBALS['container']['leaflet.definition.mapper']; $buffer = ''; $icons = array(); foreach ($collection as $model) { /** @var ImageIcon $icon */ - $icon = $mapper->handle($model); + $icon = $this->definitionMapper->handle($model); $icons[] = array( 'id' => $icon->getId(), 'type' => lcfirst($icon->getType()), @@ -178,39 +230,21 @@ class BootSubscriber implements EventSubscriberInterface // @codingStandardsIgnoreStart // TODO: Cache it. // codingStandardsIgnoreEnd - $GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/js/icons.js' . $this->staticFlag(); + $this->assets->addJavascript('assets/leaflet/js/icons.js', ContaoAssets::TYPE_FILE); } } - /** - * Set the static flag. - * - * @return string - */ - private function staticFlag() - { - if (\Config::get('debugMode') || TL_MODE !== 'FE') { - return ''; - } - - return '|static'; - } - /** * Create a new mapper. * * @param mixed $mapper The mapper class or callable factory. * * @return Mapper - * - * @SuppressWarnings(PHPMD.Superglobals) */ private function createMapper($mapper) { if (is_callable($mapper)) { - $container = $GLOBALS['container']['leaflet.service-container']; - - return $mapper($container); + return $mapper(); } return new $mapper; diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php index 03f5b78..5c61296 100644 --- a/src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/GeoJsonSubscriber.php @@ -11,7 +11,7 @@ namespace Netzmacht\Contao\Leaflet\Subscriber; -use Netzmacht\Contao\Leaflet\Dca\Vector; +use Netzmacht\Contao\Leaflet\Dca\VectorCallbacks; use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent; use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\LeafletPHP\Value\GeoJson\Feature; @@ -28,6 +28,23 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; */ class GeoJsonSubscriber implements EventSubscriberInterface { + /** + * Property mapping between models and features. + * + * @var array + */ + private $featureModelProperties; + + /** + * GeoJsonSubscriber constructor. + * + * @param array $featureModelProperties Property mapping between models and features. + */ + public function __construct(array $featureModelProperties) + { + $this->featureModelProperties = $featureModelProperties; + } + /** * {@inheritdoc} */ @@ -83,7 +100,7 @@ class GeoJsonSubscriber implements EventSubscriberInterface $definition = $event->getDefinition(); $model = $event->getModel(); - if (($definition instanceof Marker || $definition instanceof Vector) + if (($definition instanceof Marker || $definition instanceof VectorCallbacks) && $model instanceof \Model && $feature instanceof Feature) { $this->setDataProperty($model, $feature); $this->setBoundsInformation($model, $feature); @@ -113,7 +130,6 @@ class GeoJsonSubscriber implements EventSubscriberInterface * @param ConvertToGeoJsonEvent $event The subscribed events. * * @return void - * @SuppressWarnings(PHPMD.Superglobals) */ public function setModelData(ConvertToGeoJsonEvent $event) { @@ -121,11 +137,11 @@ class GeoJsonSubscriber implements EventSubscriberInterface $model = $event->getModel(); if (!$model instanceof \Model || !$feature instanceof Feature - || empty($GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES'][$model->getTable()])) { + || empty($this->featureModelProperties[$model->getTable()])) { return; } - $mapping = $GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES'][$model->getTable()]; + $mapping = $this->featureModelProperties[$model->getTable()]; $data = (array) $feature->getProperty('model'); foreach ((array) $mapping as $property) {