/**
  * 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'));
 }
 /**
  * Edit specific milestone
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->active_milestone->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_milestone->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     //$this->wireframe->addPageMessage(lang('<a href=":url">Click here</a> if you wish to reschedule this milestone', array('url' => $this->active_milestone->getRescheduleUrl())), 'info');
     $this->wireframe->addPageMessage(lang('<a href=":url">Click here</a> if you wish to reschedule this project', array('url' => $this->active_milestone->getRescheduleUrl())), 'info');
     $milestone_data = $this->request->post('milestone');
     if (!is_array($milestone_data)) {
         $milestone_data = array('name' => $this->active_milestone->getName(), 'body' => $this->active_milestone->getBody(), 'start_on' => $this->active_milestone->getStartOn(), 'due_on' => $this->active_milestone->getDueOn(), 'priority' => $this->active_milestone->getPriority(), 'assignees' => Assignments::findAssignmentDataByObject($this->active_milestone), 'tags' => $this->active_milestone->getTags(), 'project_id' => $this->active_milestone->getProjectId(), 'visibility' => $this->active_milestone->getVisibility());
     }
     // if
     //BOF:mod 20121116
     $options = array();
     $options[] = array('url' => 'javascript:convert_object_to_ticket(\'' . $this->active_milestone->getProjectId() . '\', \'' . $this->active_milestone->getId() . '\', \'' . $this->active_milestone->getType() . '\');', 'text' => 'Ticket');
     $options[] = array('url' => 'javascript:convert_object_to_page(\'' . $this->active_milestone->getProjectId() . '\', \'' . $this->active_milestone->getId() . '\', \'' . $this->active_milestone->getType() . '\');', 'text' => 'Page');
     $this->wireframe->addPageAction(lang('Convert To'), 'javascript://', $options);
     //EOF:mod 20121116
     $this->smarty->assign('milestone_data', $milestone_data);
     //BOF: task 07 | AD
     $this->smarty->assign('is_edit_mode', '1');
     //EOF: task 07 | AD
     if ($this->request->isSubmitted()) {
         if (!isset($milestone_data['assignees'])) {
             $milestone_data['assignees'] = array(array(), 0);
         }
         // if
         db_begin_work();
         $old_name = $this->active_milestone->getName();
         //BOF: task 07 | AD
         $old_project_id = $this->active_milestone->getProjectId();
         //EOF: task 07 | AD
         $this->active_milestone->setAttributes($milestone_data);
         $save = $this->active_milestone->save();
         if ($save && !is_error($save)) {
             db_commit();
             //BOF: task 07 | AD
             //BOF: mod
             $this->active_milestone->register_departments(!empty($milestone_data['departments']) ? $milestone_data['departments'] : array(), implode(',', $milestone_data['departments']));
             //EOF: mod
             //BOF:mod 20110614
             $assignees_flag_data = $this->request->post('assignee');
             $this->active_milestone->register_assignees_flag($assignees_flag_data);
             //EOF:mod 20110614
             if ($old_project_id != $this->active_milestone->getProjectId()) {
                 $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
                 mysql_select_db(DB_NAME, $link);
                 $query = "select id, integer_field_1 \n\t\t\t\t\t\t\tfrom healingcrystals_project_objects where \n\t\t\t\t\t\t\tmilestone_id ='" . $this->active_milestone->getId() . "' and \n\t\t\t\t\t\t\tproject_id='" . $old_project_id . "' and type='Ticket' and integer_field_1 is not null";
                 $result = mysql_query($query);
                 $next_id = Tickets::findNextTicketIdByProject($this->active_milestone->getProjectId());
                 while ($ticket = mysql_fetch_assoc($result)) {
                     mysql_query("update healingcrystals_project_objects \n\t\t\t\t\t\t\t\t\tset integer_field_1='" . $next_id . "'  \n\t\t\t\t\t\t\t\t\twhere id='" . $ticket['id'] . "'");
                     $next_id++;
                 }
                 $query = "select updated_on, updated_by_id, updated_by_name, integer_field_1 from healingcrystals_project_objects where id='" . $this->active_milestone->getId() . "'";
                 $result = mysql_query($query, $link);
                 $info = mysql_fetch_assoc($result);
                 $query = "update healingcrystals_project_objects set \n\t\t\t\t\t\t\tproject_id ='" . $this->active_milestone->getProjectId() . "', \n\t\t\t\t\t\t\tupdated_on =" . (empty($info['updated_on']) ? "null" : "'" . $info['updated_on'] . "'") . ", \n\t\t\t\t\t\t\tupdated_by_id =" . (empty($info['updated_by_id']) ? "null" : "'" . $info['updated_by_id'] . "'") . ", \n\t\t\t\t\t\t\tupdated_by_name =" . (empty($info['updated_by_name']) ? "null" : "'" . mysql_real_escape_string($info['updated_by_name']) . "'") . ", \n\t\t\t\t\t\t\tupdated_by_email =" . (empty($info['updated_by_email']) ? "null" : "'" . $info['updated_by_email'] . "'") . " \n\t\t\t\t\t\t\twhere milestone_id ='" . $this->active_milestone->getId() . "' and project_id='" . $old_project_id . "'";
                 mysql_query($query);
                 $query = "update healingcrystals_project_objects set category_id=null where id='" . $this->active_milestone->getId() . "'";
                 mysql_query($query);
                 mysql_close($link);
             }
             //EOF: task 07 | AD
             if ($this->request->getFormat() == FORMAT_HTML) {
                 //flash_success('Milestone ":name" has been updated', array('name' => $old_name), false, true);
                 flash_success('Project ":name" has been updated', array('name' => $old_name), false, true);
                 $this->redirectToUrl($this->active_milestone->getViewUrl());
             } else {
                 $this->serveData($this->active_milestone, 'milestone');
             }
             // if
         } else {
             db_rollback();
             if ($this->request->getFormat() == FORMAT_HTML) {
                 $this->smarty->assign('errors', $save);
             } else {
                 $this->serveData($save);
             }
             // if
         }
         // if
     }
     // if
 }
 function move_to()
 {
     $gettype = $_GET['gettype'];
     $team_id = $_GET['team_id'];
     $project_id = $_GET['project_id'];
     $ticket_id = $_GET['ticket_id'];
     $move_to_object_id = $_GET['move_to_object_id'];
     if (!empty($gettype)) {
         $this->smarty->assign('gettype', $gettype);
         switch ($gettype) {
             case 'projects':
                 $projects = array();
                 if (!empty($team_id)) {
                     $project_obj = new Project($team_id);
                     //$projects = Milestones::findByProject($project_obj, $this->logged_user);
                     $projects = Milestones::findActiveByProject($project_obj, STATE_VISIBLE, VISIBILITY_NORMAL, 'name', '', '-1');
                 }
                 $this->smarty->assign('projects', $projects);
                 break;
             case 'tickets':
                 $tickets = array();
                 if (!empty($project_id)) {
                     $milestone_obj = new Milestone($project_id);
                     //$tickets = Tickets::findByMilestone($milestone_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
                     $tickets = Tickets::findOpenByMilestone($milestone_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
                 }
                 $this->smarty->assign('tickets', $tickets);
                 break;
             case 'pages':
                 $pages_with_milestone = array();
                 $pages_with_project = array();
                 if (!empty($team_id)) {
                     $project_obj = new Project($team_id);
                     //$pages_with_project = Pages::findByProjectWithoutMilestoneAssociation($project_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
                     $pages_with_project = Pages::findUnarchivedByProjectWithoutMilestoneAssociation($project_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
                 } elseif (!empty($project_id)) {
                     if (!empty($project_id)) {
                         $milestone_obj = new Milestone($project_id);
                         $team_id = $milestone_obj->getProjectId();
                         //$pages_with_milestone = Pages::findByMilestone($milestone_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
                         $pages_with_milestone = Pages::findUnarchivedByMilestone($milestone_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
                         $project_obj = new Project($team_id);
                         //$pages_with_project = Pages::findByProjectWithoutMilestoneAssociation($project_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
                         $pages_with_project = Pages::findUnarchivedByProjectWithoutMilestoneAssociation($project_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
                     }
                 }
                 $this->smarty->assign('pages_with_milestone', $pages_with_milestone);
                 $this->smarty->assign('pages_with_project', $pages_with_project);
                 break;
             case 'action':
                 $move_to_object = new ProjectObject($move_to_object_id);
                 $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
                 mysql_select_db(DB_NAME);
                 mysql_query("update healingcrystals_attachments set parent_id='" . $move_to_object->getId() . "', parent_type='" . $move_to_object->getType() . "' where id='" . $this->active_attachment->getId() . "'");
                 mysql_close($link);
                 $type = $move_to_object->getType();
                 $obj = new $type($move_to_object_id);
                 $link = 'Attachment Moved: <a href="' . $obj->getViewUrl() . '">Click to View</a>';
                 $this->smarty->assign('link', $link);
                 break;
         }
     } else {
         $teams = Projects::findNamesByUser($this->logged_user);
         $this->smarty->assign('teams', $teams);
     }
     $parent = $this->active_attachment->getParent();
     $this->smarty->assign('cur_project_id', $parent->getProjectId());
     $this->smarty->assign('cur_attachment_id', $this->active_attachment->getId());
 }