• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/comments/task/commentposter.php
  • Класс: BitrixTasksCommentsTaskCommentPoster
  • Вызов: CommentPoster::parseReplaces
static function parseReplaces(string $message, array $params): string
{
	$userId = User::getId();
	$entityTypes = ['TK', 'TASK'];
	if (isset($params['entityType']) && in_array($params['entityType'], $entityTypes, true))
	{
		$taskId = ($params['entityId'] ?? 0);
	}

	$replaces = [
		'EFFICIENCY',
		'DEADLINE',
		'DEADLINE_CHANGE',
		'TASK_APPROVE',
		'TASK_DISAPPROVE',
		'TASK_COMPLETE',
	];
	foreach ($replaces as $key)
	{
		$start = "#{$key}_START#";
		$end = "#{$key}_END#";

		if (
			mb_strpos($message, $start) === false
			&& mb_strpos($message, $end) === false
		)
		{
			continue;
		}

		switch ($key)
		{
			case 'EFFICIENCY':
				preg_match_all('/(?<=[USER=)d+(?=])/', $message, $userIds);
				$userIds = array_map('intval', $userIds[0]);
				$replaces =
					in_array($userId, $userIds, true)
						? ["[URL=/company/personal/user/{$userId}/tasks/effective/]", "[/URL]"]
						: ""
				;
				$message = str_replace([$start, $end], $replaces, $message);
				break;

			case 'DEADLINE':
				preg_match_all("/(?<={$start})d+(?={$end})/", $message, $timestamp);
				if (!($timestamp = (int)$timestamp[0][0]))
				{
					break;
				}
				$culture = Context::getCurrent()->getCulture();
				$format = "{$culture->getDayMonthFormat()}, {$culture->getShortTimeFormat()}";
				$deadline = FormatDate($format, MakeTimeStamp(DateTime::createFromTimestamp($timestamp)));
				$message = str_replace([$timestamp, $start, $end], [$deadline, '', ''], $message);
				break;

			case 'DEADLINE_CHANGE':
			case 'TASK_APPROVE':
			case 'TASK_DISAPPROVE':
			case 'TASK_COMPLETE':
				$actionMap = [
					'DEADLINE_CHANGE' => AccessActionDictionary::ACTION_TASK_DEADLINE,
					'TASK_APPROVE' => AccessActionDictionary::ACTION_TASK_APPROVE,
					'TASK_DISAPPROVE' => AccessActionDictionary::ACTION_TASK_DISAPPROVE,
					'TASK_COMPLETE' => AccessActionDictionary::ACTION_TASK_COMPLETE,
				];
				$replace = '';
				if (isset($taskId) && AccessTaskAccessController::can($userId, $actionMap[$key], $taskId))
				{
					$actionUrl = static::getCommentActionUrl($userId, $taskId, $key);
					$replace = ["[URL={$actionUrl}]", "[/URL]"];
				}
				$message = str_replace([$start, $end], $replace, $message);
				break;

			default:
				$message = str_replace([$start, $end], '', $message);
				break;
		}
	}

	preg_match_all('/(?<=[GROUP_ID=)d+(?=])/', $message, $groupIds);
	foreach ($groupIds[0] as $groupId)
	{
		$message = str_replace(
			["[GROUP_ID={$groupId}]", "[/GROUP_ID]"],
			($groupId > 0 ? ["[URL=/workgroups/group/{$groupId}/]", "[/URL]"] : ['', '']),
			$message
		);
	}

	return $message;
}