conn = $conn; $this->ticketModel = new TicketModel($conn); $this->prefsModel = new UserPreferencesModel($conn); $this->statsModel = new StatsModel($conn); } public function index() { // Get user ID for preferences $userId = isset($_SESSION['user']['user_id']) ? $_SESSION['user']['user_id'] : null; // Get query parameters $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // Get rows per page from user preferences, fallback to cookie, then default $limit = 15; if ($userId) { $limit = (int)$this->prefsModel->getPreference($userId, 'rows_per_page', 15); } else if (isset($_COOKIE['ticketsPerPage'])) { $limit = (int)$_COOKIE['ticketsPerPage']; } $sortColumn = isset($_GET['sort']) ? $_GET['sort'] : 'ticket_id'; $sortDirection = isset($_GET['dir']) ? $_GET['dir'] : 'desc'; $category = isset($_GET['category']) ? $_GET['category'] : null; $type = isset($_GET['type']) ? $_GET['type'] : null; $search = isset($_GET['search']) ? trim($_GET['search']) : null; // Handle status filtering with user preferences $status = null; if (isset($_GET['status']) && !empty($_GET['status'])) { $status = $_GET['status']; } else if (!isset($_GET['show_all'])) { // Get default status filters from user preferences if ($userId) { $status = $this->prefsModel->getPreference($userId, 'default_status_filters', 'Open,Pending,In Progress'); } else { // Default: show Open, Pending, and In Progress (exclude Closed) $status = 'Open,Pending,In Progress'; } } // If $_GET['show_all'] exists or no status param with show_all, show all tickets (status = null) // Build advanced search filters array $filters = []; if (isset($_GET['created_from'])) $filters['created_from'] = $_GET['created_from']; if (isset($_GET['created_to'])) $filters['created_to'] = $_GET['created_to']; if (isset($_GET['updated_from'])) $filters['updated_from'] = $_GET['updated_from']; if (isset($_GET['updated_to'])) $filters['updated_to'] = $_GET['updated_to']; if (isset($_GET['priority_min'])) $filters['priority_min'] = $_GET['priority_min']; if (isset($_GET['priority_max'])) $filters['priority_max'] = $_GET['priority_max']; if (isset($_GET['created_by'])) $filters['created_by'] = $_GET['created_by']; if (isset($_GET['assigned_to'])) $filters['assigned_to'] = $_GET['assigned_to']; // Get tickets with pagination, sorting, search, and advanced filters $result = $this->ticketModel->getAllTickets($page, $limit, $status, $sortColumn, $sortDirection, $category, $type, $search, $filters); // Get categories and types for filters $categories = $this->getCategories(); $types = $this->getTypes(); // Extract data for the view $tickets = $result['tickets']; $totalTickets = $result['total']; $totalPages = $result['pages']; // Load dashboard statistics $stats = $this->statsModel->getAllStats(); // Load the dashboard view include 'views/DashboardView.php'; } private function getCategories() { $sql = "SELECT DISTINCT category FROM tickets WHERE category IS NOT NULL ORDER BY category"; $stmt = $this->conn->prepare($sql); $stmt->execute(); $result = $stmt->get_result(); $categories = []; while ($row = $result->fetch_assoc()) { $categories[] = $row['category']; } $stmt->close(); return $categories; } private function getTypes() { $sql = "SELECT DISTINCT type FROM tickets WHERE type IS NOT NULL ORDER BY type"; $stmt = $this->conn->prepare($sql); $stmt->execute(); $result = $stmt->get_result(); $types = []; while ($row = $result->fetch_assoc()) { $types[] = $row['type']; } $stmt->close(); return $types; } } ?>