Skip to content

Commit

Permalink
feat: save populated userform field data
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Nov 19, 2024
1 parent 550d875 commit ef098cc
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 24 deletions.
79 changes: 55 additions & 24 deletions client/javascript/addressfinder.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +26,7 @@ document.addEventListener("DOMContentLoaded", function () {

if (address) {
field = address.querySelector("input");
address.style.display = "block";
}

if (!field) {
Expand All @@ -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
Expand All @@ -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 });
Expand All @@ -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 === ""
Expand All @@ -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";
}
});
};

Expand All @@ -106,5 +136,6 @@ document.addEventListener("DOMContentLoaded", function () {
setupAddressFinderField(elem);
});

// @ts-ignore
window.setupAddressFinderField = setupAddressFinderField;
});
49 changes: 49 additions & 0 deletions src/EditableAddressFinderField.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class EditableAddressFinderField extends EditableFormField
'ShowManualFields' => 'Boolean',
];

private $recordData = null;

/**
* @return FieldList
*/
Expand Down Expand Up @@ -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;
}
}
32 changes: 32 additions & 0 deletions src/SubmittedAddressField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace FullscreenInteractive\SilverStripe;

use SilverStripe\UserForms\Model\Submission\SubmittedFormField;

if (!class_exists(SubmittedFormField::class)) {
return;
}

class SubmittedAddressField extends SubmittedFormField
{
private static $table_name = 'SubmittedAddressField';

private static $extensions = [
SubmittedAddressFieldExtension::class
];

private static $db = [
'PostalLine1' => '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)'
];
}
34 changes: 34 additions & 0 deletions src/SubmittedAddressFieldExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace FullscreenInteractive\SilverStripe;

use SilverStripe\Core\Extension;

class SubmittedAddressFieldExtension extends Extension
{

public function onPopulationFromField($field)
{
$recordData = $field->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];
}
}
}
}

0 comments on commit ef098cc

Please sign in to comment.