function move_event() { if (logged_user()->isGuest()) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } $event = ProjectEvents::findById(get_id()); if(!$event->canEdit(logged_user())){ flash_error(lang('no access permissions')); ajx_current("empty"); return; } $is_read = $event->getIsRead(logged_user()->getId()); $year = array_var($_GET, 'year', $event->getStart()->getYear()); $month = array_var($_GET, 'month', $event->getStart()->getMonth()); $day = array_var($_GET, 'day', $event->getStart()->getDay()); $hour = array_var($_GET, 'hour', 0); $min = array_var($_GET, 'min', 0); if ($hour == -1) $hour = format_date($event->getStart(), 'H', logged_user()->getTimezone() ); if ($min == -1) $min = format_date($event->getStart(), 'i', logged_user()->getTimezone() ); if ($event->isRepetitive()) { $orig_date = DateTimeValueLib::dateFromFormatAndString('Y-m-d H:i:s', array_var($_GET, 'orig_date')); $diff = DateTimeValueLib::get_time_difference($orig_date->getTimestamp(), mktime($hour, $min, 0, $month, $day, $year)); $new_start = new DateTimeValue($event->getStart()->getTimestamp()); $new_start->add('d', $diff['days']); $new_start->add('h', $diff['hours']); $new_start->add('m', $diff['minutes']); if ($event->getRepeatH()) { $event->setRepeatDow(date("w", mktime($hour, $min, 0, $month, $day, $year))+1); $wnum = 0; $tmp_day = $new_start->getDay(); while ($tmp_day > 0) { $tmp_day -= 7; $wnum++; } $event->setRepeatWnum($wnum); } } else { $new_start = new DateTimeValue(mktime($hour, $min, 0, $month, $day, $year) - logged_user()->getTimezone() * 3600); } $diff = DateTimeValueLib::get_time_difference($event->getStart()->getTimestamp(), $event->getDuration()->getTimestamp()); $new_duration = new DateTimeValue($new_start->getTimestamp()); $new_duration->add('d', $diff['days']); $new_duration->add('h', $diff['hours']); $new_duration->add('m', $diff['minutes']); // see if we have to reload $os = format_date($event->getStart(), 'd', logged_user()->getTimezone() ); $od = format_date($event->getDuration(), 'd', logged_user()->getTimezone() ); $ohm = format_date($event->getDuration(), 'H:i', logged_user()->getTimezone() ); $nd = format_date($new_duration, 'd', logged_user()->getTimezone() ); $nhm = format_date($new_duration, 'H:i', logged_user()->getTimezone() ); $different_days = ($os != $od && $ohm != '00:00') || ($day != $nd && $nhm != '00:00'); DB::beginWork(); $event->setStart($new_start->format("Y-m-d H:i:s")); $event->setDuration($new_duration->format("Y-m-d H:i:s")); $event->save(); $old_reminders = ObjectReminders::getByObject($event); if($old_reminders != null){ $object_controller = new ObjectController(); $object_controller->update_reminders($event, $old_reminders); //updating the old ones }else if(user_config_option("add_event_autoreminder")){ $reminder = new ObjectReminder(); $def = explode(",",user_config_option("reminders_events")); $minutes = array_var($def, 2) * array_var($def, 1); $reminder->setMinutesBefore($minutes); $reminder->setType(array_var($def, 0)); $reminder->setContext("start"); $reminder->setObject($event); $reminder->setUserId(0); $date = $event->getStart(); if ($date instanceof DateTimeValue) { $rdate = new DateTimeValue($date->getTimestamp() - $minutes * 60); $reminder->setDate($rdate); } $reminder->save(); } if (!$is_read) { $event->setIsRead(logged_user()->getId(), false); } if($event->getSpecialID() != ""){ $this->sync_calendar_extern($event); } DB::commit(); ajx_extra_data($this->get_updated_event_data($event)); if ($different_days || $event->isRepetitive()) ajx_current("reload"); else ajx_current("empty"); }
/** * 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); } } } 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()), '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 //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'))) { $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); } //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(); } 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; } 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()] = '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); if ($task->getDueDate()!= null && !$task->isCompleted() && $task->getSubscriberIds() != null){ //to make sure the task has a due date and it is not completed yet, and that it has subscribed people $old_reminders = ObjectReminders::getByObject($task); if($old_reminders != null){ $object_controller = new ObjectController(); $object_controller->add_reminders($task); //adding the new reminders, if any $object_controller->update_reminders($task, $old_reminders); //updating the old ones }else if ( (user_config_option("add_task_autoreminder") && logged_user()->getId() != $task->getAssignedTo()->getId()) //if the user is going to set up reminders for tasks assigned to its colleagues || (user_config_option("add_self_task_autoreminder") && logged_user()->getId() == $task->getAssignedTo()->getId()) //if the user is going to set up reminders for his own tasks || $task->getAssignedTo() == null ){ //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(); } } $task->apply_members_to_subtasks($member_ids, 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') == "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(); } //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(); } 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 } // edit_task