• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/livechat.php
  • Класс: BitrixImOpenLinesLiveChat
  • Вызов: LiveChat::getChatForUser
private function getChatForUser()
{
	$orm = BitrixImModelChatTable::getList(array(
		'filter' => array(
			'=ENTITY_TYPE' => 'LIVECHAT',
			'=ENTITY_ID' => $this->config['ID'].'|'.$this->userId
		),
		'limit' => 1
	));
	if($chat = $orm->fetch())
	{
		if (isset($this->temporary['FIRST_MESSAGE']) && $chat['DESCRIPTION'] != $this->temporary['FIRST_MESSAGE'])
		{
			$chatManager = new CIMChat(0);
			$chatManager->SetDescription($chat['ID'], $this->temporary['FIRST_MESSAGE']);
			$chat['DESCRIPTION'] = $this->temporary['FIRST_MESSAGE'];
		}
		$this->chat = $chat;

		$ar = CIMChat::GetRelationById($this->chat['ID'], false, true, false);
		if (!isset($ar[$this->userId]))
		{
			$chatManager = new CIMChat(0);
			$chatManager->AddUser($this->chat['ID'], $this->userId, false, true); // TODO security context
		}
		return $this->chat;
	}

	$avatarId = 0;
	$userName = '';
	$chatColorCode = '';
	$addChat['USERS'] = false;
	if ($this->userId)
	{
		$orm = BitrixMainUserTable::getById($this->userId);
		if ($user = $orm->fetch())
		{
			if ($user['PERSONAL_PHOTO'] > 0)
			{
				$avatarId = CFile::CopyFile($user['PERSONAL_PHOTO']);
			}
			$addChat['USERS'] = Array($this->userId);

			$userName = BitrixImUser::getInstance($this->userId)->getFullName(false);
			$chatColorCode = BitrixImColor::getCodeByNumber($this->userId);
			if (BitrixImUser::getInstance($this->userId)->getGender() == 'M')
			{
				$replaceColor = BitrixImColor::getReplaceColors();
				if (isset($replaceColor[$chatColorCode]))
				{
					$chatColorCode = $replaceColor[$chatColorCode];
				}
			}
		}
	}

	if (!$userName)
	{
		$result = Chat::getGuestName();
		$userName = $result['USER_NAME'];
		$chatColorCode = $result['USER_COLOR'];
	}

	$addChat['TITLE'] = Loc::getMessage('IMOL_LC_CHAT_NAME', Array("#USER_NAME#" => $userName, "#LINE_NAME#" => $this->config['LINE_NAME']));

	$addChat['TYPE'] = IM_MESSAGE_CHAT;
	$addChat['COLOR'] = $chatColorCode;
	$addChat['AVATAR_ID'] = $avatarId;
	$addChat['ENTITY_TYPE'] = 'LIVECHAT';
	$addChat['ENTITY_ID'] = $this->config['ID'].'|'.$this->userId;
	$addChat['SKIP_ADD_MESSAGE'] = 'Y';
	$addChat['AUTHOR_ID'] = $this->userId;

	if (isset($this->temporary['FIRST_MESSAGE']))
	{
		$addChat['DESCRIPTION'] = $this->temporary['FIRST_MESSAGE'];
	}

	$chat = new CIMChat(0);
	$id = $chat->Add($addChat);
	if (!$id)
	{
		return false;
	}

	$orm = BitrixImModelChatTable::getById($id);
	$this->chat = $orm->fetch();
	return $this->chat;
}