• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/agent.php
  • Класс: CAllAgent
  • Вызов: CAllAgent::AddAgent
static function AddAgent(
	$name, // PHP function name
	$module = "", // module
	$period = "N", // check for agent execution count in period of time
	$interval = 86400, // time interval between execution
	$datecheck = "", // first check for execution time
	$active = "Y", // is the agent active or not
	$next_exec = "", // first execution time
	$sort = 100, // order
	$user_id = false, // user
	$existError = true // return error, if agent already exist
)
{
	global $DB, $APPLICATION;

	$z = $DB->Query("
		SELECT ID
		FROM b_agent
		WHERE NAME = '".$DB->ForSql($name)."'
		AND USER_ID".($user_id? " = ".(int)$user_id: " IS NULL")
	);
	if (!($agent = $z->Fetch()))
	{
		$arFields = array(
			"MODULE_ID" => $module,
			"SORT" => $sort,
			"NAME" => $name,
			"ACTIVE" => $active,
			"AGENT_INTERVAL" => $interval,
			"IS_PERIOD" => $period,
			"USER_ID" => $user_id,
		);
		$next_exec = (string)$next_exec;
		if ($next_exec != '')
			$arFields["NEXT_EXEC"] = $next_exec;

		$ID = CAgent::Add($arFields);
		return $ID;
	}
	else
	{
		if (!$existError)
			return $agent['ID'];

		if ($APPLICATION instanceof CMain)
		{
			$e = new CAdminException(array(
				array(
					"id" => "agent_exist",
					"text" => ($user_id
						? Loc::getMessage("MAIN_AGENT_ERROR_EXIST_FOR_USER", array('#AGENT#' => $name, '#USER_ID#' => $user_id))
						: Loc::getMessage("MAIN_AGENT_ERROR_EXIST_EXT", array('#AGENT#' => $name))
					)
				)
			));
			$APPLICATION->throwException($e);
		}
		return false;
	}
}