Example #1
0
File: khez.php Project: Karpec/gizd
 function getTimeX(&$ammount, &$type, $time)
 {
     return strtotime($ammount . ' ' . getTimeValue($type), $time);
 }
 /**
  * Edit task
  *
  * @access public
  * @param void
  * @return null
  */
 function edit_task()
 {
     $isTemplateTask = false;
     if (array_var($_REQUEST, 'template_task') == true) {
         $isTemplateTask = true;
     }
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add_task');
     if (array_var($_REQUEST, "template_task")) {
         $task = TemplateTasks::findById(array_var($_REQUEST, "template_task_id", get_id()));
         $this->setTemplate(get_template_path('add_template_task', 'template_task'));
         if (array_var($_REQUEST, 'template_id')) {
             $template_id = array_var($_REQUEST, 'template_id');
         } else {
             $template_id = $task->getTemplateId();
         }
         tpl_assign('additional_tt_params', array_var($_REQUEST, 'additional_tt_params'));
         tpl_assign('template_id', $template_id);
         if (!$task instanceof TemplateTask) {
             flash_error(lang('task list dnx'));
             ajx_current("empty");
             return;
         }
         // if
     } else {
         $task = ProjectTasks::findById(get_id());
         if (!$task instanceof ProjectTask) {
             flash_error(lang('task list dnx'));
             ajx_current("empty");
             return;
         }
         // if
         if (!$task->canEdit(logged_user())) {
             flash_error(lang('no access permissions'));
             ajx_current("empty");
             return;
         }
         // if
     }
     if (array_var($_GET, 'replace')) {
         ajx_replace(true);
     }
     $task_data = array_var($_POST, 'task');
     $time_estimate = array_var($_POST, 'hours', 0) * 60 + array_var($_POST, 'minutes', 0);
     if ($time_estimate > 0) {
         $estimatedTime = $time_estimate;
     } else {
         $estimatedTime = $task->getTimeEstimate();
     }
     if (!is_array($task_data)) {
         // set layout for modal form
         if (array_var($_REQUEST, 'modal')) {
             $this->setLayout("json");
             tpl_assign('modal', true);
         }
         $this->getRepeatOptions($task, $occ, $rsel1, $rsel2, $rsel3, $rnum, $rend, $rjump);
         $dd = $task->getDueDate() instanceof DateTimeValue ? $task->getDueDate() : null;
         if ($dd instanceof DateTimeValue && $task->getUseDueTime()) {
             $dd->advance(logged_user()->getTimezone() * 3600);
         }
         $sd = $task->getStartDate() instanceof DateTimeValue ? $task->getStartDate() : null;
         if ($sd instanceof DateTimeValue && $task->getUseStartTime()) {
             $sd->advance(logged_user()->getTimezone() * 3600);
         }
         $post_dd = null;
         if (array_var($_POST, 'task_due_date')) {
             $post_dd = getDateValue(array_var($_POST, 'task_due_date'));
             if ($post_dd instanceof DateTimeValue) {
                 $duetime = getTimeValue(array_var($_POST, 'task_due_time'));
                 if (is_array($duetime)) {
                     $post_dd->setHour(array_var($duetime, 'hours'));
                     $post_dd->setMinute(array_var($duetime, 'mins'));
                     $post_dd->advance(logged_user()->getTimezone() * 3600);
                 }
             }
         }
         $post_st = null;
         if (array_var($_POST, 'task_start_date')) {
             $post_st = getDateValue(array_var($_POST, 'task_start_date'));
             if ($post_st instanceof DateTimeValue) {
                 $starttime = getTimeValue(array_var($_POST, 'task_start_time'));
                 if (is_array($starttime)) {
                     $post_st->setHour(array_var($starttime, 'hours'));
                     $post_st->setMinute(array_var($starttime, 'mins'));
                     $post_st->advance(logged_user()->getTimezone() * 3600);
                 }
             }
         }
         if (config_option("wysiwyg_tasks")) {
             $text_post = preg_replace("/[\n|\r|\n\r]/", '', array_var($_POST, 'text', $task->getText()));
         } else {
             $text_post = array_var($_POST, 'text', $task->getText());
         }
         $task_data = array('name' => array_var($_POST, 'name', $task->getObjectName()), 'text' => $text_post, 'milestone_id' => array_var($_POST, 'milestone_id', $task->getMilestoneId()), 'due_date' => getDateValue($post_dd, $dd), 'start_date' => getDateValue($post_st, $sd), 'parent_id' => $task->getParentId(), 'assigned_to_contact_id' => array_var($_POST, 'assigned_to_contact_id', $task->getAssignedToContactId()), 'selected_members_ids' => json_decode(array_var($_POST, 'members', null)), 'priority' => array_var($_POST, 'priority', $task->getPriority()), 'time_estimate' => $estimatedTime, 'percent_completed' => $task->getPercentCompleted(), 'forever' => $task->getRepeatForever(), 'rend' => $rend, 'rnum' => $rnum, 'rjump' => $rjump, 'rsel1' => $rsel1, 'rsel2' => $rsel2, 'rsel3' => $rsel3, 'occ' => $occ, 'repeat_by' => $task->getRepeatBy(), 'object_subtype' => array_var($_POST, "object_subtype", $task->getObjectSubtype() != 0 ? $task->getObjectSubtype() : config_option('default task co type')), 'type_content' => $task->getTypeContent(), 'multi_assignment' => $task->getColumnValue('multi_assignment', 0), 'send_notification_subscribers' => user_config_option("can notify subscribers"));
         // array
         //control dates of parent and subtasks
         $task_data['type_control'] = "";
         $parent_data = $task->getParent();
         if ($parent_data) {
             $task_data['type_control'] = "child";
             $task_data['control_title'] = $parent_data->getObjectName();
             $task_data['control_due_date'] = $parent_data->getDueDate() instanceof DateTimeValue ? $parent_data->getDueDate()->getTimestamp() + logged_user()->getTimezone() * 3600 : null;
             $task_data['control_start_date'] = $parent_data->getStartDate() instanceof DateTimeValue ? $parent_data->getStartDate()->getTimestamp() + logged_user()->getTimezone() * 3600 : null;
         }
         $subtask_data = $task->getAllSubTasks();
         if ($subtask_data) {
             $task_data['type_control'] = "father";
             $task_data['control_title'] = $task->getObjectName();
             $task_data['control_due_date'] = getDateValue($post_dd, $dd) instanceof DateTimeValue ? getDateValue($post_dd, $dd)->getTimestamp() : null;
             $task_data['control_start_date'] = getDateValue($post_st, $sd) instanceof DateTimeValue ? getDateValue($post_st, $sd)->getTimestamp() : null;
         }
     }
     // if
     //I find all those related to the task to find out if the original
     $task_related = ProjectTasks::findByRelated($task->getObjectId());
     if (!$task_related) {
         //is not the original as the original look plus other related
         if ($task->getOriginalTaskId() != "0") {
             $task_related = ProjectTasks::findByTaskAndRelated($task->getObjectId(), $task->getOriginalTaskId());
         }
     }
     if ($task_related) {
         $pending_id = 0;
         foreach ($task_related as $t_rel) {
             if ($task->getStartDate() <= $t_rel->getStartDate() && $task->getDueDate() <= $t_rel->getDueDate() && !$t_rel->isCompleted()) {
                 $pending_id = $t_rel->getId();
                 break;
             }
         }
         tpl_assign('pending_task_id', $pending_id);
         tpl_assign('task_related', true);
     } else {
         tpl_assign('pending_task_id', 0);
         tpl_assign('task_related', false);
     }
     tpl_assign('task', $task);
     tpl_assign('task_data', $task_data);
     if (is_array(array_var($_POST, 'task'))) {
         foreach ($task_data as $k => &$v) {
             $v = remove_scripts($v);
         }
         $send_edit = false;
         if ($task->getAssignedToContactId() == array_var($task_data, 'assigned_to_contact_id')) {
             $send_edit = true;
         }
         $old_owner = $task->getAssignedTo();
         if (array_var($task_data, 'parent_id') == $task->getId()) {
             flash_error(lang("task own parent error"));
             ajx_current("empty");
             return;
         }
         try {
             try {
                 $task_data['due_date'] = getDateValue(array_var($_POST, 'task_due_date'));
                 $task_data['start_date'] = getDateValue(array_var($_POST, 'task_start_date'));
             } catch (Exception $e) {
                 throw new Exception(lang('date format error', date_format_tip(user_config_option('date_format'))));
             }
             if ($task_data['due_date'] instanceof DateTimeValue) {
                 $duetime = getTimeValue(array_var($_POST, 'task_due_time'));
                 if (is_array($duetime)) {
                     $task_data['due_date']->setHour(array_var($duetime, 'hours'));
                     $task_data['due_date']->setMinute(array_var($duetime, 'mins'));
                     $task_data['due_date']->advance(logged_user()->getTimezone() * -3600);
                 }
                 $task_data['use_due_time'] = is_array($duetime);
             }
             if ($task_data['start_date'] instanceof DateTimeValue) {
                 $starttime = getTimeValue(array_var($_POST, 'task_start_time'));
                 if (is_array($starttime)) {
                     $task_data['start_date']->setHour(array_var($starttime, 'hours'));
                     $task_data['start_date']->setMinute(array_var($starttime, 'mins'));
                     $task_data['start_date']->advance(logged_user()->getTimezone() * -3600);
                 }
                 $task_data['use_start_time'] = is_array($starttime);
             }
             //control date subtask whit parent
             if (array_var($_POST, 'control_dates') == "child") {
                 $parent = $task->getParent();
                 if ($parent->getStartDate() instanceof DateTimeValue && $task_data['start_date'] instanceof DateTimeValue) {
                     if ($task_data['start_date']->getTimestamp() < $parent->getStartDate()->getTimestamp()) {
                         $parent->setStartDate($task_data['start_date']);
                         $parent->setUseStartTime($task_data['use_start_time']);
                     }
                 } else {
                     $parent->setStartDate($task_data['start_date']);
                     $parent->setUseStartTime(array_var($task_data, 'use_start_time', 0));
                 }
                 if ($parent->getDueDate() instanceof DateTimeValue && $task_data['due_date'] instanceof DateTimeValue) {
                     if ($task_data['due_date']->getTimestamp() > $parent->getDueDate()->getTimestamp()) {
                         $parent->setDueDate($task_data['due_date']);
                         $parent->setUseDueTime($task_data['use_due_time']);
                     }
                 } else {
                     $parent->setDueDate($task_data['due_date']);
                     $parent->setUseDueTime(array_var($task_data, 'use_due_time', 0));
                 }
                 // calculate and set estimated time
                 $totalMinutes = array_var($task_data, 'time_estimate_hours') * 60 + array_var($task_data, 'time_estimate_minutes');
                 $parent->setTimeEstimate($totalMinutes);
                 $parent->save();
             }
             $err_msg = $this->setRepeatOptions($task_data);
             if ($err_msg) {
                 throw new Exception($err_msg);
             }
             if (!isset($task_data['parent_id'])) {
                 $task_data['parent_id'] = 0;
             }
             $member_ids = json_decode(array_var($_POST, 'members'));
             // keep old dates to check for subtasks
             $old_start_date = $task->getStartDate();
             $old_due_date = $task->getDueDate();
             if (config_option("wysiwyg_tasks")) {
                 $task_data['type_content'] = "html";
                 $task_data['text'] = str_replace(array("\r", "\n", "\r\n"), array('', '', ''), array_var($task_data, 'text'));
             } else {
                 $task_data['type_content'] = "text";
             }
             $task->setFromAttributes($task_data);
             $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;
             }
             if (isset($task_data['percent_completed']) && $task_data['percent_completed'] >= 0 && $task_data['percent_completed'] <= 100) {
                 $task->setPercentCompleted($task_data['percent_completed']);
             }
             DB::beginWork();
             $task->save();
             if (!isset($task_data['percent_completed'])) {
                 $task->calculatePercentComplete();
             }
             // 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()] = '1';
             }
             $object_controller = new ObjectController();
             if ($isTemplateTask) {
                 $object_controller->add_to_members($task, $member_ids, null, false);
             } else {
                 $object_controller->add_to_members($task, $member_ids);
             }
             $is_template = $task instanceof TemplateTask;
             $object_controller->add_subscribers($task, null, !$is_template);
             $object_controller->link_to_new_object($task);
             $object_controller->add_custom_properties($task);
             if (!$task->isCompleted()) {
                 //to make sure the task it is not completed yet, and that it has subscribed people
                 $old_reminders = ObjectReminders::getByObject($task);
                 $object_controller->add_reminders($task);
                 //adding the new reminders, if any
                 $object_controller->update_reminders($task, $old_reminders);
                 //updating the old ones
                 if (logged_user() instanceof Contact && (!is_array($old_reminders) || count($old_reminders) == 0) && (user_config_option("add_task_autoreminder") && logged_user()->getId() != $task->getAssignedToContactId() || user_config_option("add_self_task_autoreminder") && logged_user()->getId() == $task->getAssignedToContactId())) {
                     //if there is no asignee, but it still has subscribers
                     $reminder = new ObjectReminder();
                     $def = explode(",", user_config_option("reminders_tasks"));
                     $minutes = $def[2] * $def[1];
                     $reminder->setMinutesBefore($minutes);
                     $reminder->setType($def[0]);
                     $reminder->setContext("due_date");
                     $reminder->setObject($task);
                     $reminder->setUserId(0);
                     $date = $task->getDueDate();
                     if ($date instanceof DateTimeValue) {
                         $rdate = new DateTimeValue($date->getTimestamp() - $minutes * 60);
                         $reminder->setDate($rdate);
                     }
                     $reminder->save();
                 }
             }
             if (!is_array($member_ids) || count($member_ids) == 0) {
                 $member_ids = array(0);
             }
             $members = Members::findAll(array('conditions' => "id IN (" . implode(',', $member_ids) . ")"));
             $task->apply_members_to_subtasks($members, true);
             // apply values to subtasks
             $assigned_to = $task->getAssignedToContactId();
             $subtasks = $task->getAllSubTasks();
             $milestone_id = $task->getMilestoneId();
             $apply_ms = array_var($task_data, 'apply_milestone_subtasks');
             $apply_at = array_var($task_data, 'apply_assignee_subtasks', '');
             foreach ($subtasks as $sub) {
                 $modified = false;
                 //if ($apply_at || !($sub->getAssignedToContactId() > 0)) {
                 if ($apply_at) {
                     $sub->setAssignedToContactId($assigned_to);
                     $modified = true;
                 }
                 if ($apply_ms) {
                     $sub->setMilestoneId($milestone_id);
                     $modified = true;
                 }
                 if ($modified) {
                     $sub->save();
                 }
                 //control date parent whit subtask
                 if ($_POST['control_dates'] == "father") {
                     if ($sub->getStartDate() instanceof DateTimeValue) {
                         if ($task->getStartDate() instanceof DateTimeValue) {
                             if ($task->getStartDate()->getTimestamp() > $sub->getStartDate()->getTimestamp()) {
                                 $sub->setStartDate($task->getStartDate());
                             }
                         }
                     } else {
                         if ($task->getStartDate() instanceof DateTimeValue) {
                             $sub->setStartDate($task->getStartDate());
                         }
                     }
                     $sub->setUseStartTime($task->getUseStartTime());
                     if ($sub->getDueDate() instanceof DateTimeValue) {
                         if ($task->getDueDate() instanceof DateTimeValue) {
                             if ($task->getDueDate()->getTimestamp() < $sub->getDueDate()->getTimestamp()) {
                                 $sub->setDueDate($task->getDueDate());
                             }
                         }
                     } else {
                         if ($task->getDueDate() instanceof DateTimeValue) {
                             $sub->setDueDate($task->getDueDate());
                         }
                     }
                     $sub->setUseDueTime($task->getUseDueTime());
                     $sub->save();
                 }
             }
             $task->resetIsRead();
             $log_info = '';
             if ($send_edit == true) {
                 $log_info = $task->getAssignedToContactId();
             } else {
                 if ($send_edit == false) {
                     $task->setAssignedBy(logged_user());
                     $task->save();
                 }
             }
             if (config_option('repeating_task') == 1) {
                 $opt_rep_day['saturday'] = false;
                 $opt_rep_day['sunday'] = false;
                 if (array_var($task_data, 'repeat_saturdays', false)) {
                     $opt_rep_day['saturday'] = true;
                 }
                 if (array_var($task_data, 'repeat_sundays', false)) {
                     $opt_rep_day['sunday'] = true;
                 }
                 $this->repetitive_task($task, $opt_rep_day);
             }
             if (isset($_POST['type_related'])) {
                 if ($_POST['type_related'] == "all" || $_POST['type_related'] == "news") {
                     $task_data['members'] = json_decode(array_var($_POST, 'members'));
                     unset($task_data['due_date']);
                     unset($task_data['use_due_time']);
                     unset($task_data['start_date']);
                     unset($task_data['use_start_time']);
                     $this->repetitive_tasks_related($task, "edit", $_POST['type_related'], $task_data);
                 }
             }
             if (config_option('multi_assignment') && Plugins::instance()->isActivePlugin('crpm')) {
                 if (array_var($task_data, 'multi_assignment_aplly_change') == 'subtask') {
                     $null = null;
                     Hook::fire('edit_subtasks', $task, $null);
                 }
             }
             //for calculate member status we save de task again after the object have the members
             $task->save();
             // save subtasks added in 'subtasks' tab
             $sub_tasks_to_log = $this->saveSubtasks($task, array_var($task_data, 'subtasks'), $member_ids);
             DB::commit();
             foreach ($sub_tasks_to_log['add'] as $st_to_log) {
                 ApplicationLogs::createLog($st_to_log, ApplicationLogs::ACTION_ADD);
             }
             foreach ($sub_tasks_to_log['edit'] as $st_to_log) {
                 ApplicationLogs::createLog($st_to_log, ApplicationLogs::ACTION_EDIT);
             }
             foreach ($sub_tasks_to_log['trash'] as $st_to_log) {
                 ApplicationLogs::createLog($st_to_log, ApplicationLogs::ACTION_TRASH);
             }
             //Send Template task to view
             if ($task instanceof TemplateTask) {
                 $objectId = $task->getObjectId();
                 $id = $task->getId();
                 $objectTypeName = $task->getObjectTypeName();
                 $objectName = $task->getObjectName();
                 $manager = get_class($task->manager());
                 $milestoneId = $task instanceof TemplateTask ? $task->getMilestoneId() : '0';
                 $subTasks = $task->getSubTasks();
                 $parentId = $task->getParentId();
                 $ico = "ico-task";
                 $action = "edit";
                 $object = TemplateController::prepareObject($objectId, $id, $objectName, $objectTypeName, $manager, $action, $milestoneId, $subTasks, $parentId, $ico);
                 $template_task_data = array('object' => $object);
                 if (array_var($_REQUEST, 'additional_tt_params')) {
                     $additional_tt_params = json_decode(str_replace("'", '"', array_var($_REQUEST, 'additional_tt_params')), true);
                     foreach ($additional_tt_params as $k => $v) {
                         $template_task_data[$k] = $v;
                     }
                 }
                 if (!array_var($_REQUEST, 'modal')) {
                     evt_add("template object added", $template_task_data);
                 }
             }
             try {
                 // notify asignee
                 if (array_var($task_data, 'send_notification') && $task->getAssignedToContactId() != $task->getAssignedById()) {
                     $new_owner = $task->getAssignedTo();
                     if ($new_owner instanceof Contact) {
                         Notifier::taskAssigned($task);
                     }
                     // if
                 }
                 // if
                 if (array_var($task_data, 'send_notification')) {
                     foreach ($sub_tasks_to_log['assigned'] as $st_to_log) {
                         Notifier::taskAssigned($st_to_log);
                     }
                 }
             } catch (Exception $e) {
             }
             // try
             //notify subscribers
             $isSilent = true;
             if (array_var($task_data, 'send_notification_subscribers')) {
                 $isSilent = false;
             }
             ApplicationLogs::createLog($task, ApplicationLogs::ACTION_EDIT, false, false, true, $log_info);
             //flash_success(lang('success edit task list', $task->getObjectName()));
             if (array_var($_REQUEST, 'modal')) {
                 if (array_var($_REQUEST, 'reload')) {
                     evt_add("reload current panel");
                 } else {
                     ajx_current("empty");
                     $this->setLayout("json");
                     $this->setTemplate(get_template_path("empty"));
                     // reload task info because plugins may have updated some task info (for example: name prefix)
                     if ($is_template) {
                         $task = TemplateTasks::findById($task->getId());
                     } else {
                         $task = ProjectTasks::findById($task->getId());
                     }
                     $params = array('msg' => lang('success edit task list', $task->getObjectName()), 'task' => $task->getArrayInfo(), 'reload' => array_var($_REQUEST, 'reload'));
                     if ($task instanceof TemplateTask) {
                         //$params['msg'] = lang('success edit template', $task->getObjectName());
                         $params['object'] = $template_task_data['object'];
                     }
                     //print_modal_json_response($params, true, array_var($_REQUEST, 'use_ajx'));
                     ajx_extra_data($params);
                 }
             } else {
                 ajx_current("back");
             }
             // if has subtasks and dates were changed, ask the user if the subtasks dates should also be changed
             if ($task instanceof ProjectTask && $task->countOpenSubTasks() > 0) {
                 // check if there was any due date changes
                 $dd_advance_info = null;
                 if ($task->getDueDate() instanceof DateTimeValue && $old_due_date instanceof DateTimeValue && $old_due_date->getTimestamp() != $task->getDueDate()->getTimestamp()) {
                     $dd_to_advance_ts = $task->getDueDate()->getTimestamp() - $old_due_date->getTimestamp();
                     if ($dd_to_advance_ts != 0) {
                         $dd_advance_info = get_time_info($dd_to_advance_ts);
                     }
                 }
                 // check if there was any start date changes
                 $sd_advance_info = null;
                 if ($task->getStartDate() instanceof DateTimeValue && $old_start_date instanceof DateTimeValue && $old_start_date->getTimestamp() != $task->getStartDate()->getTimestamp()) {
                     $sd_to_advance_ts = $task->getStartDate()->getTimestamp() - $old_start_date->getTimestamp();
                     if ($sd_to_advance_ts != 0) {
                         $sd_advance_info = get_time_info($sd_to_advance_ts);
                     }
                 }
                 if ($dd_advance_info != null || $sd_advance_info != null) {
                     evt_add('ask to change subtasks dates', array('dd_diff' => $dd_advance_info, 'sd_diff' => $sd_advance_info, 'task_id' => $task->getId()));
                 }
             }
         } catch (Exception $e) {
             DB::rollback();
             if (array_var($_REQUEST, 'modal')) {
                 $this->setLayout("json");
                 $this->setTemplate(get_template_path("empty"));
                 print_modal_json_response(array('errorCode' => 1, 'errorMessage' => $e->getMessage(), 'showMessage' => 1), true, array_var($_REQUEST, 'use_ajx'));
             } else {
                 flash_error($e->getMessage());
             }
             ajx_current("empty");
         }
         // try
     }
     // if
 }
 /**
  * Edit task
  *
  * @access public
  * @param void
  * @return null
  */
 function edit_task()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add_task');
     $task = ProjectTasks::findById(get_id());
     if (!$task instanceof ProjectTask) {
         flash_error(lang('task list dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$task->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     if (array_var($_GET, 'replace')) {
         ajx_replace(true);
     }
     $task_data = array_var($_POST, 'task');
     $time_estimate = array_var($_POST, 'hours', 0) * 60 + array_var($_POST, 'minutes', 0);
     if ($time_estimate > 0) {
         $estimatedTime = $time_estimate;
     } else {
         $estimatedTime = $task->getTimeEstimate();
     }
     if (!is_array($task_data)) {
         $this->getRepeatOptions($task, $occ, $rsel1, $rsel2, $rsel3, $rnum, $rend, $rjump);
         $dd = $task->getDueDate() instanceof DateTimeValue ? $task->getDueDate()->advance(logged_user()->getTimezone() * 3600, false) : null;
         $sd = $task->getStartDate() instanceof DateTimeValue ? $task->getStartDate()->advance(logged_user()->getTimezone() * 3600, false) : null;
         $post_dd = null;
         if (array_var($_POST, 'task_due_date')) {
             $post_dd = getDateValue(array_var($_POST, 'task_due_date'));
             if ($post_dd instanceof DateTimeValue) {
                 $duetime = getTimeValue(array_var($_POST, 'task_due_time'));
                 if (is_array($duetime)) {
                     $post_dd->setHour(array_var($duetime, 'hours'));
                     $post_dd->setMinute(array_var($duetime, 'mins'));
                     $post_dd->advance(logged_user()->getTimezone() * 3600, false);
                 }
             }
         }
         $post_st = null;
         if (array_var($_POST, 'task_start_date')) {
             $post_st = getDateValue(array_var($_POST, 'task_start_date'));
             if ($post_st instanceof DateTimeValue) {
                 $starttime = getTimeValue(array_var($_POST, 'task_start_time'));
                 if (is_array($starttime)) {
                     $post_st->setHour(array_var($starttime, 'hours'));
                     $post_st->setMinute(array_var($starttime, 'mins'));
                     $post_st->advance(logged_user()->getTimezone() * 3600, false);
                 }
             }
         }
         $task_data = array('name' => array_var($_POST, 'name', $task->getObjectName()), 'text' => array_var($_POST, 'text', $task->getText()), 'milestone_id' => array_var($_POST, 'milestone_id', $task->getMilestoneId()), 'due_date' => getDateValue($post_dd, $dd), 'start_date' => getDateValue($post_st, $sd), 'parent_id' => $task->getParentId(), 'assigned_to_contact_id' => array_var($_POST, 'assigned_to_contact_id', $task->getAssignedToContactId()), 'priority' => array_var($_POST, 'priority', $task->getPriority()), 'send_notification' => array_var($_POST, 'notify') == 'true', 'time_estimate' => $estimatedTime, 'percent_completed' => $task->getPercentCompleted(), 'forever' => $task->getRepeatForever(), 'rend' => $rend, 'rnum' => $rnum, 'rjump' => $rjump, 'rsel1' => $rsel1, 'rsel2' => $rsel2, 'rsel3' => $rsel3, 'occ' => $occ, 'repeat_by' => $task->getRepeatBy(), 'object_subtype' => array_var($_POST, "object_subtype", $task->getObjectSubtype() != 0 ? $task->getObjectSubtype() : config_option('default task co type')), 'type_content' => $task->getTypeContent(), 'multi_assignment' => $task->getColumnValue('multi_assignment', 0));
         // array
     }
     // if
     //I find all those related to the task to find out if the original
     $task_related = ProjectTasks::findByRelated($task->getObjectId());
     if (!$task_related) {
         //is not the original as the original look plus other related
         if ($task->getOriginalTaskId() != "0") {
             $task_related = ProjectTasks::findByTaskAndRelated($task->getObjectId(), $task->getOriginalTaskId());
         }
     }
     if ($task_related) {
         $pending_id = 0;
         foreach ($task_related as $t_rel) {
             if ($task->getStartDate() <= $t_rel->getStartDate() && $task->getDueDate() <= $t_rel->getDueDate() && !$t_rel->isCompleted()) {
                 $pending_id = $t_rel->getId();
                 break;
             }
         }
         tpl_assign('pending_task_id', $pending_id);
         tpl_assign('task_related', true);
     } else {
         tpl_assign('pending_task_id', 0);
         tpl_assign('task_related', false);
     }
     tpl_assign('task', $task);
     tpl_assign('task_data', $task_data);
     if (is_array(array_var($_POST, 'task'))) {
         $send_edit = false;
         if ($task->getAssignedToContactId() == array_var($task_data, 'assigned_to_contact_id')) {
             $send_edit = true;
         }
         $old_owner = $task->getAssignedTo();
         if (array_var($task_data, 'parent_id') == $task->getId()) {
             flash_error(lang("task own parent error"));
             ajx_current("empty");
             return;
         }
         $task_data['due_date'] = getDateValue(array_var($_POST, 'task_due_date'));
         $task_data['start_date'] = getDateValue(array_var($_POST, 'task_start_date'));
         if ($task_data['due_date'] instanceof DateTimeValue) {
             $duetime = getTimeValue(array_var($_POST, 'task_due_time'));
             if (is_array($duetime)) {
                 $task_data['due_date']->setHour(array_var($duetime, 'hours'));
                 $task_data['due_date']->setMinute(array_var($duetime, 'mins'));
             }
             $task_data['due_date']->advance(logged_user()->getTimezone() * -3600);
             $task_data['use_due_time'] = is_array($duetime);
         }
         if ($task_data['start_date'] instanceof DateTimeValue) {
             $starttime = getTimeValue(array_var($_POST, 'task_start_time'));
             if (is_array($starttime)) {
                 $task_data['start_date']->setHour(array_var($starttime, 'hours'));
                 $task_data['start_date']->setMinute(array_var($starttime, 'mins'));
             }
             $task_data['start_date']->advance(logged_user()->getTimezone() * -3600);
             $task_data['use_start_time'] = is_array($starttime);
         }
         try {
             $err_msg = $this->setRepeatOptions($task_data);
             if ($err_msg) {
                 flash_error($err_msg);
                 ajx_current("empty");
                 return;
             }
             if (!isset($task_data['parent_id'])) {
                 $task_data['parent_id'] = 0;
             }
             $member_ids = json_decode(array_var($_POST, 'members'));
             $was_template = $task->getIsTemplate();
             if (config_option("wysiwyg_tasks")) {
                 $task_data['type_content'] = "html";
                 $task_data['text'] = preg_replace("/[\n|\r|\n\r]/", '', array_var($task_data, 'text'));
             } else {
                 $task_data['type_content'] = "text";
             }
             $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();
             $this->calculate_time_estimate($task, array_var($task_data, "percent_completed"));
             // 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, $member_ids);
             $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)) {
                 if ($apply_at) {
                     $sub->setAssignedToContactId($assigned_to);
                     $modified = true;
                 }
                 if ($apply_ms) {
                     $sub->setMilestoneId($milestone_id);
                     $modified = true;
                 }
                 if ($modified) {
                     $sub->save();
                 }
             }
             $task->resetIsRead();
             $log_info = '';
             if ($send_edit == true) {
                 $log_info = $task->getAssignedToContactId();
             } else {
                 if ($send_edit == false) {
                     $task->setAssignedBy(logged_user());
                     $task->save();
                 }
             }
             ApplicationLogs::createLog($task, ApplicationLogs::ACTION_EDIT, false, false, true, $log_info);
             if (config_option('repeating_task') == 1) {
                 $opt_rep_day['saturday'] = false;
                 $opt_rep_day['sunday'] = false;
                 if (array_var($task_data, 'repeat_saturdays', false)) {
                     $opt_rep_day['saturday'] = true;
                 }
                 if (array_var($task_data, 'repeat_sundays', false)) {
                     $opt_rep_day['sunday'] = true;
                 }
                 $this->repetitive_task($task, $opt_rep_day);
             }
             if (isset($_POST['type_related'])) {
                 if ($_POST['type_related'] == "all" || $_POST['type_related'] == "news") {
                     $task_data['members'] = json_decode(array_var($_POST, 'members'));
                     unset($task_data['due_date']);
                     unset($task_data['use_due_time']);
                     unset($task_data['start_date']);
                     unset($task_data['use_start_time']);
                     $this->repetitive_tasks_related($task, "edit", $_POST['type_related'], $task_data);
                 }
             }
             if (config_option('multi_assignment') && Plugins::instance()->isActivePlugin('crpm')) {
                 if (array_var($task_data, 'multi_assignment_aplly_change') == 'subtask') {
                     $null = null;
                     Hook::fire('edit_subtasks', $task, $null);
                 }
             }
             DB::commit();
             try {
                 if (array_var($task_data, 'send_notification') == 'checked' && $send_edit == false) {
                     $new_owner = $task->getAssignedTo();
                     if ($new_owner instanceof Contact) {
                         Notifier::taskAssigned($task);
                     }
                     // if
                 }
                 // if
             } catch (Exception $e) {
             }
             // try
             flash_success(lang('success edit task list', $task->getObjectName()));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
         // try
     }
     // if
 }
