• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/classes/general/taskfiles.php
  • Класс: CTaskFiles
  • Вызов: CTaskFiles::AddMultiple
static function AddMultiple($taskId, $arFilesIds, $arParams = array())
{
	global $DB;

	$taskId = (int) $taskId;
	$userId = null;

	$bCheckRightsOnFiles = true;

	if (is_array($arParams))
	{
		if (isset($arParams['USER_ID']) && ($arParams['USER_ID'] > 0))
			$userId = (int) $arParams['USER_ID'];

		if (isset($arParams['CHECK_RIGHTS_ON_FILES']))
		{
			if ( ! in_array(
					$arParams['CHECK_RIGHTS_ON_FILES'],
					array(true, false, 'Y', 'N'),
					true
			))
			{
				throw new Exception();
			}

			if (
				($arParams['CHECK_RIGHTS_ON_FILES'] === false)
				|| ($arParams['CHECK_RIGHTS_ON_FILES'] === 'N')
			)
			{
				$bCheckRightsOnFiles = false;
			}
			else
				$bCheckRightsOnFiles = true;
		}
	}

	if ($userId === null)
	{
		$userId = User::getId();
		if(!$userId)
		{
			$userId = User::getAdminId();
		}
	}

	if ( ! self::CheckFieldsMultiple($taskId, $arFilesIds) )
		return (false);

	if ($bCheckRightsOnFiles)
	{
		$ar = self::checkFilesAccessibilityByUser($arFilesIds, $userId);

		// If we have one file, that is not accessible, than emit error
		foreach ($arFilesIds as $fileId)
		{
			if (
				( ! isset($ar['f' . $fileId]) )
				|| ($ar['f' . $fileId] === false)
			)
			{
				/** @var CMain $APPLICATION */
				global $APPLICATION;
				$e = new CAdminException(array(array('text' => GetMessage('TASKS_BAD_FILE_ID_EX'), 'id' => 'ERROR_TASKS_BAD_FILE_ID_EX')));
				$APPLICATION->ThrowException($e);
				return (false);
			}
		}
	}

	$arFields = array('ID' => 1, 'FILE_ID' => false, 'TASK_ID' => (int) $taskId);

	foreach ($arFilesIds as $fileId)
	{
		$arFields['FILE_ID'] = $fileId;
		// There is duplicate key error can occured, because CTasks::Update()
		// transmit all files ids to CTaskFiles::AddMultiple().
		// So, ignore DB errors.
		// TODO: patch CTasks::Update() to transmit only new file ids, or check duplicates here.
		$DB->Add('b_tasks_file', $arFields, array(), 'tasks', $ignore_errors = true);
	}

	// Mark that attached files is not temporary now, but permament (if it was temporary)
	$arTempFiles = self::getRegisteredTemporaryFilesList($userId);
	$arTempFilesInJustAttachedToTask = array_intersect($arFilesIds, $arTempFiles);
	self::unregisterTemporaryFiles($arTempFilesInJustAttachedToTask);

	return (true);
}