From ef098cc2df2c75c32dd3c2506e21677abca51e58 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Tue, 19 Nov 2024 16:01:13 +1300 Subject: [PATCH] feat: save populated userform field data --- client/javascript/addressfinder.js | 79 ++++++++++++++++++-------- src/EditableAddressFinderField.php | 49 ++++++++++++++++ src/SubmittedAddressField.php | 32 +++++++++++ src/SubmittedAddressFieldExtension.php | 34 +++++++++++ 4 files changed, 170 insertions(+), 24 deletions(-) create mode 100644 src/SubmittedAddressField.php create mode 100644 src/SubmittedAddressFieldExtension.php diff --git a/client/javascript/addressfinder.js b/client/javascript/addressfinder.js index 8f1e997..7373281 100644 --- a/client/javascript/addressfinder.js +++ b/client/javascript/addressfinder.js @@ -1,4 +1,10 @@ document.addEventListener("DOMContentLoaded", function () { + const setValueOnElement = (element, value) => { + if (element) { + element.value = value; + } + }; + /** * Sets up the Address Finder field. * @param {HTMLElement} elem @@ -20,6 +26,7 @@ document.addEventListener("DOMContentLoaded", function () { if (address) { field = address.querySelector("input"); + address.style.display = "block"; } if (!field) { @@ -36,10 +43,10 @@ document.addEventListener("DOMContentLoaded", function () { toggle.style.display = "block"; } - address.style.display = "block"; - if (useManual && useManual.value !== "1") { - manual.style.display = "none"; + if (manual) { + manual.style.display = "none"; + } } // Create widget @@ -49,28 +56,45 @@ document.addEventListener("DOMContentLoaded", function () { // Update manual fields and hidden metadata widget.on("result:select", function (value, item) { - for (var i = 1; i <= 6; i++) { - var postalInput = manual.querySelector( - "input[name*=PostalLine" + i + "]" - ); - if (postalInput) { - postalInput.value = item["postal_line_" + i] || ""; + if (manual) { + for (var i = 1; i <= 6; i++) { + var postalInput = manual.querySelector( + "input[name*=PostalLine" + i + "]" + ); + if (postalInput) { + postalInput.value = item["postal_line_" + i] || ""; + } } - } - if (manual) { - manual.querySelector("input[name*=Suburb]").value = - item.suburb || ""; - manual.querySelector("input[name*=Region]").value = - item.region || ""; - manual.querySelector("input[name*=City]").value = - item.city || ""; - manual.querySelector("input[name*=Postcode]").value = - item.postcode || ""; - manual.querySelector("input[name*=Longitude]").value = - item.x || ""; - manual.querySelector("input[name*=Latitude]").value = - item.y || ""; + setValueOnElement( + manual.querySelector("input[name*=Suburb]"), + item.suburb || "" + ); + + setValueOnElement( + manual.querySelector("input[name*=Region]"), + item.region || "" + ); + + setValueOnElement( + manual.querySelector("input[name*=City]"), + item.city || "" + ); + + setValueOnElement( + manual.querySelector("input[name*=Postcode]"), + item.postcode || "" + ); + + setValueOnElement( + manual.querySelector("input[name*=Longitude]"), + item.x || "" + ); + + setValueOnElement( + manual.querySelector("input[name*=Latitude]"), + item.y || "" + ); } var event = new Event("addressselected", { bubbles: true }); @@ -81,6 +105,10 @@ document.addEventListener("DOMContentLoaded", function () { toggle?.addEventListener("click", function (e) { e.preventDefault(); + if (!manual) { + return; + } + if ( manual.style.display === "none" || manual.style.display === "" @@ -97,7 +125,9 @@ document.addEventListener("DOMContentLoaded", function () { // Focus event to hide manual input?.addEventListener("focus", function () { - manual.style.display = "none"; + if (manual) { + manual.style.display = "none"; + } }); }; @@ -106,5 +136,6 @@ document.addEventListener("DOMContentLoaded", function () { setupAddressFinderField(elem); }); + // @ts-ignore window.setupAddressFinderField = setupAddressFinderField; }); diff --git a/src/EditableAddressFinderField.php b/src/EditableAddressFinderField.php index 1c7a88a..7987a69 100644 --- a/src/EditableAddressFinderField.php +++ b/src/EditableAddressFinderField.php @@ -23,6 +23,8 @@ class EditableAddressFinderField extends EditableFormField 'ShowManualFields' => 'Boolean', ]; + private $recordData = null; + /** * @return FieldList */ @@ -58,4 +60,51 @@ public function getFormField() return $field; } + + + public function getValueFromData($data) + { + $this->recordData = isset($data[$this->Name]) ? $data[$this->Name] : null; + + if ($this->recordData && is_array($this->recordData)) { + if (isset($this->recordData['Address'])) { + return $this->recordData['Address']; + } + + // no address found so return all the manul fields imploded + $manual = []; + + foreach ($this->recordData as $key => $value) { + if ($key === 'Latitude' || $key === 'Longitude') { + continue; + } + + if ($key !== 'Address') { + $manual[] = $value; + } + } + + $latLng = ''; + + if (isset($this->recordData['Latitude']) && isset($this->recordData['Longitude'])) { + $latLng = ' (' . $this->recordData['Latitude'] . ', ' . $this->recordData['Longitude'] . ')'; + } + + return implode(', ', $this->manual) . $latLng; + } + + return ''; + } + + + public function getSubmittedFormField() + { + return SubmittedAddressField::create(); + } + + + public function getRecordData() + { + return $this->recordData; + } } diff --git a/src/SubmittedAddressField.php b/src/SubmittedAddressField.php new file mode 100644 index 0000000..cbc0695 --- /dev/null +++ b/src/SubmittedAddressField.php @@ -0,0 +1,32 @@ + 'Varchar(200)', + 'PostalLine2' => 'Varchar(200)', + 'PostalLine3' => 'Varchar(200)', + 'PostalLine4' => 'Varchar(200)', + 'PostalLine5' => 'Varchar(200)', + 'PostalLine6' => 'Varchar(200)', + 'Suburb' => 'Varchar(200)', + 'City' => 'Varchar(200)', + 'Postcode' => 'Varchar(200)', + 'Latitude' => 'Varchar(200)', + 'Longitude' => 'Varchar(200)' + ]; +} diff --git a/src/SubmittedAddressFieldExtension.php b/src/SubmittedAddressFieldExtension.php new file mode 100644 index 0000000..fdc63cd --- /dev/null +++ b/src/SubmittedAddressFieldExtension.php @@ -0,0 +1,34 @@ +getRecordData(); + + $keys = [ + 'PostalLine1', + 'PostalLine2', + 'PostalLine3', + 'PostalLine4', + 'PostalLine5', + 'PostalLine6', + 'Suburb', + 'City', + 'Postcode', + 'Latitude', + 'Longitude' + ]; + + foreach ($keys as $key) { + if (isset($recordData[$key])) { + $this->owner->$key = $recordData[$key]; + } + } + } +}