Skip to content

Commit

Permalink
OS-74 updating matrikula select population
Browse files Browse the repository at this point in the history
  • Loading branch information
stankut committed Jun 28, 2024
1 parent 3411356 commit 3e384ba
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 10 additions & 13 deletions modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

0 comments on commit 3e384ba

Please sign in to comment.