• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/sonet_subscription.php
  • Класс: \CAllCrmSonetSubscription
  • Вызов: CAllCrmSonetSubscription::TransferOwnership
public function TransferOwnership($oldEntityTypeID, $oldEntityID, $newEntityTypeID, $newEntityID)
{
	if(!is_int($oldEntityTypeID))
	{
		$oldEntityTypeID = (int)$oldEntityTypeID;
	}

	if($oldEntityTypeID <= 0)
	{
		throw new \Bitrix\Main\ArgumentException('Must be greater than zero.', 'oldEntityTypeID');
	}

	if(!is_int($oldEntityID))
	{
		$oldEntityID = (int)$oldEntityID;
	}

	if($oldEntityID <= 0)
	{
		throw new \Bitrix\Main\ArgumentException('Must be greater than zero.', 'oldEntityID');
	}

	if(!is_int($newEntityTypeID))
	{
		$newEntityTypeID = (int)$newEntityTypeID;
	}

	if($newEntityTypeID <= 0)
	{
		throw new \Bitrix\Main\ArgumentException('Must be greater than zero.', 'newEntityTypeID');
	}

	if(!is_int($newEntityID))
	{
		$newEntityID = (int)$newEntityID;
	}

	if($newEntityID <= 0)
	{
		throw new \Bitrix\Main\ArgumentException('Must be greater than zero.', 'newEntityID');
	}

	$data = array();
	$tableName = $this->GetTableName();
	$connection = \Bitrix\Main\Application::getConnection();
	$sqlHelper = $connection->getSqlHelper();

	$oldSlEntityType = CCrmLiveFeedEntity::GetByEntityTypeID($oldEntityTypeID);
	$newSlEntityType = CCrmLiveFeedEntity::GetByEntityTypeID($newEntityTypeID);
	$oldSlEntityTypeSql = $sqlHelper->forSql($oldSlEntityType);

	$dbResult = $connection->query(
		"SELECT USER_ID, TYPE_ID FROM {$tableName} WHERE SL_ENTITY_TYPE = '{$oldSlEntityTypeSql}' AND ENTITY_ID = {$oldEntityID}"
	);

	while($fields = $dbResult->fetch())
	{
		$data[] = $fields;
	}

	if(empty($data))
	{
		return;
	}

	foreach($data as $item)
	{
		$queries = $connection->getSqlHelper()->prepareMerge(
			$tableName,
			array('USER_ID', 'SL_ENTITY_TYPE', 'ENTITY_ID', 'TYPE_ID'),
			array(
				'USER_ID' => $item['USER_ID'],
				'TYPE_ID' => $item['TYPE_ID'],
				'SL_ENTITY_TYPE' => $newSlEntityType,
				'ENTITY_ID' => $newEntityID
			),
			array('SL_ENTITY_TYPE' => $newSlEntityType, 'ENTITY_ID' => $newEntityID)
		);

		foreach($queries as $query)
		{
			$connection->queryExecute($query);
		}
	}

	$connection->queryExecute(
		"DELETE FROM {$tableName} WHERE SL_ENTITY_TYPE = '{$oldSlEntityTypeSql}' AND ENTITY_ID = {$oldEntityID}"
	);
}