/**
 * Handle on prepare project overview event
 *
 * @param NamedList $tabs
 * @param User $logged_user
 * @param Project $project
 * @return null
 */
function milestones_handle_on_project_tabs(&$tabs, &$logged_user, &$project)
{
    if ($logged_user->getProjectPermission('milestone', $project) >= PROJECT_PERMISSION_ACCESS) {
        $tabs->add('milestones', array('text' => lang('Milestones'), 'url' => milestones_module_url($project)));
    }
    // if
    if ($logged_user->getProjectPermission('ticket', $project) >= PROJECT_PERMISSION_ACCESS) {
        $tabs->add('tickets', array('text' => lang('Tickets'), 'url' => tickets_module_url($project)));
    }
    // if
    if ($logged_user->getProjectPermission('page', $project) >= PROJECT_PERMISSION_ACCESS) {
        $tabs->add('pages', array('text' => lang('Pages'), 'url' => pages_module_url($project)));
    }
    // if
}
 /**
  * Show and process edit task form
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->active_task->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
     }
     // if
     if (empty($this->active_task_parent)) {
         $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
     }
     // if
     if (!$this->active_task->canEdit($this->logged_user) && $this->active_task->getProjectId() != TASK_LIST_PROJECT_ID) {
         $this->httpError(HTTP_ERR_FORBIDDEN, null, true, $this->request->isApiCall());
     }
     // if
     //BOF:mod
     $this->wireframe->addPageAction(lang('Convert Task to a Ticket'), tickets_module_url($this->active_project) . '&task_id=' . $this->active_task->getId());
     //EOF:mod
     $task_data = $this->request->post('task');
     if (!is_array($task_data)) {
         $task_data = array('body' => $this->active_task->getBody(), 'priority' => $this->active_task->getPriority(), 'due_on' => $this->active_task->getDueOn(), 'assignees' => Assignments::findAssignmentDataByObject($this->active_task));
         $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
         mysql_select_db(DB_NAME);
         $query = "select * from healingcrystals_project_object_misc where object_id='" . $this->active_task->getId() . "'";
         $result = mysql_query($query, $link);
         if (mysql_num_rows($result)) {
             $info = mysql_fetch_assoc($result);
             $task_data['recurring_flag'] = '1';
             $task_data['recurring_period'] = $info['recurring_period'];
             if (empty($task_data['recurring_period'])) {
                 $task_data['recurring_flag'] = '0';
             }
             $task_data['recurring_period_type'] = $info['recurring_period_type'];
             $task_data['recurring_period_condition'] = $info['recurring_period_condition'];
             $task_data['recurring_end_date'] = empty($info['recurring_end_date']) || $info['recurring_end_date'] == '0000-00-00' ? '' : dateval($info['recurring_end_date']);
             /*if (!empty($info['reminder_date']) && $info['reminder_date']!='0000-00-00 00:00:00'){
                              list($date, $time) = explode(' ', $info['reminder_date']);
                              list($h, $m, $s) = explode(':', $time);
                              //$date = str_replace('-', '/', $date);
                              $date = dateval($date);
             		$task_data['reminder'] = $date;
                          } else {*/
             $task_data['reminder'] = '';
             /*}
               $task_data['remindermeridian'] = $h>=12 ? 'PM' : 'AM';
               $task_data['reminderhours'] = $h>12 ? $h-12 : ($h!=0 ? $h : '12');
               $task_data['reminderminutes'] = $m;*/
             //BOF:mod 20120703
             $task_data['auto_email_status'] = $info['auto_email_status'];
             //EOF:mod 20120703
             if ($info['auto_email_status'] == '1') {
                 $task_data['figure_before_due_date'] = empty($info['email_reminder_period']) ? '0' : $info['email_reminder_period'];
                 $task_data['unit_before_due_date'] = empty($info['email_reminder_unit']) ? 'D' : $info['email_reminder_unit'];
                 $email_reminder_time = $info['email_reminder_time'];
                 if (!empty($email_reminder_time)) {
                     list($h, $m, ) = explode(':', $email_reminder_time);
                     $task_data['remindermeridian'] = $h >= 12 ? 'PM' : 'AM';
                     $task_data['reminderhours'] = $h > 12 ? $h - 12 : ($h != 0 ? $h : '12');
                     $task_data['reminderminutes'] = $m;
                 } else {
                     $task_data['remindermeridian'] = 'AM';
                     $task_data['reminderhours'] = '6';
                     $task_data['reminderminutes'] = '0';
                 }
             }
         } else {
             $task_data['recurring_flag'] = '0';
             $task_data['recurring_period'] = '';
             $task_data['recurring_period_type'] = 'D';
             $task_data['recurring_period_condition'] = 'after_due_date';
             $task_data['recurring_end_date'] = '';
             $task_data['reminder'] = '';
             $task_data['reminderhours'] = '6';
             $task_data['reminderminutes'] = '0';
             $task_data['remindermeridian'] = 'AM';
             //BOF:mod 20120703
             $task_data['auto_email_status'] = '';
             //EOF:mod 20120703
             $task_data['figure_before_due_date'] = '0';
             $task_data['unit_before_due_date'] = 'D';
         }
         mysql_close($link);
     }
     // if
     $this->smarty->assign('task_data', $task_data);
     if ($this->request->isSubmitted()) {
         if (!isset($task_data['assignees'])) {
             $task_data['assignees'] = array(array(), 0);
         }
         // if
         db_begin_work();
         $old_name = $this->active_task->getBody();
         $this->active_task->setAttributes($task_data);
         $save = $this->active_task->save();
         if ($save && !is_error($save)) {
             db_commit();
             //BOF:mod
             $recurring_flag = $task_data['recurring_flag'];
             $recurring_period = $task_data['recurring_period'];
             $recurring_period_type = $task_data['recurring_period_type'];
             $recurring_period_condition = $task_data['recurring_period_condition'];
             //$recurring_end_date 		= str_replace('/', '-', $task_data['recurring_end_date']);
             //   $reminder                       = str_replace('/', '-', $task_data['reminder']);
             $recurring_end_date = dateval($task_data['recurring_end_date']);
             /*$reminder                       = dateval($task_data['reminder']);
                          $reminderhours                  = (int)$task_data['reminderhours'];
                          $reminderminutes                = (int)$task_data['reminderminutes'];
                          $remindermeridian               = $task_data['remindermeridian'];
                          if (!empty($reminder)){
                              if (!empty($remindermeridian) && $remindermeridian=='PM' && $reminderhours<12){
                                  $reminderhours += 12;
                              } elseif (!empty($remindermeridian) && $remindermeridian=='AM' && $reminderhours==12){
             			$reminderhours = 0;
             		}
                              $reminder                   = $reminder . ' ' . $reminderhours . ':' . $reminderminutes;
                          }*/
             $email_flag = empty($task_data['email_flag']) ? '0' : '1';
             if ($email_flag == '1') {
                 $email_reminder_period = (int) $task_data['figure_before_due_date'];
                 $email_reminder_unit = empty($task_data['unit_before_due_date']) ? 'D' : $task_data['unit_before_due_date'];
                 $email_reminder_hours = empty($task_data['reminderhours']) ? '6' : $task_data['reminderhours'];
                 $email_reminder_minutes = (int) $task_data['reminderminutes'];
                 $email_reminder_meridian = empty($task_data['remindermeridian']) ? 'AM' : $task_data['remindermeridian'];
                 $email_reminder_time = '';
                 if ($email_reminder_meridian == 'PM' && $email_reminder_hours < 12) {
                     $email_reminder_time = $email_reminder_hours + 12 . ':';
                 } elseif ($email_reminder_meridian == 'AM' && $email_reminder_hours == 12) {
                     $email_reminder_time = '00:';
                 } else {
                     $email_reminder_time = str_pad($email_reminder_hours, 2, '0', STR_PAD_LEFT) . ':';
                 }
                 $email_reminder_time .= $email_reminder_minutes;
             }
             $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
             mysql_select_db(DB_NAME);
             if (empty($recurring_flag) && empty($reminder) && empty($email_flag)) {
                 $query = "delete from healingcrystals_project_object_misc where object_id='" . $this->active_task->getId() . "'";
                 mysql_query($query, $link);
             } else {
                 if (empty($recurring_flag)) {
                     $recurring_period = '';
                     $recurring_period_type = '';
                     $recurring_period_condition = '';
                     $recurring_end_date = '';
                 }
                 $query = "select * from healingcrystals_project_object_misc where object_id='" . $this->active_task->getId() . "'";
                 $result = mysql_query($query, $link);
                 if (mysql_num_rows($result)) {
                     $query01 = "update healingcrystals_project_object_misc set " . " recurring_period='" . $recurring_period . "',\n                                    recurring_period_type='" . $recurring_period_type . "',\n                                    recurring_period_condition='" . $recurring_period_condition . "',\n                                    recurring_end_date='" . $recurring_end_date . "',\n                                    auto_email_status='" . $email_flag . "',\n\t\t\t\t\t\t\t\t\temail_reminder_period=" . ($email_flag == '1' ? "'" . $email_reminder_period . "'" : "null") . ", \n\t\t\t\t\t\t\t\t\temail_reminder_unit=" . ($email_flag == '1' ? "'" . $email_reminder_unit . "'" : "null") . ", \n\t\t\t\t\t\t\t\t\temail_reminder_time=" . ($email_flag == '1' ? "'" . $email_reminder_time . "'" : "null") . ", \n                                    last_modified=now() where object_id='" . $this->active_task->getId() . "'";
                     mysql_query($query01, $link);
                 } else {
                     $query01 = "insert into healingcrystals_project_object_misc\n                                    (object_id, " . " recurring_period,\n                                    recurring_period_type,\n                                    recurring_period_condition,\n                                    recurring_end_date,\n                                    date_added,\n                                    auto_email_status, \n\t\t\t\t\t\t\t\t\temail_reminder_period, \n\t\t\t\t\t\t\t\t\t email_reminder_unit, \n\t\t\t\t\t\t\t\t\t email_reminder_time) values (\n                                    '" . $this->active_task->getId() . "', " . "'" . $recurring_period . "',\n                                    '" . $recurring_period_type . "',\n                                    '" . $recurring_period_condition . "',\n                                    '" . $recurring_end_date . "',\n                                    now(),\n                                    '" . $email_flag . "', \n\t\t\t\t\t\t\t\t\t" . ($email_flag == '1' ? "'" . $email_reminder_period . "'" : "null") . ", \n\t\t\t\t\t\t\t\t\t" . ($email_flag == '1' ? "'" . $email_reminder_unit . "'" : "null") . ", \n\t\t\t\t\t\t\t\t\t" . ($email_flag == '1' ? "'" . $email_reminder_time . "'" : "null") . ")";
                     mysql_query($query01, $link);
                 }
             }
             mysql_close($link);
             //EOF:mod
             if ($this->request->isApiCall()) {
                 $this->serveData($this->active_task, 'task');
             } else {
                 //flash_success('Task ":name" has been updated', array('name' => str_excerpt($old_name, 80, '...')), false, false);
                 //bof:mod
                 flash_success('Task ":name" has been updated', array('name' => str_excerpt(strip_tags($old_name), 80, '...')), false, false);
                 //eof:mod
                 //BOF:mod 20120904
                 /*
                 //EOF:mod 20120904
                             $this->redirectToUrl($this->active_task_parent->getViewUrl());
                 //BOF:mod 20120904
                 */
                 $this->redirectToUrl($this->active_task_parent->getViewUrl() . '#task' . $this->active_task->getId());
                 //EOF:mod 20120904
             }
             // if
         } else {
             db_rollback();
             if ($this->request->isApiCall()) {
                 $this->serveData($save);
             } else {
                 $this->smarty->assign('errors', $save);
             }
             // if
         }
         // if
     } else {
         if ($this->request->isApiCall()) {
             $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, true);
         }
         // if
     }
     // if
 }
 /**
  * Show tickets index page
  *
  * @param void
  * @return null
  */
 function index()
 {
     /*
         	$selected_milestone_id = $_GET['milestone_id'];
     		$milestones = Milestones::findByProject($this->active_project, $this->logged_user);
     		//$milestones = Milestones::findActiveByProject($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility());
     		
     		if (empty($selected_milestone_id)){
     			//$tickets = Tickets::findOpenByProject($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility());
               	$tickets = Milestones::groupByMilestone(Tickets::findOpenByProject($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility()), STATE_VISIBLE, $this->logged_user->getVisibility());
     		} else {
     			$milestone = new Milestone($selected_milestone_id);
     			$tickets_in_milestone = Tickets::findByMilestone($milestone, STATE_VISIBLE, $this->logged_user->getVisibility());
     			$tickets[$milestone->getId()] = array('milestone' => $milestone, 'objects' => $tickets_in_milestone);
     		}
     		
     		$this->smarty->assign(array('milestones' => $milestones, 
     								    'selected_milestone_id' => $selected_milestone_id, 
               							'can_add_ticket' => Ticket::canAdd($this->logged_user, $this->active_project),
               							'can_manage_categories' => $this->logged_user->isProjectLeader($this->active_project) || $this->logged_user->isProjectManager(),
               							'tickets' => $tickets,
     									'categories' => Categories::findByModuleSection($this->active_project, TICKETS_MODULE, 'tickets'),
     									'tickets_count' => (!empty($selected_milestone_id) ? count($tickets[$milestone->getId()]['objects']) : '-1')
     									));
     */
     if ($this->request->isApiCall()) {
         if ($this->active_category->isLoaded()) {
             $this->serveData(Tickets::findOpenByCategory($this->active_category, STATE_VISIBLE, $this->logged_user->getVisibility()), 'tickets');
         } else {
             $this->serveData(Tickets::findOpenByProject($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility()), 'tickets');
         }
         // if
     } else {
         $tickets_url = tickets_module_url($this->active_project);
         $options_sort = new NamedList();
         $options_sort->add('category', array('url' => $tickets_url . '&sort=category', 'text' => lang('by Category')));
         $options_sort->add('star', array('url' => $tickets_url . '&sort=star', 'text' => lang('by Star')));
         $options_sort->add('priority', array('url' => $tickets_url . '&sort=priority', 'text' => lang('by Priority')));
         $options_sort->add('name', array('url' => $tickets_url . '&sort=name', 'text' => lang('by Name')));
         $options_sort->add('owner', array('url' => $tickets_url . '&sort=owner', 'text' => lang('by Owner')));
         //BOF:mod #59_266
         $options_sort->add('project', array('url' => $tickets_url . '&sort=milestone', 'text' => 'by Milestone'));
         //EOF:mod #59_266
         $this->wireframe->addPageAction(lang('Sort'), '#', $options_sort->data);
         $task_id = $_GET['task_id'];
         if (!empty($task_id)) {
             $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
             mysql_select_db(DB_NAME);
             $query = "select type, body from healingcrystals_project_objects where id='" . (int) $task_id . "'";
             $result = mysql_query($query);
             $info = mysql_fetch_assoc($result);
             if (strtolower($info['type']) == 'task') {
                 $body_wo_tags = strip_tags($info['body']);
                 $query = "select max(integer_field_1) as max_count from healingcrystals_project_objects where project_id='" . $this->active_project->getId() . "'";
                 $result = mysql_query($query);
                 $info = mysql_fetch_assoc($result);
                 $integer_field_1 = $info['max_count'] + 1;
                 $query = "update healingcrystals_project_objects set \n                                     type='Ticket', \n                                     module='tickets', \n                                     parent_id='0', \n                                     parent_type=null, \n                                     name='" . str_replace("'", "''", $body_wo_tags) . "', \n                                     body=null, \n                                     updated_on=now(), \n                                     updated_by_id='" . $this->logged_user->getId() . "', \n                                     integer_field_1='" . $integer_field_1 . "' where id='" . (int) $task_id . "'";
                 mysql_query($query);
                 $edit_url = assemble_url('project_ticket_edit', array('project_id' => $this->active_project->getId(), 'ticket_id' => $integer_field_1));
                 header('Location: ' . $edit_url);
             }
             mysql_close($link);
         }
         $cur_department = $_GET['department_id'];
         $mID = $_GET['mID'];
         $page = (int) $_GET['page'];
         $page = $page <= 0 ? 1 : $page;
         $limit = (int) $_GET['limit'];
         //$limit = $limit<=0 ? 50 : $limit;
         $limit = $limit <= 0 ? 1000 : $limit;
         $offset = ($page - 1) * $limit;
         $sort_by = trim(strtolower($_GET['sort']));
         $sort_by = empty($sort_by) ? 'category' : $sort_by;
         $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
         mysql_select_db(DB_NAME);
         $tickets = array();
         /*if($this->active_category->isLoaded()) {
             $tickets = Milestones::groupByMilestone(
               Tickets::findOpenByCategory($this->active_category, STATE_VISIBLE, $this->logged_user->getVisibility()), 
               STATE_VISIBLE, $this->logged_user->getVisibility()
             );
           } else {
             $tickets = Milestones::groupByMilestone(
               Tickets::findOpenByProject($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility()), 
               STATE_VISIBLE, $this->logged_user->getVisibility()
             );*/
         if (empty($cur_department)) {
             //BOF:mod #59_266
             /*
             //EOF:mod #59_266
             $query = "select a.id as ticket_id, b.id as milestone_id, ifnull(a.priority, '0') from healingcrystals_project_objects a left join healingcrystals_project_objects b on a.milestone_id=b.id " ;
             //BOF:mod #59_266
             */
             $query = "select distinct a.id as ticket_id, b.id as milestone_id, cast(ifnull(a.priority, '0') as signed integer) as priority ::CONTENT_PLACE_HOLDER:: from healingcrystals_project_objects a left join healingcrystals_project_objects b on a.milestone_id=b.id ";
             //EOF:mod #59_266
             //BOF:mod 20120911
             /*
             //EOF:mod 20120911
                         $query_condition = " where a.project_id='" . $this->active_project->getId() . "' and a.type='Ticket' and a.state='" . STATE_VISIBLE . "' and a.visibility='" . VISIBILITY_NORMAL . "' and a.completed_on is null " . ($this->active_category && $this->active_category->isLoaded() ? " and a.parent_id='" . $this->active_category->getId() . "' " : "") . (!empty($mID) ? " and b.id='" . $mID . "'" : "");
             //BOF:mod 20120911
             */
             $query_condition = " where a.project_id='" . $this->active_project->getId() . "' and a.type='Ticket' and a.state='" . STATE_VISIBLE . "' and a.visibility>='" . $this->logged_user->getVisibility() . "' and a.completed_on is null " . ($this->active_category && $this->active_category->isLoaded() ? " and a.parent_id='" . $this->active_category->getId() . "' " : "") . (!empty($mID) ? " and b.id='" . $mID . "'" : "");
             //EOF:mod 20120911
         } else {
             if ($cur_department == '-1') {
                 //BOF:mod #59_266
                 /*
                 //EOF:mod #59_266
                 $query = "select a.id as ticket_id, b.id as milestone_id, ifnull(a.priority, '0') from healingcrystals_project_objects a left join healingcrystals_project_objects b on a.milestone_id=b.id " ;
                 //BOF:mod #59_266
                 */
                 $query = "select distinct a.id as ticket_id, b.id as milestone_id, cast(ifnull(a.priority, '0') as signed integer) as priority ::CONTENT_PLACE_HOLDER:: from healingcrystals_project_objects a left join healingcrystals_project_objects b on a.milestone_id=b.id ";
                 //EOF:mod #59_266
                 //BOF:mod 20120911
                 /*
                 //EOF:mod 20120911
                                 $query_condition = " where not exists(select * from healingcrystals_project_object_categories obj where obj.object_id=a.id) and a.project_id='" . $this->active_project->getId() . "' and a.type='Ticket' and a.state='" . STATE_VISIBLE . "' and a.visibility='" . VISIBILITY_NORMAL . "' and a.completed_on is null " . ($this->active_category && $this->active_category->isLoaded() ? " and a.parent_id='" . $this->active_category->getId() . "' " : "") . (!empty($mID) ? " and b.id='" . $mID . "'" : "");
                 //BOF:mod 20120911
                 */
                 $query_condition = " where not exists(select * from healingcrystals_project_object_categories obj where obj.object_id=a.id) and a.project_id='" . $this->active_project->getId() . "' and a.type='Ticket' and a.state='" . STATE_VISIBLE . "' and a.visibility>='" . $this->logged_user->getVisibility() . "' and a.completed_on is null " . ($this->active_category && $this->active_category->isLoaded() ? " and a.parent_id='" . $this->active_category->getId() . "' " : "") . (!empty($mID) ? " and b.id='" . $mID . "'" : "");
                 //EOF:mod 20120911
             } elseif ($cur_department == '-99') {
                 //BOF:mod #59_266
                 /*
                 //EOF:mod #59_266
                 $query = "select a.id as ticket_id, b.id as milestone_id, ifnull(a.priority, '0') from healingcrystals_project_objects a left join healingcrystals_project_objects b on a.milestone_id=b.id left join healingcrystals_project_milestone_categories dep on a.project_id=dep.project_id left join healingcrystals_project_object_categories obj on (dep.id=obj.category_id and a.id=obj.object_id) " ;
                 //BOF:mod #59_266
                 */
                 $query = "select distinct a.id as ticket_id, b.id as milestone_id, cast(ifnull(a.priority, '0') as signed integer) as priority ::CONTENT_PLACE_HOLDER:: from healingcrystals_project_objects a left join healingcrystals_project_objects b on a.milestone_id=b.id left join healingcrystals_project_milestone_categories dep on a.project_id=dep.project_id left join healingcrystals_project_object_categories obj on (dep.id=obj.category_id and a.id=obj.object_id) ";
                 //EOF:mod #59_266
                 $query_condition = " where a.project_id='" . $this->active_project->getId() . "' and a.type='Ticket' and a.completed_on is not null " . ($this->active_category && $this->active_category->isLoaded() ? " and a.parent_id='" . $this->active_category->getId() . "' " : "") . (!empty($mID) ? " and b.id='" . $mID . "'" : "");
             } else {
                 //BOF:mod #59_266
                 /*
                 //EOF:mod #59_266
                 $query = "select a.id as ticket_id, b.id as milestone_id, ifnull(a.priority, '0') from healingcrystals_project_objects a left join healingcrystals_project_objects b on a.milestone_id=b.id inner join healingcrystals_project_milestone_categories dep on a.project_id=dep.project_id inner join healingcrystals_project_object_categories obj on (dep.id=obj.category_id and a.id=obj.object_id) " ;
                 //BOF:mod #59_266
                 */
                 $query = "select distinct a.id as ticket_id, b.id as milestone_id, cast(ifnull(a.priority, '0') as signed integer) as priority ::CONTENT_PLACE_HOLDER:: from healingcrystals_project_objects a left join healingcrystals_project_objects b on a.milestone_id=b.id inner join healingcrystals_project_milestone_categories dep on a.project_id=dep.project_id inner join healingcrystals_project_object_categories obj on (dep.id=obj.category_id and a.id=obj.object_id) ";
                 //EOF:mod #59_266
                 //BOF:mod 20120911
                 /*
                 //EOF:mod 20120911
                                 $query_condition = " where a.project_id='" . $this->active_project->getId() . "' and dep.id='" . $cur_department . "' and a.type='Ticket' and a.state='" . STATE_VISIBLE . "' and a.visibility='" . VISIBILITY_NORMAL . "' and a.completed_on is null " . ($this->active_category && $this->active_category->isLoaded() ? " and a.parent_id='" . $this->active_category->getId() . "' " : "") . (!empty($mID) ? " and b.id='" . $mID . "'" : "");
                 //BOF:mod 20120911
                 */
                 $query_condition = " where a.project_id='" . $this->active_project->getId() . "' and dep.id='" . $cur_department . "' and a.type='Ticket' and a.state='" . STATE_VISIBLE . "' and a.visibility>='" . $this->logged_user->getVisibility() . "' and a.completed_on is null " . ($this->active_category && $this->active_category->isLoaded() ? " and a.parent_id='" . $this->active_category->getId() . "' " : "") . (!empty($mID) ? " and b.id='" . $mID . "'" : "");
                 //EOF:mod 20120911
             }
         }
         //BOF:mod #59_266
         /*
         //EOF:mod #59_266
         $result = mysql_query($query . $query_condition);
         $total_tickets = mysql_num_rows($result);
         $total_pages = ceil($total_tickets / $limit);
         if ($sort_by=='category'){
         //BOF:mod #59_266
         */
         if ($sort_by == 'category' || $sort_by == 'milestone') {
             if ($sort_by == 'category') {
                 $query_1 = str_replace('::CONTENT_PLACE_HOLDER::', '', $query) . $query_condition;
             } else {
                 $query_1 = str_replace('::CONTENT_PLACE_HOLDER::', ", (select max(created_on) from healingcrystals_project_objects c where c.type='Comment' and c.parent_id=a.id) as comment_date", $query) . $query_condition;
             }
             $result_temp = mysql_query($query_1);
             $total_tickets = mysql_num_rows($result_temp);
             $total_pages = ceil($total_tickets / $limit);
             if ($sort_by == 'category') {
                 //$query_1 .= " order by isnull(b.id), b.name, a.position, priority desc limit " . $offset . ", " . $limit;
                 $query_1 .= " order by isnull(b.id), b.name, priority desc, a.position limit " . $offset . ", " . $limit;
             } else {
                 $query_1 .= " order by isnull(b.id), b.name, priority desc, comment_date desc limit " . $offset . ", " . $limit;
             }
             /*
             //EOF:mod #59_266
                 $query_1 = $query . $query_condition . " order by isnull(b.id), b.name, a.priority desc limit " . $offset . ", " . $limit;
             //BOF:mod #59_266
             */
             //EOF:mod #59_266
             $result = mysql_query($query_1, $link);
             $cur_milestone_id = -1;
             while ($ticket = mysql_fetch_assoc($result)) {
                 if ($cur_milestone_id == -1 || $cur_milestone_id != (int) $ticket['milestone_id']) {
                     $cur_milestone_id = (int) $ticket['milestone_id'];
                     if ($cur_milestone_id == 0) {
                         $cur_milestone_ref = null;
                     } else {
                         $cur_milestone_ref = new Milestone($ticket['milestone_id']);
                     }
                     $tickets[] = array('milestone' => $cur_milestone_ref, 'objects' => array());
                 }
                 $tickets[count($tickets) - 1]['objects'][] = new Ticket($ticket['ticket_id']);
             }
         } elseif ($sort_by == 'star' || $sort_by == 'priority' || $sort_by == 'name' || $sort_by == 'owner') {
             //BOF:mod #59_266
             $query = str_replace('::CONTENT_PLACE_HOLDER::', '', $query);
             $result_temp = mysql_query($query . $query_condition);
             $total_tickets = mysql_num_rows($result_temp);
             $total_pages = ceil($total_tickets / $limit);
             //EOF:mod #59_266
             switch ($sort_by) {
                 case 'star':
                     $query_1 = $query . " left join healingcrystals_starred_objects c on (a.id=c.object_id and c.user_id='" . $this->logged_user->getId() . "') left join healingcrystals_assignments d on (a.id=d.object_id and d.is_owner='1') left join healingcrystals_users e on d.user_id=e.id " . $query_condition . " order by isnull(c.object_id), 3 desc, isnull(e.first_name), e.first_name limit " . $offset . ", " . $limit;
                     break;
                 case 'priority':
                     $query_1 = $query . " left join healingcrystals_assignments d on (a.id=d.object_id and d.is_owner='1') left join healingcrystals_users e on d.user_id=e.id " . $query_condition . " order by 3 desc, isnull(e.first_name), e.first_name, a.name limit " . $offset . ", " . $limit;
                     break;
                 case 'name':
                     $query_1 = $query . $query_condition . " order by a.name limit " . $offset . ", " . $limit;
                     break;
                 case 'owner':
                     $query_1 = $query . " left join healingcrystals_assignments d on (a.id=d.object_id and d.is_owner='1') left join healingcrystals_users e on d.user_id=e.id " . $query_condition . " order by isnull(e.first_name), e.first_name, 3 desc, a.name limit " . $offset . ", " . $limit;
                     break;
             }
             $result = mysql_query($query_1, $link);
             while ($ticket = mysql_fetch_assoc($result)) {
                 $cur_milestone_id = (int) $ticket['milestone_id'];
                 if ($cur_milestone_id == 0) {
                     $cur_milestone_ref = null;
                 } else {
                     $cur_milestone_ref = new Milestone($ticket['milestone_id']);
                 }
                 $tickets[] = array('milestone' => $cur_milestone_ref, 'ticket' => new Ticket($ticket['ticket_id']));
             }
         }
         //} // if
         $all_milestones = array();
         //BOF:mod 20120911
         /*
         //EOF:mod 20120911
                 $query = "select id, name from healingcrystals_project_objects where project_id='" . $this->active_project->getId() . "' and type='Milestone' and state='" . STATE_VISIBLE . "' and visibility='" . VISIBILITY_NORMAL . "' and completed_on is null order by name";
         //BOF:mod 20120911
         */
         $query = "select id, name from healingcrystals_project_objects where project_id='" . $this->active_project->getId() . "' and type='Milestone' and state='" . STATE_VISIBLE . "' and visibility>='" . $this->logged_user->getVisibility() . "' and completed_on is null order by name";
         //EOF:mod 20120911
         $result = mysql_query($query, $link);
         while ($milestone = mysql_fetch_assoc($result)) {
             $all_milestones[] = array('id' => $milestone['id'], 'name' => $milestone['name']);
         }
         $departments = array();
         $query = "select id, category_name from healingcrystals_project_milestone_categories where project_id='" . $this->active_project->getId() . "' order by category_name";
         $result = mysql_query($query);
         while ($department = mysql_fetch_assoc($result)) {
             $departments[] = array('department_id' => $department['id'], 'department_name' => $department['category_name']);
         }
         $departments[] = array('department_id' => '-1', 'department_name' => 'Uncategorized');
         mysql_close($link);
         $this->smarty->assign(array('categories' => Categories::findByModuleSection($this->active_project, TICKETS_MODULE, 'tickets'), 'groupped_tickets' => $tickets, 'milestones' => Milestones::findActiveByProject($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility()), 'can_add_ticket' => Ticket::canAdd($this->logged_user, $this->active_project), 'can_manage_categories' => $this->logged_user->isProjectLeader($this->active_project) || $this->logged_user->isProjectManager(), 'departments_url' => assemble_url('project_manage_milestone_categories', array('project_id' => $this->active_project->getId())), 'total_tickets' => $total_tickets, 'total_pages' => $total_pages, 'current_page' => $page, 'all_milestones' => $all_milestones, 'current_milestone' => $mID, 'sort_by' => $sort_by, 'departments' => $departments, 'current_department_id' => $cur_department));
         js_assign('can_manage_tickets', Ticket::canManage($this->logged_user, $this->active_project));
     }
     // if
 }