/** * Duplicate task and return an instance of the clone. * * @param mixed[] $overrideTaskData Task data needs to be overrided externally. * @param mixed[] $parameters Various set of parameters. * * <li> CLONE_CHILD_TASKS boolean clone subtasks or not * <li> CLONE_CHECKLIST_ITEMS boolean clone check list items or not * <li> CLONE_TAGS boolean clone tags or not * <li> CLONE_REMINDERS boolean clone reminders or not * <li> CLONE_TASK_DEPENDENCY boolean clone previous tasks or not * <li> CLONE_FILES boolean clone files or not * * @throws TasksException - on access denied, task not found. * @throws CTaskAssertException. * @throws Exception - on unexpected error. * * @return CTaskItem[] */ public function duplicate($overrideTaskData = array(), $parameters = array('CLONE_CHILD_TASKS' => true, 'CLONE_CHECKLIST_ITEMS' => true, 'CLONE_TAGS' => true, 'CLONE_REMINDERS' => true, 'CLONE_TASK_DEPENDENCY' => true, 'CLONE_FILES' => true)) { if (!is_array($overrideTaskData)) { $overrideTaskData = array(); } if (!is_array($parameters)) { $parameters = array(); } if (!isset($parameters['CLONE_CHILD_TASKS'])) { $parameters['CLONE_CHILD_TASKS'] = true; } if (!isset($parameters['CLONE_CHECKLIST_ITEMS'])) { $parameters['CLONE_CHECKLIST_ITEMS'] = true; } if (!isset($parameters['CLONE_TAGS'])) { $parameters['CLONE_TAGS'] = true; } if (!isset($parameters['CLONE_REMINDERS'])) { $parameters['CLONE_REMINDERS'] = true; } if (!isset($parameters['CLONE_TASK_DEPENDENCY'])) { $parameters['CLONE_TASK_DEPENDENCY'] = true; } if (!isset($parameters['CLONE_FILES'])) { $parameters['CLONE_FILES'] = true; } $result = array(); $data = $this->getData(false); // ensure we have access to the task if (is_array($data)) { $data = array_merge($data, $overrideTaskData); // drop unwanted unset($data['ID']); unset($data['GUID']); unset($data['STATUS']); // detach forum, if any unset($data['FORUM_TOPIC_ID']); unset($data['COMMENTS_COUNT']); // clean dates unset($data['CREATED_DATE']); unset($data['CHANGED_DATE']); unset($data['VIEWED_DATE']); unset($data['STATUS_CHANGED_DATE']); unset($data['CHANGED_BY']); $files = array(); if (is_array($data['UF_TASK_WEBDAV_FILES']) && !empty($data['UF_TASK_WEBDAV_FILES'])) { $files = $data['UF_TASK_WEBDAV_FILES']; } unset($data['UF_TASK_WEBDAV_FILES']); $clone = static::add($data, $this->getExecutiveUserId()); $taskDupId = $clone->getId(); if (intval($taskDupId)) { $result[$clone->getId()] = $clone; if ($parameters['CLONE_CHECKLIST_ITEMS']) { list($arChecklistItems, $arMetaData) = CTaskCheckListItem::fetchList($this, array('SORT_INDEX' => 'ASC')); unset($arMetaData); foreach ($arChecklistItems as $oChecklistItem) { $cliData = $oChecklistItem->getData(); $cliCloneData = array('TITLE' => $cliData['TITLE'], 'IS_COMPLETE' => $cliData['IS_COMPLETE'], 'SORT_INDEX' => $cliData['SORT_INDEX']); CTaskCheckListItem::add($clone, $cliCloneData); } } if ($parameters['CLONE_TAGS']) { $tags = $this->getTags(); if (is_array($tags)) { foreach ($tags as $tag) { if ((string) $tag != '') { $oTag = new CTaskTags(); $oTag->Add(array('TASK_ID' => $taskDupId, 'NAME' => $tag), $this->getExecutiveUserId()); } } } } if ($parameters['CLONE_REMINDERS']) { $res = CTaskReminders::GetList(false, array('TASK_ID' => $this->getId())); while ($item = $res->fetch()) { $item['TASK_ID'] = $taskDupId; $item['USER_ID'] = $this->getExecutiveUserId(); $oReminder = new CTaskReminders(); $oReminder->Add($item); } } if ($parameters['CLONE_TASK_DEPENDENCY']) { $res = CTaskDependence::GetList(array(), array('TASK_ID' => $this->getId())); while ($item = $res->fetch()) { $depInstance = new CTaskDependence(); if (is_array($item)) { $depInstance->Add(array('TASK_ID' => $taskDupId, 'DEPENDS_ON_ID' => $item['DEPENDS_ON_ID'])); } } } if ($parameters['CLONE_FILES'] && !empty($files) && \Bitrix\Main\Loader::includeModule('disk')) { // find which files are new and which are old $old = array(); $new = array(); foreach ($files as $fileId) { if ((string) $fileId) { if (strpos($fileId, 'n') === 0) { $new[] = $fileId; } else { $old[] = $fileId; } } } if (!empty($old)) { $userFieldManager = \Bitrix\Disk\Driver::getInstance()->getUserFieldManager(); $old = $userFieldManager->cloneUfValuesFromAttachedObject($old, $this->getExecutiveUserId()); if (is_array($old) && !empty($old)) { $new = array_merge($new, $old); } } if (!empty($new)) { $clone->update(array('UF_TASK_WEBDAV_FILES' => $new)); } } if ($parameters['CLONE_CHILD_TASKS']) { $notifADWasDisabled = CTaskNotifications::disableAutoDeliver(); $clones = $this->duplicateChildTasks($clone); if (is_array($clones)) { foreach ($clones as $cId => $cInst) { $result[$cId] = $cInst; } } if ($notifADWasDisabled) { CTaskNotifications::enableAutoDeliver(); } } } } return $result; }
if ($ex = $APPLICATION->GetException()) { $strError = $ex->GetString(); } if ($_POST["type"] == "json") { echo "['strError' : '" . CUtil::JSEscape(htmlspecialcharsbx($strError)) . "']"; } else { echo htmlspecialcharsbx($strError); } } } elseif ($_POST["mode"] == "reminders") { CTaskReminders::Delete(array("TASK_ID" => intval($_POST["id"]), "USER_ID" => $USER->GetID())); if (isset($_POST["reminders"])) { $obReminder = new CTaskReminders(); foreach ($_POST["reminders"] as $reminder) { $arFields = array("TASK_ID" => intval($_POST["id"]), "USER_ID" => $USER->GetID(), "REMIND_DATE" => $reminder["r_date"], "TYPE" => $reminder["type"], "TRANSPORT" => $reminder["transport"]); $obReminder->Add($arFields); } } } else { $arActionsMap = array('close' => 'complete', 'start' => 'startExecution', 'accept' => 'accept', 'renew' => 'renew', 'defer' => 'defer', 'decline' => 'decline', 'approve' => 'approve', 'disapprove' => 'disapprove', 'pause' => 'pauseExecution'); $arFields = array(); if ($_POST["mode"] == "mark" && in_array($_POST["mark"], array("NULL", "P", "N"))) { if ($_POST["mark"] == "NULL") { $arFields["MARK"] = false; } else { $arFields["MARK"] = $_POST["mark"]; } if ($arTask["SUBORDINATE"] == "Y" && $arTask["RESPONSIBLE_ID"] != $USER->GetID() && isset($_POST["report"])) { $arFields["ADD_IN_REPORT"] = $_POST["report"] == "true" ? "Y" : "N"; } } elseif ($_POST["mode"] == "report" && isset($_POST["report"])) {