Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: map module #802

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 22 additions & 151 deletions source/php/Module/Map/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

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\NullController;
use Modularity\Module\Map\TemplateController\TemplateControllerInterface;


class Map extends \Modularity\Module
{
public $slug = 'map';
public $supports = array();

protected $template = 'default';
private TemplateControllerInterface $templateController;
private TemplateResolverInterface $templateResolver;


public function init()
{
Expand All @@ -18,6 +28,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->templateResolver = new TemplateResolver(
new OpenStreetMapController(),
new EmbedController($this),
new NullController()
);
}

/**
Expand All @@ -32,157 +48,12 @@ 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';

//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, <a href="%s"> you are accepting GDPR and privacy policy</a>.', '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);
}

/**
Expand Down Expand Up @@ -244,13 +115,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';
}

/**
Expand Down
26 changes: 26 additions & 0 deletions source/php/Module/Map/Resolvers/TemplateResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Modularity\Module\Map\Resolvers;

use Modularity\Module\Map\TemplateController\TemplateControllerInterface;

class TemplateResolver implements TemplateResolverInterface
{
private array $templateControllers;

public function __construct(TemplateControllerInterface ...$templateControllers)
{
$this->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');
}
}
11 changes: 11 additions & 0 deletions source/php/Module/Map/Resolvers/TemplateResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Modularity\Module\Map\Resolvers;

use Modularity\Module\Map\TemplateController\TemplateControllerInterface;

interface TemplateResolverInterface
{
public function resolve(array $fields): TemplateControllerInterface;
}

92 changes: 92 additions & 0 deletions source/php/Module/Map/TemplateController/EmbedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Modularity\Module\Map\TemplateController;

use Modularity\Module\Map\Map;

/**
* Class EmbedController
* @package Modularity\Module\Map\TemplateController
*/
class EmbedController implements TemplateControllerInterface
{
private string $templateName = 'embed';

/**
* Constructor
*/
public function __construct(private Map $module)
{
}

/**
* Add data to the template.
*/
public function addData(array $data, array $fields): array
{
//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->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, <a href="%s"> you are accepting GDPR and privacy policy</a>.', '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;
}

/**
* Get the template name.
*/
public function getTemplateName(): string
{
return $this->templateName;
}

/**
* Check if this controller can handle the given fields.
*/
public function canHandle(array $fields): bool
{
return !empty($fields['map_type']) && $fields['map_type'] === 'default' && isset($fields['map_url']);
}
}
36 changes: 36 additions & 0 deletions source/php/Module/Map/TemplateController/NullController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Modularity\Module\Map\TemplateController;

/**
* Class NullController
* @package Modularity\Module\Map\TemplateController
*/
class NullController implements TemplateControllerInterface
{
private string $templateName = 'notFound';

/**
* Add data to the template.
*/
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;
}
}
Loading