-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceplatformenCPRExtended.php
More file actions
275 lines (229 loc) · 8.67 KB
/
Copy pathServiceplatformenCPRExtended.php
File metadata and controls
275 lines (229 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Markup;
use Drupal\os2web_datalookup\LookupResult\CprLookupResult;
/**
* Defines a plugin for ServiceplatformenCPRExtended.
*
* @DataLookup(
* id = "serviceplatformen_cpr_extended",
* label = @Translation("Serviceplatformen CPR - extended (SF1520)"),
* group = "cpr_lookup"
* )
*/
class ServiceplatformenCPRExtended extends ServiceplatformenBase implements DataLookupCprInterface {
/**
* Status = DEAD.
*/
const CPR_STATUS_DEAD = 90;
/**
* Denmark country code.
*/
const DENMARK_COUNTRY_CODE = 5100;
/**
* Guardian other 1.
*/
const GUARDIAN_OTHER_1 = 5;
/**
* Guardian other 2.
*/
const GUARDIAN_OTHER_2 = 6;
/**
* Guardian mother 3.
*/
const GUARDIAN_MOTHER = 3;
/**
* Guardian father 4.
*/
const GUARDIAN_FATHER = 4;
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return array_merge(parent::defaultConfiguration(), [
'test_mode_fixed_cpr' => '',
]);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form = parent::buildConfigurationForm($form, $form_state);
$form['mode_fieldset']['test_mode_fixed_cpr'] = [
'#type' => 'textfield',
'#title' => $this->t('Fixed test CPR'),
'#default_value' => $this->configuration['test_mode_fixed_cpr'],
'#description' => $this->t('Fixed CPR that will be used for all requests to the serviceplatformen instead of the provided CPR.'),
'#states' => [
// Show the element only when running in test mode.
'visible' => [
'input[name="mode_selector"]' => ['value' => 1],
],
],
];
$form['test_cpr'] = [
'#type' => 'textfield',
'#title' => $this->t('Test CPR nr.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
parent::submitConfigurationForm($form, $form_state);
if (!empty($form_state->getValue('test_cpr'))) {
$cpr = $form_state->getValue('test_cpr');
}
if (!empty($cpr)) {
$cprResult = $this->lookup($cpr);
$response = (array) $cprResult;
\Drupal::messenger()->addMessage(
Markup::create('<pre>' . print_r($response, 1) . '</pre>'),
$cprResult->isSuccessful() ? MessengerInterface::TYPE_STATUS : MessengerInterface::TYPE_WARNING
);
}
}
/**
* {@inheritDoc}
*/
public function lookup(string $cpr, $fetchChildren = TRUE, $allowCprTestModeReplace = TRUE): CprLookupResult {
if ($this->configuration['mode_selector'] == 1 && $this->configuration['test_mode_fixed_cpr']) {
if ($allowCprTestModeReplace) {
$cpr = $this->configuration['test_mode_fixed_cpr'];
\Drupal::messenger()->addMessage(
$this->t("Test mode enabled, all CPR lookup requests are made against CPR: %cpr", ['%cpr' => $cpr]),
MessengerInterface::TYPE_STATUS
);
}
}
$request = $this->prepareRequest();
$request['PNR'] = str_replace('-', '', $cpr);
$result = $this->query('PersonLookup', $request);
$cprResult = new CprLookupResult();
// If all goes well, we return an address array.
if ($result['status']) {
$cprResult->setSuccessful();
$persondata = $result['persondata'];
$cprResult->setCpr($persondata->personnummer);
if ($persondata->status) {
if ($persondata->status->status == self::CPR_STATUS_DEAD) {
$cprResult->setAlive(FALSE);
}
else {
$cprResult->setAlive(TRUE);
}
}
if ($persondata->statsborgerskab) {
$cprResult->setCitizenshipCountryCode($persondata->statsborgerskab->landekode);
if ($persondata->statsborgerskab->landekode == self::DENMARK_COUNTRY_CODE) {
$cprResult->setCitizen(TRUE);
}
else {
$cprResult->setCitizen(FALSE);
}
$citizenshipDate = \DateTime::createFromFormat(\DateTimeInterface::RFC3339_EXTENDED, $persondata->statsborgerskab->statsborgerskabDato->dato);
$cprResult->setCitizenshipDate($citizenshipDate);
}
if ($persondata->navn) {
$cprResult->setName($persondata->navn->personadresseringsnavn ?? '');
}
if (isset($persondata->foedselsdato)) {
$birthDate = \DateTime::createFromFormat("Y-m-dP", $persondata->foedselsdato->dato);
$cprResult->setBirthDate($birthDate);
}
if ($persondata->tilmeldtDigitalpost) {
$cprResult->setDigitalPostSubscribed(TRUE);
}
if ($persondata->adressebeskyttelse) {
$cprResult->setNameAddressProtected($persondata->adressebeskyttelse->beskyttet ?? FALSE);
}
$address = $result['adresse'];
if ($address->aktuelAdresse) {
$cprResult->setStreet($address->aktuelAdresse->vejnavn ?? '');
$cprResult->setHouseNr(isset($address->aktuelAdresse->husnummer) ? ltrim($address->aktuelAdresse->husnummer, '0') : '');
$cprResult->setFloor($address->aktuelAdresse->etage ?? '');
$cprResult->setApartmentNr(isset($address->aktuelAdresse->sidedoer) ? ltrim($address->aktuelAdresse->sidedoer, '0') : '');
$cprResult->setPostalCode($address->aktuelAdresse->postnummer ?? '');
$cprResult->setCity($address->aktuelAdresse->postdistrikt ?? '');
$cprResult->setMunicipalityCode($address->aktuelAdresse->kommunekode ?? '');
// Composing full address in one line.
$address = $cprResult->getStreet();
if ($cprResult->getHouseNr()) {
$address .= ' ' . $cprResult->getHouseNr();
}
if ($cprResult->getFloor()) {
$address .= ' ' . $cprResult->getFloor();
}
if ($cprResult->getApartmentNr()) {
$address .= ' ' . $cprResult->getApartmentNr();
}
if ($cprResult->getPostalCode() && $cprResult->getCity()) {
$address .= ', ' . $cprResult->getPostalCode() . ' ' . $cprResult->getCity();
}
$cprResult->setAddress($address ?? '');
}
$relationship = $result['relationer'];
// Setting children.
$children = [];
if ($fetchChildren && isset($relationship->barn)) {
if (!is_array($relationship->barn)) {
$relationship->barn = [$relationship->barn];
}
foreach ($relationship->barn as $relationshipChild) {
$childCprResult = $this->lookup($relationshipChild->personnummer, FALSE, FALSE);
if ($childCprResult->isSuccessful() && $childCprResult->hasGuardian($cpr)) {
$child['name'] = !empty($childCprResult->getName()) ? $childCprResult->getName() : $childCprResult->getCpr();
$child = [
'cpr' => $relationshipChild->personnummer,
'name' => $child['name'],
'nameAddressProtected' => $childCprResult->isNameAddressProtected(),
];
$children[] = $child;
}
}
}
$cprResult->setChildren($children);
// Setting guardians.
$guardians = [];
if (isset($relationship->foraeldremyndighed)) {
if (!(is_array($relationship->foraeldremyndighed))) {
$relationship->foraeldremyndighed = [$relationship->foraeldremyndighed];
}
foreach ($relationship->foraeldremyndighed as $relationshipGuardian) {
if ($relationshipGuardian->foraeldremyndighedtype == self::GUARDIAN_MOTHER) {
$guardian = [
'type' => self::GUARDIAN_MOTHER,
'cpr' => $relationship->mor->personnummer ?? NULL,
];
$guardians[] = $guardian;
}
elseif ($relationshipGuardian->foraeldremyndighedtype == self::GUARDIAN_FATHER) {
$guardian = [
'type' => self::GUARDIAN_FATHER,
'cpr' => $relationship->far->personnummer ?? NULL,
];
$guardians[] = $guardian;
}
elseif ($relationshipGuardian->foraeldremyndighedtype == self::GUARDIAN_OTHER_1 || $relationshipGuardian->foraeldremyndighedtype == self::GUARDIAN_OTHER_2) {
$guardian = [
'type' => $relationshipGuardian->foraeldremyndighedtype,
'cpr' => $relationshipGuardian->relationPersonnummer,
];
$guardians[] = $guardian;
}
}
}
$cprResult->setGuardians($guardians);
// Leaving empty, no information in webservice.
$cprResult->setCoName('');
}
else {
$cprResult->setSuccessful(FALSE);
$cprResult->setErrorMessage($result['error']);
}
return $cprResult;
}
}