Example #4
0
function instantiate_template_task_parameters(TemplateTask $object, ProjectTask $copy, $parameterValues = array())
{
    $objProp = TemplateObjectProperties::getPropertiesByTemplateObject($object->getTemplateId(), $object->getId());
    $manager = $copy->manager();
    foreach ($objProp as $property) {
        $propName = $property->getProperty();
        $value = $property->getValue();
        if ($manager->getColumnType($propName) == DATA_TYPE_STRING || $manager->getColumnType($propName) == DATA_TYPE_INTEGER) {
            if (is_array($parameterValues)) {
                $is_present = false;
                foreach ($parameterValues as $param => $val) {
                    if (strpos($value, '{' . $param . '}') !== FALSE) {
                        $value = str_replace('{' . $param . '}', $val, $value);
                        $is_present = true;
                    }
                }
                // if parameter not present replace the parameter code with empty string
                if (!$is_present) {
                    $value = preg_replace('/[{].*[}]/U', '', $value);
                }
            }
        } else {
            if ($manager->getColumnType($propName) == DATA_TYPE_DATE || $manager->getColumnType($propName) == DATA_TYPE_DATETIME) {
                $operator = '+';
                if (strpos($value, '+') === false) {
                    $operator = '-';
                }
                $opPos = strpos($value, $operator);
                if ($opPos !== false) {
                    // Is parametric
                    $dateParam = substr($value, 1, strpos($value, '}') - 1);
                    if ($dateParam == 'task_creation') {
                        $date = DateTimeValueLib::now();
                    } else {
                        $date = getDateValue($parameterValues[$dateParam]);
                        if (!$date instanceof DateTimeValue) {
                            $date = DateTimeValueLib::now();
                        }
                        if ($copy instanceof ProjectTask && config_option('use_time_in_task_dates') && $propName == "due_date") {
                            $copy->setUseDueTime(1);
                            $hour_min = getTimeValue(user_config_option('work_day_end_time'));
                            $hour_min['hours'];
                            $hour_min['mins'];
                            $date->setHour($hour_min['hours']);
                            $date->setMinute($hour_min['mins']);
                            $date = $date->add('s', -logged_user()->getTimezone() * 3600);
                        }
                        if ($copy instanceof ProjectTask && config_option('use_time_in_task_dates') && $propName == "start_date") {
                            $copy->setUseStartTime(1);
                            $hour_min = getTimeValue(user_config_option('work_day_start_time'));
                            $hour_min['hours'];
                            $hour_min['mins'];
                            $date->setHour($hour_min['hours']);
                            $date->setMinute($hour_min['mins']);
                            $date = $date->add('s', -logged_user()->getTimezone() * 3600);
                        }
                    }
                    $dateUnit = substr($value, strlen($value) - 1);
                    // d, w or m (for days, weeks or months)
                    if ($dateUnit == 'm') {
                        $dateUnit = 'M';
                        // make month unit uppercase to call DateTimeValue::add with correct parameter
                    }
                    $dateNum = (int) substr($value, strpos($value, $operator), strlen($value) - 2);
                    //Hook::fire('template_param_date_calculation', array('op' => $operator, 'date' => $date, 'template_id' => $object->getTemplateId(), 'original' => $object, 'copy' => $copy), $dateNum);
                    $value = $date->add($dateUnit, $dateNum);
                } else {
                    $value = DateTimeValueLib::dateFromFormatAndString(user_config_option('date_format'), $value);
                }
            }
        }
        if ($value != '') {
            if (!$copy->setColumnValue($propName, $value)) {
                $copy->object->setColumnValue($propName, $value);
            }
            if ($propName == 'text' && $copy->getTypeContent() == 'text') {
                $copy->setText(html_to_text($copy->getText()));
            }
            $copy->save();
        }
    }
    // Ensure that assigned user is subscribed
    if ($copy instanceof ProjectTask && $copy->getAssignedTo() instanceof Contact) {
        $copy->subscribeUser($copy->getAssignedTo());
    }
    $ret = null;
    Hook::fire('after_template_object_param_instantiation', array('template_id' => $object->getTemplateId(), 'original' => $object, 'copy' => $copy, 'parameter_values' => $parameterValues), $ret);
}