• Модуль: sender
  • Путь к файлу: ~/bitrix/modules/sender/lib/posting/builder.php
  • Класс: BitrixSenderPostingBuilder
  • Вызов: Builder::run
public function run($postingId, $checkDuplicates = true): bool
{
	$postingData = PostingTable::getList(array(
		'select' => array(
			'*',
			'MESSAGE_TYPE' => 'MAILING_CHAIN.MESSAGE_CODE',
			'WAITING_RECIPIENT' => 'MAILING_CHAIN.WAITING_RECIPIENT',
			'MAILING_STATUS' => 'MAILING_CHAIN.STATUS',
			'MESSAGE_ID' => 'MAILING_CHAIN.MESSAGE_ID'
		),
		'filter' => array('ID' => $postingId),
		'limit' => 1
	))->fetch();

	if(!$postingData)
	{
		return true;
	}

	if ($postingData['MAILING_STATUS'] === ModelLetterTable::STATUS_END)
	{
		ModelLetterTable::update($postingData['MAILING_CHAIN_ID'], [
			'WAITING_RECIPIENT' => 'N'
		]);

		return true;
	}

	$entityProcessed = $this->groupQueueService->isEntityProcessed(ModelGroupQueueTable::TYPE['POSTING'], $postingId);
	if (
		$postingData['MAILING_STATUS'] === ModelLetterTable::STATUS_SEND && $postingData['WAITING_RECIPIENT'] === 'N'
		&& !$entityProcessed
	)
	{
		return true;
	}

	$this->postingData = $postingData;
	$this->checkDuplicates = $checkDuplicates;
	$this->postingId = $postingId;
	$this->groupCount = array();
	
	try
	{
		$this->messageConfiguration = MessageAdapter::getInstance($postingData['MESSAGE_TYPE'])
			->loadConfiguration($postingData['MESSAGE_ID']);
	}
	catch (ArgumentException $e)
	{
		return true;
	}
	
	if(!$checkDuplicates)
	{
		if($this->postingData['STATUS'] === PostingTable::STATUS_NEW)
		{
			self::clean($postingId);
			$this->checkDuplicates = false;
		}
	}

	$messageFields = ModelMessageFieldTable::getList(
		['filter' => ['=MESSAGE_ID' => $postingData['MESSAGE_ID']]]
	)->fetchAll();

	$personalizeFields = [];
	foreach ($messageFields as $messageField)
	{
		if (!in_array(
			$messageField['CODE'],
			[
				'MESSAGE_PERSONALIZE',
				'SUBJECT_PERSONALIZE',
				'TITLE_PERSONALIZE'
			]
		))
		{
			continue;
		}

		$personalizeFields[$messageField['CODE']] =
			json_decode($messageField['VALUE'], true)[1];
	}

	try
	{
		$groups = $this->prepareGroups();
		$message = MessageAdapter::create($this->postingData['MESSAGE_TYPE']);
		foreach ($message->getSupportedRecipientTypes() as $typeId)
		{
			if (!RecipientType::getCode($typeId))
			{
				continue;
			}

			$this->typeId = $typeId;
			$this->runForRecipientType($personalizeFields, $groups);
		}
	} catch (NotCompletedException $e)
	{
		return false;
	}


	ModelPostingTable::update(
		$postingId,
		array(
			'COUNT_SEND_ALL' => PostingRecipientTable::getCount(array('POSTING_ID' => $postingId))
		)
	);

	$usedGroups = [];
	foreach ($groups as $group)
	{
		if ($group['GROUP_ID'] && !isset($usedGroups[$group['GROUP_ID']]))
		{
			RecipientBuilderJob::removeAgentFromDB($this->postingId);

			$this->groupQueueService->releaseGroup(
				ModelGroupQueueTable::TYPE['POSTING'],
				$this->postingId,
				$group['GROUP_ID']
			);
			$usedGroups[$group['GROUP_ID']] = $group['GROUP_ID'];
		}
	}

	$this->postingData['WAITING_RECIPIENT'] = 'N';
	ModelLetterTable::update($this->postingData['MAILING_CHAIN_ID'], [
		'WAITING_RECIPIENT' => $this->postingData['WAITING_RECIPIENT']
	]);

	return true;
}