From 3e384badb1c818680a246e43f5db04987c382d4e Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 28 Jun 2024 15:50:47 +0300 Subject: [PATCH] OS-74 updating matrikula select population --- CHANGELOG.md | 1 + .../Element/DawaElementAddressMatrikula.php | 23 ++++++++----------- .../src/Entity/DatafordelerMatrikula.php | 10 +++----- .../DataLookup/DatafordelerDataLookup.php | 22 +++++++++++++----- .../DatafordelerDataLookupInterface.php | 12 +++++----- 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3f7690..526ef4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa Encrypts all elements if encryption enabled. - [#114](https://github.com/OS2Forms/os2forms/pull/114) Encrypted computed elements. +- [OS-74] Updating DAWA matrikula select with Datafordeler select ## [3.15.3] 2024-06-25 diff --git a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php index 2e75805..fdfc0ea 100644 --- a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php +++ b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php @@ -114,22 +114,19 @@ private static function getMatrikulaOptions($addressValue, array $element) { $addressAccessId = $address->getAccessAddressId(); // Find matrikula list from the houseid (husnummer): - $matrikulaIdList = $datafordelerLookup->getMatrikulaIds($addressAccessId); + $matrikulaId = $datafordelerLookup->getMatrikulaId($addressAccessId); - // Find Matrikula entry from matrikulas ID. - if (!empty($matrikulaIdList)) { - foreach ($matrikulaIdList as $matrikulaId) { - $matrikula = $datafordelerLookup->getMatrikulaEntry($matrikulaId); + // Find Matrikula entries from matrikulas ID. + if ($matrikulaId) { + $matrikulaEnties = $datafordelerLookup->getMatrikulaEntries($matrikulaId); + foreach ($matrikulaEnties as $matrikula) { + $matrikulaOption = $matrikula->getMatrikulaNumber() . ' ' . $matrikula->getOwnershipName(); - if ($matrikula) { - $matrikulaOption = $matrikula->getMatrikulaNumber() . ' ' . $matrikula->getOwnershipName(); - - if (isset($element['#remove_code']) && !$element['#remove_code']) { - $matrikulaOption .= ' (' . $matrikula->getOwnerLicenseCode() . ')'; - } - - $options[$matrikulaOption] = $matrikulaOption; + if (isset($element['#remove_code']) && !$element['#remove_code']) { + $matrikulaOption .= ' (' . $matrikula->getOwnerLicenseCode() . ')'; } + + $options[$matrikulaOption] = $matrikulaOption; } } } diff --git a/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php index 0e05a27..41be22a 100644 --- a/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php +++ b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php @@ -41,13 +41,9 @@ class DatafordelerMatrikula { * Address properties as JSON metadata. */ public function __construct(array $json) { - if (isset($json['features']) && is_array($json['features'])) { - $jordstykke = $json['features'][0]['properties']['jordstykke'][0]; - - $this->ownerLicenseCode = $jordstykke['properties']['ejerlavskode']; - $this->ownershipName = $jordstykke['properties']['ejerlavsnavn']; - $this->matrikulaNumber = $jordstykke['properties']['matrikelnummer']; - } + $this->ownerLicenseCode = $json['properties']['ejerlavskode']; + $this->ownershipName = $json['properties']['ejerlavsnavn']; + $this->matrikulaNumber = $json['properties']['matrikelnummer']; } /** diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php index a702e2d..6f7a7cf 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php @@ -2,6 +2,7 @@ namespace Drupal\os2forms_dawa\Plugin\os2web\DataLookup; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\os2forms_dawa\Entity\DatafordelerMatrikula; @@ -49,7 +50,7 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function getMatrikulaIds(string $addressAccessId) : array { + public function getMatrikulaId(string $addressAccessId) : ?string { $url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund"; $configuration = $this->getConfiguration(); @@ -64,16 +65,19 @@ public function getMatrikulaIds(string $addressAccessId) : array { $jsonDecoded = json_decode($json, TRUE); if (is_array($jsonDecoded)) { - return $jsonDecoded[0]['jordstykkeList']; + if (NestedArray::keyExists($jsonDecoded, [0, 'jordstykkeList', 0])) { + return NestedArray::getValue($jsonDecoded, [0, 'jordstykkeList', 0]); + } } - return []; + return NULL; } /** * {@inheritdoc} */ - public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula { + public function getMatrikulaEntries(string $matrikulaId) : array { + $matrikulaEntries = []; $url = "https://services.datafordeler.dk/Matriklen2/Matrikel/2.0.0/rest/SamletFastEjendom"; $configuration = $this->getConfiguration(); @@ -86,11 +90,17 @@ public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula ])->getBody(); $jsonDecoded = json_decode($json, TRUE); + if (is_array($jsonDecoded)) { - return new DatafordelerMatrikula($jsonDecoded); + if (NestedArray::keyExists($jsonDecoded, ['features', 0, 'properties', 'jordstykke'])) { + $jordstykker = NestedArray::getValue($jsonDecoded, ['features', 0, 'properties', 'jordstykke']); + foreach ($jordstykker as $jordstyk) { + $matrikulaEntries[] = new DatafordelerMatrikula($jordstyk); + } + } } - return NULL; + return $matrikulaEntries; } /** diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php index 4dd71a3..5498719 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php @@ -20,20 +20,20 @@ interface DatafordelerDataLookupInterface extends DataLookupInterface { * @param string $addressAccessId * Address to make search against. * - * @return array + * @return string|null * List if IDs. */ - public function getMatrikulaIds(string $addressAccessId) : array; + public function getMatrikulaId(string $addressAccessId) : ?string; /** - * Returns matrikule entry that is found byt this ID. + * Returns matrikula entries that is found byt this ID. * * @param string $matrikulaId * Id to make search against. * - * @return \Drupal\os2forms_dawa\Entity\DatafordelerMatrikula|null - * Matrikula entry or NULL. + * @return array + * Matrikula entries list. */ - public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula; + public function getMatrikulaEntries(string $matrikulaId) : array; }