8
8
use Drupal \Core \Entity \EntityTypeManagerInterface ;
9
9
use Drupal \Core \Mail \MailManagerInterface ;
10
10
use Drupal \maestro \Engine \MaestroEngine ;
11
+ use Drupal \os2forms_attachment \Element \AttachmentElement ;
11
12
use Drupal \os2forms_maestro_webform \Form \SettingsForm ;
12
13
use Drupal \os2forms_maestro_webform \Plugin \WebformHandler \NotificationHandler ;
13
14
use Drupal \webform \WebformSubmissionInterface ;
18
19
* Maestro helper.
19
20
*/
20
21
class MaestroHelper {
22
+ private const OS2FORMS_MAESTRO_WEBFORM_IS_NOTIFICATION = 'os2forms_maestro_webform_is_notification ' ;
23
+ private const OS2FORMS_MAESTRO_WEBFORM_NOTIFICATION_CONTENT = 'os2forms_maestro_webform_notification_content ' ;
21
24
/**
22
25
* The config.
23
26
*
@@ -83,7 +86,7 @@ public function maestroCanUserExecuteTaskAlter(bool &$returnValue, int $queueID,
83
86
$ assignments = explode (', ' , $ templateTask ['assigned ' ]);
84
87
85
88
$ knownAnonymousAssignments = array_map (
86
- static fn (string $ role ) => 'role:fixed: ' . $ role ,
89
+ static fn (string $ role ) => 'role:fixed: ' . $ role ,
87
90
array_filter ($ this ->config ->get ('known_anonymous_roles ' ) ?: [])
88
91
);
89
92
@@ -111,7 +114,11 @@ public function maestroCanUserExecuteTaskAlter(bool &$returnValue, int $queueID,
111
114
/**
112
115
* Handle submission notification.
113
116
*/
114
- private function handleSubmissionNotification (WebformSubmissionInterface $ submission , array $ templateTask , int $ queueID ): void {
117
+ private function handleSubmissionNotification (
118
+ WebformSubmissionInterface $ submission ,
119
+ array $ templateTask ,
120
+ int $ queueID
121
+ ): void {
115
122
$ data = $ submission ->getData ();
116
123
$ webform = $ submission ->getWebform ();
117
124
$ handlers = $ webform ->getHandlers ('os2forms_maestro_webform_notification ' );
@@ -151,10 +158,10 @@ private function handleSubmissionNotification(WebformSubmissionInterface $submis
151
158
}
152
159
153
160
if (filter_var ($ recipient , FILTER_VALIDATE_EMAIL )) {
154
- $ this ->sendNotificationEmail ($ recipient , $ subject , $ content );
161
+ $ this ->sendNotificationEmail ($ recipient , $ subject , $ content, $ submission );
155
162
}
156
163
else {
157
- $ this ->sendNotificationDigitalPost ($ recipient , $ subject , $ content );
164
+ $ this ->sendNotificationDigitalPost ($ recipient , $ subject , $ content, $ submission );
158
165
}
159
166
}
160
167
}
@@ -163,7 +170,12 @@ private function handleSubmissionNotification(WebformSubmissionInterface $submis
163
170
/**
164
171
* Send notification email.
165
172
*/
166
- private function sendNotificationEmail (string $ recipient , string $ subject , string $ content ): void {
173
+ private function sendNotificationEmail (
174
+ string $ recipient ,
175
+ string $ subject ,
176
+ string $ content ,
177
+ WebformSubmissionInterface $ submission
178
+ ): void {
167
179
$ result = $ this ->mailManager ->mail (
168
180
'os2forms_maestro_webform ' ,
169
181
'notification ' ,
@@ -174,25 +186,112 @@ private function sendNotificationEmail(string $recipient, string $subject, strin
174
186
'body ' => $ content ,
175
187
]
176
188
);
189
+
190
+ if (!$ result ['result ' ]) {
191
+ // @todo Log this error.
192
+ }
177
193
}
178
194
179
195
/**
180
196
* Send notification digital post.
181
197
*/
182
- private function sendNotificationDigitalPost (string $ recipient , string $ subject , string $ content ): void {
183
- $ this ->sendNotificationEmail (
184
- $ recipient . '@digital-post.example.com ' ,
185
- '(this should have been a digital post) ' . $ subject ,
186
- $ content );
198
+ private function sendNotificationDigitalPost (
199
+ string $ recipient ,
200
+ string $ subject ,
201
+ string $ content ,
202
+ WebformSubmissionInterface $ submission
203
+ ): void {
204
+ $ element = [
205
+ // Cf. AttachmentElement::getFileContent().
206
+ '#view_mode ' => 'html ' ,
207
+ '#export_type ' => 'pdf ' ,
208
+ ];
209
+
210
+ $ submission ->setData ($ submission ->getData () + [
211
+ self ::OS2FORMS_MAESTRO_WEBFORM_IS_NOTIFICATION => TRUE ,
212
+ self ::OS2FORMS_MAESTRO_WEBFORM_NOTIFICATION_CONTENT => $ content ,
213
+ ]);
214
+
215
+ $ content = AttachmentElement::getFileContent ($ element , $ submission );
216
+
217
+ // @todo Send real digital post
218
+ $ recipient .= '@digital-post.example.com ' ;
219
+ $ subject .= ' (digital post) ' ;
220
+
221
+ $ result = $ this ->mailManager ->mail (
222
+ 'os2forms_maestro_webform ' ,
223
+ 'notification ' ,
224
+ $ recipient ,
225
+ '' ,
226
+ [
227
+ 'subject ' => $ subject ,
228
+ 'body ' => $ content ,
229
+ 'attachments ' => [
230
+ [
231
+ 'filecontent ' => $ content ,
232
+ 'filename ' => 'stuff.pdf ' ,
233
+ 'filemime ' => 'application/pdf ' ,
234
+ ],
235
+ ],
236
+ ]
237
+ );
238
+
239
+ if (!$ result ['result ' ]) {
240
+ // @todo Log this error.
241
+ }
187
242
}
188
243
189
- public function mail (string $ key , array &$ message , array $ params )
190
- {
244
+ /**
245
+ * Implements hook_mail().
246
+ */
247
+ public function mail (string $ key , array &$ message , array $ params ) {
191
248
switch ($ key ) {
192
249
case 'notification ' :
193
250
$ message ['subject ' ] = $ params ['subject ' ];
194
251
$ message ['body ' ][] = Html::escape ($ params ['body ' ]);
252
+ if (isset ($ params ['attachments ' ])) {
253
+ foreach ($ params ['attachments ' ] as $ attachment ) {
254
+ $ message ['params ' ]['attachments ' ][] = $ attachment ;
255
+ }
256
+ }
195
257
break ;
196
258
}
197
259
}
260
+
261
+ /**
262
+ * Implements hook_preprocess_entity_print().
263
+ */
264
+ public function preprocessEntityPrint (array &$ variables ) {
265
+ $ submission = $ this ->getWebformSubmission ($ variables );
266
+ if (NULL === $ submission ) {
267
+ return ;
268
+ }
269
+ $ data = $ submission ->getData ();
270
+ if (TRUE !== ($ data [self ::OS2FORMS_MAESTRO_WEBFORM_IS_NOTIFICATION ] ?? FALSE )) {
271
+ return ;
272
+ }
273
+
274
+ $ variables ['content ' ] = [
275
+ '#markup ' => $ data [self ::OS2FORMS_MAESTRO_WEBFORM_NOTIFICATION_CONTENT ] ?? '' ,
276
+ ];
277
+ }
278
+
279
+ /**
280
+ * Dig for webform submission in variables.
281
+ */
282
+ private function getWebformSubmission (array $ variables ): ?WebformSubmissionInterface {
283
+ $ iterator = new \RecursiveArrayIterator ($ variables );
284
+ $ recursive = new \RecursiveIteratorIterator (
285
+ $ iterator ,
286
+ \RecursiveIteratorIterator::SELF_FIRST
287
+ );
288
+ foreach ($ recursive as $ key => $ value ) {
289
+ if ('#webform_submission ' === $ key && $ value instanceof WebformSubmissionInterface) {
290
+ return $ value ;
291
+ }
292
+ }
293
+
294
+ return NULL ;
295
+ }
296
+
198
297
}
0 commit comments