156 lines
5.7 KiB
PHP
156 lines
5.7 KiB
PHP
<?php
|
|
// Use absolute paths for model includes
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
require_once dirname(__DIR__) . '/models/CommentModel.php';
|
|
|
|
class TicketController {
|
|
private $ticketModel;
|
|
private $commentModel;
|
|
|
|
public function __construct($conn) {
|
|
$this->ticketModel = new TicketModel($conn);
|
|
$this->commentModel = new CommentModel($conn);
|
|
}
|
|
|
|
public function view($id) {
|
|
// Get ticket data
|
|
$ticket = $this->ticketModel->getTicketById($id);
|
|
|
|
if (!$ticket) {
|
|
header("HTTP/1.0 404 Not Found");
|
|
echo "Ticket not found";
|
|
return;
|
|
}
|
|
|
|
// Get comments for this ticket
|
|
$comments = $this->ticketModel->getTicketComments($id);
|
|
|
|
// Load the view
|
|
include dirname(__DIR__) . '/views/TicketView.php';
|
|
}
|
|
|
|
public function create() {
|
|
// Check if form was submitted
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$ticketData = [
|
|
'title' => $_POST['title'] ?? '',
|
|
'description' => $_POST['description'] ?? '',
|
|
'priority' => $_POST['priority'] ?? '4',
|
|
'category' => $_POST['category'] ?? 'General',
|
|
'type' => $_POST['type'] ?? 'Issue'
|
|
];
|
|
|
|
// Validate input
|
|
if (empty($ticketData['title'])) {
|
|
$error = "Title is required";
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
return;
|
|
}
|
|
|
|
// Create ticket
|
|
$result = $this->ticketModel->createTicket($ticketData);
|
|
|
|
if ($result['success']) {
|
|
// Redirect to the new ticket
|
|
header("Location: /tinkertickets/ticket/" . $result['ticket_id']);
|
|
exit;
|
|
} else {
|
|
$error = $result['error'];
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
return;
|
|
}
|
|
} else {
|
|
// Display the create ticket form
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
}
|
|
}
|
|
|
|
public function update($id) {
|
|
// Debug function
|
|
$debug = function($message, $data = null) {
|
|
$log_message = date('Y-m-d H:i:s') . " - [Controller] " . $message;
|
|
if ($data !== null) {
|
|
$log_message .= ": " . (is_string($data) ? $data : json_encode($data));
|
|
}
|
|
$log_message .= "\n";
|
|
file_put_contents('/tmp/api_debug.log', $log_message, FILE_APPEND);
|
|
};
|
|
|
|
// Check if this is an AJAX request
|
|
$debug("Request method", $_SERVER['REQUEST_METHOD']);
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// For AJAX requests, get JSON data
|
|
$input = file_get_contents('php://input');
|
|
$debug("Raw input", $input);
|
|
$data = json_decode($input, true);
|
|
$debug("Decoded data", $data);
|
|
|
|
// Add ticket_id to the data
|
|
$data['ticket_id'] = $id;
|
|
$debug("Added ticket_id to data", $id);
|
|
|
|
// Validate input data
|
|
if (empty($data['title'])) {
|
|
$debug("Title is empty");
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Title cannot be empty'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// Update ticket
|
|
$debug("Calling model updateTicket method");
|
|
try {
|
|
$result = $this->ticketModel->updateTicket($data);
|
|
$debug("Model updateTicket result", $result);
|
|
} catch (Exception $e) {
|
|
$debug("Exception in model updateTicket", $e->getMessage());
|
|
$debug("Stack trace", $e->getTraceAsString());
|
|
throw $e;
|
|
}
|
|
|
|
// Return JSON response
|
|
header('Content-Type: application/json');
|
|
if ($result) {
|
|
$debug("Update successful, sending success response");
|
|
echo json_encode([
|
|
'success' => true,
|
|
'status' => $data['status']
|
|
]);
|
|
} else {
|
|
$debug("Update failed, sending error response");
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Failed to update ticket'
|
|
]);
|
|
}
|
|
} else {
|
|
// For direct access, redirect to view
|
|
$debug("Not a POST request, redirecting");
|
|
header("Location: " . $GLOBALS['config']['BASE_URL'] . "/ticket/$id");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function index() {
|
|
// Get query parameters
|
|
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
|
$limit = isset($_COOKIE['ticketsPerPage']) ? (int)$_COOKIE['ticketsPerPage'] : 15;
|
|
$status = isset($_GET['status']) ? $_GET['status'] : 'Open';
|
|
$sortColumn = isset($_COOKIE['defaultSortColumn']) ? $_COOKIE['defaultSortColumn'] : 'ticket_id';
|
|
$sortDirection = isset($_COOKIE['sortDirection']) ? $_COOKIE['sortDirection'] : 'desc';
|
|
|
|
// Get tickets with pagination
|
|
$result = $this->ticketModel->getAllTickets($page, $limit, $status, $sortColumn, $sortDirection);
|
|
|
|
// Extract data for the view
|
|
$tickets = $result['tickets'];
|
|
$totalTickets = $result['total'];
|
|
$totalPages = $result['pages'];
|
|
|
|
// Load the dashboard view
|
|
include 'views/DashboardView.php';
|
|
}
|
|
} |