• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/binding/dealcontact.php
  • Класс: Bitrix\Crm\Binding\DealContactTable
  • Вызов: DealContactTable::prepareFilterJoinSql
static function prepareFilterJoinSql($entityTypeID, $entityID, $tableAlias)
{
	if(!is_int($entityTypeID))
	{
		$entityTypeID = (int)$entityTypeID;
	}

	if(!\CCrmOwnerType::IsDefined($entityTypeID))
	{
		throw new Main\ArgumentOutOfRangeException('entityTypeID',
			\CCrmOwnerType::FirstOwnerType,
			\CCrmOwnerType::LastOwnerType
		);
	}

	if($entityTypeID !== \CCrmOwnerType::Contact && $entityTypeID !== \CCrmOwnerType::Deal)
	{
		$entityTypeName = \CCrmOwnerType::ResolveName($entityTypeID);
		throw new Main\NotSupportedException("Entity type: '{$entityTypeName}' is not supported in current context");
	}

	$entityIDs = is_array($entityID) ? $entityID : array($entityID);
	$effectiveIDs = array();
	foreach($entityIDs as $ID)
	{
		$ID = (int)$ID;
		if($ID > 0)
		{
			$effectiveIDs[] = $ID;
		}
	}

	$qty = count($effectiveIDs);
	if($qty > 1)
	{
		$slug = implode(',', $effectiveIDs);
		if($entityTypeID === \CCrmOwnerType::Contact)
		{
			return "INNER JOIN b_crm_deal_contact DC ON DC.CONTACT_ID IN({$slug}) AND DC.DEAL_ID = {$tableAlias}.ID";
		}
		else//if($entityTypeID === \CCrmOwnerType::Deal)
		{
			return "INNER JOIN b_crm_deal_contact DC ON DC.DEAL_ID IN({$slug}) AND DC.CONTACT_ID = {$tableAlias}.ID";
		}
	}
	elseif($qty === 1)
	{
		if($entityTypeID === \CCrmOwnerType::Contact)
		{
			return "INNER JOIN b_crm_deal_contact DC ON DC.CONTACT_ID = {$effectiveIDs[0]} AND DC.DEAL_ID = {$tableAlias}.ID";
		}
		else//if($entityTypeID === \CCrmOwnerType::Deal)
		{
			return "INNER JOIN b_crm_deal_contact DC ON DC.DEAL_ID = {$effectiveIDs[0]} AND DC.CONTACT_ID = {$tableAlias}.ID";
		}
	}
	return "";
}