|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup; |
| 4 | + |
| 5 | +use Drupal\Core\Form\FormStateInterface; |
| 6 | +use Drupal\Core\Messenger\MessengerInterface; |
| 7 | +use Drupal\Core\Render\Markup; |
| 8 | + |
| 9 | +/** |
| 10 | + * Defines a plugin for ServiceplatformenPNumber. |
| 11 | + * |
| 12 | + * @DataLookup( |
| 13 | + * id = "serviceplatformen_p_number", |
| 14 | + * label = @Translation("Serviceplatformen P-number"), |
| 15 | + * ) |
| 16 | + */ |
| 17 | +class ServiceplatformenPNumber extends ServiceplatformenBase { |
| 18 | + |
| 19 | + /** |
| 20 | + * {@inheritdoc} |
| 21 | + */ |
| 22 | + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
| 23 | + $form = parent::buildConfigurationForm($form, $form_state); |
| 24 | + $form['test_p_number'] = [ |
| 25 | + '#type' => 'textfield', |
| 26 | + '#title' => $this->t('Test P-number'), |
| 27 | + ]; |
| 28 | + return $form; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
| 35 | + parent::submitConfigurationForm($form, $form_state); |
| 36 | + if (!empty($form_state->getValue('test_p_number'))) { |
| 37 | + $response = $this->getInfo($form_state->getValue('test_p_number')); |
| 38 | + \Drupal::messenger()->addMessage( |
| 39 | + Markup::create('<pre>' . print_r($response, 1) . '</pre>'), |
| 40 | + $response['status'] ? MessengerInterface::TYPE_STATUS : MessengerInterface::TYPE_WARNING |
| 41 | + ); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Implementation of getLegalUnit call. |
| 47 | + * |
| 48 | + * @param string $pNumber |
| 49 | + * Requested P-Number. |
| 50 | + * |
| 51 | + * @return array |
| 52 | + * [status] => TRUE/FALSE |
| 53 | + * [cvr] => CVR code |
| 54 | + * [p_number] => P-number |
| 55 | + * [company_name] => Name of the organization |
| 56 | + * [company_street] => Street name |
| 57 | + * [company_house_nr] => House nr |
| 58 | + * [company_floor] => Floor nr |
| 59 | + * [company_zipcode] => ZIP code |
| 60 | + * [company_city] => City |
| 61 | + */ |
| 62 | + public function getProductionUnit($pNumber) { |
| 63 | + $request = $this->prepareRequest(); |
| 64 | + $request['GetProductionUnitRequest'] = [ |
| 65 | + 'level' => 1, |
| 66 | + 'UserId' => NULL, |
| 67 | + 'Password' => NULL, |
| 68 | + 'ProductionUnitIdentifier' => $pNumber, |
| 69 | + ]; |
| 70 | + return $this->query('getProductionUnit', $request); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Translates the fetch P-number information to a nice looking array. |
| 75 | + * |
| 76 | + * @param string $pNumber |
| 77 | + * Requested P-number. |
| 78 | + * |
| 79 | + * @return array |
| 80 | + * [status] => TRUE/FALSE |
| 81 | + * [cvr] => CVR code, |
| 82 | + * [p_number] => CVR code, |
| 83 | + * [company_name] => Name of the organization, |
| 84 | + * [company_street] => Street name, |
| 85 | + * [company_house_nr] => House nr, |
| 86 | + * [company_floor] => Floor nr, |
| 87 | + * [company_zipcode] => ZIP code |
| 88 | + * [company_city] => City, |
| 89 | + */ |
| 90 | + public function getInfo($pNumber) { |
| 91 | + $result = $this->getProductionUnit($pNumber); |
| 92 | + if ($result['status']) { |
| 93 | + $productionUnit = (array) $result['GetProductionUnitResponse']->ProductionUnit; |
| 94 | + return [ |
| 95 | + 'status' => TRUE, |
| 96 | + 'cvr' => $productionUnit['LegalUnitAffiliation']->LegalUnitIdentifier, |
| 97 | + 'p_number' => $productionUnit['ProductionUnitIdentifier'], |
| 98 | + 'company_name' => $productionUnit['ProductionUnitName']->name, |
| 99 | + 'company_street' => $productionUnit['AddressLocation']->AddressPostalExtended->StreetName, |
| 100 | + 'company_house_nr' => $productionUnit['AddressLocation']->AddressPostalExtended->StreetBuildingIdentifier, |
| 101 | + 'company_floor' => $productionUnit['AddressLocation']->AddressPostalExtended->FloorIdentifier, |
| 102 | + 'company_zipcode' => $productionUnit['AddressLocation']->AddressPostalExtended->PostCodeIdentifier, |
| 103 | + 'company_city' => $productionUnit['AddressLocation']->AddressPostalExtended->DistrictName, |
| 104 | + ]; |
| 105 | + } |
| 106 | + else { |
| 107 | + return $result; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments