• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/classes/general/rest.php
  • Класс: CSocNetLogRestService
  • Вызов: CSocNetLogRestService::shareBlogPost
static function shareBlogPost($fields): bool
{
	$postId = (int)$fields['POST_ID'];

	if ($postId <= 0)
	{
		throw new ArgumentException('Wrong post ID');
	}

	if (!Loader::includeModule('blog'))
	{
		throw new InvalidOperationException('Blog module not installed');
	}

	$siteId = (
		is_set($fields, "SITE_ID")
		&& !empty($fields["SITE_ID"])
			? $fields["SITE_ID"]
			: SITE_ID
	);

	$blogId = false;

	if (
		!is_set($fields, "BLOG_ID")
		|| (int)$fields["BLOG_ID"] <= 0
	)
	{
		$res = BitrixBlogPostTable::getList(array(
			'filter' => array(
				'=ID' => $postId
			),
			'select' => array('BLOG_ID')
		));
		if (
			($postFields = $res->fetch())
			&& !empty($postFields['BLOG_ID'])
		)
		{
			$blogId = (int)$postFields['BLOG_ID'];
		}
	}
	else
	{
		$blogId = (int)$fields["BLOG_ID"];
	}

	$blogPostPermsNewList = $fields['DEST'];

	if (!is_array($blogPostPermsNewList))
	{
		$blogPostPermsNewList = array($blogPostPermsNewList);
	}

	foreach ($blogPostPermsNewList as $key => $code)
	{
		if (
			$code !== 'UA'
			&& !preg_match('/^SG(d+)$/', $code, $matches)
			&& !preg_match('/^U(d+)$/', $code, $matches)
			&& !preg_match('/^UE(.+)$/', $code, $matches)
			&& !preg_match('/^DR(d+)$/', $code, $matches)
		)
		{
			unset($blogPostPermsNewList[$key]);
		}
	}

	if (empty($blogPostPermsNewList))
	{
		throw new ArgumentException('Wrong destinations');
	}

	$currentUserId = (
		isset($fields["USER_ID"])
		&& (int)$fields["USER_ID"] > 0
		&& self::isAdmin()
			? $fields["USER_ID"]
			: self::getCurrentUserId()
	);

	$currentUserPerm = self::getBlogPostPerm(array(
		'USER_ID' => $currentUserId,
		'POST_ID' => $postId
	));

	if ($currentUserPerm <= BitrixBlogItemPermissions::READ)
	{
		throw new AccessDeniedException('No read perms');
	}

	$resultFields = array(
		'ERROR_MESSAGE' => false,
		'PUBLISH_STATUS' => BLOG_PUBLISH_STATUS_PUBLISH
	);

	if (ModuleManager::isModuleInstalled('mail')
		&& ModuleManager::isModuleInstalled('intranet')
		&& (
			!Loader::includeModule('bitrix24')
			|| CBitrix24::isEmailConfirmed()
		)
	)
	{
		$destinationList = $blogPostPermsNewList;
		ComponentHelper::processBlogPostNewMailUserDestinations($destinationList);
		$blogPostPermsNewList = array_unique($destinationList);
	}

	$permsNew = ComponentHelper::checkBlogPostDestinationList(array(
		'DEST' => $blogPostPermsNewList,
		'SITE_ID' => $siteId,
		'AUTHOR_ID' => $currentUserId,
	), $resultFields);

	if ($resultFields['ERROR_MESSAGE'])
	{
		throw new SystemException($resultFields['ERROR_MESSAGE']);
	}

	if ($resultFields['PUBLISH_STATUS'] !== BLOG_PUBLISH_STATUS_PUBLISH)
	{
		throw new AccessDeniedException('No permissions to share by this user (ID =' . $currentUserId . ')');
	}

	$permsFull = array();
	$blogPostPermsOldList = CBlogPost::getSocNetPerms($postId);

	foreach ($blogPostPermsOldList as $type => $val)
	{
		foreach ($val as $id => $values)
		{
			if ($type !== 'U')
			{
				$permsFull[] = $type.$id;
			}
			else
			{
				$permsFull[] = (
					in_array('US' . $id, $values, true)
						? 'UA'
						: $type . $id
					);
			}
		}
	}

	foreach ($permsNew as $key => $code)
	{
		if (!in_array($code, $permsFull))
		{
			$permsFull[] = $code;
		}
		else
		{
			unset($permsNew[$key]);
		}
	}

	if (!empty($permsNew))
	{
		ComponentHelper::processBlogPostShare(
			array(
				"POST_ID" => $postId,
				"BLOG_ID" => $blogId,
				"SITE_ID" => $siteId,
				"SONET_RIGHTS" => $permsFull,
				"NEW_RIGHTS" => $permsNew,
				"USER_ID" => $currentUserId
			),
			array(
				'PATH_TO_POST' => BitrixSocialnetworkHelperPath::get('userblogpost_page', $siteId)
			)
		);
	}

	return true;
}