Extract ticket creation from TicketController into TicketCreationService (#111)
Required-field and required-custom-field validation, createTicket (which enforces visibility rules), the audit log, stats-cache invalidation, custom field values, the optional 'duplicates' link and the new-ticket notification move into services/TicketCreationService.php with the same order and error messages, so the MCP create_ticket tool runs one code path with the web form. TicketController::create keeps CSRF, the redirect, and re-rendering the form on error (the view never read the removed locals). The service also accepts visibility_groups as a string for API callers; the controller still passes only the form's array, so web behaviour is unchanged. Verified the real web form over HTTP (logged in via Remote-User, real CSRF token): a valid submit redirects to the new ticket, created and audit-logged as the user; a blank description re-renders the form with 'Description is required' and creates nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MGDKHiU5RJdo3dqQUDow3X
This commit is contained in:
@@ -101,115 +101,27 @@ class TicketController
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle visibility groups (comes as array from checkboxes)
|
||||
$visibilityGroups = null;
|
||||
if (isset($_POST['visibility_groups']) && is_array($_POST['visibility_groups'])) {
|
||||
$visibilityGroups = implode(',', array_map('trim', $_POST['visibility_groups']));
|
||||
}
|
||||
|
||||
// Honor the posted status, validated against the app's canonical list
|
||||
$validStatuses = $GLOBALS['config']['TICKET_STATUSES'] ?? ['Open', 'Pending', 'In Progress', 'Closed'];
|
||||
$status = $_POST['status'] ?? 'Open';
|
||||
if (!in_array($status, $validStatuses, true)) {
|
||||
$status = 'Open';
|
||||
}
|
||||
|
||||
$ticketData = [
|
||||
'title' => trim($_POST['title'] ?? ''),
|
||||
// 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'] ?? '',
|
||||
'description' => $_POST['description'] ?? '',
|
||||
'priority' => $_POST['priority'] ?? '4',
|
||||
'category' => $_POST['category'] ?? 'General',
|
||||
'type' => $_POST['type'] ?? 'Issue',
|
||||
'status' => $status,
|
||||
'status' => $_POST['status'] ?? 'Open',
|
||||
'visibility' => $_POST['visibility'] ?? 'public',
|
||||
'visibility_groups' => $visibilityGroups,
|
||||
'assigned_to' => !empty($_POST['assigned_to']) ? $_POST['assigned_to'] : null
|
||||
];
|
||||
|
||||
// Validate input (server-side; form is novalidate)
|
||||
if ($ticketData['title'] === '') {
|
||||
$error = "Title is required";
|
||||
$templates = $this->templateModel->getAllTemplates();
|
||||
$allUsers = $this->userModel->getAllUsers();
|
||||
$conn = $this->conn; // Make $conn available to view
|
||||
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
||||
return;
|
||||
}
|
||||
|
||||
if (trim($ticketData['description']) === '') {
|
||||
$error = "Description is required";
|
||||
$templates = $this->templateModel->getAllTemplates();
|
||||
$allUsers = $this->userModel->getAllUsers();
|
||||
$conn = $this->conn; // Make $conn available to view
|
||||
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
||||
return;
|
||||
}
|
||||
|
||||
// Custom fields applicable to the submitted category — validate
|
||||
// is_required server-side (the form is novalidate, and a field
|
||||
// hidden by the client-side category toggle must not silently
|
||||
// bypass a requirement that applies to the category actually
|
||||
// submitted).
|
||||
$submittedCustomFields = is_array($_POST['custom_fields'] ?? null) ? $_POST['custom_fields'] : [];
|
||||
$applicableFieldDefs = array_filter(
|
||||
$allCustomFieldDefs,
|
||||
fn($def) => $def['category'] === null || $def['category'] === $ticketData['category']
|
||||
);
|
||||
$customFieldsToSave = [];
|
||||
foreach ($applicableFieldDefs as $def) {
|
||||
$fieldId = (int)$def['field_id'];
|
||||
$raw = $submittedCustomFields[$fieldId] ?? null;
|
||||
$normalized = $def['field_type'] === 'checkbox'
|
||||
? (!empty($raw) ? '1' : '0')
|
||||
: (is_scalar($raw) ? trim((string)$raw) : '');
|
||||
|
||||
if (!empty($def['is_required']) && $def['field_type'] !== 'checkbox' && $normalized === '') {
|
||||
$error = $def['field_label'] . ' is required';
|
||||
$templates = $this->templateModel->getAllTemplates();
|
||||
$allUsers = $this->userModel->getAllUsers();
|
||||
$conn = $this->conn;
|
||||
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
||||
return;
|
||||
}
|
||||
|
||||
if ($normalized !== '') {
|
||||
$customFieldsToSave[$fieldId] = $normalized;
|
||||
}
|
||||
}
|
||||
|
||||
// Create ticket with user tracking
|
||||
$result = $this->ticketModel->createTicket($ticketData, $userId);
|
||||
'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'] ?? '',
|
||||
]);
|
||||
|
||||
if ($result['success']) {
|
||||
// Log ticket creation to audit log
|
||||
if (isset($GLOBALS['auditLog']) && $userId) {
|
||||
$GLOBALS['auditLog']->logTicketCreate($userId, $result['ticket_id'], $ticketData);
|
||||
}
|
||||
|
||||
// Ticket counts changed — invalidate the cached dashboard stats
|
||||
require_once dirname(__DIR__) . '/models/StatsModel.php';
|
||||
(new StatsModel($this->conn))->invalidateCache();
|
||||
|
||||
// Persist custom field values for the fields applicable to
|
||||
// this ticket's category
|
||||
if (!empty($customFieldsToSave)) {
|
||||
$this->customFieldModel->setValues($result['ticket_id'], $customFieldsToSave);
|
||||
}
|
||||
|
||||
// Auto-link as duplicate if requested from create form
|
||||
$linkDupOfRaw = trim($_POST['link_duplicate_of'] ?? '');
|
||||
if ($linkDupOfRaw !== '' && ctype_digit($linkDupOfRaw)) {
|
||||
$depSql = "INSERT IGNORE INTO ticket_dependencies (ticket_id, depends_on_id, dependency_type, created_by)
|
||||
VALUES (?, ?, 'duplicates', ?)";
|
||||
$depStmt = $this->conn->prepare($depSql);
|
||||
$depStmt->bind_param("ssi", $result['ticket_id'], $linkDupOfRaw, $userId);
|
||||
$depStmt->execute();
|
||||
$depStmt->close();
|
||||
}
|
||||
|
||||
// Send Matrix notification for new ticket
|
||||
NotificationHelper::sendTicketNotification($result['ticket_id'], $ticketData, 'manual');
|
||||
|
||||
// Redirect to the new ticket
|
||||
header("Location: " . $GLOBALS['config']['BASE_URL'] . "/ticket/" . $result['ticket_id']);
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user