/
FormBlockSubmissionEmailNotification.php
127 lines (113 loc) · 4.9 KB
/
FormBlockSubmissionEmailNotification.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
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 Concrete\Core\Express\Entry\Notifier\Notification;
use Concrete\Core\Entity\Attribute\Value\ExpressValue;
use Concrete\Core\Entity\Express\Entry;
use Concrete\Core\User\UserInfoRepository;
use Doctrine\ORM\EntityManager;
class FormBlockSubmissionEmailNotification extends AbstractFormBlockSubmissionNotification
{
protected $from;
protected $replyTo;
protected $attributeValues;
protected function getFromEmail(Entry $entry = null)
{
if (!isset($this->from)) {
$config = $this->app->make('config');
if ($entry && $config->get('concrete.email.form_block.'. $entry->getEntity()->getHandle() .'.address') && strstr($config->get('concrete.email.form_block.'. $entry->getEntity()->getHandle() .'.address'), '@')) {
$this->from = $config->get('concrete.email.form_block.'. $entry->getEntity()->getHandle() .'.address');
}
else{
if ($config->get('concrete.email.form_block.address') && strstr($config->get('concrete.email.form_block.address'), '@')) {
$this->from = $config->get('concrete.email.form_block.address');
} else {
$adminUserInfo = $this->app->make(UserInfoRepository::class)->getByID(USER_SUPER_ID);
$this->from = $adminUserInfo->getUserEmail();
}
}
}
return $this->from;
}
protected function getReplyToEmail(Entry $entry)
{
$entityManager = $this->app->make(EntityManager::class);
$replyToEmailAddress = $this->getFromEmail($entry);
$email = false;
if ($this->blockController->replyToEmailControlID) {
$control = $entityManager->getRepository('Concrete\Core\Entity\Express\Control\Control')
->findOneById($this->blockController->replyToEmailControlID);
if (is_object($control)) {
foreach ($this->getAttributeValues($entry) as $attribute) {
if ($attribute->getAttributeKey()->getAttributeKeyID() == $control->getAttributeKey()->getAttributeKeyID()) {
$email = $attribute->getValue();
}
}
if ($email) {
$replyToEmailAddress = $email;
}
}
}
return $replyToEmailAddress;
}
protected function getToEmail()
{
return $this->blockController->recipientEmail;
}
protected function getAttributeValues(Entry $entry)
{
$entity = $entry->getEntity();
if (!isset($this->attributeValues)) {
$this->attributeValues = $entity->getAttributeKeyCategory()->getAttributeValues($entry);
}
return $this->attributeValues;
}
/**
* @param ExpressValue[] $attributeValues
*/
public function setAttributeValues($attributeValues)
{
$this->attributeValues = $attributeValues;
}
protected function getFormName(Entry $entry)
{
$entity = $entry->getEntity();
$formName = $entity->getName();
return $formName;
}
public function notify(Entry $entry, $updateType)
{
$files = [];
if ($this->blockController->notifyMeOnSubmission) {
$mh = $this->app->make('mail');
$mh->to($this->getToEmail());
$mh->from($this->getFromEmail($entry));
$mh->replyto($this->getReplyToEmail($entry));
$mh->addParameter('entity', $entry->getEntity());
$mh->addParameter('formName', $this->getFormName($entry));
$mh->addParameter("dataSaveEnabled", $this->blockController->storeFormSubmission);
if (!$this->blockController->storeFormSubmission) {
//if save submitted data is not active we send also files as attachments in email becuase it will be removed after entry remove
foreach ($this->getAttributeValues($entry) as $attributeValue) {
if ($attributeValue->getAttributeTypeObject()->getAttributeTypeHandle() == "image_file") {
$file = $attributeValue->getValue();
if ($file) {
$files[] = $file;
$mh->addAttachment($file);
}
}
}
}
$mh->addParameter('attributes', $this->getAttributeValues($entry));
$mh->load('block_express_form_submission');
if (empty($mh->getSubject())) {
$mh->setSubject(t('Website Form Submission – %s', $this->getFormName($entry)));
}
$mh->sendMail();
//we have to delete the files as they are created automatically and are not attached to the entry save
if (!$this->blockController->storeFormSubmission) {
foreach ($files as $file) {
$file->delete();
}
}
}
}
}