From 83d72a4e61965da6a150736360eb8ff42e0ca3cf Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Wed, 19 Jun 2024 15:19:18 +0300 Subject: [PATCH] OS-74 - Replacing DAWA matrikula select with Datafordeler select --- CHANGELOG.md | 2 + .../Element/DawaElementAddressMatrikula.php | 47 +++-- .../src/Entity/DatafordelerMatrikula.php | 64 ++++++ .../os2forms_dawa/src/Entity/DawaAddress.php | 18 ++ .../DataLookup/DatafordelerDataLookup.php | 182 ++++++++++++++++++ .../DatafordelerDataLookupInterface.php | 38 ++++ .../os2forms_dawa/src/Service/DawaService.php | 4 +- 7 files changed, 334 insertions(+), 21 deletions(-) create mode 100644 modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php create mode 100644 modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php create mode 100644 modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index db0ded3f..3f71ffaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- [OS-74] Replacing DAWA matrikula select with Datafordeler select + ## [3.15.2] 2024-05-27 - [#108](https://github.com/OS2Forms/os2forms/pull/108) diff --git a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php index fcaf3c51..12cb99f1 100644 --- a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php +++ b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php @@ -3,6 +3,7 @@ namespace Drupal\os2forms_dawa\Element; use Drupal\Core\Form\FormStateInterface; +use Drupal\os2forms_dawa\Plugin\os2web\DataLookup\DatafordelerDataLookupInterface; use Drupal\webform\Element\WebformCompositeBase; use Symfony\Component\HttpFoundation\ParameterBag; @@ -94,9 +95,16 @@ public static function getCompositeElements(array $element) { * Array of matrikula options key and the values are identical. */ private static function getMatrikulaOptions($addressValue, array $element) { + $options = []; + /** @var \Drupal\os2forms_dawa\Service\DawaService $dawaService */ $dawaService = \Drupal::service('os2forms_dawa.service'); + /** @var \Drupal\os2forms_dawa\Service\DatafordelerService $datafordelerService */ + + /** @var DatafordelerDataLookupInterface $datafordelerLookup */ + $datafordelerLookup = \Drupal::service('plugin.manager.os2web_datalookup')->createInstance('datafordeler_data_lookup'); + // Getting address. $addressParams = new ParameterBag(); $addressParams->set('q', $addressValue); @@ -106,29 +114,30 @@ private static function getMatrikulaOptions($addressValue, array $element) { $address = $dawaService->getSingleAddress($addressParams); if ($address) { - // Getting matrikula options. - $matrikulaParams = new ParameterBag(); - // Getting municipality code from address. - if ($municipality_code = $address->getMunicipalityCode()) { - $matrikulaParams->set('limit_by_municipality', $municipality_code); - } - // Getting property nr from address. - if ($property_nr = $address->getPropertyNumber()) { - $matrikulaParams->set('limit_by_property', $property_nr); - } - // If the matrikula option must not have the code. - if (isset($element['#remove_code'])) { - $matrikulaParams->set('remove_code', $element['#remove_code']); - } + $addressAccessId = $address->getAccessAddressId(); - // Get the options. - $matrikulaOptions = $dawaService->getMatrikulaMatches($matrikulaParams); + // Find matrikula list from the houseid (husnummer): + $matrikulaIdList = $datafordelerLookup->getMatrikulaIds($addressAccessId); - // Use values as keys. - return array_combine($matrikulaOptions, $matrikulaOptions); + // Find Matrikula entry from matrikulas ID. + if (!empty($matrikulaIdList)) { + foreach ($matrikulaIdList as $matrikulaId) { + $matrikula = $datafordelerLookup->getMatrikulaEntry($matrikulaId); + + if ($matrikula) { + $matrikulaOption = $matrikula->getMatrikulaNumber() . ' ' . $matrikula->getOwnershipName(); + + if (isset($element['#remove_code']) && !$element['#remove_code']) { + $matrikulaOption .= ' (' . $matrikula->getOwnerLicenseCode() . ')'; + } + + $options[$matrikulaOption] = $matrikulaOption; + } + } + } } - return []; + return $options; } /** diff --git a/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php new file mode 100644 index 00000000..96ab322b --- /dev/null +++ b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php @@ -0,0 +1,64 @@ +ownerLicenseCode = $jordstykke['properties']['ejerlavskode']; + $this->ownershipName = $jordstykke['properties']['ejerlavsnavn']; + $this->matrikulaNumber = $jordstykke['properties']['matrikelnummer']; + } + } + + public function getOwnerLicenseCode(): string { + return $this->ownerLicenseCode; + } + + public function getOwnershipName(): string { + return $this->ownershipName; + } + + public function getMatrikulaNumber(): string { + return $this->matrikulaNumber; + } + + + +} diff --git a/modules/os2forms_dawa/src/Entity/DawaAddress.php b/modules/os2forms_dawa/src/Entity/DawaAddress.php index 723dde3e..89fe92a7 100644 --- a/modules/os2forms_dawa/src/Entity/DawaAddress.php +++ b/modules/os2forms_dawa/src/Entity/DawaAddress.php @@ -45,6 +45,13 @@ class DawaAddress { */ protected $longitude; + /** + * Address access ID. + * + * @var string + */ + protected $accessAddressId; + /** * DawaAddress constructor. * @@ -61,6 +68,7 @@ public function __construct(array $json) { $this->propertyNumber = $json['adgangsadresse']['esrejendomsnr']; $this->longitude = $json['adgangsadresse']['adgangspunkt']['koordinater'][0]; $this->latitude = $json['adgangsadresse']['adgangspunkt']['koordinater'][1]; + $this->accessAddressId = $json['adgangsadresse']['id']; } } @@ -114,4 +122,14 @@ public function getLongitude() { return $this->longitude; } + /** + * Gets Address access ID. + * + * @return string + * Address access ID. + */ + public function getAccessAddressId() { + return $this->accessAddressId; + } + } diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php new file mode 100644 index 00000000..4a5ef8d7 --- /dev/null +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php @@ -0,0 +1,182 @@ +httpClient = $httpClient; + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('http_client') + ); + } + + /** + * {@inheritdoc} + */ + public function getMatrikulaIds(string $addressAccessId) : array { + $url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund";// + + $configuration = $this->getConfiguration(); + $json = $this->httpClient->request('GET', $url, [ + 'query' => [ + 'husnummer' => $addressAccessId, + 'status' => 7, + 'username' => $configuration['username'], + 'password' => $configuration['password'] + ] + ])->getBody(); + + $jsonDecoded = json_decode($json, TRUE); + if (is_array($jsonDecoded)) { + return $jsonDecoded[0]['jordstykkeList']; + } + + return []; + } + + /** + * {@inheritdoc} + */ + public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula { + $url = "https://services.datafordeler.dk/Matriklen2/Matrikel/2.0.0/rest/SamletFastEjendom"; + + $configuration = $this->getConfiguration(); + $json = $this->httpClient->request('GET', $url, [ + 'query' => [ + 'jordstykkeid' => $matrikulaId, + 'username' => $configuration['username'], + 'password' => $configuration['password'] + ] + ])->getBody(); + + $jsonDecoded = json_decode($json, TRUE); + if (is_array($jsonDecoded)) { + return new DatafordelerMatrikula($jsonDecoded); + } + + return NULL; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'username' => '', + 'password' => '', + ] + parent::defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['username'] = [ + '#type' => 'textfield', + '#title' => $this->t('Username for service calls'), + '#default_value' => $this->configuration['username'], + '#required' => TRUE, + '#description' => $this->t('Username required for performing API requests'), + ]; + $form['password'] = [ + '#type' => 'textfield', + '#title' => $this->t('Password for service calls'), + '#default_value' => $this->configuration['password'], + '#required' => TRUE, + '#description' => $this->t('Password required for performing API requests'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + parent::validateConfigurationForm($form, $form_state); + +// // Validating 'address_autocomplete_path', 'block_autocomplete_path', +// // 'matrikula_autocomplete_path'. +// $elementsToValidate = [ +// 'address_autocomplete_path', +// 'block_autocomplete_path', +// 'matrikula_autocomplete_path', +// ]; +// foreach ($elementsToValidate as $elementKey) { +// $autocomplete_path = $form_state->getValue($elementKey); +// $json = file_get_contents($autocomplete_path); +// $jsonDecoded = json_decode($json, TRUE); +// if (empty($jsonDecoded)) { +// $form_state->setErrorByName($elementKey, $this->t('URL is not valid or it does not provide the result in the required format')); +// } +// else { +// $entry = reset($jsonDecoded); +// if (!array_key_exists('tekst', $entry)) { +// $form_state->setErrorByName($elementKey, $this->t('URL is not valid or it does not provide the result in the required format')); +// } +// } +// } +// +// // Validating address_api_path. +// $autocomplete_path = $form_state->getValue('address_api_path'); +// // Limiting the output. +// $json = file_get_contents($autocomplete_path . '?per_side=1'); +// $jsonDecoded = json_decode($json, TRUE); +// if (empty($jsonDecoded)) { +// $form_state->setErrorByName('address_api_path', $this->t('URL is not valid or it does not provide the result in the required format')); +// } +// else { +// $entry = reset($jsonDecoded); +// if (!array_key_exists('id', $entry)) { +// $form_state->setErrorByName('address_api_path', $this->t('URL is not valid or it does not provide the result in the required format')); +// } +// } + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $configuration = $this->getConfiguration(); + $configuration['username'] = $form_state->getValue('username'); + $configuration['password'] = $form_state->getValue('password'); + $this->setConfiguration($configuration); + } +} diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php new file mode 100644 index 00000000..fb24a715 --- /dev/null +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php @@ -0,0 +1,38 @@ +