Example #1
0
 /**
  * Create a DNC entry for a lead
  *
  * @param Lead         $lead
  * @param string|array $channel  If an array with an ID, use the structure ['email' => 123]
  * @param string       $comments
  * @param int          $reason
  * @param bool         $flush
  *
  * @return boolean If a DNC entry is added or updated, returns true. If a DNC is already present
  *                 and has the specified reason, nothing is done and this returns false.
  */
 public function addDncForLead(Lead $lead, $channel, $comments = '', $reason = DoNotContact::BOUNCED, $flush = true)
 {
     $isContactable = $this->isContactable($lead, $channel);
     $reason = $this->determineReasonFromTag($reason);
     // If they don't have a DNC entry yet
     if ($isContactable === DoNotContact::IS_CONTACTABLE) {
         $dnc = new DoNotContact();
         if (is_array($channel)) {
             $channelId = reset($channel);
             $channel = key($channel);
             $dnc->setChannelId((int) $channelId);
         }
         $dnc->setChannel($channel);
         $dnc->setReason($reason);
         $dnc->setLead($lead);
         $dnc->setDateAdded(new \DateTime());
         $dnc->setComments($comments);
         $lead->addDoNotContactEntry($dnc);
         $this->getRepository()->saveEntity($lead);
         if ($flush) {
             $this->em->flush();
         }
         return true;
     } elseif ($isContactable !== $reason) {
         /** @var DoNotContact $dnc */
         foreach ($lead->getDoNotContact() as $dnc) {
             // Only update if the contact did not unsubscribe themselves
             if ($dnc->getChannel() === $channel && $dnc->getReason() !== DoNotContact::UNSUBSCRIBED) {
                 // Remove the outdated entry
                 $lead->removeDoNotContactEntry($dnc);
                 // Update the DNC entry
                 $dnc->setChannel($channel);
                 $dnc->setReason($reason);
                 $dnc->setLead($lead);
                 $dnc->setDateAdded(new \DateTime());
                 $dnc->setComments($comments);
                 // Re-add the entry to the lead
                 $lead->addDoNotContactEntry($dnc);
                 // Persist
                 $this->getRepository()->saveEntity($lead);
                 if ($flush) {
                     $this->em->flush();
                 }
                 return true;
             }
         }
     }
     return false;
 }
Example #2
0
 /**
  * Create a DNC entry for a lead.
  *
  * @param Lead         $lead
  * @param string|array $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
  *
  * @return bool|DoNotContact 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 addDncForLead(Lead $lead, $channel, $comments = '', $reason = DoNotContact::BOUNCED, $persist = true, $checkCurrentStatus = true)
 {
     // if !$checkCurrentStatus, assume is contactable due to already being valided
     $isContactable = $checkCurrentStatus ? $this->isContactable($lead, $channel) : DoNotContact::IS_CONTACTABLE;
     // If they don't have a DNC entry yet
     if ($isContactable === DoNotContact::IS_CONTACTABLE) {
         $dnc = new DoNotContact();
         if (is_array($channel)) {
             $channelId = reset($channel);
             $channel = key($channel);
             $dnc->setChannelId((int) $channelId);
         }
         $dnc->setChannel($channel);
         $dnc->setReason($reason);
         $dnc->setLead($lead);
         $dnc->setDateAdded(new \DateTime());
         $dnc->setComments($comments);
         $lead->addDoNotContactEntry($dnc);
         if ($persist) {
             // Use model saveEntity to trigger events for DNC change
             $this->saveEntity($lead);
         }
         return $dnc;
     } elseif ($isContactable !== $reason) {
         /** @var DoNotContact $dnc */
         foreach ($lead->getDoNotContact() as $dnc) {
             // Only update if the contact did not unsubscribe themselves
             if ($dnc->getChannel() === $channel && $dnc->getReason() !== DoNotContact::UNSUBSCRIBED) {
                 // Remove the outdated entry
                 $lead->removeDoNotContactEntry($dnc);
                 // Update the DNC entry
                 $dnc->setChannel($channel);
                 $dnc->setReason($reason);
                 $dnc->setLead($lead);
                 $dnc->setDateAdded(new \DateTime());
                 $dnc->setComments($comments);
                 // Re-add the entry to the lead
                 $lead->addDoNotContactEntry($dnc);
                 if ($persist) {
                     // Use model saveEntity to trigger events for DNC change
                     $this->saveEntity($lead);
                 }
                 return $dnc;
             }
         }
     }
     return false;
 }