• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/classes/general/tasktemplates.php
  • Класс: CTaskTemplates
  • Вызов: CTaskTemplates::CheckFields
function CheckFields(&$arFields, $ID = false, $arParams = array())
{
	global $APPLICATION;

	if(intval($ID))
	{
		if(!is_array($this->currentData))
			$this->currentData = self::GetById($ID)->fetch();
	}

	////////////////////////////////////
	// deal with TYPE

	if(intval($this->currentData['ID']) && isset($arFields['TPARAM_TYPE']) && $this->currentData['TPARAM_TYPE'] != $arFields['TPARAM_TYPE'])
	{
		$this->_errors[] = array("text" => 'You can not change TYPE of an existing template', "id" => "ERROR_TASKS_BAD_TEMPLATE_PARAMETER_TYPE");
		unset($arFields['TPARAM_TYPE']); // You may not change template type of an existing template
	}

	// check allowed value
	if(isset($arFields['TPARAM_TYPE']))
	{
		if($arFields['TPARAM_TYPE'] != static::TYPE_FOR_NEW_USER) // white list here later
			$this->_errors[] = array("text" => 'Unknown template type id passed', "id" => "ERROR_TASKS_BAD_TEMPLATE_PARAMETER_TYPE");
	}

	// unset some data, if type is TYPE_FOR_NEW_USER
	if((!intval($ID) && $arFields['TPARAM_TYPE'] == CTaskTemplates::TYPE_FOR_NEW_USER) || (intval($ID) && $this->currentData['TPARAM_TYPE'] == CTaskTemplates::TYPE_FOR_NEW_USER))
	{
		$arFields['BASE_TEMPLATE_ID'] = ''; // such kind of template can not have parent template ...
		$arFields['REPLICATE_PARAMS'] = serialize(array()); // ... and replication params
		$arFields['RESPONSIBLE_ID'] = '0'; // ... and responsible_id
		$arFields['RESPONSIBLES'] = serialize(array(0)); // ... and other responsibles
		$arFields['MULTITASK'] = 'N'; // ... and be a multitask
	}

	////////////////////////////////////
	// deal with RESPONSIBLES

	if(
		($ID === false && $arFields['TPARAM_TYPE'] != CTaskTemplates::TYPE_FOR_NEW_USER)
		|| ($ID !== false && $this->currentData['TPARAM_TYPE'] != CTaskTemplates::TYPE_FOR_NEW_USER)
	)
	{
		if(isset($arFields["RESPONSIBLE_ID"]))
		{

			$r = CUser::GetByID($arFields["RESPONSIBLE_ID"]);
			if (!$r->Fetch())
			{
				$this->_errors[] = array("text" => GetMessage("TASKS_BAD_RESPONSIBLE_ID_EX"), "id" => "ERROR_TASKS_BAD_RESPONSIBLE_ID_EX");
			}
		}
		else
		{
			if($ID === false)
				$this->_errors[] = array("text" => GetMessage("TASKS_BAD_RESPONSIBLE_ID"), "id" => "ERROR_TASKS_BAD_RESPONSIBLE_ID");
		}
	}

	////////////////////////////////////
	// deal with other data

	if ((is_set($arFields, "TITLE") || $ID === false) && $arFields["TITLE"] == '')
	{
		$this->_errors[] = array("text" => GetMessage("TASKS_BAD_TITLE"), "id" => "ERROR_BAD_TASKS_TITLE");
	}

	if(array_key_exists('TPARAM_REPLICATION_COUNT', $arFields))
	{
		$arFields['TPARAM_REPLICATION_COUNT'] = intval($arFields['TPARAM_REPLICATION_COUNT']);
	}
	elseif(!$ID)
	{
		$arFields['TPARAM_REPLICATION_COUNT'] = 1;
	}

	/*
	if(!$ID)
	{
		// since this time we dont allow to create tasks with a non-bbcode description
		if($arFields['DESCRIPTION_IN_BBCODE'] == 'N')
		{
			$this->_errors[] = array("text" => GetMessage("TASKS_DESCRIPTION_IN_BBCODE_NO_NOT_ALLOWED"), "id" => "ERROR_TASKS_DESCRIPTION_IN_BBCODE_NO_NOT_ALLOWED");
		}
		else
		{
			$arFields['DESCRIPTION_IN_BBCODE'] = 'Y';
		}
	}
	*/

	if (isset($arFields['BASE_TEMPLATE_ID']))
	{
		try
		{
			if(intval($arFields['BASE_TEMPLATE_ID']))
			{
				$template = static::GetList(array(), array('ID' => $arFields['BASE_TEMPLATE_ID']), false, false, array('ID'))->fetch();
				if(!is_array($template))
					$this->_errors[] = array("text" => Loc::getMessage("TASKS_TEMPLATE_BASE_TEMPLATE_ID_NOT_EXISTS"), "id" => "ERROR_TASKS_BASE_TEMPLATE_ID_NOT_EXISTS");

				// you cannot add a template with both PARENT_ID and BASE_TEMPLATE_ID set. BASE_TEMPLATE_ID has greather priority
				if(isset($arFields['PARENT_ID']))
					$arFields['PARENT_ID'] = '';

				// you cannot add REPLICATE parameters here in case of BASE_TEMPLATE_ID is set
				if(isset($arFields['REPLICATE']))
					$arFields['REPLICATE'] = serialize(array());

				$arFields['REPLICATE_PARAMS'] = serialize(array());
			}
		}
		catch(BitrixMainArgumentException $e)
		{
			$this->_errors[] = array("text" => Loc::getMessage("TASKS_TEMPLATE_BAD_BASE_TEMPLATE_ID"), "id" => "ERROR_TASKS_BAD_BASE_TEMPLATE_ID");
		}
	}

	// move 0 to null in PARENT_ID to avoid constraint and query problems
	// todo: move PARENT_ID, GROUP_ID and other "foreign keys" to the unique way of keeping absense of relation: null, 0 or ''
	if(array_key_exists('PARENT_ID', $arFields))
	{
		$parentId = intval($arFields['PARENT_ID']);
		if(!intval($parentId))
		{
			$arFields['PARENT_ID'] = false;
		}
	}

	if (is_set($arFields, "PARENT_ID") && intval($arFields["PARENT_ID"]) > 0)
	{
		$r = CTasks::GetList(array(), array("ID" => $arFields["PARENT_ID"]));
		if (!$r->Fetch())
		{
			$this->_errors[] = array("text" => GetMessage("TASKS_BAD_PARENT_ID"), "id" => "ERROR_TASKS_BAD_PARENT_ID");
		}
	}

	if (
		isset($arFields['FILES'])
		&& isset($arParams['CHECK_RIGHTS_ON_FILES'])
		&& (
			($arParams['CHECK_RIGHTS_ON_FILES'] === true)
			|| ($arParams['CHECK_RIGHTS_ON_FILES'] === 'Y')
		)
	)
	{
		CTaskAssert::assert(
			isset($arParams['USER_ID'])
			&& CTaskAssert::isLaxIntegers($arParams['USER_ID'])
			&& ($arParams['USER_ID'] > 0)
		);

		// Are there any files?
		if ($arFields['FILES'] !== false)
		{
			// There is must be serialized array
			$arFilesIds = unserialize($arFields['FILES'], ['allowed_classes' => false]);

			if (is_array($arFilesIds))
			{
				$ar = CTaskFiles::checkFilesAccessibilityByUser($arFilesIds, (int) $arParams['USER_ID']);

				// 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)
					)
					{
						$this->_errors[] = array('text' => GetMessage('TASKS_BAD_FILE_ID_EX'), 'id' => 'ERROR_TASKS_BAD_FILE_ID_EX');
					}
				}
			}
			else
			{
				$this->_errors[] = array('text' => GetMessage('TASKS_BAD_FILE_ID_EX'), 'id' => 'ERROR_TASKS_BAD_FILE_ID_EX');
			}
		}
	}

	// REPLICATE_PARAMS comes serialized 0_o
	if((string) $arFields['REPLICATE_PARAMS'] != '')
	{
		$params = BitrixTasksUtilType::unSerializeArray($arFields['REPLICATE_PARAMS']);

		$arFields['REPLICATE_PARAMS'] = serialize(self::parseReplicationParams($params));
	}

	// ensure RESPONSIBLES filled anyway
	if(!$ID)
	{
		// set RESPONSIBLES from RESPONSIBLE_ID if not set on add()
		if((string) $arFields['RESPONSIBLES'] == '')
		{
			$arFields['RESPONSIBLES'] = serialize(array($arFields['RESPONSIBLE_ID']));
		}
	}
	else
	{
		if((string) $arFields['RESPONSIBLES'] == '' && (string) $this->currentData['RESPONSIBLES'] == '')
		{
			$arFields['RESPONSIBLES'] = serialize(array($arFields['RESPONSIBLE_ID']));
		}
	}

	if (!empty($this->_errors))
	{
		$e = new CAdminException($this->_errors);
		$APPLICATION->ThrowException($e);
		return false;
	}

	//Defaults
	if (is_set($arFields, "PRIORITY") && !in_array($arFields["PRIORITY"], Array(0, 1, 2)))
		$arFields["PRIORITY"] = 1;

	return true;
}