• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/tracking/internals/traceentity.php
  • Класс: Bitrix\Crm\Tracking\Internals\TraceEntityTable
  • Вызов: TraceEntityTable::addEntity
static function addEntity($traceId, $entityTypeId, $entityId)
{
	if (!$entityTypeId || !$entityId)
	{
		return false;
	}

	/** @var Main\Orm\Data\DataManager $entityClass */
	$entityClass = null;
	switch ($entityTypeId)
	{
		case \CCrmOwnerType::Lead:
			$entityClass = Crm\LeadTable::class;
			break;
		case \CCrmOwnerType::Deal:
			$entityClass = Crm\DealTable::class;
			break;
		case \CCrmOwnerType::Contact:
			$entityClass = Crm\ContactTable::class;
			break;
		case \CCrmOwnerType::Company:
			$entityClass = Crm\CompanyTable::class;
			break;
	}

	if ($entityClass)
	{
		$entityRow = $entityClass::getRow(['select' => ['ID'], 'filter' => ['=ID' => $entityId]]);
		if (!$entityRow)
		{
			return false;
		}
	}

	$tags = [];
	$hasChild = false;
	$rows = TraceTable::getList([
		'select' => [
			'HAS_CHILD',
			'TAGS_RAW',
			'ENTITY_TYPE_ID' => 'ENTITY.ENTITY_TYPE_ID',
			'ENTITY_ID' => 'ENTITY.ENTITY_ID'
		],
		'filter' => ['=ID' => $traceId]
	])->fetchAll();
	foreach ($rows as $row)
	{
		if (empty($tags) && !empty($row['TAGS_RAW']) && is_array($row['TAGS_RAW']))
		{
			$tags = $row['TAGS_RAW'];
		}
		if (!$hasChild && $row['HAS_CHILD'] === 'Y')
		{
			$hasChild = true;
		}

		if ($row['ENTITY_TYPE_ID'] != $entityTypeId)
		{
			continue;
		}

		if ($row['ENTITY_ID'] != $entityId)
		{
			continue;
		}

		return true;
	}

	if ($hasChild)
	{
		$traces = TraceTreeTable::getList([
			'select' => ['CHILD_ID'],
			'filter' => ['=PARENT_ID' => $traceId],
			'limit' => 15
		])->fetchAll();
		foreach (array_column($traces, 'CHILD_ID') as $previousTraceId)
		{
			static::addEntity($previousTraceId, $entityTypeId, $entityId);
		}
	}

	if (!empty($tags))
	{
		$utmRow = UtmTable::getRow([
			'select' => ['CODE'],
			'filter' => [
				'=ENTITY_TYPE_ID' => $entityTypeId,
				'=ENTITY_ID' => $entityId,
			]
		]);
		if (!$utmRow)
		{
			UtmTable::addEntityUtmFromFields(
				$entityTypeId,
				$entityId,
				array_change_key_case($tags, CASE_UPPER)
			);
		}
	}

	return static::add([
		'TRACE_ID' => $traceId,
		'ENTITY_TYPE_ID' => $entityTypeId,
		'ENTITY_ID' => $entityId
	])->isSuccess();
}