• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/classes/general/rest.php
  • Класс: CSocNetLogRestService
  • Вызов: CSocNetLogRestService::getUserBlogPost
static function getUserBlogPost($arParams, $offset, CRestServer $server): array
{
	$arParams = array_change_key_case($arParams, CASE_UPPER);

	$result = Array(
		'POSTS' => array(),
		'FILES' => array(),
	);

	if (!Loader::includeModule("blog"))
	{
		return $result;
	}

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

	$otherUserMode = ($userId !== self::getCurrentUserId());

	if ($userId <= 0)
	{
		throw new BitrixRestRestException("User ID can't be empty", "ID_EMPTY", CRestServer::STATUS_WRONG_REQUEST);
	}

	if (isset($arParams['FIRST_ID']))
	{
		$options['FIRST_ID'] = (int)$arParams['FIRST_ID'];
	}
	else
	{
		$options['LAST_ID'] = isset($arParams['LAST_ID']) && (int)$arParams['LAST_ID'] > 0 ? (int)$arParams['LAST_ID'] : 0;
	}

	$options['LIMIT'] = isset($arParams['LIMIT'])? ((int)$arParams['LIMIT'] > 1000 ? 1000 : (int)$arParams['LIMIT']) : 100;

	$filter = [
		'=USER_ID' => $userId,
		'@EVENT_ID' => self::getBlogPostEventId()
	];

	if (isset($options['FIRST_ID']))
	{
		$order = [];

		if ((int)$options['FIRST_ID'] > 0)
		{
			$filter['>ID'] = $options['FIRST_ID'];
		}
	}
	else
	{
		$order = [ 'ID' => 'DESC' ];

		if (isset($options['LAST_ID']) && (int)$options['LAST_ID'] > 0)
		{
			$filter[' $filter,
		'select' => array(
			'ID', 'SOURCE_ID'
		),
		'order' => $order,
		'limit' => $options['LIMIT']
	));

	$postIdList = array();
	while ($logFields = $res->fetch())
	{
		if ((int)$logFields['SOURCE_ID'] > 0)
		{
			$postIdList[] = $logFields['SOURCE_ID'];
			$logIdList[$logFields['SOURCE_ID']] = $logFields['ID'];
		}
	}

	$postIdList = array_unique($postIdList);
	if (empty($postIdList))
	{
		return $result;
	}

	$res = BitrixBlogPostTable::getList([
		'filter' => [
			'@ID' => $postIdList,
		],
		'select' => [
			'ID', 'DATE_CREATE', 'TITLE', 'DETAIL_TEXT', 'UF_BLOG_POST_FILE'
		],
		'order' => [ 'ID' => 'DESC' ],
	]);

	$attachedIdList = [];
	$postAttachedList = [];

	while ($postFields = $res->fetch())
	{
		$result['POSTS'][$postFields['ID']] = [
			'ID' => (int)$logIdList[$postFields['ID']],
			'POST_ID' => (int)$postFields['ID'],
			'DATE_CREATE' => $postFields['DATE_CREATE'],
			'TITLE' => ($otherUserMode ? '' : (string)$postFields['TITLE']),
			'TEXT' => ($otherUserMode ? '' : (string)$postFields['DETAIL_TEXT']),
			'ATTACH' => [],
		];
		if (!empty($postFields['UF_BLOG_POST_FILE']))
		{
			if (is_array($postFields['UF_BLOG_POST_FILE']))
			{
				$attached = $postFields['UF_BLOG_POST_FILE'];
			}
			elseif ((int)$postFields['UF_BLOG_POST_FILE'] > 0)
			{
				$attached = array((int)$postFields['UF_BLOG_POST_FILE']);
			}
			else
			{
				$attached = array();
			}

			if (!empty($attached))
			{
				$attachedIdList = array_merge($attachedIdList, $attached);
			}

			$postAttachedList[$postFields['ID']] = $attached;
		}
	}

	$attachedObjectList = [];

	if (
		!empty($attachedIdList)
		&& Loader::includeModule('disk')
	)
	{
		$res = BitrixDiskAttachedObject::getList([
			'filter' => [
				'@ID' => array_unique($attachedIdList)
			],
			'select' => [ 'ID', 'OBJECT_ID' ],
		]);
		while ($attachedObjectFields = $res->fetch())
		{
			$diskObjectId = $attachedObjectFields['OBJECT_ID'];

			if ($fileData = self::getFileData($diskObjectId))
			{
				$attachedObjectList[$attachedObjectFields['ID']] = $diskObjectId;
				$result['FILES'][$diskObjectId] = $fileData;
			}
		}
	}

	foreach ($result['POSTS'] as $key => $value)
	{
		if ($value['DATE_CREATE'] instanceof BitrixMainTypeDateTime)
		{
			$result['POSTS'][$key]['DATE_CREATE'] = date('c', $value['DATE_CREATE']->getTimestamp());
		}

		if (!empty($postAttachedList[$key]))
		{
			foreach ($postAttachedList[$key] as $attachedId)
			{
				if (!empty($attachedObjectList[$attachedId]))
				{
					$result['POSTS'][$key]['ATTACH'][] = $attachedObjectList[$attachedId];
				}
			}
		}

		$result['POSTS'][$key] = array_change_key_case($result['POSTS'][$key], CASE_LOWER);
	}

	$result['POSTS'] = array_values($result['POSTS']);
	$result['FILES'] = self::convertFileData($result['FILES']);

	return $result;
}