CCrmEntityBindingProxy::setItems

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmEntityBindingProxy
  4. setItems
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
  • Класс: \CCrmEntityBindingProxy
  • Вызов: CCrmEntityBindingProxy::setItems
public function setItems($ownerEntityID, $items)
{
	$ownerEntityID = (int)$ownerEntityID;
	if($ownerEntityID <= 0)
	{
		throw new RestException('The parameter ownerEntityID is invalid or not defined.');
	}

	if(!is_array($items))
	{
		throw new RestException('The parameter items must be array.');
	}

	$effectiveItems = array();
	$fieldInfos = $this->getFieldsInfo();
	for($i = 0, $l = count($items); $i < $l; $i++)
	{
		$item = $items[$i];
		$this->internalizeFields($item, $fieldInfos, array());
		$effectiveItems[] = $item;
	}

	$userPermissions = CCrmPerms::GetCurrentUserPermissions();
	if(
		$this->ownerEntityTypeID === CCrmOwnerType::Deal
		&& $this->entityTypeID === CCrmOwnerType::Contact
	)
	{
		//DEAL -> CONTACT
		$categoryID = CCrmDeal::GetCategoryID($ownerEntityID);
		if($categoryID < 0)
		{
			throw new RestException(
				!CCrmDeal::CheckUpdatePermission(0, $userPermissions) ? 'Access denied.' : 'Not found.'
			);
		}
		elseif(!CCrmDeal::CheckUpdatePermission($ownerEntityID, $userPermissions, $categoryID))
		{
			throw new AccessException();
		}

		if(!CCrmDeal::Exists($ownerEntityID))
		{
			throw new RestException('Not found.');
		}

		try
		{
			EntityBinding::normalizeEntityBindings(CCrmOwnerType::Contact, $effectiveItems);
		}
		catch(Main\SystemException $ex)
		{
			throw new RestException(
				$ex->getMessage(),
				RestException::ERROR_CORE,
				CRestServer::STATUS_INTERNAL,
				$ex
			);
		}

		$removedItems = array();
		$addedItems = array();

		EntityBinding::prepareBindingChanges(
			CCrmOwnerType::Contact,
			DealContactTable::getDealBindings($ownerEntityID),
			$effectiveItems,
			$addedItems,
			$removedItems
		);

		if(!empty($removedItems))
		{
			DealContactTable::unbindContacts($ownerEntityID, $removedItems);
		}

		if(!empty($addedItems))
		{
			DealContactTable::bindContacts($ownerEntityID, $addedItems);
		}

		return true;
	}
	elseif(
		$this->ownerEntityTypeID === CCrmOwnerType::Lead
		&& $this->entityTypeID === CCrmOwnerType::Contact
	)
	{
		// LEAD -> CONTACT
		if(!CCrmLead::CheckUpdatePermission($ownerEntityID, $userPermissions))
		{
			throw new AccessException();
		}

		if(!CCrmLead::Exists($ownerEntityID))
		{
			throw new RestException('Not found.');
		}

		try
		{
			EntityBinding::normalizeEntityBindings(CCrmOwnerType::Contact, $effectiveItems);
		}
		catch(Main\SystemException $ex)
		{
			throw new RestException(
				$ex->getMessage(),
				RestException::ERROR_CORE,
				CRestServer::STATUS_INTERNAL,
				$ex
			);
		}

		$removedItems = [];
		$addedItems = [];

		EntityBinding::prepareBindingChanges(
			CCrmOwnerType::Contact,
			LeadContactTable::getLeadBindings($ownerEntityID),
			$effectiveItems,
			$addedItems,
			$removedItems
		);

		if(!empty($removedItems))
		{
			LeadContactTable::unbindContacts($ownerEntityID, $removedItems);
		}

		if(!empty($addedItems))
		{
			LeadContactTable::bindContacts($ownerEntityID, $addedItems);
		}

		return true;
	}
	elseif(
		$this->ownerEntityTypeID === CCrmOwnerType::Quote
		&& $this->entityTypeID === CCrmOwnerType::Contact
	)
	{
		//QUOTE -> CONTACT
		if(!CCrmQuote::CheckUpdatePermission($ownerEntityID, $userPermissions))
		{
			throw new AccessException();
		}

		if(!CCrmQuote::Exists($ownerEntityID))
		{
			throw new RestException('Not found.');
		}

		try
		{
			EntityBinding::normalizeEntityBindings(CCrmOwnerType::Contact, $effectiveItems);
		}
		catch(Main\SystemException $ex)
		{
			throw new RestException(
				$ex->getMessage(),
				RestException::ERROR_CORE,
				CRestServer::STATUS_INTERNAL,
				$ex
			);
		}

		$removedItems = array();
		$addedItems = array();

		EntityBinding::prepareBindingChanges(
			CCrmOwnerType::Contact,
			QuoteContactTable::getQuoteBindings($ownerEntityID),
			$effectiveItems,
			$addedItems,
			$removedItems
		);

		if(!empty($removedItems))
		{
			QuoteContactTable::unbindContacts($ownerEntityID, $removedItems);
		}

		if(!empty($addedItems))
		{
			QuoteContactTable::bindContacts($ownerEntityID, $addedItems);
		}
		return true;
	}
	elseif(
		$this->ownerEntityTypeID === CCrmOwnerType::Contact
		&& $this->entityTypeID === CCrmOwnerType::Company
	)
	{
		//CONTACT -> COMPANY
		if(!CCrmContact::CheckUpdatePermission($ownerEntityID, $userPermissions))
		{
			throw new AccessException();
		}

		if(!CCrmContact::Exists($ownerEntityID))
		{
			throw new RestException('Not found.');
		}

		try
		{
			EntityBinding::normalizeEntityBindings(CCrmOwnerType::Company, $effectiveItems);
		}
		catch(Main\SystemException $ex)
		{
			throw new RestException(
				$ex->getMessage(),
				RestException::ERROR_CORE,
				CRestServer::STATUS_INTERNAL,
				$ex
			);
		}

		$removedItems = array();
		$addedItems = array();

		EntityBinding::prepareBindingChanges(
			CCrmOwnerType::Company,
			ContactCompanyTable::getContactBindings($ownerEntityID),
			$effectiveItems,
			$addedItems,
			$removedItems
		);

		if(!empty($removedItems))
		{
			ContactCompanyTable::unbindCompanies($ownerEntityID, $removedItems);
		}

		if(!empty($addedItems))
		{
			ContactCompanyTable::bindCompanies($ownerEntityID, $addedItems);
		}
		return true;
	}
	elseif(
		$this->ownerEntityTypeID === CCrmOwnerType::Company
		&& $this->entityTypeID === CCrmOwnerType::Contact
	)
	{
		//COMPANY -> CONTACT
		if(!CCrmCompany::CheckUpdatePermission($ownerEntityID, $userPermissions))
		{
			throw new AccessException();
		}

		if(!CCrmCompany::Exists($ownerEntityID))
		{
			throw new RestException('Not found.');
		}

		try
		{
			EntityBinding::normalizeEntityBindings(CCrmOwnerType::Contact, $effectiveItems);
		}
		catch(Main\SystemException $ex)
		{
			throw new RestException(
				$ex->getMessage(),
				RestException::ERROR_CORE,
				CRestServer::STATUS_INTERNAL,
				$ex
			);
		}

		$removedItems = array();
		$addedItems = array();

		EntityBinding::prepareBindingChanges(
			CCrmOwnerType::Contact,
			ContactCompanyTable::getCompanyBindings($ownerEntityID),
			$effectiveItems,
			$addedItems,
			$removedItems
		);

		if(!empty($removedItems))
		{
			ContactCompanyTable::unbindContacts($ownerEntityID, $removedItems);
		}

		if(!empty($addedItems))
		{
			ContactCompanyTable::bindContacts($ownerEntityID, $addedItems);
		}
		return true;
	}

	$ownerEntityTypeName = CCrmOwnerType::ResolveName($this->ownerEntityTypeID);
	$entityTypeName = CCrmOwnerType::ResolveName($this->entityTypeID);

	throw new RestException("The binding type '{$ownerEntityTypeName} - {$entityTypeName}' is not supported in current context.");
}

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