• Модуль: sender
  • Путь к файлу: ~/bitrix/modules/sender/lib/posting/builder.php
  • Класс: BitrixSenderPostingBuilder
  • Вызов: Builder::setRecipientIdentificators
protected function setRecipientIdentificators(array &$dataList, bool $include = true)
{
	if (count($dataList) === 0)
	{
		return;
	}

	$codes = array_keys($dataList);
	$tableName = ContactTable::getTableName();
	$subsTableName = MailingSubscriptionTable::getTableName();

	$existed = [];
	$contactCodeFilter = [];

	$connection = Application::getConnection();
	$primariesString = SqlBatch::getInString($codes);

	$recipientDb = $connection->query(
		"select 
		c.ID, 
		c.NAME, 
		c.CODE, 
		c.BLACKLISTED, 
		c.CONSENT_STATUS, 
		c.CONSENT_REQUEST, 
		c.IS_UNSUB, 
		s.IS_UNSUB as IS_MAILING_UNSUB " .
		"from $tableName c " .
		"left join $subsTableName s on " .
			"c.ID = s.CONTACT_ID " .
			"and s.MAILING_ID=" . (int) $this->postingData['MAILING_ID'] . " " .
		"where c.TYPE_ID = " . (int) $this->typeId . " and c.CODE in ($primariesString)"
	);
	while ($row = $recipientDb->fetch())
	{
		$existed[] = $row['CODE'];
		$dataList[$row['CODE']]['CONTACT_ID'] = $row['ID'];
		$dataList[$row['CODE']]['EXCLUDED'] = $this->isExcluded($include, $row);

		$name = $dataList[$row['CODE']]['NAME'] ?? null;
		if ($name && $name !== $row['NAME'])
		{
			$contactCodeFilter[] = $row['CODE'];
		}
	}

	// update existed contact names
	$this->updateContacts($dataList, $contactCodeFilter);

	// exit if no new contacts
	if (count($existed) === count($codes))
	{
		return;
	}

	// add new contacts
	$list = array_diff($codes, $existed);
	$batch = array();
	$sqlDateTimeFunction = Application::getConnection()->getSqlHelper()->getCurrentDateTimeFunction();
	$updateFieldsOnDuplicate = array(
		array('NAME' => 'DATE_UPDATE', 'VALUE' => $sqlDateTimeFunction),
	);
	foreach ($list as $code)
	{
		$batchItem = array(
			'TYPE_ID' => $this->typeId,
			'CODE' => $code,
			'DATE_INSERT' => array('VALUE' => $sqlDateTimeFunction),
			'DATE_UPDATE' => array('VALUE' => $sqlDateTimeFunction),
		);

		$key = 'NAME';
		if (isset($dataList[$key]) && $dataList[$key])
		{
			$batchItem[$key] = $dataList[$key];
			if (!in_array($key, $updateFieldsOnDuplicate))
			{
				$updateFieldsOnDuplicate[] = $key;
			}
		}

		$batch[] = $batchItem;
	}


	SqlBatch::insert($tableName, $batch, $updateFieldsOnDuplicate);


	$recipientDb = $connection->query(
		"select ID, CODE " .
		"from $tableName " .
		"where TYPE_ID = " . (int) $this->typeId . " and CODE in ($primariesString)"
	);
	while ($row = $recipientDb->fetch())
	{
		$dataList[$row['CODE']]['CONTACT_ID'] = $row['ID'];
	}
}