forked from OS2Forms/os2forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOs2formsDigitalPostSubscriber.php
More file actions
127 lines (106 loc) · 4.8 KB
/
Os2formsDigitalPostSubscriber.php
File metadata and controls
127 lines (106 loc) · 4.8 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
<?php
namespace Drupal\os2forms_digital_post\EventSubscriber;
use Drupal\entity_print\Event\PrintEvents;
use Drupal\entity_print\Event\PrintHtmlAlterEvent;
use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;
use Drupal\os2web_datalookup\LookupResult\CprLookupResult;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Used to alter the generated PDF to align with digital post requirements.
*/
final class Os2formsDigitalPostSubscriber implements EventSubscriberInterface {
public function __construct(private readonly SessionInterface $session) {
}
/**
* Post render entity_print event.
*
* Injects an envelope-window element containing address information.
*/
public function onPrintRender(PrintHtmlAlterEvent $event): void {
$html = &$event->getHtml();
// Only modify HTML if there is exactly one submission.
if (count($event->getEntities()) === 1) {
$submission = $event->getEntities()[0];
if ($submission instanceof WebformSubmissionInterface) {
// Check whether generation is for digital post.
if ($lookupResult = $this->getDigitalPostContext($submission)) {
// Combine address parts.
$streetAddress = $lookupResult->getStreet();
if ($lookupResult->getHouseNr()) {
$streetAddress .= ' ' . $lookupResult->getHouseNr();
}
$extendedAddress = '';
if ($lookupResult->getFloor()) {
// Add a comma to align with danish address specifications.
$streetAddress .= ',';
$extendedAddress = $lookupResult->getFloor();
}
if ($lookupResult->getApartmentNr()) {
$extendedAddress .= ' ' . $lookupResult->getApartmentNr();
}
// Generate address HTML.
$addressHtml = '<div id="envelope-window-digital-post"><div class="h-card">';
$addressHtml .= '<div class="p-name">' . htmlspecialchars($lookupResult->getName()) . '</div>';
if ($lookupResult instanceof CprLookupResult && $lookupResult->getCoName()) {
$addressHtml .= '<div class="p-name p-co-name">c/o ' . htmlspecialchars($lookupResult->getCoName()) . '</div>';
}
$addressHtml .= '<div>';
$addressHtml .= '<span class="p-street-address">' . htmlspecialchars($streetAddress) . '</span>';
if (!empty($extendedAddress)) {
$addressHtml .= ' <span class="p-extended-address">' . htmlspecialchars($extendedAddress) . '</span>';
}
$addressHtml .= '</div>';
$addressHtml .= '<div>';
$addressHtml .= '<span class="p-postal-code">' . htmlspecialchars($lookupResult->getPostalCode()) . '</span>';
$addressHtml .= ' <span class="p-locality">' . htmlspecialchars($lookupResult->getCity()) . '</span>';
$addressHtml .= '</div>';
$addressHtml .= '</div>';
$addressHtml .= '</div>';
// Insert address HTML immediately after body opening tag.
$html = preg_replace('@<body[^>]*>@', '${0}' . $addressHtml, $html);
}
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
PrintEvents::POST_RENDER => ['onPrintRender'],
];
}
/**
* Indicate Digital Post context in the current session.
*/
public function setDigitalPostContext(WebformSubmissionInterface $submission, CompanyLookupResult|CprLookupResult $lookupResult): void {
$key = $this->createSessionKeyFromSubmission($submission);
$this->session->set($key, $lookupResult);
}
/**
* Check for Digital Post context in the current session.
*/
public function getDigitalPostContext(WebformSubmissionInterface $submission): CompanyLookupResult|CprLookupResult|null {
$key = $this->createSessionKeyFromSubmission($submission);
return $this->session->get($key);
}
/**
* Delete Digital Post context from the current request.
*/
public function deleteDigitalPostContext(WebformSubmissionInterface $submission): bool {
$key = $this->createSessionKeyFromSubmission($submission);
return (bool) $this->session->remove($key);
}
/**
* Create a session key from a submission that is unique to the submission.
*/
public function createSessionKeyFromSubmission(WebformSubmissionInterface $submission): string {
// Due to cloning of submission during attachment logic, we cannot use
// submission id or uuid. Webform serial, however, is copied along, so a
// combination of webform id and serial is used for uniqueness.
// @see \Drupal\os2forms_attachment\Element\AttachmentElement::overrideWebformSettings
return 'digital_post_context_' . $submission->getWebform()->id() . '_' . $submission->serial();
}
}