/** * Add new task * * @access public * @param void * @return null */ function add_task() { if (logged_user()->isGuest()) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } $project = active_or_personal_project(); if (!ProjectTask::canAdd(logged_user(), $project)) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $task = new ProjectTask(); $task_data = array_var($_POST, 'task'); if (!is_array($task_data)) { $task_data = array('milestone_id' => array_var($_POST, 'milestone_id', 0), 'project_id' => array_var($_POST, 'project_id', active_or_personal_project()->getId()), 'title' => array_var($_POST, 'title', ''), 'assigned_to' => array_var($_POST, 'assigned_to', '0:0'), 'parent_id' => array_var($_POST, 'parent_id', 0), 'priority' => array_var($_POST, 'priority', ProjectTasks::PRIORITY_NORMAL), 'text' => array_var($_POST, 'text', ''), 'start_date' => getDateValue(array_var($_POST, 'task_start_date', '')), 'due_date' => getDateValue(array_var($_POST, 'task_due_date', '')), 'is_template' => array_var($_POST, "is_template", array_var($_GET, "is_template", false)), 'tags' => array_var($_POST, "tags", ''), 'object_subtype' => array_var($_POST, "object_subtype", config_option('default task co type')), 'send_notification' => array_var($_POST, 'notify') && array_var($_POST, 'notify') == 'true'); // array $from_email = array_var($_GET, 'from_email'); $email = MailContents::findById($from_email); if ($email instanceof MailContent) { $task_data['title'] = $email->getSubject(); $task_data['text'] = lang('create task from email description', $email->getSubject(), $email->getFrom(), $email->getTextBody()); $task_data['tags'] = implode(", ", $email->getTagNames()); tpl_assign('from_email', $email); } } // if if (array_var($_GET, 'replace')) { ajx_replace(true); } tpl_assign('task_data', $task_data); tpl_assign('task', $task); if (is_array(array_var($_POST, 'task'))) { $proj = Projects::findById(array_var($task_data, 'project_id')); if ($proj instanceof Project) { $project = $proj; } // order $task->setOrder(ProjectTasks::maxOrder(array_var($task_data, "parent_id", 0), array_var($task_data, "milestone_id", 0))); $task_data['due_date'] = getDateValue(array_var($_POST, 'task_due_date')); $task_data['start_date'] = getDateValue(array_var($_POST, 'task_start_date')); try { $err_msg = $this->setRepeatOptions($task_data); if ($err_msg) { flash_error($err_msg); ajx_current("empty"); return; } $task->setFromAttributes($task_data); $totalMinutes = array_var($task_data, 'time_estimate_hours', 0) * 60 + array_var($task_data, 'time_estimate_minutes', 0); $task->setTimeEstimate($totalMinutes); $task->setIsPrivate(false); // Not used, but defined as not null. // Set assigned to $assigned_to = explode(':', array_var($task_data, 'assigned_to', '')); $company_id = array_var($assigned_to, 0, 0); $user_id = array_var($assigned_to, 1, 0); $can_assign = can_assign_task_to_company_user(logged_user(), $task, $company_id, $user_id); if ($can_assign !== true) { flash_error($can_assign); ajx_current("empty"); return; } $task->setAssignedToCompanyId($company_id); $task->setAssignedToUserId($user_id); $id = array_var($_GET, 'id', 0); $parent = ProjectTasks::findById($id); if ($parent instanceof ProjectTask) { $task->setParentId($id); if ($parent->getIsTemplate()) { $task->setIsTemplate(true); } } if ($task->getParentId() > 0 && $task->hasChild($task->getParentId())) { flash_error(lang('task child of child error')); ajx_current("empty"); return; } //Add handins $handins = array(); for ($i = 0; $i < 4; $i++) { if (isset($task_data["handin{$i}"]) && is_array($task_data["handin{$i}"]) && trim(array_var($task_data["handin{$i}"], 'title')) != '') { $assigned_to = explode(':', array_var($task_data["handin{$i}"], 'assigned_to', '')); $handins[] = array('title' => array_var($task_data["handin{$i}"], 'title'), 'responsible_company_id' => array_var($assigned_to, 0, 0), 'responsible_user_id' => array_var($assigned_to, 1, 0)); // array } // if } // for DB::beginWork(); $task->save(); //$task->setProject($project); //echo 'pepe'; DB::rollback(); die(); $task->setTagsFromCSV(array_var($task_data, 'tags')); foreach ($handins as $handin_data) { $handin = new ObjectHandin(); $handin->setFromAttributes($handin_data); $handin->setObjectId($task->getId()); $handin->setObjectManager(get_class($task->manager())); $handin->save(); } // foreach*/ if (array_var($_GET, 'copyId', 0) > 0) { // copy remaining stuff from the task with id copyId $toCopy = ProjectTasks::findById(array_var($_GET, 'copyId')); if ($toCopy instanceof ProjectTask) { ProjectTasks::copySubTasks($toCopy, $task, array_var($task_data, 'is_template', false)); } } //Link objects $object_controller = new ObjectController(); if ($parent instanceof ProjectTask) { // task is being added as subtask of another, so place in same workspace $task->addToWorkspace($parent->getProject()); } else { $object_controller->add_to_workspaces($task); } $object_controller->link_to_new_object($task); $object_controller->add_subscribers($task); $object_controller->add_custom_properties($task); $object_controller->add_reminders($task); ApplicationLogs::createLog($task, $task->getWorkspaces(), ApplicationLogs::ACTION_ADD); DB::commit(); // notify asignee if (array_var($task_data, 'send_notification') == 'checked') { try { Notifier::taskAssigned($task); } catch (Exception $e) { evt_add("debug", $e->getMessage()); } // try } if ($task->getIsTemplate()) { flash_success(lang('success add template', $task->getTitle())); } else { flash_success(lang('success add task list', $task->getTitle())); } if (array_var($task_data, 'inputtype') != 'taskview') { ajx_current("back"); } else { ajx_current("reload"); } } catch (Exception $e) { DB::rollback(); flash_error($e->getMessage()); ajx_current("empty"); } // try } // if }
/** * Update, delete and add new handins * * @access public * @param void * @return null */ function update_handins() { $this->setTemplate('add_handins'); $manager_class = array_var($_GET, 'manager'); $object_id = get_id('object_id'); $obj = get_object_by_manager_and_id($object_id, $manager_class); if (!$obj instanceof ProjectDataObject) { flash_error(lang('object dnx')); ajx_current("empty"); return; } // if if ($obj->canEdit()) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $new_handins = array_var($_POST, 'new_handins'); $update_handins = array_var($_POST, 'update_handins'); $delete_handins = array_var($_POST, 'delete_handins'); if (is_array(array_var($_POST, 'new_handins')) || is_array(array_var($_POST, 'update_handins')) || is_array(array_var($_POST, 'delete_handins'))) { try { DB::beginWork(); //add new handins foreach ($new_handins as $handin) { $handin = new ObjectHandin(); $handin->setFromAttributes($handin); $handin->setRelObjectId($object_id); $handin->setRelObjectManager($manager_class); $handin->save(); } foreach ($update_handins as $handin) { $handin = ObjectHandins::getHandin(array_var($handin, 'id')); $handin->setFromAttributes($handin); $handin->save(); } foreach ($delete_handins as $handin) { $handin = ObjectHandins::getHandin(array_var($handin, 'id')); $handin->delete(); } tpl_assign('handins', ObjectHandins::getAllHandinsByObject($obj)); ApplicationLogs::createLog($obj, $obj->getWorkspaces(), ApplicationLogs::ACTION_EDIT); DB::commit(); flash_success(lang('success add handins')); $this->redirectToReferer($obj->getObjectUrl()); } catch (Exception $e) { DB::rollback(); flash_error($e->getMessage()); ajx_current("empty"); } // } // if }