Spaces:
No application file
No application file
File size: 6,294 Bytes
d2897cd |
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
<?php
declare(strict_types=1);
namespace Mautic\EmailBundle\Helper;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Helper\DTO\AddressDTO;
use Mautic\EmailBundle\Helper\Exception\OwnerNotFoundException;
use Mautic\EmailBundle\Helper\Exception\TokenNotFoundOrEmptyException;
use Mautic\LeadBundle\Entity\LeadRepository;
class FromEmailHelper
{
/**
* @var array<int,mixed[]>
*/
private array $owners = [];
private ?AddressDTO $defaultFrom = null;
/**
* @var mixed[]|null
*/
private ?array $lastOwner = null;
public function __construct(private CoreParametersHelper $coreParametersHelper, private LeadRepository $leadRepository)
{
}
public function setDefaultFrom(AddressDTO $from): void
{
$this->defaultFrom = $from;
}
/**
* @param mixed[] $contact
*/
public function getFromAddressConsideringOwner(AddressDTO $address, array $contact = null, Email $email = null): AddressDTO
{
// Reset last owner
$this->lastOwner = null;
// Check for token
if ($address->isEmailTokenized() || $address->isNameTokenized()) {
return $this->getEmailFromToken($address, $contact, true, $email);
}
if (!$contact) {
return $address;
}
try {
return $this->getFromEmailAsOwner($contact, $email);
} catch (OwnerNotFoundException) {
return $this->getFrom($email);
}
}
/**
* @param mixed[] $contact
*/
public function getFromAddressDto(AddressDTO $address, array $contact = null, Email $email = null): AddressDTO
{
// Reset last owner
$this->lastOwner = null;
// Check for token
if ($address->isEmailTokenized() || $address->isNameTokenized()) {
return $this->getEmailFromToken($address, $contact, false, $email);
}
return $address;
}
/**
* @return mixed[]
*
* @throws OwnerNotFoundException
*/
public function getContactOwner(int $userId, Email $email = null): array
{
// Reset last owner
$this->lastOwner = null;
if ($email) {
if (!$email->getUseOwnerAsMailer()) {
throw new OwnerNotFoundException("mailer_is_owner is not enabled for this email ({$email->getId()})");
}
} elseif (!$this->coreParametersHelper->get('mailer_is_owner')) {
throw new OwnerNotFoundException('mailer_is_owner is not enabled in global configuration');
}
if (isset($this->owners[$userId])) {
return $this->lastOwner = $this->owners[$userId];
}
if ($owner = $this->leadRepository->getLeadOwner($userId)) {
$this->owners[$userId] = $this->lastOwner = $owner;
return $owner;
}
throw new OwnerNotFoundException();
}
public function getSignature(): string
{
if (!$this->lastOwner) {
return '';
}
return $this->replaceSignatureTokens($this->lastOwner);
}
/**
* @param mixed[] $owner
*/
private function replaceSignatureTokens(array $owner): string
{
$signature = nl2br($owner['signature'] ?? '');
$signature = str_replace('|FROM_NAME|', $owner['first_name'].' '.$owner['last_name'], $signature);
foreach ($owner as $key => $value) {
$token = sprintf('|USER_%s|', strtoupper($key));
$signature = str_replace($token, (string) $value, (string) $signature);
}
return $signature;
}
private function getFrom(?Email $email): AddressDTO
{
if ($email && $email->getFromAddress()) {
return new AddressDTO($email->getFromAddress(), $email->getFromName());
}
return $this->getDefaultFrom();
}
private function getDefaultFrom(): AddressDTO
{
if ($this->defaultFrom) {
return $this->defaultFrom;
}
return $this->getSystemDefaultFrom();
}
private function getSystemDefaultFrom(): AddressDTO
{
$email = $this->coreParametersHelper->get('mailer_from_email');
$name = $this->coreParametersHelper->get('mailer_from_name') ?: null;
return new AddressDTO($email, $name);
}
/**
* @param mixed[] $contact
*/
private function getEmailFromToken(AddressDTO $address, array $contact = null, bool $asOwner = true, Email $email = null): AddressDTO
{
try {
if (!$contact) {
throw new TokenNotFoundOrEmptyException();
}
$name = $address->isNameTokenized() ? $address->getNameTokenValue($contact) : $address->getName();
} catch (TokenNotFoundOrEmptyException) {
$name = $this->defaultFrom ? $this->defaultFrom->getName() : $this->getSystemDefaultFrom()->getName();
}
try {
if (!$contact) {
throw new TokenNotFoundOrEmptyException();
}
$emailAddress = $address->isEmailTokenized() ? $address->getEmailTokenValue($contact) : $address->getEmail();
return new AddressDTO($emailAddress, $name);
} catch (TokenNotFoundOrEmptyException) {
if ($contact && $asOwner) {
try {
return $this->getFromEmailAsOwner($contact, $email);
} catch (OwnerNotFoundException) {
}
}
return $this->getDefaultFrom();
}
}
/**
* @param mixed[] $contact
*
* @throws OwnerNotFoundException
*/
private function getFromEmailAsOwner(array $contact, Email $email = null): AddressDTO
{
if (empty($contact['owner_id'])) {
throw new OwnerNotFoundException();
}
$owner = $this->getContactOwner((int) $contact['owner_id'], $email);
$ownerEmail = $owner['email'];
$ownerName = sprintf('%s %s', $owner['first_name'], $owner['last_name']);
// Decode apostrophes and other special characters
$ownerName = trim(html_entity_decode($ownerName, ENT_QUOTES));
return new AddressDTO($ownerEmail, $ownerName);
}
}
|