/** * Add a new check list item to a specified task */ public function add(array $data, array $parameters = array()) { global $USER; $result = array(); if ($taskId = $this->checkTaskId($data['TASK_ID'])) { $task = \CTaskItem::getInstanceFromPool($taskId, $USER->GetId()); // or directly, new \CTaskItem($taskId, $USER->GetId()); list($task, $id) = \CTaskCheckListItem::add($task, $data); $result['DATA']['CHECKLIST']['ID'] = $id; } return $result; }
public static function add($userId, $taskId, array $data, array $parameters = array('PUBLIC_MODE' => false)) { $errors = static::ensureHaveErrorCollection($parameters); if ($parameters['PUBLIC_MODE']) { $data = static::filterData($data, $errors); } if ($errors->checkNoFatals()) { $task = static::getTask($userId, $taskId); $item = \CTaskCheckListItem::add($task, $data); $itemId = $item->getId(); } return array('TASK' => $task, 'ITEM_ID' => $itemId, 'ERRORS' => $errors); }
/** * Create a task by a template. * * @param integer $templateId - Id of task template. * @param integer $executiveUserId User id. Put 1 here to skip rights. * @param mixed[] $overrideTaskData Task data needs to be overrided externally. * @param mixed[] $parameters Various set of parameters. * * <li> TEMPLATE_DATA mixed[] pre-cached data, if available we can get rid of additional queries * <li> CREATE_CHILD_TASKS boolean if false, sub-tasks wont be created * <li> CREATE_MULTITASK boolean if false, discards template rule of "copying task to several responsibles" * <li> BEFORE_ADD_CALLBACK callable callback called before each task added, allows to modify data passed to CTaskItem::add() * * @throws TasksException - on access denied, task not found * @throws CTaskAssertException * @throws Exception - on unexpected error * * @return CTaskItem[] */ public static function addByTemplate($templateId, $executiveUserId, $overrideTaskData = array(), $parameters = array('TEMPLATE_DATA' => array(), 'CREATE_CHILD_TASKS' => true, 'CREATE_MULTITASK' => true, 'BEFORE_ADD_CALLBACK' => null, 'SPAWNED_BY_AGENT' => false)) { CTaskAssert::assertLaxIntegers($executiveUserId); CTaskAssert::assert($executiveUserId > 0); global $DB; $templateId = (int) $templateId; if (!$templateId) { return array(); // template id not set } if (!is_array($overrideTaskData)) { $overrideTaskData = array(); } if (!is_array($parameters)) { $parameters = array(); } if (!isset($parameters['CREATE_CHILD_TASKS'])) { $parameters['CREATE_CHILD_TASKS'] = true; } if (!isset($parameters['CREATE_MULTITASK'])) { $parameters['CREATE_MULTITASK'] = true; } if (!isset($parameters['BEFORE_ADD_CALLBACK'])) { $parameters['BEFORE_ADD_CALLBACK'] = null; } if (!isset($parameters['SPAWNED_BY_AGENT'])) { $parameters['SPAWNED_BY_AGENT'] = false; } // read template data if (is_array($parameters['TEMPLATE_DATA']) && !empty($parameters['TEMPLATE_DATA'])) { $arTemplate = $parameters['TEMPLATE_DATA']; } else { $arFilter = array('ID' => $templateId); $rsTemplate = CTaskTemplates::GetList(array(), $arFilter); $arTemplate = $rsTemplate->Fetch(); if (!$arTemplate) { return array(); // nothing to do } } $arTemplate = array_merge($arTemplate, $overrideTaskData); if (!isset($arTemplate['CHECK_LIST'])) { // get template checklist $arTemplate['CHECK_LIST'] = array(); $res = \Bitrix\Tasks\Template\CheckListItemTable::getList(array('filter' => array('TEMPLATE_ID' => $templateId), 'select' => array('IS_COMPLETE', 'SORT_INDEX', 'TITLE'))); while ($item = $res->fetch()) { $arTemplate['CHECK_LIST'][] = $item; } } ////////////////////////////////////////////// ////////////////////////////////////////////// ////////////////////////////////////////////// unset($arTemplate['STATUS']); $arFields = $arTemplate; $arFields['CREATED_DATE'] = date($DB->DateFormatToPHP(CSite::GetDateFormat('FULL')), time() + CTimeZone::GetOffset()); $arFields['ACCOMPLICES'] = unserialize($arFields['ACCOMPLICES']); $arFields['AUDITORS'] = unserialize($arFields['AUDITORS']); $arFields['TAGS'] = unserialize($arFields['TAGS']); $arFields['FILES'] = unserialize($arFields['FILES']); $arFields['DEPENDS_ON'] = unserialize($arFields['DEPENDS_ON']); $arFields['REPLICATE'] = 'N'; $arFields['CHANGED_BY'] = $arFields['CREATED_BY']; $arFields['CHANGED_DATE'] = $arFields['CREATED_DATE']; if (!$arFields['ACCOMPLICES']) { $arFields['ACCOMPLICES'] = array(); } if (!$arFields['AUDITORS']) { $arFields['AUDITORS'] = array(); } unset($arFields['ID'], $arFields['REPLICATE'], $arFields['REPLICATE_PARAMS']); if ($arTemplate['DEADLINE_AFTER']) { $deadlineAfter = $arTemplate['DEADLINE_AFTER'] / (24 * 60 * 60); $deadline = strtotime(date('Y-m-d 00:00') . ' +' . $deadlineAfter . ' days'); $arFields['DEADLINE'] = date($DB->DateFormatToPHP(CSite::GetDateFormat('SHORT')), $deadline); } $multitaskMode = false; if ($parameters['CREATE_MULTITASK']) { $arFields['RESPONSIBLES'] = unserialize($arFields['RESPONSIBLES']); // copy task to multiple responsibles if ($arFields['MULTITASK'] == 'Y' && !empty($arFields['RESPONSIBLES'])) { $arFields['RESPONSIBLE_ID'] = $arFields['CREATED_BY']; $multitaskMode = true; } else { $arFields['RESPONSIBLES'] = array(); } } else { $arFields['MULTITASK'] = 'N'; $arFields['RESPONSIBLES'] = array(); } $arFields['FORKED_BY_TEMPLATE_ID'] = $templateId; // add main task to the create list $tasksToCreate = array($arFields); // if MULTITASK where set to Y, create a duplicate task for each of RESPONSIBLES if (!empty($arFields['RESPONSIBLES'])) { $arFields['MULTITASK'] = 'N'; foreach ($arFields['RESPONSIBLES'] as $responsible) { $arFields['RESPONSIBLE_ID'] = $responsible; $tasksToCreate[] = $arFields; } } // get sub-templates $subTasksToCreate = array(); if ($parameters['CREATE_CHILD_TASKS'] !== false) { $subTasksToCreate = static::getChildTemplateData($templateId); } $created = array(); // first, create ROOT tasks $multitaskTaskId = false; $i = 0; foreach ($tasksToCreate as $arFields) { if ($multitaskMode && $i > 0) { if ($multitaskTaskId) { // all following tasks will be subtasks of a base task in case of MULTITASK was turned on $arFields['PARENT_ID'] = $multitaskTaskId; } else { break; // no child tasks will be created, because parent task failed to be created } } $add = true; if (is_callable($parameters['BEFORE_ADD_CALLBACK'])) { $result = call_user_func_array($parameters['BEFORE_ADD_CALLBACK'], array(&$arFields)); if ($result === false) { $add = false; } } if ($add) { // temporary commented out, because there is currently no way to pass // 'SPAWNED_BY_AGENT' => true // parameter to CTaskTemplate::add() : //$taskInstance = static::add($arFields, $executiveUserId); //$taskId = $taskInstance->getId(); $task = new CTasks(); $taskId = $task->Add($arFields, array('SPAWNED_BY_AGENT' => !!$parameters['SPAWNED_BY_AGENT'], 'USER_ID' => $executiveUserId)); if (intval($taskId)) { $taskInstance = static::getInstance($taskId, $executiveUserId); // the first task should be mom in case of multitasking if ($multitaskMode && $i == 0) { $multitaskTaskId = $taskId; } // check list items for root task foreach ($arTemplate['CHECK_LIST'] as $item) { CTaskCheckListItem::add($taskInstance, $item); } $created[$taskId] = $taskInstance; if (!empty($subTasksToCreate)) { $notifADWasDisabled = CTaskNotifications::disableAutoDeliver(); $createdSubtasks = $taskInstance->addChildTasksByTemplate($templateId, array('CHILD_TEMPLATE_DATA' => $subTasksToCreate, 'BEFORE_ADD_CALLBACK' => $parameters['BEFORE_ADD_CALLBACK'], 'SPAWNED_BY_AGENT' => $parameters['SPAWNED_BY_AGENT'])); if ($notifADWasDisabled) { CTaskNotifications::enableAutoDeliver(); } if (is_array($createdSubtasks) && !empty($createdSubtasks)) { foreach ($createdSubtasks as $ctId => $ctInst) { $created[$ctId] = $ctInst; } } } } } $i++; } return $created; }
$arCurOperationResult = array('returnValue' => $justCreatedLogId, 'justCreatedLogId' => $justCreatedLogId, 'requestedData' => $arAction['elapsedTimeData'], 'errors' => $arErrors); if ($justCreatedLogId === false) { $breakExecution = true; } break; case 'CTaskCheckListItem::add()': CTaskAssert::assert(isset($arAction['taskId'], $arAction['checklistData'], $arAction['checklistData']['TITLE']) && count($arAction['checklistData']) === 1 && is_string($arAction['checklistData']['TITLE'])); // Is task id the result of previous operation in batch? $taskId = BXTasksResolveDynaParamValue($arAction['taskId'], array('$arOperationsResults' => $arOperationsResults)); CTaskAssert::assertLaxIntegers($taskId); $justCreatedId = false; $arErrors = array(); try { $oTask = CTaskItem::getInstanceFromPool($taskId, $loggedInUserId); $arFields = array('TITLE' => $arAction['checklistData']['TITLE']); $oCheckListItem = CTaskCheckListItem::add($oTask, $arFields); $justCreatedId = $oCheckListItem->getId(); } catch (Exception $e) { if ($e->GetCode() & TasksException::TE_FLAG_SERIALIZED_ERRORS_IN_MESSAGE) { $arErrors = unserialize($e->GetMessage()); } else { $arErrors[] = array('text' => 'UNKNOWN ERROR OCCURED', 'id' => 'ERROR_TASKS_UNKNOWN'); } } $arCurOperationResult = array('returnValue' => $justCreatedId, 'justCreatedId' => $justCreatedId, 'requestedData' => $arAction['checklistData'], 'errors' => $arErrors); if ($justCreatedId === false) { $breakExecution = true; } break; case 'CTaskCheckListItem::complete()': case 'CTaskCheckListItem::renew()':