2025-05-16 20:02:49 -04:00
|
|
|
<?php
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Use absolute paths for model includes
|
|
|
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
|
|
|
require_once dirname(__DIR__) . '/models/CommentModel.php';
|
2026-01-01 18:25:19 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
2026-01-01 18:36:34 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/UserModel.php';
|
2026-01-01 18:57:23 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/WorkflowModel.php';
|
2026-01-01 19:00:42 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/TemplateModel.php';
|
2026-09-11 13:12:42 -04:00
|
|
|
require_once dirname(__DIR__) . '/models/CustomFieldModel.php';
|
2026-01-30 18:51:16 -05:00
|
|
|
require_once dirname(__DIR__) . '/helpers/UrlHelper.php';
|
2026-02-21 19:17:46 -05:00
|
|
|
require_once dirname(__DIR__) . '/helpers/NotificationHelper.php';
|
2025-05-16 20:02:49 -04:00
|
|
|
|
2026-04-13 20:56:10 -04:00
|
|
|
class TicketController
|
|
|
|
|
{
|
2025-05-16 20:02:49 -04:00
|
|
|
private $ticketModel;
|
|
|
|
|
private $commentModel;
|
2026-01-01 18:25:19 -05:00
|
|
|
private $auditLogModel;
|
2026-01-01 18:36:34 -05:00
|
|
|
private $userModel;
|
2026-01-01 18:57:23 -05:00
|
|
|
private $workflowModel;
|
2026-01-01 19:00:42 -05:00
|
|
|
private $templateModel;
|
2026-09-11 13:12:42 -04:00
|
|
|
private $customFieldModel;
|
2026-01-23 10:01:50 -05:00
|
|
|
private $conn;
|
2026-01-01 18:57:23 -05:00
|
|
|
|
2026-04-13 20:56:10 -04:00
|
|
|
public function __construct($conn)
|
|
|
|
|
{
|
2026-01-23 10:01:50 -05:00
|
|
|
$this->conn = $conn;
|
2025-05-16 20:02:49 -04:00
|
|
|
$this->ticketModel = new TicketModel($conn);
|
|
|
|
|
$this->commentModel = new CommentModel($conn);
|
2026-01-01 18:25:19 -05:00
|
|
|
$this->auditLogModel = new AuditLogModel($conn);
|
2026-01-01 18:36:34 -05:00
|
|
|
$this->userModel = new UserModel($conn);
|
2026-01-01 18:57:23 -05:00
|
|
|
$this->workflowModel = new WorkflowModel($conn);
|
2026-01-01 19:00:42 -05:00
|
|
|
$this->templateModel = new TemplateModel($conn);
|
2026-09-11 13:12:42 -04:00
|
|
|
$this->customFieldModel = new CustomFieldModel($conn);
|
2025-05-16 20:02:49 -04:00
|
|
|
}
|
2026-04-13 20:56:10 -04:00
|
|
|
|
|
|
|
|
public function view($id)
|
|
|
|
|
{
|
2026-01-01 15:40:32 -05:00
|
|
|
// Get current user
|
|
|
|
|
$currentUser = $GLOBALS['currentUser'] ?? null;
|
|
|
|
|
$userId = $currentUser['user_id'] ?? null;
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Get ticket data
|
|
|
|
|
$ticket = $this->ticketModel->getTicketById($id);
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2026-04-12 00:34:15 -04:00
|
|
|
if (!$ticket || !$this->ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
|
|
|
http_response_code(404);
|
|
|
|
|
include dirname(__DIR__) . '/views/error_404.php';
|
2026-01-23 10:01:50 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 21:34:16 -04:00
|
|
|
// Load first page of comments; show "load more" if ticket has many
|
|
|
|
|
$commentPageSize = 50;
|
|
|
|
|
$totalComments = $this->commentModel->getCommentCount((int)$id);
|
|
|
|
|
$comments = $this->commentModel->getCommentsByTicketId($id, true, $commentPageSize, 0);
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2026-01-01 18:25:19 -05:00
|
|
|
// Get timeline for this ticket
|
|
|
|
|
$timeline = $this->auditLogModel->getTicketTimeline($id);
|
|
|
|
|
|
2026-01-01 18:36:34 -05:00
|
|
|
// Get all users for assignment dropdown
|
|
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
|
|
|
|
|
2026-01-01 18:57:23 -05:00
|
|
|
// Get allowed status transitions for this ticket
|
|
|
|
|
$allowedTransitions = $this->workflowModel->getAllowedTransitions($ticket['status']);
|
|
|
|
|
|
2026-09-11 13:12:42 -04:00
|
|
|
// Custom fields applicable to this ticket's category, with any
|
|
|
|
|
// already-saved values for it
|
|
|
|
|
$customFieldDefs = $this->customFieldModel->getAllDefinitions($ticket['category'], true);
|
|
|
|
|
$customFieldValues = $this->customFieldModel->getValuesForTicket($id);
|
|
|
|
|
|
2026-01-23 10:23:19 -05:00
|
|
|
// Make $conn available to view for visibility groups
|
|
|
|
|
$conn = $this->conn;
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Load the view
|
|
|
|
|
include dirname(__DIR__) . '/views/TicketView.php';
|
|
|
|
|
}
|
2026-01-23 10:23:19 -05:00
|
|
|
|
2026-04-13 20:56:10 -04:00
|
|
|
public function create()
|
|
|
|
|
{
|
2026-01-01 15:40:32 -05:00
|
|
|
// Get current user
|
|
|
|
|
$currentUser = $GLOBALS['currentUser'] ?? null;
|
|
|
|
|
$userId = $currentUser['user_id'] ?? null;
|
|
|
|
|
|
2026-09-11 13:12:42 -04:00
|
|
|
// All active custom field definitions (every category, plus
|
|
|
|
|
// category-less ones) — the create form renders them all and toggles
|
|
|
|
|
// visibility client-side as the Category select changes, since the
|
|
|
|
|
// ticket doesn't exist yet to scope the query to one category.
|
|
|
|
|
$allCustomFieldDefs = $this->customFieldModel->getAllDefinitions(null, true);
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Check if form was submitted
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
2026-03-28 21:26:52 -04:00
|
|
|
// Validate CSRF token
|
|
|
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
|
|
|
$csrfToken = $_POST['csrf_token'] ?? '';
|
|
|
|
|
if (!CsrfMiddleware::validateToken($csrfToken)) {
|
|
|
|
|
$error = "Invalid or expired security token. Please try again.";
|
|
|
|
|
$templates = $this->templateModel->getAllTemplates();
|
|
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
|
|
|
|
$conn = $this->conn;
|
|
|
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 19:13:31 -04:00
|
|
|
// Validation (incl. required custom fields), creation, audit log,
|
|
|
|
|
// stats cache, custom field values, duplicate link and notification
|
|
|
|
|
// live in TicketCreationService so the MCP create_ticket tool runs
|
|
|
|
|
// the same code path.
|
|
|
|
|
require_once dirname(__DIR__) . '/services/TicketCreationService.php';
|
|
|
|
|
$result = TicketCreationService::create($this->conn, $currentUser ?? [], [
|
|
|
|
|
'title' => $_POST['title'] ?? '',
|
2025-05-16 20:02:49 -04:00
|
|
|
'description' => $_POST['description'] ?? '',
|
|
|
|
|
'priority' => $_POST['priority'] ?? '4',
|
|
|
|
|
'category' => $_POST['category'] ?? 'General',
|
2026-01-23 10:01:50 -05:00
|
|
|
'type' => $_POST['type'] ?? 'Issue',
|
2026-09-24 19:13:31 -04:00
|
|
|
'status' => $_POST['status'] ?? 'Open',
|
2026-01-23 10:01:50 -05:00
|
|
|
'visibility' => $_POST['visibility'] ?? 'public',
|
2026-09-24 19:13:31 -04:00
|
|
|
'visibility_groups' => (isset($_POST['visibility_groups']) && is_array($_POST['visibility_groups']))
|
|
|
|
|
? $_POST['visibility_groups'] : null,
|
|
|
|
|
'assigned_to' => $_POST['assigned_to'] ?? null,
|
|
|
|
|
'custom_fields' => $_POST['custom_fields'] ?? [],
|
|
|
|
|
'link_duplicate_of' => $_POST['link_duplicate_of'] ?? '',
|
|
|
|
|
]);
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
if ($result['success']) {
|
|
|
|
|
// Redirect to the new ticket
|
2025-09-05 11:08:56 -04:00
|
|
|
header("Location: " . $GLOBALS['config']['BASE_URL'] . "/ticket/" . $result['ticket_id']);
|
2025-05-16 20:02:49 -04:00
|
|
|
exit;
|
|
|
|
|
} else {
|
|
|
|
|
$error = $result['error'];
|
2026-01-01 19:00:42 -05:00
|
|
|
$templates = $this->templateModel->getAllTemplates();
|
2026-02-05 10:24:00 -05:00
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
2026-01-23 10:01:50 -05:00
|
|
|
$conn = $this->conn; // Make $conn available to view
|
2025-05-16 20:02:49 -04:00
|
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2026-01-01 19:00:42 -05:00
|
|
|
// Get all templates for the template selector
|
|
|
|
|
$templates = $this->templateModel->getAllTemplates();
|
2026-02-05 10:24:00 -05:00
|
|
|
// Get all users for assignment dropdown
|
|
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
2026-01-23 10:01:50 -05:00
|
|
|
$conn = $this->conn; // Make $conn available to view
|
2026-01-01 19:00:42 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Display the create ticket form
|
|
|
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-05 11:08:56 -04:00
|
|
|
}
|