• Модуль: landing
  • Путь к файлу: ~/bitrix/modules/landing/lib/dataloader/chat.php
  • Класс: BitrixLandingDataLoaderChat
  • Вызов: Chat::getInitData
protected function getInitData(): array
{
	if (!BitrixMainLoader::includeModule('im'))
	{
		return [];
	}

	$return = [];
	$blockSave = false;
	$type = null;
	$filter = $this->getSettingsValue('additional');
	/** @var BitrixLandingBlock $block */
	$block = $this->getOptionsValue('block');
	$dom = $block->getDom();

	// chat type
	if (
		isset($filter['type']) &&
		is_string($filter['type'])
	)
	{
		$type = mb_strtolower(trim($filter['type']));
	}

	// ID of user chat or local ID of group chat
	if (
		isset($filter['attributeData']) &&
		is_string($filter['attributeData']) &&
		mb_strpos($filter['attributeData'], '@')
	)
	{
		[$attrSelector, $attrCode] = explode('@', $filter['attributeData']);
		$attrCode = mb_strtolower($attrCode);
		$resultNode = $dom->querySelector($attrSelector);
		if ($resultNode)
		{
			if ($type == self::CHAT_TYPE_PERSONAL)
			{
				$return['CHAT_ID'] = (int) $resultNode->getAttribute($attrCode);
				// by default we set current user id
				if (!$return['CHAT_ID'])
				{
					$return['CHAT_ID'] = Manager::getUserId();
					$resultNode->setAttribute($attrCode, $return['CHAT_ID']);
					$blockSave = true;
				}
			}
			else if ($type == self::CHAT_TYPE_GROUP)
			{
				$return['CHAT_ID'] = trim($resultNode->getAttribute($attrCode));
				// create new one
				if (preg_match('/[^d]+/', $return['CHAT_ID']))
				{
					$chantEntity = json_decode(htmlspecialcharsback($return['CHAT_ID']), true);
					if (is_array($chantEntity))
					{
						if (isset($chantEntity['ID']))
						{
							$chatId = $chantEntity['ID'];
							unset($chantEntity['ID']);
							$res = BitrixLandingChatChat::update(
								$chatId,
								$chantEntity
							);
						}
						else
						{
							$res = BitrixLandingChatChat::add(
								$chantEntity
							);
						}
						if ($res->isSuccess())
						{
							$return['CHAT_ID'] = $res->getId();
							$resultNode->setAttribute(
								$attrCode, $return['CHAT_ID']
							);
							$blockSave = true;
						}
					}
				}
				else if (!$return['CHAT_ID'])
				{
					unset($return['CHAT_ID']);
				}
				$return['CHAT_ID'] = (int)$return['CHAT_ID'];
				if ($return['CHAT_ID'])
				{
					BitrixLandingChatBinding::bindingBlock(
						$return['CHAT_ID'], $block->getId()
					);
				}
			}
		}
	}

	// button title we recive from attribute too
	if (
		$return &&
		isset($filter['attributeButton']) &&
		is_string($filter['attributeButton']) &&
		mb_strpos($filter['attributeButton'], '@')
	)
	{
		[$attrSelector, $attrCode] = explode('@', $filter['attributeButton']);
		$attrCode = mb_strtolower($attrCode);
		$resultNode = $dom->querySelector($attrSelector);
		if ($resultNode)
		{
			$return['SEND_TITLE'] = $resultNode->getAttribute($attrCode);
			if (!$return['SEND_TITLE'])
			{
				$return['SEND_TITLE'] = Loc::getMessage('LANDING_SUBTYPE_BUTTON_SEND');
				$resultNode->setAttribute($attrCode, $return['SEND_TITLE']);
				$blockSave = true;
			}
		}
	}

	// chat type
	if ($return && $type)
	{
		$return['TYPE'] = $type;
	}

	// save attributes to the block if necessary
	if ($blockSave)
	{
		$block->saveContent($dom->saveHTML());
		$block->save();
	}

	return $return;
}