• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/lib/helper/servicecomment.php
  • Класс: BitrixSocialnetworkHelperServiceComment
  • Вызов: ServiceComment::processBlogCreateEntity
static function processBlogCreateEntity($params): bool
{
	$entityType = ($params['ENTITY_TYPE'] ?? false);
	$entityId = (int)($params['ENTITY_ID'] ?? 0);
	$sourceEntityType = (isset($params['SOURCE_ENTITY_TYPE']) && in_array($params['SOURCE_ENTITY_TYPE'], [ 'BLOG_POST', 'BLOG_COMMENT' ]) ? $params['SOURCE_ENTITY_TYPE'] : false);
	$sourceEntityId = (int)($params['SOURCE_ENTITY_ID'] ?? 0);
	$logId = 0;

	if (
		empty($sourceEntityType)
		|| $sourceEntityId <= 0
		|| empty($entityType)
		|| $entityId <= 0
		|| !Loader::includeModule('blog')
	)
	{
		return false;
	}

	$entity = static::getEntityData([
		'entityType' => $entityType,
		'entityId' => $entityId,
	]);

	if (empty($entity))
	{
		return false;
	}

	$source = static::getSourceData([
		'sourceEntityType' => $sourceEntityType,
		'sourceEntityId' => $sourceEntityId,
	]);

	if (empty($source))
	{
		return false;
	}

	$authorId = static::getEntityAuthorId([
		'entityType' => $entityType,
		'entityData' => $entity,
	]);
	$userIP = CBlogUser::getUserIP();
	$auxText = CommentAuxCreateEntity::getPostText();

	$logCommentFields = [
		'POST_ID' => $source['post']['ID'],
		'BLOG_ID' => $source['post']['BLOG_ID'],
		'POST_TEXT' => $auxText,
		'DATE_CREATE' => convertTimeStamp(time() + CTimeZone::getOffset(), 'FULL'),
		'AUTHOR_IP' => $userIP[0],
		'AUTHOR_IP1' => $userIP[1],
		'PARENT_ID' => false,
		'AUTHOR_ID' => $authorId,
		'SHARE_DEST' => Json::encode([
			'sourceType' => $sourceEntityType,
			'sourceId' => $sourceEntityId,
			'entityType' => $entityType,
			'entityId' => $entityId,
		]),
	];

	$entityLivefeedPovider = Provider::getProvider($sourceEntityType);
	$entityLivefeedPovider->setEntityId($sourceEntityId);
	$entityLivefeedPovider->initSourceFields();

	$url = $entityLivefeedPovider->getLiveFeedUrl();
	if (!empty($url))
	{
		$metaData = UrlPreview::getMetadataAndHtmlByUrl($url, true, false);

		if (
			!empty($metaData)
			&& !empty($metaData['ID'])
			&& (int)$metaData['ID'] > 0
		)
		{
			$signer = new Signer();
			$logCommentFields['UF_BLOG_COMM_URL_PRV'] = $signer->sign($metaData['ID'] . '', UrlPreview::SIGN_SALT);
		}
	}

	$newCommentId = CBlogComment::add($logCommentFields, false);
	if (!$newCommentId)
	{
		return false;
	}

	BXClearCache(true, '/blog/comment/' . (int)($source['post']['ID'] / 100) . '/' . $source['post']['ID'] . '/');

	$blogPostLivefeedProvider = new LivefeedBlogPost;

	$res = CSocNetLog::getList(
		[],
		[
			'EVENT_ID' => $blogPostLivefeedProvider->getEventId(),
			'SOURCE_ID' => $source['post']['ID']
		],
		false,
		[ 'nTopCount' => 1 ],
		[ 'ID' ]
	);
	if ($log = $res->fetch())
	{
		$logId = (int)$log['ID'];
	}

	if ($logId > 0)
	{
		$connection = Application::getConnection();
		$helper = $connection->getSqlHelper();

		$logCommentFields = [
			'ENTITY_TYPE' => SONET_ENTITY_USER,
			'ENTITY_ID' => $source['post']['AUTHOR_ID'],
			'EVENT_ID' => 'blog_comment',
			'=LOG_DATE' => $helper->getCurrentDateTimeFunction(),
			'LOG_ID' => $logId,
			'USER_ID' => $authorId,
			'MESSAGE' => $auxText,
			'TEXT_MESSAGE' => $auxText,
			'MODULE_ID' => false,
			'SOURCE_ID' => $newCommentId,
			'RATING_TYPE_ID' => 'BLOG_COMMENT',
			'RATING_ENTITY_ID' => $newCommentId,
		];

		CSocNetLogComments::add($logCommentFields, false, false);

		if (
			isset($params['LIVE'])
			&& $params['LIVE'] === 'Y'
		)
		{
			$userPage = Option::get('socialnetwork', 'user_page', SITE_DIR . 'company/personal/');
			$userPath = $userPage . 'user/' . $source['post']['AUTHOR_ID'] . '/';

			$auxLiveParamList = static::getAuxLiveParams([
				'sourceEntityType' => $sourceEntityType,
				'sourceEntityId' => $sourceEntityId,
				'sourceData' => $source,
				'entityType' => $entityType,
				'entityId' => $entityId,
				'entityData' => $entity,
				'userPath' => $userPath,
				'logId' => $logId,
			]);

			$provider = CommentAuxBase::init(CommentAuxCreateEntity::getType(), [
				'liveParamList' => $auxLiveParamList
			]);

			CBlogComment::addLiveComment($newCommentId, [
				'PATH_TO_USER' => $userPath,
				'LOG_ID' => $logId,
				'MODE' => 'PULL_MESSAGE',
				'AUX' => 'createentity',
				'AUX_LIVE_PARAMS' => $provider->getLiveParams(),
			]);
		}
	}

	return true;
}