• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/rest.php
  • Класс: BitrixImOpenLinesRest
  • Вызов: Rest::widgetCrmBindingsGet
static function widgetCrmBindingsGet($params, $n, CRestServer $server)
{
	if ($server->getAuthType() != WidgetAuth::AUTH_TYPE)
	{
		throw new RestException('Access for this method allowed only by livechat authorization.', 'WRONG_AUTH_TYPE', CRestServer::STATUS_FORBIDDEN);
	}

	if (!Loader::includeModule('crm'))
	{
		throw new RestException('CRM module is not installed.', 'NO_CRM', CRestServer::STATUS_WRONG_REQUEST);
	}

	$params = array_change_key_case($params, CASE_UPPER);

	if (!is_string($params['OPENLINES_CODE']) || $params['OPENLINES_CODE' === ''])
	{
		throw new RestException('Wrong imopenlines code.', 'WRONG_IMOL_CODE', CRestServer::STATUS_WRONG_REQUEST);
	}

	$parsedOpenlinesCode = Chat::parseLinesChatEntityId($params['OPENLINES_CODE']);
	$configId = $parsedOpenlinesCode['lineId'];
	$clientChatId = $parsedOpenlinesCode['connectorChatId'];
	$userId = $parsedOpenlinesCode['connectorUserId'];

	if (!$configId || !$clientChatId || !$userId)
	{
		throw new RestException('Wrong imopenlines code.', 'WRONG_IMOL_CODE', CRestServer::STATUS_WRONG_REQUEST);
	}

	// get operator chat from IMOL code
	$operatorChat = new Chat();
	$chatLoadResult = $operatorChat->load(['USER_CODE' => $params['OPENLINES_CODE'], 'ONLY_LOAD' => 'Y']);
	if (!$chatLoadResult)
	{
		throw new RestException('Error loading chat', 'CHAT_LOAD_ERROR', CRestServer::STATUS_WRONG_REQUEST);
	}

	// check if current user is in chat
	$isUserInChat = ImChat::isUserInChat($operatorChat->getData('ID'));
	if (!$isUserInChat)
	{
		throw new RestException('You dont have access to this chat', 'ACCESS_DENIED', CRestServer::STATUS_WRONG_REQUEST);
	}

	// get crm bindings from field data
	$crmBindings = $operatorChat->getFieldData(Chat::FIELD_CRM);

	// sign bindings if they exist
	$signedData = (new WebFormEmbedSign);
	$bindingsExist = false;
	foreach ($crmBindings as $bindingType => $bindingId)
	{
		if ($bindingId > 0)
		{
			$bindingsExist = true;
			$signedData->addEntity(CCrmOwnerType::ResolveId($bindingType), $bindingId);
		}
	}

	if (!$bindingsExist)
	{
		return '';
	}

	return $signedData->pack();
}