From 8a28738b56127af54d12bfd6185329cbf29b22ea Mon Sep 17 00:00:00 2001 From: Niclas Date: Tue, 4 Feb 2025 15:13:18 +0100 Subject: [PATCH 1/4] refactor: map module --- source/php/Module/Map/Map.php | 163 ++---------------- .../Module/Map/Resolvers/TemplateResolver.php | 26 +++ .../TemplateController/EmbedController.php | 75 ++++++++ .../Map/TemplateController/NullController.php | 23 +++ .../OpenStreetMapController.php | 84 +++++++++ .../TemplateControllerInterface.php | 10 ++ .../{default.blade.php => embed.blade.php} | 0 .../php/Module/Map/views/notFound.blade.php | 11 ++ .../Module/Map/views/openStreetMap.blade.php | 2 +- 9 files changed, 246 insertions(+), 148 deletions(-) create mode 100644 source/php/Module/Map/Resolvers/TemplateResolver.php create mode 100644 source/php/Module/Map/TemplateController/EmbedController.php create mode 100644 source/php/Module/Map/TemplateController/NullController.php create mode 100644 source/php/Module/Map/TemplateController/OpenStreetMapController.php create mode 100644 source/php/Module/Map/TemplateController/TemplateControllerInterface.php rename source/php/Module/Map/views/{default.blade.php => embed.blade.php} (100%) create mode 100644 source/php/Module/Map/views/notFound.blade.php diff --git a/source/php/Module/Map/Map.php b/source/php/Module/Map/Map.php index 190cb8be3..7cee77b83 100644 --- a/source/php/Module/Map/Map.php +++ b/source/php/Module/Map/Map.php @@ -2,6 +2,12 @@ namespace Modularity\Module\Map; +use Modularity\Module\Map\TemplateController\OpenStreetMapController; +use Modularity\Module\Map\TemplateController\EmbedController; +use Modularity\Module\Map\TemplateController\TemplateControllerInterface; +use Modularity\Module\Map\TemplateController\NullController; +use Modularity\Module\Map\Resolvers\TemplateResolver; + class Map extends \Modularity\Module { public $slug = 'map'; @@ -9,6 +15,9 @@ class Map extends \Modularity\Module protected $template = 'default'; + private TemplateControllerInterface $templateController; + + public function init() { $this->nameSingular = __('Map', 'modularity'); @@ -18,6 +27,12 @@ public function init() add_filter('acf/load_field/name=map_url', array($this,'sslNotice')); add_filter('acf/load_value/name=map_url', array($this,'filterMapUrl'), 10, 3); add_filter('acf/update_value/name=map_url', array($this,'filterMapUrl'), 10, 3); + + $this->templateController = new TemplateResolver( + new OpenStreetMapController(), + new EmbedController($this), + new NullController() + ); } /** @@ -36,153 +51,7 @@ public function data() : array //Shared template data $data['height'] = !empty($fields['height']) ? $fields['height'] : '400'; - //Set map type - if (empty($fields['map_type'])) { - $fields['map_type'] = 'default'; - } - $this->template = $fields['map_type']; - - //Handle as OpenStreetMap - if ($fields['map_type'] == 'openStreetMap') { - return $this->openStreetMapTemplateData($data, $fields); - } - - //Handle as default - return $this->defaultTemplateData($data, $fields); - } - - /** - * The function `openStreetMapTemplateData` processes marker data and start position data for an - * OpenStreetMap template. - * - * @param data The `openStreetMapTemplateData` function takes two parameters: `` and - * ``. - * @param fields The `openStreetMapTemplateData` function takes two parameters: `` and - * ``. - * - * @return The function `openStreetMapTemplateData` is returning the modified `` array after - * processing the input data and fields. The function adds pins with latitude, longitude, and - * tooltip information to the `['pins']` array based on the provided markers. It also sets the - * start position with latitude, longitude, and zoom level if the `osm_start_position` field is not - * empty. Finally - */ - private function openStreetMapTemplateData($data, $fields) { - - $data['pins'] = array(); - $start = $fields['osm_start_position']; - - if(!empty($fields['osm_markers']) && is_array($fields['osm_markers'])) { - foreach ($fields['osm_markers'] as $marker) { - if ($this->hasCorrectPlaceData($marker['position'])) { - $pin = array(); - $pin['lat'] = $marker['position']['lat']; - $pin['lng'] = $marker['position']['lng']; - $pin['tooltip'] = $this->createMarkerTooltip($marker); - - array_push($data['pins'], $pin); - } - } - } - - if (!empty($start)) { - $data['startPosition'] = [ - 'lat' => $start['lat'], - 'lng' => $start['lng'], - 'zoom' => $start['zoom'] - ]; - } - - return $data; - } - - /** - * Generates default template data for the Map module. - * - * @param array $data The existing data array. - * @param array $fields The fields array containing module settings. - * @return array The updated data array with default template data. - */ - private function defaultTemplateData($data, $fields) { - //Get and sanitize url - $map_url = $fields['map_url']; - $map_url = str_replace('http://', 'https://', $map_url, $replaced); // Enforce ssl - - /** - * If the scheme is not altered with str_replace, the url may only contain // without https: - */ - if(0 === $replaced) { - $parsedUrl = parse_url( $map_url ); - if(!isset($parsedUrl['scheme']) ) { - $map_url = str_replace('//', 'https://', $map_url); // Ensure url scheme is literal - } - } - - $map_url = str_replace('disable_scroll=false', 'disable_scroll=true', $map_url); //Remove scroll arcgis - - //Create data array - $data['map_url'] = $map_url; - $data['map_description'] = !empty($fields['map_description']) ? $fields['map_description'] : ''; - - $data['show_button'] = !empty($fields['show_button']) ? $fields['show_button'] : false; - $data['button_label'] = !empty($fields['button_label']) ? $fields['button_label'] : false; - $data['button_url'] = !empty($fields['button_url']) ? $fields['button_url'] : false; - $data['more_info_button'] = !empty($fields['more_info_button']) ? $fields['more_info_button'] : false; - $data['more_info'] = !empty($fields['more_info']) ? $fields['more_info'] : false; - $data['more_info_title'] = !empty($fields['more_info_title']) ? $fields['more_info_title'] : false; - - $data['cardMapCss'] = ($data['more_info_button']) ? 'o-grid-12@xs o-grid-8@md' : 'o-grid-12@md'; - $data['cardMoreInfoCss'] = ($data['more_info_button']) ? 'o-grid-12@xs o-grid-4@md' : ''; - - $data['uid'] = uniqid(); - $data['id'] = $this->ID; - - $data['lang'] = [ - 'knownLabels' => [ - 'title' => __('We need your consent to continue', 'modularity'), - 'info' => sprintf(__('This part of the website shows content from %s. By continuing, you are accepting GDPR and privacy policy.', 'modularity'), '{SUPPLIER_WEBSITE}', '{SUPPLIER_POLICY}'), - 'button' => __('I understand, continue.', 'modularity'), - ], - - 'unknownLabels' => [ - 'title' => __('We need your consent to continue', 'modularity'), - 'info' => sprintf(__('This part of the website shows content from another website (%s). By continuing, you are accepting GDPR and privacy policy.', 'municipio'), '{SUPPLIER_WEBSITE}'), - 'button' => __('I understand, continue.', 'modularity'), - ], - ]; - - return $data; - } - - /** - * The function checks if the position data contains non-empty latitude and longitude values. - * - * @param position The `hasCorrectPlaceData` function is checking if the `position` parameter is - * not empty and if it contains both `lat` and `lng` keys with non-empty values. This function - * returns a boolean value indicating whether the `position` data is in the correct format. - * - * @return bool a boolean value, either true or false. - */ - private function hasCorrectPlaceData($position): bool { - return !empty($position) && !empty($position['lat'] && !empty($position['lng'])); - } - - /** - * The function createMarkerTooltip in PHP creates a tooltip array based on marker data. - * - * @param marker The `createMarkerTooltip` function takes a `` parameter, which is expected - * to be an associative array containing the following keys: - * - * @return An array containing the title, excerpt, directions label, and directions URL of the - * marker. - */ - private function createMarkerTooltip($marker) { - $tooltip = array(); - $tooltip['title'] = $marker['title']; - $tooltip['excerpt'] = $marker['description']; - $tooltip['directions']['label'] = $marker['link_text']; - $tooltip['directions']['url'] = $marker['url']; - - return $tooltip; + return $this->templateController->addData($data, $fields); } /** diff --git a/source/php/Module/Map/Resolvers/TemplateResolver.php b/source/php/Module/Map/Resolvers/TemplateResolver.php new file mode 100644 index 000000000..a6671a734 --- /dev/null +++ b/source/php/Module/Map/Resolvers/TemplateResolver.php @@ -0,0 +1,26 @@ +templateControllers = $templateControllers; + } + + public function Resolve(array $fields): TemplateControllerInterface + { + foreach ($this->templateControllers as $templateController) { + if ($templateController->canHandle($fields)) { + return $templateController; + } + } + + throw new \Exception('No template controller found'); + } +} \ No newline at end of file diff --git a/source/php/Module/Map/TemplateController/EmbedController.php b/source/php/Module/Map/TemplateController/EmbedController.php new file mode 100644 index 000000000..08e9e1c9f --- /dev/null +++ b/source/php/Module/Map/TemplateController/EmbedController.php @@ -0,0 +1,75 @@ +module->ID; + + $data['lang'] = [ + 'knownLabels' => [ + 'title' => __('We need your consent to continue', 'modularity'), + 'info' => sprintf(__('This part of the website shows content from %s. By continuing, you are accepting GDPR and privacy policy.', 'modularity'), '{SUPPLIER_WEBSITE}', '{SUPPLIER_POLICY}'), + 'button' => __('I understand, continue.', 'modularity'), + ], + + 'unknownLabels' => [ + 'title' => __('We need your consent to continue', 'modularity'), + 'info' => sprintf(__('This part of the website shows content from another website (%s). By continuing, you are accepting GDPR and privacy policy.', 'municipio'), '{SUPPLIER_WEBSITE}'), + 'button' => __('I understand, continue.', 'modularity'), + ], + ]; + + return $data; + } + + /** + * Check if this controller can handle the given fields. + */ + public function canHandle(array $fields): bool + { + return empty($fields['map_type']) && isset($fields['map_url']); + } +} \ No newline at end of file diff --git a/source/php/Module/Map/TemplateController/NullController.php b/source/php/Module/Map/TemplateController/NullController.php new file mode 100644 index 000000000..9eacc5334 --- /dev/null +++ b/source/php/Module/Map/TemplateController/NullController.php @@ -0,0 +1,23 @@ +hasCorrectPlaceData($marker['position'])) { + $pin = array(); + $pin['lat'] = $marker['position']['lat']; + $pin['lng'] = $marker['position']['lng']; + $pin['tooltip'] = $this->createMarkerTooltip($marker); + + array_push($data['pins'], $pin); + } + } + } + + if (!empty($start)) { + $data['startPosition'] = [ + 'lat' => $start['lat'], + 'lng' => $start['lng'], + 'zoom' => $start['zoom'] + ]; + } + + return $data; + } + + /** + * The function hasCorrectPlaceData in PHP checks if the given place data is correct. + */ + private function hasCorrectPlaceData($position): bool { + return !empty($position) && !empty($position['lat'] && !empty($position['lng'])); + } + + /** + * The function createMarkerTooltip in PHP creates a tooltip for the given marker. + */ + private function createMarkerTooltip(array $marker) { + if (!is_array($marker) || empty($marker)) { + return []; + } + + $tooltip = []; + $tooltip['title'] = $marker['title'] ?? ''; + $tooltip['excerpt'] = $marker['description'] ?? ''; + $tooltip['directions']['label'] = $marker['link_text'] ?? ''; + $tooltip['directions']['url'] = $marker['url'] ?? ''; + + return $tooltip; + } + + /** + * Check if this controller can handle the given fields. + */ + public function canHandle(array $fields): bool + { + return $fields['map_type'] === $this->templateName; + } + + /** + * Get the template name. + */ + public function getTemplateName(): string + { + return $this->templateName; + } +} \ No newline at end of file diff --git a/source/php/Module/Map/TemplateController/TemplateControllerInterface.php b/source/php/Module/Map/TemplateController/TemplateControllerInterface.php new file mode 100644 index 000000000..9c07ead0b --- /dev/null +++ b/source/php/Module/Map/TemplateController/TemplateControllerInterface.php @@ -0,0 +1,10 @@ + 'h2', + 'variant' => 'h2', + 'classList' => ['module-title'] + ]) + {!! $postTitle !!} + @endtypography + @endif +@endelement \ No newline at end of file diff --git a/source/php/Module/Map/views/openStreetMap.blade.php b/source/php/Module/Map/views/openStreetMap.blade.php index a377f318b..1071b694a 100644 --- a/source/php/Module/Map/views/openStreetMap.blade.php +++ b/source/php/Module/Map/views/openStreetMap.blade.php @@ -11,6 +11,6 @@ @openStreetMap([ 'startPosition' => $startPosition, 'pins' => $pins, - 'height' => $height . 'px' + 'height' => 'min(' . $height . 'px,' . '100vh)', ]) @endopenStreetMap \ No newline at end of file From de6530e9e5536c61834be4da360a7e1ebf12327d Mon Sep 17 00:00:00 2001 From: Niclas Date: Tue, 4 Feb 2025 16:35:30 +0100 Subject: [PATCH 2/4] fix: corrected interface --- source/php/Module/Map/Map.php | 10 +++++++--- .../Module/Map/Resolvers/TemplateResolver.php | 4 ++-- .../Map/Resolvers/TemplateResolverInterface.php | 11 +++++++++++ .../Map/TemplateController/EmbedController.php | 17 +++++++++++++++++ .../Map/TemplateController/NullController.php | 13 +++++++++++++ 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 source/php/Module/Map/Resolvers/TemplateResolverInterface.php diff --git a/source/php/Module/Map/Map.php b/source/php/Module/Map/Map.php index 7cee77b83..8fa7c810d 100644 --- a/source/php/Module/Map/Map.php +++ b/source/php/Module/Map/Map.php @@ -2,11 +2,13 @@ namespace Modularity\Module\Map; +use Modularity\Module\Map\Resolvers\TemplateResolver; +use Modularity\Module\Map\Resolvers\TemplateResolverInterface; use Modularity\Module\Map\TemplateController\OpenStreetMapController; use Modularity\Module\Map\TemplateController\EmbedController; -use Modularity\Module\Map\TemplateController\TemplateControllerInterface; use Modularity\Module\Map\TemplateController\NullController; -use Modularity\Module\Map\Resolvers\TemplateResolver; +use Modularity\Module\Map\TemplateController\TemplateControllerInterface; + class Map extends \Modularity\Module { @@ -16,6 +18,7 @@ class Map extends \Modularity\Module protected $template = 'default'; private TemplateControllerInterface $templateController; + private TemplateResolverInterface $templateResolver; public function init() @@ -28,7 +31,7 @@ public function init() add_filter('acf/load_value/name=map_url', array($this,'filterMapUrl'), 10, 3); add_filter('acf/update_value/name=map_url', array($this,'filterMapUrl'), 10, 3); - $this->templateController = new TemplateResolver( + $this->templateResolver = new TemplateResolver( new OpenStreetMapController(), new EmbedController($this), new NullController() @@ -47,6 +50,7 @@ public function data() : array { $fields = $this->getFields(); $data = array(); + $this->templateController = $this->templateResolver->resolve($fields); //Shared template data $data['height'] = !empty($fields['height']) ? $fields['height'] : '400'; diff --git a/source/php/Module/Map/Resolvers/TemplateResolver.php b/source/php/Module/Map/Resolvers/TemplateResolver.php index a6671a734..0c3b27b0b 100644 --- a/source/php/Module/Map/Resolvers/TemplateResolver.php +++ b/source/php/Module/Map/Resolvers/TemplateResolver.php @@ -4,7 +4,7 @@ use Modularity\Module\Map\TemplateController\TemplateControllerInterface; -class TemplateResolver +class TemplateResolver implements TemplateResolverInterface { private array $templateControllers; @@ -13,7 +13,7 @@ public function __construct(TemplateControllerInterface ...$templateControllers) $this->templateControllers = $templateControllers; } - public function Resolve(array $fields): TemplateControllerInterface + public function resolve(array $fields): TemplateControllerInterface { foreach ($this->templateControllers as $templateController) { if ($templateController->canHandle($fields)) { diff --git a/source/php/Module/Map/Resolvers/TemplateResolverInterface.php b/source/php/Module/Map/Resolvers/TemplateResolverInterface.php new file mode 100644 index 000000000..d3cdd7fbb --- /dev/null +++ b/source/php/Module/Map/Resolvers/TemplateResolverInterface.php @@ -0,0 +1,11 @@ +templateName; + } + /** * Check if this controller can handle the given fields. */ diff --git a/source/php/Module/Map/TemplateController/NullController.php b/source/php/Module/Map/TemplateController/NullController.php index 9eacc5334..03411c6bf 100644 --- a/source/php/Module/Map/TemplateController/NullController.php +++ b/source/php/Module/Map/TemplateController/NullController.php @@ -8,6 +8,8 @@ */ class NullController implements TemplateControllerInterface { + private string $templateName = 'notFound'; + /** * Add data to the template. */ @@ -16,6 +18,17 @@ public function addData(array $data, array $fields): array return $data; } + /** + * Get the template name. + */ + public function getTemplateName(): string + { + return $this->templateName; + } + + /** + * Check if the controller can handle the fields. + */ public function canHandle(array $fields): bool { return true; From 00b68407146f7ffd5a196c9115baf5d4b889c6d3 Mon Sep 17 00:00:00 2001 From: Niclas Date: Wed, 5 Feb 2025 09:37:58 +0100 Subject: [PATCH 3/4] fix: corrected embed map --- source/php/Module/Map/Map.php | 6 +++--- .../php/Module/Map/TemplateController/EmbedController.php | 2 +- source/php/Module/Map/views/embed.blade.php | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/source/php/Module/Map/Map.php b/source/php/Module/Map/Map.php index 8fa7c810d..5c885d507 100644 --- a/source/php/Module/Map/Map.php +++ b/source/php/Module/Map/Map.php @@ -117,13 +117,13 @@ public function filterMapUrl($value, $post_id, $field) * @return string The template file path. */ public function template() { - $path = __DIR__ . "/views/" . $this->template . ".blade.php"; + $path = __DIR__ . "/views/" . $this->templateController->getTemplateName() . ".blade.php"; if (file_exists($path)) { - return $this->template . ".blade.php"; + return $this->templateController->getTemplateName() . ".blade.php"; } - return 'default.blade.php'; + return 'notFound.blade.php'; } /** diff --git a/source/php/Module/Map/TemplateController/EmbedController.php b/source/php/Module/Map/TemplateController/EmbedController.php index 3d709a66b..dc3f2e794 100644 --- a/source/php/Module/Map/TemplateController/EmbedController.php +++ b/source/php/Module/Map/TemplateController/EmbedController.php @@ -87,6 +87,6 @@ public function getTemplateName(): string */ public function canHandle(array $fields): bool { - return empty($fields['map_type']) && isset($fields['map_url']); + return !empty($fields['map_type']) && $fields['map_type'] === 'default' && isset($fields['map_url']); } } \ No newline at end of file diff --git a/source/php/Module/Map/views/embed.blade.php b/source/php/Module/Map/views/embed.blade.php index 1ca345a6a..b9571653e 100644 --- a/source/php/Module/Map/views/embed.blade.php +++ b/source/php/Module/Map/views/embed.blade.php @@ -16,7 +16,6 @@
@typography([ 'element' => 'h2', - 'variant' => 'body', 'id' => 'mod-map-' . $id .'-label' ]) {!! $postTitle !!} From 432dac2a67c2be4bb559d67ba6aaad5cf4356ed6 Mon Sep 17 00:00:00 2001 From: Niclas Date: Wed, 5 Feb 2025 09:39:59 +0100 Subject: [PATCH 4/4] fix: remove unused --- source/php/Module/Map/Map.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/php/Module/Map/Map.php b/source/php/Module/Map/Map.php index 5c885d507..bdb693e80 100644 --- a/source/php/Module/Map/Map.php +++ b/source/php/Module/Map/Map.php @@ -15,8 +15,6 @@ class Map extends \Modularity\Module public $slug = 'map'; public $supports = array(); - protected $template = 'default'; - private TemplateControllerInterface $templateController; private TemplateResolverInterface $templateResolver;