-
Notifications
You must be signed in to change notification settings - Fork 68
/
SendNotification.php
95 lines (73 loc) · 3.16 KB
/
SendNotification.php
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
<?php
namespace verbb\formie\jobs;
use verbb\formie\Formie;
use verbb\formie\elements\Submission;
use Craft;
use craft\helpers\Json;
use Exception;
class SendNotification extends BaseJob
{
// Properties
// =========================================================================
public ?int $submissionId = null;
public ?array $submission = null;
public ?int $notificationId = null;
public ?array $notification = null;
public mixed $email = null;
// Public Methods
// =========================================================================
/**
* @inheritDoc
*/
public function execute($queue): void
{
$this->setProgress($queue, 0.25);
$notification = Formie::$plugin->getNotifications()->getNotificationById($this->notificationId);
// Be sure to fetch spam submissions too, if we have Formie set to email those
$submission = Submission::find()->id($this->submissionId)->isSpam(null)->one();
if (!$notification) {
throw new Exception('Unable to find notification: ' . $this->notificationId . '.');
}
if (!$submission) {
throw new Exception('Unable to find submission: ' . $this->submissionId . '.');
}
$this->setProgress($queue, 0.5);
// Ensure we set the correct language for a potential CLI request
Craft::$app->language = $submission->getSite()->language;
Craft::$app->set('locale', Craft::$app->getI18n()->getLocaleById($submission->getSite()->language));
Craft::$app->getSites()->setCurrentSite($submission->getSite());
$this->submission = $submission->toArray();
$this->notification = $notification->toArray();
$this->notification['content'] = $notification->getParsedContent();
// Add a little extra info for submission fields
if ($fieldLayout = $submission->getFieldLayout()) {
foreach ($fieldLayout->getCustomFields() as $field) {
$this->submission['fields'][] = [
'type' => get_class($field),
'handle' => $field->handle,
'settings' => $field->settings,
'value' => $submission->getFieldValue($field->handle),
];
}
}
$this->setProgress($queue, 0.75);
$sentResponse = Formie::$plugin->getSubmissions()->sendNotificationEmail($notification, $submission, $this);
$success = $sentResponse['success'] ?? false;
$error = $sentResponse['error'] ?? false;
if ($error) {
// Check if we should send the nominated admin(s) an email about this error.
Formie::$plugin->getEmails()->sendFailAlertEmail($notification, $submission, $sentResponse);
throw new Exception('Failed to send notification email: ' . Json::encode($sentResponse) . '.');
}
$this->setProgress($queue, 1);
}
// Protected Methods
// =========================================================================
/**
* @inheritDoc
*/
protected function defaultDescription(): string
{
return Craft::t('formie', 'Sending form notification.');
}
}