• Модуль: sender
  • Путь к файлу: ~/bitrix/modules/sender/lib/trigger/manager.php
  • Класс: BitrixSenderTriggerManager
  • Вызов: Manager::addRecipient
static function addRecipient($chain, $settings, $data)
{
	if(!$data || empty($data['EMAIL']))
	{
		return;
	}

	$code = $data['EMAIL'];
	$typeId = RecipientType::detect($data['EMAIL']);
	$code = RecipientNormalizer::normalize($code, $typeId);

	// check email to unsubscription
	if(Subscription::isUnsubscibed($chain['MAILING_ID'], $code))
	{
		return;
	}

	// if this is event for child
	if(!empty($chain['PARENT_ID']))
	{
		$recipientDb = PostingRecipientTable::getList(array(
			'select' => array('ID', 'STATUS'),
			'filter' => array(
				'=CONTACT.CODE' => $code,
				'=CONTACT.TYPE_ID' => $typeId,
				'=POSTING.MAILING_CHAIN_ID' => $chain['ID'],
				'=POSTING.STATUS' => array(PostingTable::STATUS_NEW, PostingTable::STATUS_PART)
			)
		));

		while($recipient = $recipientDb->fetch())
		{
			// check if event should came or didn't came
			$statusNew = null;
			if($settings->isEventOccur() && $recipient['STATUS'] == PostingRecipientTable::SEND_RESULT_WAIT)
			{
				$statusNew = PostingRecipientTable::SEND_RESULT_NONE;
			}
			elseif(!$settings->isEventOccur() && $recipient['STATUS'] == PostingRecipientTable::SEND_RESULT_NONE)
			{
				$statusNew = PostingRecipientTable::SEND_RESULT_WAIT;
			}

			if($statusNew !== null)
			{
				ModelPostingRecipientTable::update(
					$recipient['ID'],
					['STATUS' => $statusNew]
				)->isSuccess();
			}
		}
	}
	else
	{
		// check email to have not finished mailing
		$recipientExistsDb = PostingRecipientTable::getList(array(
			'select' => array('ID'),
			'filter' => array(
				'=CONTACT.CODE' => $code,
				'=CONTACT.TYPE_ID' => $typeId,
				'=POSTING.MAILING_ID' => $chain['MAILING_ID'],
				'=STATUS' => array(
					PostingRecipientTable::SEND_RESULT_NONE,
					PostingRecipientTable::SEND_RESULT_WAIT,
				)
			),
			'limit' => 1
		));
		if($recipientExistsDb->fetch())
		{
			return;
		}

		if(static::$postingId)
		{
			$postingId = static::$postingId;
		}
		else
		{
			$postingAddDb = PostingTable::add(array(
				'MAILING_ID' => $chain['MAILING_ID'],
				'MAILING_CHAIN_ID' => $chain['ID'],
			));
			if(!$postingAddDb->isSuccess()) return;

			$postingId = $postingAddDb->getId();
			static::$postingId = $postingId;
		}

		$contact = ContactTable::getRow(['filter' => [
			'=CODE' => $code,
			'=TYPE_ID' => $typeId,
		]]);
		if (!$contact)
		{
			$contact = [
				'CODE' => $code,
				'TYPE_ID' => $typeId,
				'NAME' => !empty($data['NAME']) ? $data['NAME'] : null,
				'USER_ID' => !empty($data['USER_ID']) ? $data['USER_ID'] : null,
			];
			$contact['ID'] = ContactTable::add($contact)->getId();
		}

		PostingRecipientTable::add([
			'POSTING_ID' => $postingId,
			'CONTACT_ID' => $contact['ID'],
			'FIELDS' => !empty($data['FIELDS']) ? $data['FIELDS'] : null,
			'USER_ID' => !empty($data['USER_ID']) ? $data['USER_ID'] : null,
		])->isSuccess();
	}
}