Files
tinker_tickets/dashboard.php

150 lines
5.9 KiB
PHP
Raw Normal View History

2024-11-30 19:48:01 -05:00
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Load environment variables
$envFile = __DIR__ . '/.env';
$envVars = parse_ini_file($envFile);
// Database connection settings
$dbHost = $envVars['REACT_APP_DB_HOST'];
$dbUser = $envVars['REACT_APP_DB_USER'];
$dbPass = $envVars['REACT_APP_DB_PASS'];
$dbName = $envVars['REACT_APP_DB_NAME'];
// Create database connection
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Pagination settings from localStorage or defaults
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = isset($_COOKIE['ticketsPerPage']) ? (int)$_COOKIE['ticketsPerPage'] : 15;
$defaultSortColumn = isset($_COOKIE['defaultSortColumn']) ? $_COOKIE['defaultSortColumn'] : 'ticket_id';
$sortDirection = isset($_COOKIE['sortDirection']) ? $_COOKIE['sortDirection'] : 'desc';
$offset = ($page - 1) * $limit;
// Get total number of tickets
$totalTicketsQuery = "SELECT COUNT(*) as total FROM tickets";
$totalTicketsResult = $conn->query($totalTicketsQuery);
$totalTickets = $totalTicketsResult->fetch_assoc()['total'];
$totalPages = ceil($totalTickets / $limit);
// Modify SQL to use these settings
$sql = "SELECT * FROM tickets ORDER BY $defaultSortColumn $sortDirection LIMIT $limit OFFSET $offset";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2024-12-01 21:22:33 -05:00
<title>Ticket Dashboard</title>
2024-11-30 19:48:01 -05:00
<link rel="icon" type="image/png" href="assets/images/favicon.png">
<link rel="stylesheet" href="assets/css/dashboard.css">
<script src="assets/js/dashboard.js"></script>
</head>
<body>
<div class="dashboard-header">
<h1>Tinker Tickets</h1>
<button onclick="window.location.href='new_ticket.php'" class="btn create-ticket">New Ticket</button>
</div>
<div class="table-controls">
<div class="ticket-count">
Total Tickets: <?php echo $totalTickets; ?>
</div>
<div class="table-actions">
<div class="pagination">
<?php
// Previous page button
if ($page > 1) {
echo "<button onclick='window.location.href=\"?page=" . ($page - 1) . "\"'>«</button>";
}
// Page number buttons
for ($i = 1; $i <= $totalPages; $i++) {
$activeClass = ($i === $page) ? 'active' : '';
echo "<button class='$activeClass' onclick='window.location.href=\"?page=$i\"'>$i</button>";
}
// Next page button
if ($page < $totalPages) {
echo "<button onclick='window.location.href=\"?page=" . ($page + 1) . "\"'>»</button>";
}
?>
</div>
<div class="settings-icon">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="3"></circle>
<path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path>
</svg>
</div>
</div>
</div>
<table>
<thead>
<tr>
<th>Ticket ID</th>
<th>Priority</th>
<th>Title</th>
<th>Category</th>
<th>Type</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr class='priority-{$row['priority']}'>";
echo "<td><a href='ticket.php?id={$row['ticket_id']}' class='ticket-link'>{$row['ticket_id']}</a></td>";
echo "<td><span>{$row['priority']}</span></td>";
echo "<td>{$row['title']}</td>";
echo "<td>{$row['category']}</td>";
echo "<td>{$row['type']}</td>";
echo "<td class='status-{$row['status']}'>{$row['status']}</td>";
echo "<td>" . date('Y-m-d H:i', strtotime($row['created_at'])) . "</td>";
echo "<td>" . date('Y-m-d H:i', strtotime($row['updated_at'])) . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='8'>No tickets found</td></tr>";
}
?>
</tbody>
</table>
<?php $conn->close(); ?>
<script>
document.body.dataset.categories = JSON.stringify([
<?php
$categories = [];
mysqli_data_seek($categoriesResult, 0);
while($row = $categoriesResult->fetch_assoc()) {
$categories[] = "'" . $row['category'] . "'";
}
echo implode(',', $categories);
?>
]);
document.body.dataset.types = JSON.stringify([
<?php
$types = [];
mysqli_data_seek($typesResult, 0);
while($row = $typesResult->fetch_assoc()) {
$types[] = "'" . $row['type'] . "'";
}
echo implode(',', $types);
?>
]);
</script>
</body>
</html>