• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/lib/componenthelper.php
  • Класс: BitrixSocialnetworkComponentHelper
  • Вызов: ComponentHelper::addLiveComment
static function addLiveComment($comment = [], $logEntry = [], $commentEvent = [], $params = []): array
{
	global $USER_FIELD_MANAGER;

	$result = [];

	if (
		!is_array($comment)
		|| empty($comment)
		|| !is_array($logEntry)
		|| empty($logEntry)
		|| !is_array($commentEvent)
		|| empty($commentEvent)
	)
	{
		return $result;
	}

	$aux = !empty($params['AUX']);

	if (
		!isset($params["ACTION"])
		|| !in_array($params["ACTION"], array("ADD", "UPDATE"))
	)
	{
		$params["ACTION"] = "ADD";
	}

	if (
		!isset($params["LANGUAGE_ID"])
		|| empty($params["LANGUAGE_ID"])
	)
	{
		$params["LANGUAGE_ID"] = LANGUAGE_ID;
	}

	if (
		!isset($params["SITE_ID"])
		|| empty($params["SITE_ID"])
	)
	{
		$params["SITE_ID"] = SITE_ID;
	}

	if ($params["ACTION"] === "ADD")
	{
		if (
			!empty($commentEvent)
			&& !empty($commentEvent["METHOD_CANEDIT"])
			&& !empty($comment["SOURCE_ID"])
			&& (int)$comment["SOURCE_ID"] > 0
			&& !empty($logEntry["SOURCE_ID"])
			&& (int)$logEntry["SOURCE_ID"] > 0
		)
		{
			$canEdit = call_user_func($commentEvent["METHOD_CANEDIT"], array(
				"LOG_SOURCE_ID" => $logEntry["SOURCE_ID"],
				"COMMENT_SOURCE_ID" => $comment["SOURCE_ID"],
				"USER_ID" => $comment["USER_ID"]
			));
		}
		else
		{
			$canEdit = !$aux;
		}
	}

	$result["hasEditCallback"] = (
		$canEdit
		&& is_array($commentEvent)
		&& isset($commentEvent["UPDATE_CALLBACK"])
		&& (
			$commentEvent["UPDATE_CALLBACK"] === "NO_SOURCE"
			|| is_callable($commentEvent["UPDATE_CALLBACK"])
		)
			? "Y"
			: "N"
	);

	$result["hasDeleteCallback"] = (
		$canEdit
		&& is_array($commentEvent)
		&& isset($commentEvent["DELETE_CALLBACK"])
		&& (
			$commentEvent["DELETE_CALLBACK"] === "NO_SOURCE"
			|| is_callable($commentEvent["DELETE_CALLBACK"])
		)
			? "Y"
			: "N"
	);

	if (
		!isset($params["SOURCE_ID"])
		|| (int)$params["SOURCE_ID"] <= 0
	)
	{
		foreach (EventManager::getInstance()->findEventHandlers('socialnetwork', 'OnAfterSonetLogEntryAddComment') as $handler)  // send notification
		{
			ExecuteModuleEventEx($handler, array($comment));
		}
	}

	$result["arComment"] = $comment;
	foreach($result["arComment"] as $key => $value)
	{
		if (mb_strpos($key, "~") === 0)
		{
			unset($result["arComment"][$key]);
		}
	}

	$result["arComment"]["RATING_USER_HAS_VOTED"] = "N";

	$result["sourceID"] = $comment["SOURCE_ID"];
	$result["timestamp"] = makeTimeStamp(
		array_key_exists("LOG_DATE_FORMAT", $comment)
			? $comment["LOG_DATE_FORMAT"]
			: $comment["LOG_DATE"]
	);

	$comment["UF"] = $USER_FIELD_MANAGER->getUserFields("SONET_COMMENT", $comment["ID"], LANGUAGE_ID);

	if (
		array_key_exists("UF_SONET_COM_DOC", $comment["UF"])
		&& array_key_exists("VALUE", $comment["UF"]["UF_SONET_COM_DOC"])
		&& is_array($comment["UF"]["UF_SONET_COM_DOC"]["VALUE"])
		&& count($comment["UF"]["UF_SONET_COM_DOC"]["VALUE"]) > 0
		&& $commentEvent["EVENT_ID"] !== "tasks_comment"
	)
	{
		$logEntryRights = array();
		$res = CSocNetLogRights::getList(array(), array("LOG_ID" => $logEntry["ID"]));
		while ($right = $res->fetch())
		{
			$logEntryRights[] = $right["GROUP_CODE"];
		}

		CSocNetLogTools::setUFRights($comment["UF"]["UF_SONET_COM_DOC"]["VALUE"], $logEntryRights);
	}

	$result['timeFormatted'] = formatDateFromDB(
		($comment['LOG_DATE_FORMAT'] ?? $comment['LOG_DATE']),
		self::getTimeFormat($params['TIME_FORMAT'])
	);

	$authorFields = self::getAuthorData([
		'userId' => $comment['USER_ID'],
	]);
	$createdBy = self::getCreatedByData([
		'userFields' => $authorFields,
		'languageId' => $params['LANGUAGE_ID'],
		'nameTemplate' => $params['NAME_TEMPLATE'],
		'showLogin' => $params['SHOW_LOGIN'],
		'pathToUser' => $params['PATH_TO_USER'],
	]);

	$commentFormatted = array(
		'LOG_DATE' => $comment["LOG_DATE"],
		"LOG_DATE_FORMAT" => $comment["LOG_DATE_FORMAT"] ?? null,
		"LOG_DATE_DAY" => ConvertTimeStamp(MakeTimeStamp($comment['LOG_DATE']), 'SHORT'),
		'LOG_TIME_FORMAT' => $result['timeFormatted'],
		"MESSAGE" => $comment["MESSAGE"],
		"MESSAGE_FORMAT" => $comment["~MESSAGE"] ?? null,
		'CREATED_BY' => $createdBy,
		"AVATAR_SRC" => CSocNetLogTools::formatEvent_CreateAvatar($authorFields, $params, ""),
		'USER_ID' => $comment['USER_ID'],
	);

	if (
		array_key_exists("CLASS_FORMAT", $commentEvent)
		&& array_key_exists("METHOD_FORMAT", $commentEvent)
	)
	{
		$fieldsFormatted = call_user_func(
			array($commentEvent["CLASS_FORMAT"], $commentEvent["METHOD_FORMAT"]),
			$comment,
			$params
		);
		$commentFormatted["MESSAGE_FORMAT"] = htmlspecialcharsback($fieldsFormatted["EVENT_FORMATTED"]["MESSAGE"]);
	}
	else
	{
		$commentFormatted["MESSAGE_FORMAT"] = $comment["MESSAGE"];
	}

	if (
		array_key_exists("CLASS_FORMAT", $commentEvent)
		&& array_key_exists("METHOD_FORMAT", $commentEvent)
	)
	{
		$fieldsFormatted = call_user_func(
			array($commentEvent["CLASS_FORMAT"], $commentEvent["METHOD_FORMAT"]),
			$comment,
			array_merge(
				$params,
				array(
					"MOBILE" => "Y"
				)
			)
		);
		$messageMobile = htmlspecialcharsback($fieldsFormatted["EVENT_FORMATTED"]["MESSAGE"]);
	}
	else
	{
		$messageMobile = $comment["MESSAGE"];
	}

	$result["arCommentFormatted"] = $commentFormatted;

	if (
		isset($params["PULL"])
		&& $params["PULL"] === "Y"
	)
	{
		$liveFeedCommentsParams = self::getLFCommentsParams([
			'ID' => $logEntry['ID'],
			'EVENT_ID' => $logEntry['EVENT_ID'],
			'ENTITY_TYPE' => $logEntry['ENTITY_TYPE'],
			'ENTITY_ID' => $logEntry['ENTITY_ID'],
			'SOURCE_ID' => $logEntry['SOURCE_ID'],
			'PARAMS' => $logEntry['PARAMS']
		]);

		$entityXMLId = $liveFeedCommentsParams['ENTITY_XML_ID'];

		$listCommentId = (
			!!$comment["SOURCE_ID"]
				? $comment["SOURCE_ID"]
				: $comment["ID"]
		);

		$eventHandlerID = EventManager::getInstance()->addEventHandlerCompatible("main", "system.field.view.file", array("CSocNetLogTools", "logUFfileShow"));
		$rights = CSocNetLogComponent::getCommentRights([
			'EVENT_ID' => $logEntry['EVENT_ID'],
			'SOURCE_ID' => $logEntry['SOURCE_ID'],
			'USER_ID' => $comment['USER_ID']
		]);

		$postContentTypeId = '';
		$commentContentTypeId = '';

		$postContentId = BitrixSocialnetworkLivefeedProvider::getContentId($logEntry);
		$canGetCommentContent = false;

		if (
			!empty($postContentId['ENTITY_TYPE'])
			&& ($postProvider = BitrixSocialnetworkLivefeedProvider::getProvider($postContentId['ENTITY_TYPE']))
			&& ($commentProvider = $postProvider->getCommentProvider())
		)
		{
			$postContentTypeId = $postProvider->getContentTypeId();
			$commentProviderClassName = get_class($commentProvider);
			$reflectionClass = new ReflectionClass($commentProviderClassName);

			$canGetCommentContent = ($reflectionClass->getMethod('initSourceFields')->class == $commentProviderClassName);
			if ($canGetCommentContent)
			{
				$commentContentTypeId = $commentProvider->getContentTypeId();
			}
		}

		$records = static::getLiveCommentRecords([
			'commentId' => $listCommentId,
			'ratingTypeId' => $comment['RATING_TYPE_ID'],
			'timestamp' => $result['timestamp'],
			'author' => [
				'ID' => $authorFields['ID'],
				'NAME' => $authorFields['NAME'],
				'LAST_NAME' => $authorFields['LAST_NAME'],
				'SECOND_NAME' => $authorFields['SECOND_NAME'],
				'PERSONAL_GENDER' => $authorFields['PERSONAL_GENDER'],
				'AVATAR' => $commentFormatted['AVATAR_SRC'],
			],
			'uf' => $comment['UF'],
			'ufFormatted' => $commentFormatted['UF'],
			'postMessageTextOriginal' => $comment['~MESSAGE'],
			'postMessageTextFormatted' => $commentFormatted['MESSAGE_FORMAT'],
			'mobileMessage' => $messageMobile,
			'aux' => ($params['AUX'] ?? ''),
			'auxLiveParams' => ($params['AUX_LIVE_PARAMS'] ?? []),
		]);

		$viewUrl = (string)($comment['EVENT']['URL'] ?? '');
		if (empty($viewUrl))
		{
			$pathToLogEntry = (string)($params['PATH_TO_LOG_ENTRY'] ?? '');
			if (!empty($pathToLogEntry))
			{
				$viewUrl = CComponentEngine::makePathFromTemplate(
						$pathToLogEntry,
						[
							'log_id' => $logEntry['ID']
						]
					) . (mb_strpos($pathToLogEntry, '?') === false ? '?' : '&') . 'commentId=#ID#';
			}
		}

		$rights['COMMENT_RIGHTS_CREATETASK'] = (
			$canGetCommentContent
			&& ModuleManager::isModuleInstalled('tasks')
				? 'Y'
				: 'N'
		);

		$res = static::sendLiveComment([
			'ratingTypeId' => $comment['RATING_TYPE_ID'],
			'entityXMLId' => $entityXMLId,
			'postContentTypeId' => $postContentTypeId,
			'commentContentTypeId' => $commentContentTypeId,
			'records' => $records,
			'rights' => $rights,
			'commentId' => $listCommentId,
			'action' => ($params['ACTION'] === 'UPDATE' ? 'EDIT' : 'REPLY'),
			'urlList' => [
				'view' => $viewUrl,
				'edit' => "__logEditComment('" . $entityXMLId . "', '#ID#', '" . $logEntry["ID"] . "');",
				'delete' => '/bitrix/components/bitrix/socialnetwork.log.entry/ajax.php?lang=' . $params['LANGUAGE_ID'] . '&action=delete_comment&delete_comment_id=#ID#&post_id=' . $logEntry['ID'] . '&site=' . $params['SITE_ID'],
			],
			'avatarSize' => $params['AVATAR_SIZE_COMMENT'],
			'nameTemplate' => $params['NAME_TEMPLATE'],
			'dateTimeFormat' => $params['DATE_TIME_FORMAT'],
		]);

		if ($eventHandlerID > 0)
		{
			EventManager::getInstance()->removeEventHandler('main', 'system.field.view.file', $eventHandlerID);
		}

		$result['return_data'] = $res['JSON'];
	}

	return $result;
}