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..2083fb1 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,31 +50,30 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function getMatrikulaIds(string $addressAccessId) : array { - $url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund"; + public function getMatrikulaId(string $addressAccessId) : ?string { + $url = "https://services.datafordeler.dk/DAR/DAR/3.0.0/rest/husnummerTilJordstykke"; - $configuration = $this->getConfiguration(); $json = $this->httpClient->request('GET', $url, [ 'query' => [ - 'husnummer' => $addressAccessId, - 'status' => 7, - 'username' => $configuration['username'], - 'password' => $configuration['password'], + 'husnummerid' => $addressAccessId, ], ])->getBody(); $jsonDecoded = json_decode($json, TRUE); if (is_array($jsonDecoded)) { - return $jsonDecoded[0]['jordstykkeList']; + if (NestedArray::keyExists($jsonDecoded, ['gældendeJordstykke', 'jordstykkeLokalId'])) { + return NestedArray::getValue($jsonDecoded, ['gældendeJordstykke', 'jordstykkeLokalId']); + } } - 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 +86,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..1694724 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php @@ -2,7 +2,6 @@ namespace Drupal\os2forms_dawa\Plugin\os2web\DataLookup; -use Drupal\os2forms_dawa\Entity\DatafordelerMatrikula; use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterface; /** @@ -20,20 +19,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; }