/**
  * List of tickets
  *
  */
 function index()
 {
     $this->addBreadcrumb(lang('List'));
     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
     $this->smarty->assign(array('categories' => Categories::findByModuleSection($this->active_project, TICKETS_MODULE, 'tickets'), 'tickets' => $tickets, 'pagination_url' => assemble_url('mobile_access_view_tickets', array('project_id' => $this->active_project->getId())), 'page_back_url' => assemble_url('mobile_access_view_project', array('project_id' => $this->active_project->getId()))));
 }
 /**
  * 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
 }