/** * Reschedule selected milestone * * @param void * @return null */ function reschedule() { if ($this->active_milestone->isNew()) { $this->httpError(HTTP_ERR_NOT_FOUND); } // if if (!$this->active_milestone->canEdit($this->logged_user)) { $this->httpError($this->logged_user); } // if $milestone_data = $this->request->post('milestone'); if (!is_array($milestone_data)) { $milestone_data = array('start_on' => $this->active_milestone->getStartOn(), 'due_on' => $this->active_milestone->getDueOn(), 'reschedule_milstone_objects' => false); } // if $this->smarty->assign('milestone_data', $milestone_data); if ($this->request->isSubmitted()) { db_begin_work(); $old_due_on = new DateValue($this->active_milestone->getDueOn()); $new_start_on = new DateValue(array_var($milestone_data, 'start_on')); $new_due_on = new DateValue(array_var($milestone_data, 'due_on')); $reschedule_tasks = (bool) array_var($milestone_data, 'reschedule_milstone_objects'); $successive_milestones = Milestones::findSuccessiveByMilestone($this->active_milestone, STATE_VISIBLE, $this->logged_user->getVisibility()); // before we update timestamp $reschedule = $this->active_milestone->reschedule($new_start_on, $new_due_on, $reschedule_tasks); if ($reschedule && !is_error($reschedule)) { //if (instance_of($new_due_on, 'DateValue')){ if ($new_due_on->getTimestamp() != $old_due_on->getTimestamp()) { $with_successive = array_var($milestone_data, 'with_sucessive'); $to_move = null; switch (array_var($with_successive, 'action')) { case 'move_all': $to_move = $successive_milestones; break; case 'move_selected': $selected_milestones = array_var($with_successive, 'milestones'); if (is_foreachable($selected_milestones)) { $to_move = Milestones::findByIds($selected_milestones, STATE_VISIBLE, $this->logged_user->getVisibility()); } // if break; } // switch if (is_foreachable($to_move)) { $diff = $new_due_on->getTimestamp() - $old_due_on->getTimestamp(); foreach ($to_move as $to_move_milestone) { $milestone_start_on = $to_move_milestone->getStartOn(); $milestone_due_on = $to_move_milestone->getDueOn(); $new_milestone_start_on = $milestone_start_on->advance($diff, false); $new_milestone_due_on = $milestone_due_on->advance($diff, false); $to_move_milestone->reschedule($new_milestone_start_on, $new_milestone_due_on, $reschedule_tasks); } // foreach } // if } // if db_commit(); if ($this->request->getFormat() == FORMAT_HTML) { //flash_success('Milestone ":name" has been updated', array('name' => $this->active_milestone->getName()), false, true); flash_success('Project ":name" has been updated', array('name' => $this->active_milestone->getName()), false, true); $this->redirectToUrl($this->active_milestone->getViewUrl()); } else { $this->serveData($this->active_milestone); } // if //} } else { db_rollback(); if ($this->request->getFormat() == FORMAT_HTML) { $this->smarty->assign('errors', $reschedule); } else { $this->serveData($save); } // if } // if } // if }
/** * Find successive milestones by a given milestone * * @param Milestone $milestone * @param integer $min_state * @param integer $min_visibility * @return array */ function findSuccessiveByMilestone($milestone, $min_state = STATE_VISIBLE, $min_visibility = VISIBILITY_NORMAL) { return Milestones::find(array('conditions' => array('project_id = ? AND type = ? AND due_on >= ? AND state >= ? AND visibility >= ? AND id != ?', $milestone->getProjectId(), 'Milestone', $milestone->getDueOn(), $min_state, $min_visibility, $milestone->getId()), 'order' => 'name')); }