function add()
 {
     $pt = DB::escape(array_var($_GET, 'pt'));
     $t = DB::escape(array_var($_GET, 't'));
     $dep = ProjectTaskDependencies::findOne(array('conditions' => "`previous_task_id` = {$pt} AND `task_id` = {$t}"));
     if (!$dep instanceof ProjectTaskDependency) {
         try {
             DB::beginWork();
             $dep = new ProjectTaskDependency();
             $dep->setPreviousTaskId(array_var($_GET, 'pt'));
             $dep->setTaskId(array_var($_GET, 't'));
             $dep->save();
             DB::commit();
         } catch (Exception $e) {
             flash_error($e->getMessage());
             DB::rollback();
         }
     }
     flash_success(lang('success add task dependency'));
     $reload = array_var($_GET, 'reload', true);
     if ($reload) {
         ajx_current("reload");
     } else {
         ajx_current("empty");
     }
 }
Ejemplo n.º 2
0
 function repetitive_task_related_edit($task, $task_data)
 {
     $was_template = $task->getIsTemplate();
     $task->setFromAttributes($task_data);
     $task->setIsTemplate($was_template);
     // is_template value must not be changed from ui
     $totalMinutes = array_var($task_data, 'time_estimate_hours') * 60 + array_var($task_data, 'time_estimate_minutes');
     $task->setTimeEstimate($totalMinutes);
     if ($task->getParentId() > 0 && $task->hasChild($task->getParentId())) {
         flash_error(lang('task child of child error'));
         ajx_current("empty");
         return;
     }
     DB::beginWork();
     $task->save();
     $task->setObjectName(array_var($task_data, 'name'));
     $task->save();
     // dependencies
     if (config_option('use tasks dependencies')) {
         $previous_tasks = array_var($task_data, 'previous');
         if (is_array($previous_tasks)) {
             foreach ($previous_tasks as $ptask) {
                 if ($ptask == $task->getId()) {
                     continue;
                 }
                 $dep = ProjectTaskDependencies::findById(array('previous_task_id' => $ptask, 'task_id' => $task->getId()));
                 if (!$dep instanceof ProjectTaskDependency) {
                     $dep = new ProjectTaskDependency();
                     $dep->setPreviousTaskId($ptask);
                     $dep->setTaskId($task->getId());
                     $dep->save();
                 }
             }
             $saved_ptasks = ProjectTaskDependencies::findAll(array('conditions' => 'task_id = ' . $task->getId()));
             foreach ($saved_ptasks as $pdep) {
                 if (!in_array($pdep->getPreviousTaskId(), $previous_tasks)) {
                     $pdep->delete();
                 }
             }
         } else {
             ProjectTaskDependencies::delete('task_id = ' . $task->getId());
         }
     }
     // Add assigned user to the subscibers list
     if ($task->getAssignedToContactId() > 0 && Contacts::instance()->findById($task->getAssignedToContactId())) {
         if (!isset($_POST['subscribers'])) {
             $_POST['subscribers'] = array();
         }
         $_POST['subscribers']['user_' . $task->getAssignedToContactId()] = 'checked';
     }
     $object_controller = new ObjectController();
     $object_controller->add_to_members($task, array_var($task_data, 'members'));
     $object_controller->add_subscribers($task);
     $object_controller->link_to_new_object($task);
     $object_controller->add_custom_properties($task);
     $object_controller->add_reminders($task);
     // apply values to subtasks
     $assigned_to = $task->getAssignedToContactId();
     $subtasks = $task->getAllSubTasks();
     $milestone_id = $task->getMilestoneId();
     $apply_ms = array_var($task_data, 'apply_milestone_subtasks') == "checked";
     $apply_at = array_var($task_data, 'apply_assignee_subtasks', '') == "checked";
     foreach ($subtasks as $sub) {
         $modified = false;
         if ($apply_at || !($sub->getAssignedToContactId() > 0)) {
             $sub->setAssignedToContactId($assigned_to);
             $modified = true;
         }
         if ($apply_ms) {
             $sub->setMilestoneId($milestone_id);
             $modified = true;
         }
         if ($modified) {
             $sub->save();
         }
     }
     $task->resetIsRead();
     ApplicationLogs::createLog($task, ApplicationLogs::ACTION_EDIT);
     DB::commit();
 }
 function instantiate($arguments = null)
 {
     $selected_members = array();
     $id = array_var($arguments, 'id', get_id());
     $template = COTemplates::findById($id);
     if (!$template instanceof COTemplate) {
         flash_error(lang("template dnx"));
         ajx_current("empty");
         return;
     }
     $parameters = TemplateParameters::getParametersByTemplate($id);
     $parameterValues = array_var($arguments, 'parameterValues', array_var($_POST, 'parameterValues'));
     if (count($parameters) > 0 && !isset($parameterValues)) {
         ajx_current("back");
         return;
     }
     $instantiation_id = 0;
     if (count($parameters) > 0) {
         $instantiation_id = $this->save_instantiated_parameters($template, $parameters, $parameterValues);
     }
     if (array_var($_POST, 'members') || array_var($arguments, 'members')) {
         $selected_members = array_var($arguments, 'members', json_decode(array_var($_POST, 'members')));
     } else {
         $context = active_context();
         foreach ($context as $selection) {
             if ($selection instanceof Member) {
                 $selected_members[] = $selection->getId();
             }
         }
     }
     if (array_var($_POST, 'additional_member_ids')) {
         $add_mem_ids = explode(',', array_var($_POST, 'additional_member_ids'));
         if (is_array($add_mem_ids)) {
             foreach ($add_mem_ids as $add_mem_id) {
                 if (is_numeric($add_mem_id)) {
                     $selected_members[] = $add_mem_id;
                 }
             }
         }
     }
     $objects = $template->getObjects();
     $controller = new ObjectController();
     if (count($selected_members) > 0) {
         $selected_members_instances = Members::findAll(array('conditions' => 'id IN (' . implode($selected_members) . ')'));
     } else {
         $selected_members_instances = array();
     }
     DB::beginWork();
     $active_context = active_context();
     $copies = array();
     $copiesIds = array();
     $dependencies = array();
     Hook::fire("verify_objects_before_template_instantiation", $template, $objects);
     foreach ($objects as $object) {
         if (!$object instanceof ContentDataObject) {
             continue;
         }
         // copy object
         if ($object instanceof TemplateTask) {
             //dependencies
             $ptasks = ProjectTaskDependencies::getDependenciesForTemplateTask($object->getId());
             if (!empty($ptasks)) {
                 foreach ($ptasks as $d) {
                     $dependencies[] = $d;
                 }
             }
             $copy = $object->copyToProjectTask($instantiation_id);
             //if is subtask
             if ($copy->getParentId() > 0) {
                 foreach ($copies as $c) {
                     if ($c instanceof ProjectTask) {
                         if ($c->getFromTemplateObjectId() == $object->getParentId()) {
                             $copy->setParentId($c->getId());
                         }
                     }
                 }
             }
         } else {
             if ($object instanceof TemplateMilestone) {
                 $copy = $object->copyToProjectMilestone();
             } else {
                 $copy = $object->copy(false);
                 if ($copy->columnExists('from_template_id')) {
                     $copy->setColumnValue('from_template_id', $object->getId());
                 }
             }
         }
         if ($copy->columnExists('is_template')) {
             $copy->setColumnValue('is_template', false);
         }
         $copy->save();
         $copies[] = $copy;
         $copiesIds[$object->getId()] = $copy->getId();
         $ret = null;
         Hook::fire('after_template_object_instantiation', array('template' => $template, 'original' => $object, 'copy' => $copy), $ret);
         /* Set instantiated object members:
          * 		if no member is active then the instantiated object is put in the same members as the original
          * 		if any members are selected then the instantiated object will be put in those members  
          */
         $template_object_members = $object->getMembers();
         $object_members = array();
         if (count($selected_members) == 0) {
             //change members according to context
             foreach ($active_context as $selection) {
                 if ($selection instanceof Member) {
                     // member selected
                     foreach ($template_object_members as $i => $object_member) {
                         if ($object_member instanceof Member && $object_member->getObjectTypeId() == $selection->getObjectTypeId()) {
                             unset($template_object_members[$i]);
                         }
                     }
                     $object_members[] = $selection->getId();
                 }
             }
         } else {
             $object_members = $selected_members;
         }
         foreach ($template_object_members as $object_member) {
             $object_members[] = $object_member->getId();
         }
         $controller->add_to_members($copy, $object_members);
         // set property values as defined in template
         instantiate_template_task_parameters($object, $copy, $parameterValues);
         // subscribe assigned to
         if ($copy instanceof ProjectTask) {
             foreach ($copy->getOpenSubTasks(false) as $m_task) {
                 if ($m_task->getAssignedTo() instanceof Contact) {
                     $m_task->subscribeUser($m_task->getAssignedTo());
                 }
             }
             if ($copy->getAssignedTo() instanceof Contact) {
                 $copy->subscribeUser($copy->getAssignedTo());
             }
         } else {
             if ($copy instanceof ProjectMilestone) {
                 foreach ($copy->getTasks(false) as $m_task) {
                     if ($m_task->getAssignedTo() instanceof Contact) {
                         $m_task->subscribeUser($m_task->getAssignedTo());
                     }
                 }
             }
         }
     }
     foreach ($copies as $c) {
         if ($c instanceof ProjectTask) {
             if ($c->getMilestoneId() > 0) {
                 // find milestone in copies
                 foreach ($copies as $m) {
                     if ($m instanceof ProjectMilestone && $m->getFromTemplateObjectId() == $c->getMilestoneId()) {
                         $c->setMilestoneId($m->getId());
                         $c->save();
                         break;
                     }
                 }
             }
         }
     }
     //copy dependencies
     foreach ($dependencies as $dependencie) {
         if ($dependencie instanceof ProjectTaskDependency) {
             $dep = new ProjectTaskDependency();
             $dep->setPreviousTaskId($copiesIds[$dependencie->getPreviousTaskId()]);
             $dep->setTaskId($copiesIds[$dependencie->getTaskId()]);
             $dep->save();
         }
     }
     DB::commit();
     foreach ($copies as $c) {
         if ($c instanceof ProjectTask) {
             ApplicationLogs::createLog($c, ApplicationLogs::ACTION_ADD);
         }
     }
     if (is_array($parameters) && count($parameters) > 0) {
         ajx_current("back");
     } else {
         ajx_current("back");
     }
 }