function Update($ID, $arFields, $arParams = array()) { global $DB, $USER, $USER_FIELD_MANAGER; $ID = intval($ID); if ($ID < 1) { return false; } $bCheckFilesPermissions = false; if (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); $bCheckFilesPermissions = true; } $arParamsForCheckFields = array('CHECK_RIGHTS_ON_FILES' => $bCheckFilesPermissions); if (isset($arParams['USER_ID'])) { $arParamsForCheckFields['USER_ID'] = $arParams['USER_ID']; } // We need understand, does REPLICATE_PARAMS changed $rsCurData = self::GetByID($ID); $arCurData = $rsCurData->Fetch(); $this->currentData = $arCurData; if (intval($arCurData['BASE_TEMPLATE_ID']) > 0) { unset($arFields['REPLICATE']); // ... you cannot set replicate params unset($arFields['PARENT_ID']); // ... and base task $isReplicateParamsChanged = false; } else { $isReplicateParamsChanged = isset($arFields['REPLICATE']) && $arCurData['REPLICATE'] !== $arFields['REPLICATE'] || isset($arFields['REPLICATE_PARAMS']) && $arCurData['REPLICATE_PARAMS'] !== $arFields['REPLICATE_PARAMS']; } if ($userID === null) { $userID = is_object($USER) ? intval($USER->GetID()) : 1; } if ($this->CheckFields($arFields, $ID, $arParamsForCheckFields)) { if ($USER_FIELD_MANAGER->CheckFields("TASKS_TASK_TEMPLATE", $ID, $arFields)) { unset($arFields['ID']); $arBinds = array('DESCRIPTION' => $arFields['DESCRIPTION'], 'REPLICATE_PARAMS' => $arFields['REPLICATE_PARAMS'], 'ACCOMPLICES' => $arFields['ACCOMPLICES'], 'AUDITORS' => $arFields['AUDITORS'], 'FILES' => $arFields['FILES'], 'TAGS' => $arFields['TAGS'], 'DEPENDS_ON' => $arFields['DEPENDS_ON']); $strUpdate = $DB->PrepareUpdate('b_tasks_template', $arFields, 'tasks'); if ((string) $strUpdate !== '') { $strSql = "UPDATE b_tasks_template SET " . $strUpdate . " WHERE ID=" . $ID; $DB->QueryBind($strSql, $arBinds, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } if (isset($arFields['FILES'])) { CTaskFiles::removeTemporaryStatusForFiles(unserialize($arFields['FILES']), $arParams['USER_ID']); } $USER_FIELD_MANAGER->Update("TASKS_TASK_TEMPLATE", $ID, $arFields, $userID); // update template tree, if required if (isset($arFields['BASE_TEMPLATE_ID'])) { try { Template\DependencyTable::link($ID, intval($arFields['BASE_TEMPLATE_ID']), array('CREATE_PARENT_NODE_ON_NOTFOUND' => true)); } catch (\Bitrix\Tasks\DB\Tree\LinkExistsException $e) { } } if ($isReplicateParamsChanged) { // Firstly, remove all agents for this template /** @noinspection PhpDynamicAsStaticMethodCallInspection */ CAgent::RemoveAgent('CTasks::RepeatTaskByTemplateId(' . $ID . ');', 'tasks'); /** @noinspection PhpDynamicAsStaticMethodCallInspection */ CAgent::RemoveAgent('CTasks::RepeatTaskByTemplateId(' . $ID . ', 0);', 'tasks'); /** @noinspection PhpDynamicAsStaticMethodCallInspection */ CAgent::RemoveAgent('CTasks::RepeatTaskByTemplateId(' . $ID . ', 1);', 'tasks'); // Set up new agent if ($arFields['REPLICATE'] === 'Y') { $nextTime = CTasks::GetNextTime(unserialize($arFields['REPLICATE_PARAMS'])); if ($nextTime) { CTimeZone::Disable(); /** @noinspection PhpDynamicAsStaticMethodCallInspection */ CAgent::AddAgent('CTasks::RepeatTaskByTemplateId(' . $ID . ', 0);', 'tasks', 'N', 86400, $nextTime, 'Y', $nextTime); CTimeZone::Enable(); } } } return true; } } return false; }
/** * Agent handler for repeating tasks. * Create new task based on given template. * * @param integer $templateId - id of task template * @param integer $flipFlop - this param needs for prevent duplicate names * of agent (when adding agent, there is still exists current agent, * which will be removed later, when our function returns empty string), * must be 1 or 0. * * @return string empty string. */ public static function RepeatTaskByTemplateId($templateId, $flipFlop = 0) { global $DB; $curFlipFlop = (int) $flipFlop; if ($curFlipFlop === 0) { $newFlipFlop = 1; } else { $newFlipFlop = 0; } $templateId = (int) $templateId; $arFilter = array('ID' => $templateId); $rsTemplate = CTaskTemplates::GetList(array(), $arFilter, false, false, array('*', 'UF_*')); $arTemplate = $rsTemplate->Fetch(); if (!$arTemplate) { return ''; } // nothing to do if ($arTemplate['REPLICATE'] !== 'Y') { return ''; } // nothing to do if (intval($arTemplate['CREATED_BY'])) { CTasksTools::setOccurAsUserId($arTemplate['CREATED_BY']); } // not admin in logs, but template creator CTaskItem::addByTemplate($templateId, CTasksTools::GetCommanderInChief(), array(), array('TEMPLATE_DATA' => $arTemplate, 'SPAWNED_BY_AGENT' => true)); $arTemplate['REPLICATE_PARAMS'] = unserialize($arTemplate['REPLICATE_PARAMS']); // get next time task will be created, i.e. next Thursday if today is Thursday (and task marked as repeated) and so on $nextTime = CTasks::GetNextTime($arTemplate['REPLICATE_PARAMS']); if ($nextTime) { CTimeZone::Disable(); $currentAgentCallStr = 'CTasks::RepeatTaskByTemplateId(' . $templateId . ', ' . $curFlipFlop . ');'; $newAgentCallStr = 'CTasks::RepeatTaskByTemplateId(' . $templateId . ', ' . $newFlipFlop . ');'; CAgent::RemoveAgent($currentAgentCallStr); CAgent::AddAgent($newAgentCallStr, 'tasks', 'N', 86400, $nextTime, 'Y', $nextTime); CTimeZone::Enable(); } return ''; }