Spaces:
No application file
No application file
File size: 6,706 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 |
<?php
namespace Mautic\LeadBundle\Model;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\PersistentCollection;
use Mautic\CoreBundle\Model\MauticModelInterface;
use Mautic\LeadBundle\Entity\DoNotContact as DNC;
use Mautic\LeadBundle\Entity\DoNotContactRepository;
use Mautic\LeadBundle\Entity\Lead;
class DoNotContact implements MauticModelInterface
{
public function __construct(
protected LeadModel $leadModel,
protected DoNotContactRepository $dncRepo
) {
}
/**
* Remove a Lead's DNC entry based on channel.
*
* @param int $contactId
* @param string $channel
* @param bool|true $persist
* @param int|null $reason
*/
public function removeDncForContact($contactId, $channel, $persist = true, $reason = null): bool
{
$contact = $this->leadModel->getEntity($contactId);
/** @var DNC $dnc */
foreach ($contact->getDoNotContact() as $dnc) {
if ($dnc->getChannel() === $channel) {
// Skip if reason doesn't match
// Some integrations (Sugar CRM) can use both reasons (unsubscribed, bounced)
if ($reason && $dnc->getReason() != $reason) {
continue;
}
$contact->removeDoNotContactEntry($dnc);
if ($persist) {
$this->leadModel->saveEntity($contact);
}
return true;
}
}
return false;
}
/**
* Create a DNC entry for a lead.
*
* @param Lead|int|null $contactId
* @param string|mixed[] $channel If an array with an ID, use the structure ['email' => 123]
* @param string $comments
* @param int $reason Must be a class constant from the DoNotContact class
* @param bool $persist
* @param bool $checkCurrentStatus
* @param bool $allowUnsubscribeOverride
*
* @return bool|DNC If a DNC entry is added or updated, returns the DoNotContact object. If a DNC is already present
* and has the specified reason, nothing is done and this returns false
*/
public function addDncForContact(
$contactId,
$channel,
$reason = DNC::BOUNCED,
$comments = '',
$persist = true,
$checkCurrentStatus = true,
$allowUnsubscribeOverride = false
) {
$dnc = null;
$contact = $this->leadModel->getEntity($contactId);
if (null === $contact) {
// Contact not found, nothing to do
return false;
}
// if !$checkCurrentStatus, assume is contactable due to already being validated
$isContactable = ($checkCurrentStatus) ? $this->isContactable($contact, $channel) : DNC::IS_CONTACTABLE;
/** @var ArrayCollection<int, DNC> $dncEntities */
$dncEntities = new ArrayCollection();
// If they don't have a DNC entry yet
if (DNC::IS_CONTACTABLE === $isContactable) {
$dnc = $dncEntities[] = $this->createDncRecord($contact, $channel, $reason, $comments);
} elseif ($isContactable !== $reason) {
// Or if the given reason is different than the stated reason
$dncEntities = $contact->getDoNotContact();
foreach ($dncEntities as $dnc) {
// Only update if the contact did not unsubscribe themselves or if the code forces it
$allowOverride = ($allowUnsubscribeOverride || DNC::UNSUBSCRIBED !== $dnc->getReason());
// Only update if the contact did not unsubscribe themselves
if ($allowOverride && $dnc->getChannel() === $channel) {
// Note the outdated entry for listeners
$contact->removeDoNotContactEntry($dnc);
// Update the entry with the latest
$this->updateDncRecord($dnc, $contact, $channel, $reason, $comments);
break;
}
}
}
if (null !== $dnc && $persist) {
// Use model saveEntity to trigger events for DNC change
$this->leadModel->saveEntity($contact);
$this->dncRepo->detachEntities($dncEntities->toArray());
// need to force a collection to load items in the next call.
$collection = $contact->getDoNotContact();
if ($collection instanceof PersistentCollection) {
$collection->setInitialized(false);
}
}
return $dnc;
}
/**
* @param string $channel
*
* @return int
*
* @see DNC This method can return boolean false, so be
* sure to always compare the return value against
* the class constants of DoNotContact
*/
public function isContactable(Lead $contact, $channel)
{
if (is_array($channel)) {
$channel = key($channel);
}
$dncEntries = $this->dncRepo->getEntriesByLeadAndChannel($contact, $channel);
// If the lead has no entries in the DNC table, we're good to go
if (empty($dncEntries)) {
return DNC::IS_CONTACTABLE;
}
foreach ($dncEntries as $dnc) {
if (DNC::IS_CONTACTABLE !== $dnc->getReason()) {
return $dnc->getReason();
}
}
return DNC::IS_CONTACTABLE;
}
public function createDncRecord(Lead $contact, $channel, $reason, $comments = null): DNC
{
$dnc = new DNC();
if (is_array($channel)) {
$channelId = reset($channel);
$channel = key($channel);
$dnc->setChannelId((int) $channelId);
}
$dnc->setChannel($channel);
$dnc->setReason($reason);
$dnc->setLead($contact);
$dnc->setDateAdded(new \DateTime());
$dnc->setComments($comments);
$contact->addDoNotContactEntry($dnc);
return $dnc;
}
public function updateDncRecord(DNC $dnc, Lead $contact, $channel, $reason, $comments = null): void
{
// Update the DNC entry
$dnc->setChannel($channel);
$dnc->setReason($reason);
$dnc->setLead($contact);
$dnc->setDateAdded(new \DateTime());
$dnc->setComments($comments);
// Re-add the entry to the lead
$contact->addDoNotContactEntry($dnc);
}
/**
* @return DoNotContactRepository
*/
public function getDncRepo()
{
return $this->dncRepo;
}
}
|