|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_nemid\Plugin\WebformElement; |
| 4 | + |
| 5 | +use Drupal\Component\Utility\NestedArray; |
| 6 | +use Drupal\Core\Form\FormStateInterface; |
| 7 | + |
| 8 | +/** |
| 9 | + * Provides a abstract ServicePlatformenCompany Element. |
| 10 | + * |
| 11 | + * Implements the prepopulate logic. |
| 12 | + * |
| 13 | + * @see \Drupal\webform\Plugin\WebformElementBase |
| 14 | + * @see \Drupal\webform\Plugin\WebformElementInterface |
| 15 | + * @see \Drupal\webform\Annotation\WebformElement |
| 16 | + */ |
| 17 | +abstract class ServiceplatformenCompanyElementBase extends NemidElementBase { |
| 18 | + |
| 19 | + /** |
| 20 | + * {@inheritdoc} |
| 21 | + */ |
| 22 | + public function handleElementPrepopulate(array &$element, FormStateInterface &$form_state) { |
| 23 | + $prepopulateKey = $this->getPrepopulateFieldFieldKey(); |
| 24 | + $spCompanyData = NULL; |
| 25 | + |
| 26 | + // Handling P-number being changed/reset. |
| 27 | + if ($form_state->isRebuilding() && $this->isPNumberTrigger($form_state)) { |
| 28 | + // Resetting the current field value - it fetch is successfull, |
| 29 | + // it will be filled later. |
| 30 | + $element['#value'] = NULL; |
| 31 | + |
| 32 | + $pNumber = $this->getPNumberValue($form_state); |
| 33 | + |
| 34 | + // If another pNumber, resetting cached servicePlatformenCompanyData. |
| 35 | + if (strcmp($pNumber, $form_state->get('nemidCompanyPNumber')) !== 0) { |
| 36 | + $storage = $form_state->getStorage(); |
| 37 | + unset($storage['servicePlatformenCompanyData']); |
| 38 | + $form_state->setStorage($storage); |
| 39 | + |
| 40 | + // Saving the new P-number. |
| 41 | + $form_state->set('nemidCompanyPNumber', $pNumber); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Trying to fetch company data from cache. |
| 46 | + if ($form_state->has('servicePlatformenCompanyData')) { |
| 47 | + $spCompanyData = $form_state->get('servicePlatformenCompanyData'); |
| 48 | + } |
| 49 | + else { |
| 50 | + // Cached version does not exist. |
| 51 | + // |
| 52 | + // Making the request to the plugin, and storing the data, so that it's |
| 53 | + // available on the next element within the same webform render. |
| 54 | + if ($spCompanyData = $this->fetchCompanyData($form_state)) { |
| 55 | + $form_state->set('servicePlatformenCompanyData', $spCompanyData); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (!empty($spCompanyData)) { |
| 60 | + if (isset($spCompanyData[$prepopulateKey])) { |
| 61 | + $value = $spCompanyData[$prepopulateKey]; |
| 62 | + $element['#value'] = $value; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Makes request to serviceplatformen. |
| 69 | + * |
| 70 | + * Uses CVR or P-number based services depending on the available values/ |
| 71 | + * |
| 72 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 73 | + * Form state object. |
| 74 | + * |
| 75 | + * @return array|null |
| 76 | + * Company information or NULL if request could not be performed. |
| 77 | + */ |
| 78 | + private function fetchCompanyData(FormStateInterface $form_state) { |
| 79 | + $spCompanyData = NULL; |
| 80 | + |
| 81 | + // 1. Attempt to fetch data via CVR. |
| 82 | + /** @var \Drupal\os2web_nemlogin\Service\AuthProviderService $authProviderService */ |
| 83 | + $authProviderService = \Drupal::service('os2web_nemlogin.auth_provider'); |
| 84 | + /** @var \Drupal\os2web_nemlogin\Plugin\AuthProviderInterface $nemloginAuth */ |
| 85 | + $nemloginAuth = $authProviderService->getActivePlugin(); |
| 86 | + if ($nemloginAuth->isAuthenticated()) { |
| 87 | + $cvr = $nemloginAuth->fetchValue('cvr'); |
| 88 | + |
| 89 | + $pluginManager = \Drupal::service('plugin.manager.os2web_datalookup'); |
| 90 | + /** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\ServiceplatformenCVR $servicePlatformentCvrPlugin */ |
| 91 | + $servicePlatformentCvrPlugin = $pluginManager->createInstance('serviceplatformen_cvr'); |
| 92 | + |
| 93 | + if ($servicePlatformentCvrPlugin->isReady()) { |
| 94 | + $spCompanyData = $servicePlatformentCvrPlugin->getInfo($cvr); |
| 95 | + } |
| 96 | + } |
| 97 | + // 2. Attempt to fetch data via P-number. |
| 98 | + else { |
| 99 | + if ($form_state->isRebuilding() && $this->isPNumberTrigger($form_state)) { |
| 100 | + $pNumber = $this->getPNumberValue($form_state); |
| 101 | + |
| 102 | + if ($pNumber) { |
| 103 | + $pluginManager = \Drupal::service('plugin.manager.os2web_datalookup'); |
| 104 | + /** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\ServiceplatformenPNumber $servicePlatformentPNumberPlugin */ |
| 105 | + $servicePlatformentPNumberPlugin = $pluginManager->createInstance('serviceplatformen_p_number'); |
| 106 | + |
| 107 | + if ($servicePlatformentPNumberPlugin->isReady()) { |
| 108 | + $spCompanyData = $servicePlatformentPNumberPlugin->getInfo($pNumber); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Post fetch procedure - manipulationg the address fields. |
| 115 | + if (isset($spCompanyData['status']) && $spCompanyData['status']) { |
| 116 | + // Making composite field, company_address. |
| 117 | + $spCompanyData['company_address'] = $spCompanyData['company_street'] . ' ' . $spCompanyData['company_house_nr'] . ' ' . $spCompanyData['company_floor']; |
| 118 | + |
| 119 | + // Making composite field, city. |
| 120 | + $spCompanyData['company_city'] = $spCompanyData['company_zipcode'] . ' ' . $spCompanyData['company_city']; |
| 121 | + } |
| 122 | + |
| 123 | + return $spCompanyData; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Checks if form rebuild trigger is P-number fetch button. |
| 128 | + * |
| 129 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 130 | + * Form state object. |
| 131 | + * |
| 132 | + * @return bool |
| 133 | + * TRUE or FALSE. |
| 134 | + */ |
| 135 | + public function isPNumberTrigger(FormStateInterface $form_state) { |
| 136 | + if ($triggerElement = $form_state->getTriggeringElement()) { |
| 137 | + //Checking trigger element parent. |
| 138 | + $form_array = $form_state->getCompleteForm(); |
| 139 | + $triggerElParents = $triggerElement['#array_parents']; |
| 140 | + |
| 141 | + // Removing last element = current trigger elements. |
| 142 | + array_pop($triggerElParents); |
| 143 | + $parentElement = NestedArray::getValue($form_array, $triggerElParents); |
| 144 | + |
| 145 | + // Checking if parent element is 'os2forms_nemid_company_p_number' |
| 146 | + if ($parentElement && isset($parentElement['#type']) && $parentElement['#type'] == 'os2forms_nemid_company_p_number') { |
| 147 | + return TRUE; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return FALSE; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Gets the value from P-number field. |
| 156 | + * |
| 157 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 158 | + * Form state object. |
| 159 | + * |
| 160 | + * @return string |
| 161 | + * P-Number value from the field. |
| 162 | + */ |
| 163 | + public function getPNumberValue(FormStateInterface $form_state) { |
| 164 | + $triggerElement = $form_state->getTriggeringElement(); |
| 165 | + |
| 166 | + $pNumberParents = $triggerElement['#parents']; |
| 167 | + |
| 168 | + // Removing last element = current trigger elements. |
| 169 | + array_pop($pNumberParents); |
| 170 | + |
| 171 | + array_push($pNumberParents, 'p_number_value'); |
| 172 | + $pNumber = $form_state->getValue($pNumberParents); |
| 173 | + |
| 174 | + return $pNumber; |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments