Ticket CCs in the API: ticket_ccs read, create and delete endpoints scoped through the ticket's client, adding one address or several with an optional email to each new CC, removing by id or by address at Modify as on the ticket page, closed tickets left as they are, ticket history and audit lines; tickets/create takes ticket_ccs before its first email goes out and tickets/read returns each ticket's CCs; 26.12.3 changelog

This commit is contained in:
johnnyq
2026-11-30 14:12:08 -05:00
parent b8db50adfa
commit c98ea0e686
10 changed files with 280 additions and 3 deletions

View File

@@ -157,6 +157,7 @@ $resource_module = [
'files' => 'module_support',
'folders' => 'module_support',
'ticket_replies' => 'module_support',
'ticket_ccs' => 'module_support',
'technicians' => 'module_support',
'projects' => 'module_support',
'tasks' => 'module_support',
@@ -248,7 +249,13 @@ if (!isset($resource_module[$resource])) {
apiDeny("This API resource is not mapped to a permission module.");
}
$required_level = $operation_level[$operation_file] ?? 2;
// Where a resource's page asks less than its file's name says - a CC comes off a ticket as an edit of the
// ticket, at Modify, as the x beside them on its page does, not a delete at Full
$resource_operation_level = [
'ticket_ccs' => ['delete.php' => 2],
];
$required_level = $resource_operation_level[$resource][$operation_file] ?? $operation_level[$operation_file] ?? 2;
if ($resource_module[$resource] !== '' && lookupUserPermission($resource_module[$resource]) < $required_level) {
apiDeny(apiPermissionText($resource_module[$resource], $required_level));
}

View File

@@ -30,6 +30,11 @@ if ($sql && mysqli_num_rows($sql) > 0) {
$return_arr['data'] = rmmApiAttach($return_arr['data']);
}
// Each ticket's CCs, as ticket_ccs ($api_ticket_ccs, set by api/v1/tickets/read.php)
if (!empty($api_ticket_ccs)) {
$return_arr['data'] = ticketCcsApiAttach($return_arr['data']);
}
echo json_encode($return_arr);
exit();
}

View File

@@ -0,0 +1,83 @@
<?php
/*
* API - Ticket CCs - Create
* POST /api/v1/ticket_ccs/create.php
*
* Add CCs to a ticket, as Add CC does on its page - each is copied in on every
* email about the ticket from then on, and their replies count as the client's.
* Not on a closed ticket, and never one of ITFlow's own addresses.
*
* Parameters (POST, JSON body):
* api_key required - Your API key
* ticket_id required - The ticket
* ticket_cc_email required - An email address, or several: a list, or one string
* separated by commas
* ticket_cc_notify optional - 1 emails each new CC a link to the ticket now, as
* "Email them a link to this ticket now" does
* client_id the ticket's client - needed when the key's user is limited to
* certain clients
*
* Answer: insert_id (the first added), added - each CC added, with its ticket_cc_id -
* and notified, how many were emailed.
*/
require_once '../validate_api_key.php';
require_once '../require_post_method.php';
// Whether ITFlow sends email ($config_smtp_provider)
require_once "../../../includes/load_global_settings.php";
$insert_id = false;
unset($_POST['ticket_cc_id']);
require_once 'ticket_cc_find.php';
if ($ticket) {
if ($cc_locked) {
apiFormRefuse($cc_locked);
}
$emails = ticketCcEmailsValid($_POST['ticket_cc_email'] ?? '', $cc_bad);
if ($emails === null) {
apiFormRefuse("ticket_cc_email has '$cc_bad', which isn't an email address");
}
if (!$emails) {
apiFormRefuse('ticket_cc_email is needed - an email address, or several');
}
$ticket_id = intval($ticket['ticket_id']);
$reference = ticketReference($ticket);
$added = ticketAddCcs($ticket_id, $emails);
if (!$added) {
apiFormRefuse('No new CCs - ' . implode(', ', $emails) . (count($emails) === 1 ? ' is' : ' are') . " CC'd on ticket $reference already, or ITFlow's own");
}
// Each one added, with its id - to take them off again
$added_sql = implode(',', array_map(function ($email) { return "'" . escapeSql($email) . "'"; }, $added));
$added_rows = [];
$sql = mysqli_query($mysqli, "SELECT ticket_cc_id, ticket_cc_email FROM ticket_ccs WHERE ticket_cc_ticket_id = $ticket_id AND ticket_cc_email IN ($added_sql) ORDER BY ticket_cc_id");
while ($row = mysqli_fetch_assoc($sql)) {
$added_rows[] = ['ticket_cc_id' => intval($row['ticket_cc_id']), 'ticket_cc_email' => $row['ticket_cc_email']];
}
$insert_id = $added_rows[0]['ticket_cc_id'] ?? false;
$return_arr['added'] = $added_rows;
// Each told, when asked and ITFlow sends email - as the box on Add CC, shown only then
$notified = 0;
if (intval($_POST['ticket_cc_notify'] ?? 0) === 1 && !empty($config_smtp_provider)) {
$notified = ticketMailCcsAdded($ticket_id, $added, stripslashes($session_name));
}
$return_arr['notified'] = $notified;
$reference_sql = escapeSql($reference);
foreach ($added as $cc_email) {
$cc_email_sql = escapeSql($cc_email);
logTicketHistory($ticket_id, "$session_name added $cc_email_sql as a CC via the API ($api_key_name)");
logAudit("Ticket", "Edit", "Added $cc_email_sql as a CC on ticket $reference_sql via API ($api_key_name)", $client_id, $ticket_id);
}
logAudit("API", "Success", "Added " . count($added) . " CC(s) to ticket $reference_sql via API ($api_key_name)", $client_id);
}
// Output
require_once '../create_output.php';

View File

@@ -0,0 +1,58 @@
<?php
/*
* API - Ticket CCs - Delete
* POST /api/v1/ticket_ccs/delete.php
*
* Take a CC off a ticket, as the x beside them on its page does - they get no
* more of its emails. Needs Modify on Support, as on the page, not Full. Not on
* a closed ticket.
*
* Parameters (POST, JSON body):
* api_key required - Your API key
* ticket_cc_id The CC - this, or ticket_id and ticket_cc_email
* ticket_id, ticket_cc_email The ticket, and the CC's email address
* client_id the ticket's client - needed when the key's user is limited to
* certain clients
*/
require_once '../validate_api_key.php';
require_once '../require_post_method.php';
$delete_count = false;
require_once 'ticket_cc_find.php';
// By the ticket and the address, without the CC's id
if ($ticket && !$cc_row && empty($_POST['ticket_cc_id'])) {
$cc_email = strtolower(trim(is_scalar($_POST['ticket_cc_email'] ?? '') ? (string) ($_POST['ticket_cc_email'] ?? '') : ''));
if ($cc_email === '') {
apiFormRefuse('ticket_cc_id is needed, or ticket_id and ticket_cc_email');
}
$cc_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT ticket_cc_id, ticket_cc_name, ticket_cc_email, ticket_cc_ticket_id FROM ticket_ccs
WHERE ticket_cc_ticket_id = " . intval($ticket['ticket_id']) . " AND ticket_cc_email = '" . escapeSql($cc_email) . "' LIMIT 1"));
if (!$cc_row) {
apiFormRefuse("$cc_email isn't a CC on ticket " . ticketReference($ticket));
}
}
if ($ticket && $cc_row) {
if ($cc_locked) {
apiFormRefuse($cc_locked);
}
$ticket_id = intval($ticket['ticket_id']);
mysqli_query($mysqli, "DELETE FROM ticket_ccs WHERE ticket_cc_id = " . intval($cc_row['ticket_cc_id']));
$delete_count = mysqli_affected_rows($mysqli);
if ($delete_count > 0) {
$cc_email_sql = escapeSql($cc_row['ticket_cc_email']);
$reference_sql = escapeSql(ticketReference($ticket));
logTicketHistory($ticket_id, "$session_name removed $cc_email_sql as a CC via the API ($api_key_name)");
logAudit("Ticket", "Edit", "Removed $cc_email_sql as a CC on ticket $reference_sql via API ($api_key_name)", $client_id, $ticket_id);
logAudit("API", "Success", "Removed $cc_email_sql as a CC on ticket $reference_sql via API ($api_key_name)", $client_id);
}
}
// Output
require_once '../delete_output.php';

View File

@@ -0,0 +1,34 @@
<?php
/*
* API - Ticket CCs - Read
* GET /api/v1/ticket_ccs/read.php
*
* A ticket's CCs - the people copied in on every email about it, whose replies
* count as the client's - as they were added, or one CC.
*
* ticket_id This ticket's CCs - this or ticket_cc_id is needed
* ticket_cc_id Just this CC
* client_id optional - Only if the ticket is this client's
*/
require_once '../validate_api_key.php';
require_once '../require_get_method.php';
$ticket_id = intval($_GET['ticket_id'] ?? 0);
$cc_id = intval($_GET['ticket_cc_id'] ?? 0);
if (!$ticket_id && !$cc_id) {
http_response_code(400);
$return_arr['success'] = "False";
$return_arr['message'] = "ticket_ccs/read.php needs ticket_id or ticket_cc_id.";
echo json_encode($return_arr);
exit();
}
$sql = mysqli_query($mysqli, "SELECT ticket_ccs.*, ticket_prefix, ticket_number, ticket_client_id
FROM ticket_ccs JOIN tickets ON ticket_id = ticket_cc_ticket_id
WHERE " . ($cc_id ? "ticket_cc_id = $cc_id" : "ticket_cc_ticket_id = $ticket_id") . " " . apiClientScopeSql('ticket_client_id') . "
ORDER BY ticket_cc_id");
// Output
require_once "../read_output.php";

View File

@@ -0,0 +1,29 @@
<?php
// The ticket a request names (ticket_id), or the CC (ticket_cc_id) and its ticket - of a client
// the key's user can reach, and client_id's when it is sent - as $ticket and $cc_row (null when
// not found). $client_id becomes the ticket's client. A closed ticket keeps its CCs as they are,
// as on its page - $cc_locked says why.
if (!function_exists('apiDeny')) {
http_response_code(404);
exit();
}
require_once '../form_input.php';
$cc_row = !empty($_POST['ticket_cc_id']) ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT ticket_cc_id, ticket_cc_name, ticket_cc_email, ticket_cc_ticket_id
FROM ticket_ccs WHERE ticket_cc_id = " . intval($_POST['ticket_cc_id']))) : null;
$cc_ticket_id = $cc_row ? intval($cc_row['ticket_cc_ticket_id']) : intval($_POST['ticket_id'] ?? 0);
$ticket = $cc_ticket_id ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT ticket_id, ticket_prefix, ticket_number, ticket_subject, ticket_status, ticket_client_id, ticket_closed_at
FROM tickets WHERE ticket_id = $cc_ticket_id " . clientScopeSql('ticket_client_id') . ($client_id ? " AND ticket_client_id = $client_id" : '') . " LIMIT 1")) : null;
$cc_locked = '';
if ($ticket) {
$client_id = intval($ticket['ticket_client_id']);
if (!empty($ticket['ticket_closed_at']) || intval($ticket['ticket_status']) === TICKET_STATUS_CLOSED) {
$cc_locked = 'Ticket ' . ticketReference($ticket) . ' is closed - its CCs stay as they are';
}
} else {
$cc_row = null;
}

View File

@@ -12,8 +12,11 @@
* starts it in an open status of your choosing - On Hold, say - and
* ticket_invoice_id ties it to one of the client's invoices: once that is paid
* in full the ticket gets a note, goes from On Hold back to Open, and its agent
* is told (invoicePaidFollowUp()). The answer has the ticket's number as people
* see it and where its contact opens it.
* is told (invoicePaidFollowUp()). ticket_ccs are its CCs, as New Ticket's CC
* field - email addresses, a list or one string separated by commas - on it
* before anyone is told, so ticket_notify's email goes to them too. The answer
* has the ticket's number as people see it, where its contact opens it, and the
* CCs added when ticket_ccs was sent.
*/
require_once '../validate_api_key.php';
@@ -97,6 +100,15 @@ if ($invoice_link && (!$client_id || !mysqli_num_rows(mysqli_query($mysqli, "SEL
$ticket_refuse("ticket_invoice_id $invoice_link isn't one of this client's invoices.");
}
// Its CCs, as New Ticket's CC field - each an email address, or it isn't raised
$ccs_posted = null;
if (array_key_exists('ticket_ccs', $_POST)) {
$ccs_posted = ticketCcEmailsValid($_POST['ticket_ccs'], $cc_bad);
if ($ccs_posted === null) {
$ticket_refuse("ticket_ccs has '$cc_bad', which isn't an email address - send a list of addresses, or one string separated by commas.");
}
}
// A client that exists, or none (internal work)
$client_exists = !$client_id || mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM clients WHERE client_id = " . intval($client_id)));
@@ -148,6 +160,9 @@ if (!empty($subject) && $client_exists) {
// Pictures in its details as data kept as its files (functions/ticket_pictures.php)
ticketPicturesSaveTicket($insert_id);
// Its CCs - on it before anyone is told, so its first email copies them in. Never ITFlow's own addresses.
$ccs_added = $ccs_posted ? ticketAddCcs($insert_id, $ccs_posted) : [];
// Part of a project
$project = $ticket_project_id ? projectRow($ticket_project_id) : null;
if ($project) {
@@ -173,6 +188,9 @@ if (!empty($subject) && $client_exists) {
$new_row = ticketRow($insert_id);
$return_arr['ticket_reference'] = ticketReference($new_row);
$return_arr['ticket_url'] = ticketEmailUrl($new_row);
if ($ccs_posted !== null) {
$return_arr['ticket_ccs'] = $ccs_added;
}
if ($template) {
$return_arr['ticket_tasks'] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM tasks WHERE task_ticket_id = " . intval($insert_id)))[0]);
}

View File

@@ -22,5 +22,6 @@ if (isset($_GET['ticket_id'])) {
// Output
$api_tag_entity = 'ticket'; // each one's tags
$api_ticket_ccs = true; // and its CCs
require_once "../read_output.php";