Example #1
0
 /**
  * Unsubscribe email from mailing
  *
  * @param array $data
  * @return bool
  * @throws \Bitrix\Main\ArgumentException
  */
 public static function unsubscribe($data)
 {
     $result = false;
     if (isset($data['TEST']) && $data['TEST'] == 'Y') {
         return true;
     }
     $posting = null;
     if ($data['RECIPIENT_ID']) {
         $postingDb = PostingRecipientTable::getList(array('select' => array('POSTING_ID', 'POSTING_MAILING_ID' => 'POSTING.MAILING_ID'), 'filter' => array('ID' => $data['RECIPIENT_ID'], 'EMAIL' => $data['EMAIL'])));
         $posting = $postingDb->fetch();
     }
     $mailingDb = MailingTable::getList(array('select' => array('ID'), 'filter' => array('ID' => $data['UNSUBSCRIBE_LIST'])));
     while ($mailing = $mailingDb->fetch()) {
         $unsub = null;
         if ($posting && $posting['POSTING_MAILING_ID'] == $mailing['ID']) {
             $unsub = array('POSTING_ID' => $posting['POSTING_ID'], 'RECIPIENT_ID' => $data['RECIPIENT_ID']);
         } else {
             $mailingPostingDb = PostingRecipientTable::getList(array('select' => array('RECIPIENT_ID' => 'ID', 'POSTING_ID'), 'filter' => array('=POSTING.MAILING_ID' => $mailing['ID'], 'EMAIL' => $data['EMAIL'])));
             if ($mailingPosting = $mailingPostingDb->fetch()) {
                 $unsub = $mailingPosting;
             }
         }
         // add mark in statistic if there is no previous mark
         if (!empty($unsub)) {
             $unsubExists = PostingUnsubTable::getRowById($unsub);
             if (!$unsubExists) {
                 $unsubResult = PostingUnsubTable::add($unsub);
                 if ($unsubResult->isSuccess()) {
                     $eventData = array('MAILING_ID' => $mailing['ID'], 'RECIPIENT_ID' => $unsub['RECIPIENT_ID'], 'EMAIL' => $data['EMAIL']);
                     $event = new \Bitrix\Main\Event('sender', 'OnAfterRecipientUnsub', array($eventData));
                     $event->send();
                 }
             }
             $result = true;
         }
         // add row to unsubscribe list
         $contactId = ContactTable::addIfNotExist(array('EMAIL' => $data['EMAIL']));
         if ($contactId) {
             MailingSubscriptionTable::addUnSubscription(array('MAILING_ID' => $mailing['ID'], 'CONTACT_ID' => $contactId));
             $result = true;
         }
     }
     return $result;
 }
Example #2
0
 public static function updateMailingSubscription()
 {
     // get list of all subscribers and unsubscribers
     $existedSubList = array();
     $existedSubDb = \Bitrix\Sender\MailingSubscriptionTable::getList(array('select' => array('EMAIL' => 'CONTACT.EMAIL', 'MAILING_ID')));
     while ($existedSub = $existedSubDb->fetch()) {
         $existedSubList[$existedSub['EMAIL']][] = $existedSub['MAILING_ID'];
     }
     // get list of all unsubscribers
     $dataDb = \Bitrix\Sender\PostingUnsubTable::getList(array('select' => array('EMAIL' => 'UPDATE_RECIPIENT.EMAIL', 'MAILING_ID' => 'UPDATE_POSTING.MAILING_ID'), 'filter' => array(), 'runtime' => array(new \Bitrix\Main\Entity\ReferenceField('UPDATE_RECIPIENT', 'Bitrix\\Sender\\PostingRecipientTable', array('=this.RECIPIENT_ID' => 'ref.ID'), array('join_type' => 'INNER')), new \Bitrix\Main\Entity\ReferenceField('UPDATE_POSTING', 'Bitrix\\Sender\\PostingTable', array('=this.POSTING_ID' => 'ref.ID'), array('join_type' => 'INNER'))), 'order' => array('ID' => 'ASC')));
     while ($data = $dataDb->fetch()) {
         if (self::isTimeUp()) {
             return true;
         }
         if (isset($existedSubList[$data['EMAIL']]) && in_array($data['MAILING_ID'], $existedSubList[$data['EMAIL']])) {
             continue;
         }
         $contactId = \Bitrix\Sender\ContactTable::addIfNotExist(array('EMAIL' => $data['EMAIL']));
         $primary = array('MAILING_ID' => $data['MAILING_ID'], 'CONTACT_ID' => $contactId);
         $fields = array('IS_UNSUB' => 'Y');
         $row = \Bitrix\Sender\MailingSubscriptionTable::getRowById($primary);
         if (!$row) {
             $result = \Bitrix\Sender\MailingSubscriptionTable::add($fields + $primary);
             $result->isSuccess();
         }
     }
     return false;
 }