Skip to content

Commit e07f404

Browse files
authored
Merge pull request #113 from OS2Forms/release/3.15.3
Release/3.15.3
2 parents c360e61 + 9eed4d9 commit e07f404

File tree

7 files changed

+309
-21
lines changed

7 files changed

+309
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ before starting to add changes. Use example [placed in the end of the page](#exa
1111

1212
## [Unreleased]
1313

14+
## [3.15.3] 2024-06-25
15+
16+
- [OS-74] Replacing DAWA matrikula select with Datafordeler select
17+
1418
## [3.15.2] 2024-05-27
1519

1620
- [#108](https://github.com/OS2Forms/os2forms/pull/108)

modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,14 @@ public static function getCompositeElements(array $element) {
9494
* Array of matrikula options key and the values are identical.
9595
*/
9696
private static function getMatrikulaOptions($addressValue, array $element) {
97+
$options = [];
98+
9799
/** @var \Drupal\os2forms_dawa\Service\DawaService $dawaService */
98100
$dawaService = \Drupal::service('os2forms_dawa.service');
99101

102+
/** @var \Drupal\os2forms_dawa\Plugin\os2web\DataLookup\DatafordelerDataLookupInterface $datafordelerLookup */
103+
$datafordelerLookup = \Drupal::service('plugin.manager.os2web_datalookup')->createInstance('datafordeler_data_lookup');
104+
100105
// Getting address.
101106
$addressParams = new ParameterBag();
102107
$addressParams->set('q', $addressValue);
@@ -106,29 +111,30 @@ private static function getMatrikulaOptions($addressValue, array $element) {
106111
$address = $dawaService->getSingleAddress($addressParams);
107112

108113
if ($address) {
109-
// Getting matrikula options.
110-
$matrikulaParams = new ParameterBag();
111-
// Getting municipality code from address.
112-
if ($municipality_code = $address->getMunicipalityCode()) {
113-
$matrikulaParams->set('limit_by_municipality', $municipality_code);
114-
}
115-
// Getting property nr from address.
116-
if ($property_nr = $address->getPropertyNumber()) {
117-
$matrikulaParams->set('limit_by_property', $property_nr);
118-
}
119-
// If the matrikula option must not have the code.
120-
if (isset($element['#remove_code'])) {
121-
$matrikulaParams->set('remove_code', $element['#remove_code']);
122-
}
114+
$addressAccessId = $address->getAccessAddressId();
123115

124-
// Get the options.
125-
$matrikulaOptions = $dawaService->getMatrikulaMatches($matrikulaParams);
116+
// Find matrikula list from the houseid (husnummer):
117+
$matrikulaIdList = $datafordelerLookup->getMatrikulaIds($addressAccessId);
126118

127-
// Use values as keys.
128-
return array_combine($matrikulaOptions, $matrikulaOptions);
119+
// Find Matrikula entry from matrikulas ID.
120+
if (!empty($matrikulaIdList)) {
121+
foreach ($matrikulaIdList as $matrikulaId) {
122+
$matrikula = $datafordelerLookup->getMatrikulaEntry($matrikulaId);
123+
124+
if ($matrikula) {
125+
$matrikulaOption = $matrikula->getMatrikulaNumber() . ' ' . $matrikula->getOwnershipName();
126+
127+
if (isset($element['#remove_code']) && !$element['#remove_code']) {
128+
$matrikulaOption .= ' (' . $matrikula->getOwnerLicenseCode() . ')';
129+
}
130+
131+
$options[$matrikulaOption] = $matrikulaOption;
132+
}
133+
}
134+
}
129135
}
130136

131-
return [];
137+
return $options;
132138
}
133139

134140
/**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_dawa\Entity;
4+
5+
/**
6+
* Class DatafordelerMatrikula.
7+
*
8+
* Wrapper class for Datafordeler matrikula object that easies
9+
* the matrikula property access.
10+
*/
11+
class DatafordelerMatrikula {
12+
13+
/**
14+
* Owner licence code / ejerlavskode.
15+
*
16+
* @var string
17+
*/
18+
protected string $ownerLicenseCode;
19+
20+
/**
21+
* Ownership name / ejerlavsnavn.
22+
*
23+
* @var string
24+
*/
25+
protected string $ownershipName;
26+
27+
28+
/**
29+
* Matrikula number / matrikelnummer.
30+
*
31+
* @var string
32+
*/
33+
protected string $matrikulaNumber;
34+
35+
/**
36+
* DawaAddress constructor.
37+
*
38+
* Fills the property from the provided JSON metadata.
39+
*
40+
* @param array $json
41+
* Address properties as JSON metadata.
42+
*/
43+
public function __construct(array $json) {
44+
if (isset($json['features']) && is_array($json['features'])) {
45+
$jordstykke = $json['features'][0]['properties']['jordstykke'][0];
46+
47+
$this->ownerLicenseCode = $jordstykke['properties']['ejerlavskode'];
48+
$this->ownershipName = $jordstykke['properties']['ejerlavsnavn'];
49+
$this->matrikulaNumber = $jordstykke['properties']['matrikelnummer'];
50+
}
51+
}
52+
53+
/**
54+
* Returns owner licence code.
55+
*
56+
* @return string
57+
* Owners licence code.
58+
*/
59+
public function getOwnerLicenseCode(): string {
60+
return $this->ownerLicenseCode;
61+
}
62+
63+
/**
64+
* Returns ownership name.
65+
*
66+
* @return string
67+
* ownership name.
68+
*/
69+
public function getOwnershipName(): string {
70+
return $this->ownershipName;
71+
}
72+
73+
/**
74+
* Returns makrikula number.
75+
*
76+
* @return string
77+
* Matrikula number
78+
*/
79+
public function getMatrikulaNumber(): string {
80+
return $this->matrikulaNumber;
81+
}
82+
83+
}

modules/os2forms_dawa/src/Entity/DawaAddress.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class DawaAddress {
4545
*/
4646
protected $longitude;
4747

48+
/**
49+
* Address access ID.
50+
*
51+
* @var string
52+
*/
53+
protected $accessAddressId;
54+
4855
/**
4956
* DawaAddress constructor.
5057
*
@@ -61,6 +68,7 @@ public function __construct(array $json) {
6168
$this->propertyNumber = $json['adgangsadresse']['esrejendomsnr'];
6269
$this->longitude = $json['adgangsadresse']['adgangspunkt']['koordinater'][0];
6370
$this->latitude = $json['adgangsadresse']['adgangspunkt']['koordinater'][1];
71+
$this->accessAddressId = $json['adgangsadresse']['id'];
6472
}
6573
}
6674

@@ -114,4 +122,14 @@ public function getLongitude() {
114122
return $this->longitude;
115123
}
116124

125+
/**
126+
* Gets Address access ID.
127+
*
128+
* @return string
129+
* Address access ID.
130+
*/
131+
public function getAccessAddressId() {
132+
return $this->accessAddressId;
133+
}
134+
117135
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_dawa\Plugin\os2web\DataLookup;
4+
5+
use Drupal\Core\Form\FormStateInterface;
6+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7+
use Drupal\os2forms_dawa\Entity\DatafordelerMatrikula;
8+
use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupBase;
9+
use GuzzleHttp\ClientInterface;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
12+
/**
13+
* Defines a plugin for Datafordeler Data.
14+
*
15+
* @DataLookup(
16+
* id = "datafordeler_data_lookup",
17+
* label = @Translation("Datafordeler Address Lookup"),
18+
* )
19+
*/
20+
class DatafordelerDataLookup extends DataLookupBase implements DatafordelerDataLookupInterface, ContainerFactoryPluginInterface {
21+
22+
/**
23+
* The HTTP client to fetch the feed data with.
24+
*
25+
* @var \GuzzleHttp\ClientInterface
26+
*/
27+
protected $httpClient;
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function __construct(array $configuration, $plugin_id, $plugin_definition, ClientInterface $httpClient) {
33+
$this->httpClient = $httpClient;
34+
parent::__construct($configuration, $plugin_id, $plugin_definition);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
41+
return new static(
42+
$configuration,
43+
$plugin_id,
44+
$plugin_definition,
45+
$container->get('http_client'),
46+
);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getMatrikulaIds(string $addressAccessId) : array {
53+
$url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund";
54+
55+
$configuration = $this->getConfiguration();
56+
$json = $this->httpClient->request('GET', $url, [
57+
'query' => [
58+
'husnummer' => $addressAccessId,
59+
'status' => 7,
60+
'username' => $configuration['username'],
61+
'password' => $configuration['password'],
62+
],
63+
])->getBody();
64+
65+
$jsonDecoded = json_decode($json, TRUE);
66+
if (is_array($jsonDecoded)) {
67+
return $jsonDecoded[0]['jordstykkeList'];
68+
}
69+
70+
return [];
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula {
77+
$url = "https://services.datafordeler.dk/Matriklen2/Matrikel/2.0.0/rest/SamletFastEjendom";
78+
79+
$configuration = $this->getConfiguration();
80+
$json = $this->httpClient->request('GET', $url, [
81+
'query' => [
82+
'jordstykkeid' => $matrikulaId,
83+
'username' => $configuration['username'],
84+
'password' => $configuration['password'],
85+
],
86+
])->getBody();
87+
88+
$jsonDecoded = json_decode($json, TRUE);
89+
if (is_array($jsonDecoded)) {
90+
return new DatafordelerMatrikula($jsonDecoded);
91+
}
92+
93+
return NULL;
94+
}
95+
96+
/**
97+
* {@inheritdoc}
98+
*/
99+
public function defaultConfiguration() {
100+
return [
101+
'username' => '',
102+
'password' => '',
103+
] + parent::defaultConfiguration();
104+
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
110+
$form['username'] = [
111+
'#type' => 'textfield',
112+
'#title' => $this->t('Username for service calls'),
113+
'#default_value' => $this->configuration['username'],
114+
'#required' => TRUE,
115+
'#description' => $this->t('Username required for performing API requests'),
116+
];
117+
$form['password'] = [
118+
'#type' => 'textfield',
119+
'#title' => $this->t('Password for service calls'),
120+
'#default_value' => $this->configuration['password'],
121+
'#required' => TRUE,
122+
'#description' => $this->t('Password required for performing API requests'),
123+
];
124+
125+
return $form;
126+
}
127+
128+
/**
129+
* {@inheritdoc}
130+
*/
131+
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
132+
$configuration = $this->getConfiguration();
133+
$configuration['username'] = $form_state->getValue('username');
134+
$configuration['password'] = $form_state->getValue('password');
135+
$this->setConfiguration($configuration);
136+
}
137+
138+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_dawa\Plugin\os2web\DataLookup;
4+
5+
use Drupal\os2forms_dawa\Entity\DatafordelerMatrikula;
6+
use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterface;
7+
8+
/**
9+
* DatafordelerDataLookupInterface plugin interface.
10+
*
11+
* Provides functions for getting the plugin configuration values.
12+
*
13+
* @ingroup plugin_api
14+
*/
15+
interface DatafordelerDataLookupInterface extends DataLookupInterface {
16+
17+
/**
18+
* Returns list of ID for Matrikula / jordstykke related with this address.
19+
*
20+
* @param string $addressAccessId
21+
* Address to make search against.
22+
*
23+
* @return array
24+
* List if IDs.
25+
*/
26+
public function getMatrikulaIds(string $addressAccessId) : array;
27+
28+
/**
29+
* Returns matrikule entry that is found byt this ID.
30+
*
31+
* @param string $matrikulaId
32+
* Id to make search against.
33+
*
34+
* @return \Drupal\os2forms_dawa\Entity\DatafordelerMatrikula|null
35+
* Matrikula entry or NULL.
36+
*/
37+
public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula;
38+
39+
}

modules/os2forms_dawa/src/Service/DawaService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use Symfony\Component\HttpFoundation\ParameterBag;
88

99
/**
10-
* Class AuthProviderService.
10+
* DAWA API service class.
1111
*
12-
* @package Drupal\os2web_nemlogin\Service
12+
* @package Drupal\os2forms_dawa\Service
1313
*/
1414
class DawaService {
1515

0 commit comments

Comments
 (0)