CCrmActivity::DoSaveBindings

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmActivity
  4. DoSaveBindings
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/mysql/crm_activity.php
  • Класс: \CCrmActivity
  • Вызов: CCrmActivity::DoSaveBindings
static function DoSaveBindings($ID, &$arBindings, $registerBindingsChanges = true)
{
	global $DB;

	$ID = intval($ID);
	if($ID <= 0 || !is_array($arBindings))
	{
		self::RegisterError(array('text' => 'Invalid arguments are supplied.'));
		return false;
	}

	if(!is_array($existedBindings = self::GetBindings($ID)))
	{
		self::RegisterError(array('text' => self::GetLastErrorMessage()));
		return false;
	}

	$added = array();
	$removed = array();
	self::PrepareBindingChanges($existedBindings, $arBindings, $added, $removed);

	if($ID > 0)
	{
		self::DeleteBindings($ID, false);
	}

	if(count($arBindings) == 0)
	{
		if (!empty($existedBindings))
		{
			if ($registerBindingsChanges)
			{
				\Bitrix\Crm\Activity\UncompletedActivity::synchronizeForActivity($ID, $existedBindings);
			}

			\Bitrix\Crm\Activity\Provider\ProviderManager::syncBadgesOnBindingsChange($ID, [], $existedBindings);
		}

		$monitor = \Bitrix\Crm\Service\Timeline\Monitor::getInstance();
		foreach ($existedBindings as $binding)
		{
			if (\CCrmOwnerType::IsDefined($binding['OWNER_TYPE_ID']) && (int)$binding['OWNER_ID'] > 0)
			{
				$monitor->onActivityRemoveIfSuitable(
					new \Bitrix\Crm\ItemIdentifier((int)$binding['OWNER_TYPE_ID'], (int)$binding['OWNER_ID']),
					$ID
				);
			}
		}

		return true;
	}

	$existedBindingsMap = [];
	foreach ($existedBindings as $binding)
	{
		$existedBindingsMap[$binding['OWNER_TYPE_ID']][$binding['OWNER_ID']] = true;
	}
	$newBindings = [];

	$bulkColumns = '';
	$bulkValues = array();

	foreach($arBindings as &$arBinding)
	{
		if(isset($arBinding['ID']))
		{
			unset($arBinding['ID']);
		}
		if (!($existedBindingsMap[$arBinding['OWNER_TYPE_ID']][$arBinding['OWNER_ID']] ?? null))
		{
			$newBindings[] = $arBinding;
		}

		$data = $DB->PrepareInsert(self::BINDING_TABLE_NAME, $arBinding);
		if($bulkColumns == '')
		{
			$bulkColumns = $data[0];
		}

		$bulkValues[] = $data[1];
	}
	unset($arComm);

	if(count($bulkValues) == 0)
	{
		self::RegisterError(array('text' => 'There are no values for insert.'));
		return false;
	}

	$query = '';
	foreach($bulkValues as &$value)
	{
		if($query !== '')
		{
			$query .= ',';
		}

		$query .= "($value)";
	}

	if($query == '')
	{
		self::RegisterError(array('text' => 'Could not build query.'));
		return false;
	}

	$DB->Query(
		'INSERT INTO '.self::BINDING_TABLE_NAME.'('.$bulkColumns.') VALUES'.$query,
		false,
		'File: '.__FILE__.'
Line: '.__LINE__ ); if (!empty($newBindings) && $registerBindingsChanges) { \Bitrix\Crm\Activity\UncompletedActivity::synchronizeForActivity($ID, $newBindings); } \Bitrix\Crm\Activity\Provider\ProviderManager::syncBadgesOnBindingsChange($ID, $added, $removed); if ($registerBindingsChanges) { \Bitrix\Crm\Counter\Monitor::getInstance()->onChangeActivityBindings($ID, $existedBindings, $arBindings); } $monitor = \Bitrix\Crm\Service\Timeline\Monitor::getInstance(); foreach ($added as $binding) { if (\CCrmOwnerType::IsDefined($binding['OWNER_TYPE_ID']) && (int)$binding['OWNER_ID'] > 0) { $monitor->onActivityAddIfSuitable(new \Bitrix\Crm\ItemIdentifier((int)$binding['OWNER_TYPE_ID'], (int)$binding['OWNER_ID']), $ID); } } foreach ($removed as $binding) { if (\CCrmOwnerType::IsDefined($binding['OWNER_TYPE_ID']) && (int)$binding['OWNER_ID'] > 0) { $monitor->onActivityRemoveIfSuitable(new \Bitrix\Crm\ItemIdentifier((int)$binding['OWNER_TYPE_ID'], (int)$binding['OWNER_ID']), $ID); } } return true; }

Добавить комментарий