74 $resultMailingList = array();
76 $mailing = MailingTable::getRowById(array(
'ID' => $data[
'MAILING_ID']));
77 if(isset($data[
'TEST']) && $data[
'TEST'] ==
'Y')
79 $resultMailingList[] = array(
80 'ID' => $mailing[
'ID'],
81 'NAME' => $mailing[
'NAME'],
82 'DESC' => $mailing[
'DESCRIPTION'],
86 return $resultMailingList;
88 $contactData = $contactId =
null;
89 if($data[
'RECIPIENT_ID'] && $recipient = PostingRecipientTable::getRowById(array(
'ID' => $data[
'RECIPIENT_ID'])))
91 if(isset($data[
'CONTACT_ID']) && $recipient[
'CONTACT_ID'] != $data[
'CONTACT_ID'])
95 $contactData = ContactTable::getRowById($contactId = $recipient[
'CONTACT_ID']);
97 elseif($data[
'CONTACT_ID'] && $contactData = ContactTable::getRowById($data[
'CONTACT_ID']))
99 $contactId = $contactData[
'ID'];
106 if ($contactData && $contactData[
'BLACKLISTED'] ===
'Y')
111 $mailingUnsub = array();
113 'select' => array(
'MAILING_ID'),
115 '=CONTACT.ID' => $contactId,
116 '=MAILING.SITE_ID' => $mailing[
'SITE_ID']
119 while($unSub = $unSubDb->fetch())
121 $mailingUnsub[] = $unSub[
'MAILING_ID'];
124 $mailingList = array();
126 $mailingDb = PostingRecipientTable::getList(array(
127 'select' => array(
'MAILING_ID' =>
'POSTING.MAILING.ID'),
129 '=CONTACT_ID' => $contactId,
130 '=POSTING.MAILING.ACTIVE' =>
'Y',
131 '=POSTING.MAILING.SITE_ID' => $mailing[
'SITE_ID']
133 'group' => array(
'MAILING_ID')
135 while ($mailing = $mailingDb->fetch())
137 $mailingList[] = $mailing[
'MAILING_ID'];
142 'select' => array(
'MAILING_ID'),
144 '=CONTACT.ID' => $contactId,
145 '=MAILING.ACTIVE' =>
'Y',
146 '=MAILING.SITE_ID' => $mailing[
'SITE_ID']
149 while ($mailing = $mailingDb->fetch())
151 $mailingList[] = $mailing[
'MAILING_ID'];
154 $mailingList = array_unique($mailingList);
155 foreach($mailingList as $mailingId)
157 if(!in_array($mailingId, $mailingUnsub))
159 $mailingDesc = MailingTable::getRowById($mailingId);
162 $resultMailingList[] = array(
163 'ID' => $mailingDesc[
'ID'],
164 'NAME' => $mailingDesc[
'NAME'],
165 'DESC' => $mailingDesc[
'DESCRIPTION'],
166 'SELECTED' => in_array($mailingDesc[
'ID'], array($data[
'MAILING_ID'])),
172 return $resultMailingList;
211 if(isset($data[
'TEST']) && $data[
'TEST'] ==
'Y')
216 if(!$data[
'RECIPIENT_ID'])
221 $data[
'ABUSE'] = isset($data[
'ABUSE']) ? (bool) $data[
'ABUSE'] :
false;
222 $data[
'ABUSE_TEXT'] = isset($data[
'ABUSE_TEXT']) ? $data[
'ABUSE_TEXT'] :
null;
224 $recipient = PostingRecipientTable::getRow([
226 'ID',
'CONTACT_ID',
'CONTACT_CODE' =>
'CONTACT.CODE',
'CONTACT_TYPE_ID' =>
'CONTACT.TYPE_ID',
227 'POSTING_ID',
'POSTING_MAILING_ID' =>
'POSTING.MAILING_ID'
229 'filter' => [
'=ID' => $data[
'RECIPIENT_ID']]
231 $recipient = ((!$recipient && $data[
'CONTACT_ID'])? ContactTable::getRow([
233 'CONTACT_ID' =>
'ID',
'CONTACT_TYPE_ID' =>
'TYPE_ID',
'CONTACT_CODE' =>
'CODE'
235 'filter' => [
'=CONTACT_ID' => $data[
'CONTACT_ID']]
240 !$recipient[
'CONTACT_ID'] ||
241 ($data[
'CONTACT_ID']? $data[
'CONTACT_ID'] != $recipient[
'CONTACT_ID'] :
false)
246 $contactId = $recipient[
'CONTACT_ID'];
247 $mailingDb = MailingTable::getList(array(
248 'select' => array(
'ID'),
250 '=ID' => $data[
'UNSUBSCRIBE_LIST'],
253 while($mailing = $mailingDb->fetch())
257 isset($recipient[
'POSTING_MAILING_ID'],$recipient[
'POSTING_ID'],$recipient[
'ID']) &&
258 $recipient[
'POSTING_MAILING_ID'] == $mailing[
'ID']
262 'POSTING_ID' => $recipient[
'POSTING_ID'],
263 'RECIPIENT_ID' => $recipient[
'ID'],
268 $mailingPostingDb = PostingRecipientTable::getList(array(
269 'select' => array(
'RECIPIENT_ID' =>
'ID',
'POSTING_ID'),
271 '=POSTING.MAILING_ID' => $mailing[
'ID'],
272 '=CONTACT_ID' => $contactId
275 if($mailingPosting = $mailingPostingDb->fetch())
277 $primary = $mailingPosting;
284 $unsubExists = PostingUnsubTable::getRowById($primary);
287 $unsubResult = PostingUnsubTable::add($primary);
288 if($unsubResult->isSuccess())
291 'ABUSE' => $data[
'ABUSE'],
292 'ABUSE_TEXT' => $data[
'ABUSE_TEXT'],
293 'MAILING_ID' => $mailing[
'ID'],
294 'RECIPIENT_ID' => $primary[
'RECIPIENT_ID'],
295 'CONTACT_ID' => $contactId,
296 'EMAIL' => $data[
'EMAIL'],
298 $event =
new Event(
'sender',
'OnAfterRecipientUnsub', array($eventData));
303 AbuseTable::add(array(
304 'TEXT' => $data[
'ABUSE_TEXT'],
305 'CONTACT_ID' => $contactId,
306 'CONTACT_CODE' => $recipient[
'CONTACT_CODE'],
307 'CONTACT_TYPE_ID' => $recipient[
'CONTACT_TYPE_ID'],
311 Integration\EventHandler::onAfterPostingRecipientUnsubscribe($eventData);
319 'MAILING_ID' => $mailing[
'ID'],
320 'CONTACT_ID' => $contactId
323 if ($contactId && $data[
'ABUSE'])
325 ContactTable::update($contactId, array(
'BLACKLISTED' =>
'Y'));
342 $resultMailingList = array();
344 $mailing = MailingTable::getRowById(array(
'ID' => $data[
'MAILING_ID']));
345 if(isset($data[
'TEST']) && $data[
'TEST'] ==
'Y')
347 $resultMailingList[] = array(
348 'ID' => $mailing[
'ID'],
349 'NAME' => $mailing[
'NAME'],
350 'DESC' => $mailing[
'DESCRIPTION'],
354 return $resultMailingList;
357 $mailingUnsub = array();
359 'select' => array(
'MAILING_ID'),
361 '=CONTACT.EMAIL' => trim(mb_strtolower($data[
'EMAIL'])),
362 '=MAILING.SITE_ID' => $mailing[
'SITE_ID']
365 while($unSub = $unSubDb->fetch())
366 $mailingUnsub[] = $unSub[
'MAILING_ID'];
368 $mailingList = array();
370 $receiveMailingDb = PostingRecipientTable::getList(array(
371 'select' => array(
'MAILING_ID' =>
'POSTING.MAILING.ID'),
373 '=EMAIL' => trim(mb_strtolower($data[
'EMAIL'])),
374 '=POSTING.MAILING.ACTIVE' =>
'Y',
375 '=POSTING.MAILING.SITE_ID' => $mailing[
'SITE_ID']
377 'group' => array(
'MAILING_ID')
379 while ($receiveMailing = $receiveMailingDb->fetch())
381 $mailingList[] = $receiveMailing[
'MAILING_ID'];
386 'select' => array(
'MAILING_ID'),
388 '=CONTACT.EMAIL' => trim(mb_strtolower($data[
'EMAIL'])),
389 '=MAILING.ACTIVE' =>
'Y',
390 '=MAILING.SITE_ID' => $mailing[
'SITE_ID']
393 while ($subscribedMailing = $subscribedMailingDb->fetch())
395 $mailingList[] = $subscribedMailing[
'MAILING_ID'];
398 $mailingList = array_unique($mailingList);
399 foreach($mailingList as $mailingId)
401 if(!in_array($mailingId, $mailingUnsub))
403 $mailingDesc = MailingTable::getRowById($mailingId);
406 $resultMailingList[] = array(
407 'ID' => $mailingDesc[
'ID'],
408 'NAME' => $mailingDesc[
'NAME'],
409 'DESC' => $mailingDesc[
'DESCRIPTION'],
410 'SELECTED' => in_array($mailingDesc[
'ID'], array($data[
'MAILING_ID'])),
416 return $resultMailingList;
448 if(isset($data[
'TEST']) && $data[
'TEST'] ==
'Y')
451 $data[
'ABUSE'] = isset($data[
'ABUSE']) ? (bool) $data[
'ABUSE'] :
false;
452 $data[
'ABUSE_TEXT'] = isset($data[
'ABUSE_TEXT']) ? $data[
'ABUSE_TEXT'] :
null;
455 if($data[
'RECIPIENT_ID'])
457 $postingDb = PostingRecipientTable::getList(array(
458 'select' => array(
'POSTING_ID',
'POSTING_MAILING_ID' =>
'POSTING.MAILING_ID'),
460 '=ID' => $data[
'RECIPIENT_ID'],
461 '=CONTACT.CODE' => $data[
'EMAIL'],
462 '=CONTACT.TYPE_ID' => Recipient\Type::EMAIL,
465 $posting = $postingDb->fetch();
468 $mailingDb = MailingTable::getList(array(
469 'select' => array(
'ID'),
471 '=ID' => $data[
'UNSUBSCRIBE_LIST'],
474 while($mailing = $mailingDb->fetch())
478 if($posting && $posting[
'POSTING_MAILING_ID'] == $mailing[
'ID'])
481 'POSTING_ID' => $posting[
'POSTING_ID'],
482 'RECIPIENT_ID' => $data[
'RECIPIENT_ID'],
483 'CONTACT_ID' => isset($data[
'CONTACT_ID']) ? (
int) $data[
'CONTACT_ID'] :
null,
488 $mailingPostingDb = PostingRecipientTable::getList(array(
489 'select' => array(
'RECIPIENT_ID' =>
'ID',
'CONTACT_ID',
'POSTING_ID'),
491 '=POSTING.MAILING_ID' => $mailing[
'ID'],
492 '=CONTACT.CODE' => $data[
'EMAIL'],
493 '=CONTACT.TYPE_ID' => Recipient\Type::EMAIL,
497 if($mailingPosting = $mailingPostingDb->fetch())
499 $unsub = $mailingPosting;
506 if ($unsub[
'CONTACT_ID'] && $data[
'ABUSE'])
508 ContactTable::update($unsub[
'CONTACT_ID'], array(
'BLACKLISTED' =>
'Y'));
511 $unsubExists = PostingUnsubTable::getRowById($unsub);
514 $unsubResult = PostingUnsubTable::add($unsub);
515 if($unsubResult->isSuccess())
518 'ABUSE' => $data[
'ABUSE'],
519 'ABUSE_TEXT' => $data[
'ABUSE_TEXT'],
520 'CAMPAIGN_ID' => $mailing[
'ID'],
521 'MAILING_ID' => $mailing[
'ID'],
522 'RECIPIENT_ID' => $unsub[
'RECIPIENT_ID'],
523 'EMAIL' => $data[
'EMAIL'],
525 $event =
new Event(
'sender',
'OnAfterRecipientUnsub', array($eventData));
530 AbuseTable::add(array(
531 'TEXT' => $data[
'ABUSE_TEXT'],
532 'CONTACT_ID' => $unsub[
'CONTACT_ID'],
533 'CONTACT_CODE' => $data[
'EMAIL'],
534 'CONTACT_TYPE_ID' => Recipient\Type::EMAIL,
538 Integration\EventHandler::onAfterPostingRecipientUnsubscribe($eventData);
564 public static function add($code, array $mailingIdList)
568 $typeId = Recipient\Type::detect($code);
569 $code = Recipient\Normalizer::normalize($code, $typeId);
570 $contact = ContactTable::getRow([
574 '=TYPE_ID' => $typeId,
579 $contactId = $contact[
'ID'];
583 $contactAddDb = ContactTable::add([
'TYPE_ID' => $typeId,
'CODE' => $code]);
584 if($contactAddDb->isSuccess())
586 $contactId = $contactAddDb->getId();
590 if(!empty($contactId))
592 foreach ($mailingIdList as $mailingId)
595 'MAILING_ID' => $mailingId,
'CONTACT_ID' => $contactId
645 $mailingNameList = array();
646 $mailingDb = MailingTable::getList(array(
'select' => array(
'NAME'),
'filter' => array(
"IS_TRIGGER" =>
"N",
'ID' => $mailingIdList)));
647 while($mailing = $mailingDb->fetch())
649 $mailingNameList[] = $mailing[
'NAME'];
652 $subscription = array(
654 'SITE_ID' => $siteId,
655 'MAILING_LIST' => $mailingIdList,
657 $confirmUrl = static::getLinkSub($subscription);
659 $eventSendFields = array(
660 "EVENT_NAME" =>
"SENDER_SUBSCRIBE_CONFIRM",
663 "DATE" => $date->toString(),
664 "CONFIRM_URL" => $confirmUrl,
665 "MAILING_LIST" => implode(
"\r\n",$mailingNameList),
667 "LID" => is_array($siteId)? implode(
",", $siteId): $siteId,
669 \Bitrix\Main\Mail\Event::send($eventSendFields);