Re-did everything, Now is modulaar and better bro.

This commit is contained in:
2025-05-16 20:02:49 -04:00
parent 5b50964d06
commit f8ada1d6d1
16 changed files with 1234 additions and 187 deletions

View File

@ -0,0 +1,43 @@
<?php
require_once 'models/CommentModel.php';
class CommentController {
private $commentModel;
public function __construct($conn) {
$this->commentModel = new CommentModel($conn);
}
public function getCommentsByTicketId($ticketId) {
return $this->commentModel->getCommentsByTicketId($ticketId);
}
public function addComment($ticketId) {
// Check if this is an AJAX request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get JSON data
$data = json_decode(file_get_contents('php://input'), true);
// Validate input
if (empty($data['comment_text'])) {
header('Content-Type: application/json');
echo json_encode([
'success' => false,
'error' => 'Comment text cannot be empty'
]);
return;
}
// Add comment
$result = $this->commentModel->addComment($ticketId, $data);
// Return JSON response
header('Content-Type: application/json');
echo json_encode($result);
} else {
// For direct access, redirect to ticket view
header("Location: /tinkertickets/ticket/$ticketId");
exit;
}
}
}

View File

@ -0,0 +1,30 @@
<?php
require_once 'models/TicketModel.php';
class DashboardController {
private $ticketModel;
public function __construct($conn) {
$this->ticketModel = new TicketModel($conn);
}
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';
}
}

View File

@ -0,0 +1,156 @@
<?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';
}
}