|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_digital_post\EventSubscriber; |
| 4 | + |
| 5 | +use Drupal\entity_print\Event\PrintEvents; |
| 6 | +use Drupal\entity_print\Event\PrintHtmlAlterEvent; |
| 7 | +use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult; |
| 8 | +use Drupal\os2web_datalookup\LookupResult\CprLookupResult; |
| 9 | +use Drupal\webform\WebformSubmissionInterface; |
| 10 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 11 | +use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Used to alter the generated PDF to align with digital post requirements. |
| 15 | + */ |
| 16 | +final class Os2formsDigitalPostSubscriber implements EventSubscriberInterface { |
| 17 | + |
| 18 | + public function __construct(private readonly SessionInterface $session) { |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Post render entity_print event. |
| 23 | + * |
| 24 | + * Injects an envelope-window element containing address information. |
| 25 | + */ |
| 26 | + public function onPrintRender(PrintHtmlAlterEvent $event): void { |
| 27 | + $html = &$event->getHtml(); |
| 28 | + |
| 29 | + // Only modify HTML if there is exactly one submission. |
| 30 | + if (count($event->getEntities()) === 1) { |
| 31 | + $submission = $event->getEntities()[0]; |
| 32 | + if ($submission instanceof WebformSubmissionInterface) { |
| 33 | + // Check whether generation is for digital post. |
| 34 | + if ($context = $this->getDigitalPostContext($submission)) { |
| 35 | + $lookupResult = $context['lookupResult'] ?? NULL; |
| 36 | + if (!$lookupResult instanceof CprLookupResult && !$lookupResult instanceof CompanyLookupResult) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + $senderAddress = $context['senderAddress'] ?? ''; |
| 41 | + if (!is_string($senderAddress)) { |
| 42 | + $senderAddress = ''; |
| 43 | + } |
| 44 | + |
| 45 | + // Combine address parts. |
| 46 | + $streetAddress = $lookupResult->getStreet(); |
| 47 | + |
| 48 | + if ($lookupResult->getHouseNr()) { |
| 49 | + $streetAddress .= ' ' . $lookupResult->getHouseNr(); |
| 50 | + } |
| 51 | + |
| 52 | + $extendedAddress = ''; |
| 53 | + |
| 54 | + if ($lookupResult->getFloor()) { |
| 55 | + // Add a comma to align with danish address specifications. |
| 56 | + $streetAddress .= ','; |
| 57 | + $extendedAddress = $lookupResult->getFloor(); |
| 58 | + } |
| 59 | + if ($lookupResult->getApartmentNr()) { |
| 60 | + $extendedAddress .= ' ' . $lookupResult->getApartmentNr(); |
| 61 | + } |
| 62 | + |
| 63 | + // Generate address HTML. |
| 64 | + $addressHtml = '<div id="envelope-window-digital-post">'; |
| 65 | + if (!empty($senderAddress)) { |
| 66 | + $addressHtml .= '<div id="sender-address-digital-post">' . htmlspecialchars($senderAddress) . '</div>'; |
| 67 | + } |
| 68 | + $addressHtml .= '<div class="h-card">'; |
| 69 | + $addressHtml .= '<div class="p-name">' . htmlspecialchars($lookupResult->getName()) . '</div>'; |
| 70 | + if ($lookupResult instanceof CprLookupResult && $lookupResult->getCoName()) { |
| 71 | + $addressHtml .= '<div class="p-name p-co-name">c/o ' . htmlspecialchars($lookupResult->getCoName()) . '</div>'; |
| 72 | + } |
| 73 | + $addressHtml .= '<div>'; |
| 74 | + $addressHtml .= '<span class="p-street-address">' . htmlspecialchars($streetAddress) . '</span>'; |
| 75 | + if (!empty($extendedAddress)) { |
| 76 | + $addressHtml .= ' <span class="p-extended-address">' . htmlspecialchars($extendedAddress) . '</span>'; |
| 77 | + } |
| 78 | + $addressHtml .= '</div>'; |
| 79 | + $addressHtml .= '<div>'; |
| 80 | + $addressHtml .= '<span class="p-postal-code">' . htmlspecialchars($lookupResult->getPostalCode()) . '</span>'; |
| 81 | + $addressHtml .= ' <span class="p-locality">' . htmlspecialchars($lookupResult->getCity()) . '</span>'; |
| 82 | + $addressHtml .= '</div>'; |
| 83 | + $addressHtml .= '</div>'; |
| 84 | + $addressHtml .= '</div>'; |
| 85 | + |
| 86 | + // Insert address HTML immediately after body opening tag. |
| 87 | + $html = preg_replace('@<body[^>]*>@', '${0}' . $addressHtml, $html); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * {@inheritdoc} |
| 96 | + */ |
| 97 | + public static function getSubscribedEvents(): array { |
| 98 | + return [ |
| 99 | + PrintEvents::POST_RENDER => ['onPrintRender'], |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Indicate Digital Post context in the current session. |
| 105 | + */ |
| 106 | + public function setDigitalPostContext(WebformSubmissionInterface $submission, CompanyLookupResult|CprLookupResult $lookupResult, string $senderAddress = ''): void { |
| 107 | + $key = $this->createSessionKeyFromSubmission($submission); |
| 108 | + $this->session->set($key, [ |
| 109 | + 'lookupResult' => $lookupResult, |
| 110 | + 'senderAddress' => $senderAddress, |
| 111 | + ]); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Check for Digital Post context in the current session. |
| 116 | + * |
| 117 | + * @return array<string, mixed>|null |
| 118 | + * - 'lookupResult': the lookup result |
| 119 | + * - 'senderAddress': the sender address |
| 120 | + */ |
| 121 | + public function getDigitalPostContext(WebformSubmissionInterface $submission): ?array { |
| 122 | + $key = $this->createSessionKeyFromSubmission($submission); |
| 123 | + |
| 124 | + return $this->session->get($key); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Delete Digital Post context from the current request. |
| 129 | + */ |
| 130 | + public function deleteDigitalPostContext(WebformSubmissionInterface $submission): bool { |
| 131 | + $key = $this->createSessionKeyFromSubmission($submission); |
| 132 | + |
| 133 | + return (bool) $this->session->remove($key); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Create a session key from a submission that is unique to the submission. |
| 138 | + */ |
| 139 | + public function createSessionKeyFromSubmission(WebformSubmissionInterface $submission): string { |
| 140 | + // Due to cloning of submission during attachment logic, we cannot use |
| 141 | + // submission id or uuid. Webform serial, however, is copied along, so a |
| 142 | + // combination of webform id and serial is used for uniqueness. |
| 143 | + // @see \Drupal\os2forms_attachment\Element\AttachmentElement::overrideWebformSettings |
| 144 | + return 'digital_post_context_' . $submission->getWebform()->id() . '_' . $submission->serial(); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments