Tickets: Email reply has a To line starting with the contact, watchers and colleagues who asked for every update, where the assigned agent, the client's other contacts or a typed address can be added and anyone taken out, and the choice is kept per ticket for its next email reply; moving a ticket to another client takes off watchers who are neither agents nor the new client's contacts; database 3.7.5 adds ticket_mail_choices; 26.11.9 changelog entries

This commit is contained in:
johnnyq
2026-11-17 17:54:20 -05:00
parent 7105d00b77
commit c8fa1ba3b4
8 changed files with 352 additions and 13 deletions

View File

@@ -1198,10 +1198,15 @@ if (isset($_POST['add_ticket_reply'])) {
// Email the client and the watchers - the reply keeps who it went to, for the conversation
$emailed_to = 0;
$email_to = null;
if ($send_email) {
// Only the people in its To line, when the reply box had one - kept for the ticket (functions/ticket_reply_recipients.php)
if (!empty($_POST['email_to_shown']) && ticketReplyRecipientsReady()) {
$email_to = ticketReplyRecipientsPick($ticket_id, (array) ($_POST['email_to'] ?? []));
}
// The reply as saved - with the signature - read back
$emailed_to = ticketMailReply($ticket_id, (string) getFieldById('ticket_replies', $ticket_reply_id, 'ticket_reply'), stripslashes($session_name),
$ticket_status === TICKET_STATUS_RESOLVED, $emailable_attachments['send'], $ticket_reply_id);
$ticket_status === TICKET_STATUS_RESOLVED, $emailable_attachments['send'], $ticket_reply_id, $email_to);
// And a text saying so, to a contact who agreed to texts - when Texts to clients says each reply (functions/sms.php)
if ($emailed_to) {
smsTicketReplyNotify($row);
@@ -1245,7 +1250,7 @@ if (isset($_POST['add_ticket_reply'])) {
flashAlert("Reply added to ticket $reference_html and emailed to <strong>$emailed_to</strong> " . ($emailed_to === 1 ? 'person' : 'people'));
} else {
// They chose to email it - say plainly that it didn't go
flashAlert("Reply added to ticket $reference_html but not emailed - " . (emailReady() ? 'there\'s no contact or watcher with an email address to send it to' : 'mail isn\'t set up'), 'warning');
flashAlert("Reply added to ticket $reference_html but not emailed - " . (!emailReady() ? 'mail isn\'t set up' : ($email_to === [] ? 'nobody was in To' : 'there\'s no contact or watcher with an email address to send it to')), 'warning');
}
} else {
@@ -1460,6 +1465,13 @@ if (isset($_POST['change_client_ticket'])) {
mysqli_query($mysqli, "UPDATE tickets SET ticket_client_id = $client_id, ticket_contact_id = $contact_id, ticket_asset_id = 0, ticket_location_id = 0,
ticket_vendor_id = $vendor_id, ticket_project_id = $project_id WHERE ticket_id = $ticket_id LIMIT 1");
mysqli_query($mysqli, "DELETE FROM ticket_assets WHERE ticket_id = $ticket_id");
// Nobody at the old client hears about it any more: its watchers come off - all but our own
// agents and the new client's contacts - and its email replies' To line starts again
mysqli_query($mysqli, "DELETE FROM ticket_watchers WHERE watcher_ticket_id = $ticket_id
AND watcher_email NOT IN (SELECT user_email FROM users WHERE user_type = 1 AND user_archived_at IS NULL AND user_email IS NOT NULL)
AND watcher_email NOT IN (SELECT contact_email FROM contacts WHERE contact_client_id = $client_id AND contact_archived_at IS NULL AND contact_email IS NOT NULL)");
$watchers_off = max(0, mysqli_affected_rows($mysqli));
ticketReplyRecipientsForget($ticket_id);
// The driving for it goes with it - not to the old client's site
mysqli_query($mysqli, "UPDATE trips SET trip_client_id = $client_id, trip_location_id = 0 WHERE trip_ticket_id = $ticket_id");
applyTicketSla($ticket_id);
@@ -1467,13 +1479,15 @@ if (isset($_POST['change_client_ticket'])) {
$old_client_name = $old_client_id ? (string) getFieldById('clients', $old_client_id, 'client_name') : 'no client';
$new_client_name = (string) getFieldById('clients', $client_id, 'client_name');
logTicketHistory($ticket_id, "$session_name moved the ticket from " . escapeSql($old_client_name) . " to " . escapeSql($new_client_name));
logTicketHistory($ticket_id, "$session_name moved the ticket from " . escapeSql($old_client_name) . " to " . escapeSql($new_client_name)
. ($watchers_off ? " - $watchers_off watcher" . ($watchers_off === 1 ? '' : 's') . " of the old client taken off" : ''));
logAudit("Ticket", "Change", "$session_name moved ticket " . escapeSql(ticketReference($row)) . " from " . escapeSql($old_client_name) . " to " . escapeSql($new_client_name), $client_id, $ticket_id);
triggerCustomAction('ticket_update', $ticket_id);
flashAlert("Ticket <strong>" . escapeHtml(ticketReference($row)) . "</strong> moved to <strong>" . escapeHtml($new_client_name) . "</strong>");
flashAlert("Ticket <strong>" . escapeHtml(ticketReference($row)) . "</strong> moved to <strong>" . escapeHtml($new_client_name) . "</strong>"
. ($watchers_off ? " - $watchers_off watcher" . ($watchers_off === 1 ? '' : 's') . " of the old client taken off" : ''));
redirect(ticketUrl($ticket_id, $client_id));

View File

@@ -451,6 +451,8 @@ if (isset($_GET['ticket_id'])) {
// An emailed reply needs an address to go to and mail set up to send it -
// without mail configured the option used to save as public and email nobody
$can_email_reply = !empty($contact_email) && !empty($config_smtp_provider);
// Who it goes to - the To line, kept for the ticket (functions/ticket_reply_recipients.php)
$reply_to = $can_email_reply && $can_edit_ticket && $ticket_is_open && ticketReplyRecipientsReady() ? ticketReplyRecipients($ticket_id) : [];
// A text reply - to its contact's mobile, or the number texting on it (functions/sms.php)
$sms_recipient = smsTicketRecipient($ticket);
@@ -839,7 +841,11 @@ if (isset($_GET['ticket_id'])) {
<div class="ticket-reply-audience small mb-3" id="replyAudience">
<div data-audience="0" hidden><i class="fas fa-fw fa-lock me-1"></i>Only your team can see this.</div>
<div data-audience="1" hidden><i class="fas fa-fw fa-eye me-1"></i>The client can see this in the portal. <strong>No email is sent.</strong></div>
<?php if ($can_email_reply) { ?>
<?php if ($reply_to) { ?>
<div data-audience="2" hidden>
<i class="fas fa-fw fa-paper-plane me-1"></i>Emails the people in To, and shows in the portal.
</div>
<?php } elseif ($can_email_reply) { ?>
<div data-audience="2" hidden>
<i class="fas fa-fw fa-paper-plane me-1"></i>Emails <strong><?= $contact_name ?></strong> &lt;<?= $contact_email ?>&gt;<?php if ($watcher_count) { ?> and <?= $watcher_count ?> watcher<?= $watcher_count == 1 ? '' : 's' ?><?php } ?>, and shows in the portal.
</div>
@@ -853,6 +859,27 @@ if (isset($_GET['ticket_id'])) {
<?php } ?>
</div>
<?php if ($reply_to) { ?>
<!--
* Who an email reply goes to: the contact, watchers and colleagues to start
* with; anyone else here, or an address typed in, can be added. Whatever is
* changed is kept for the ticket, so its next email reply starts from it.
-->
<div class="mb-3" id="replyTo" hidden>
<input type="hidden" name="email_to_shown" value="1" disabled>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-envelope me-1"></i>To</span>
<select class="form-select select2" name="email_to[]" id="replyToSelect" multiple data-tags="true" data-avatar="initials" data-placeholder="Type a name or an email address" aria-label="Email it to">
<?php foreach ($reply_to as $reply_to_email => $reply_to_person) { ?>
<option value="<?= escapeHtml($reply_to_email) ?>"<?= $reply_to_person['send'] ? ' selected' : '' ?><?= $reply_to_person['look'] ?><?= selectOptionData(['description' => TICKET_REPLY_TO_GROUPS[$reply_to_person['group']] . ($reply_to_person['name'] !== '' ? " - $reply_to_email" : ''), 'keywords' => $reply_to_email]) ?>><?= escapeHtml($reply_to_person['name'] !== '' ? $reply_to_person['name'] : $reply_to_email) ?></option>
<?php } ?>
</select>
</div>
<div class="invalid-feedback d-block" id="replyToProblem" hidden></div>
<div class="form-text">Who's in To is kept for this ticket - its next email reply starts with the same people.</div>
</div>
<?php } ?>
<?php
/*
* Canned responses offered on this ticket: the agent's own, then
@@ -2101,6 +2128,40 @@ if (isset($ticket_is_open) && empty($ticket_closed_at)) { ?>
if (replySubmitLabel) {
replySubmitLabel.textContent = replySubmitLabels[type] || 'Send';
}
// The To line is for an email reply - and only then is it sent with the reply
if (replyTo) {
replyTo.hidden = type !== '2';
replyTo.querySelector('[name="email_to_shown"]').disabled = type !== '2';
replyToProblem.hidden = true;
}
}
// What's wrong with the To line, if anything - nobody in it, or something that isn't an email address
const replyTo = document.getElementById('replyTo');
const replyToSelect = document.getElementById('replyToSelect');
const replyToProblem = document.getElementById('replyToProblem');
function replyToCheck() {
if (!replyTo || replyTo.hidden) {
return '';
}
const picked = Array.from(replyToSelect.selectedOptions).map(option => option.value.trim());
const wrong = picked.filter(value => !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value));
if (!picked.length) {
return 'Put someone in To - or pick Public note to post it without an email.';
}
if (wrong.length) {
return 'Not an email address: ' + wrong.join(', ') + ' - take it out of To, or type the whole address.';
}
return '';
}
if (replyToSelect) {
replyToSelect.addEventListener('change', function () {
if (!replyToProblem.hidden) {
const problem = replyToCheck();
replyToProblem.textContent = problem;
replyToProblem.hidden = problem === '';
}
});
}
// Only ever opens. Switching Internal -> Public mid-draft must not throw the draft away.
@@ -2140,6 +2201,14 @@ if (isset($ticket_is_open) && empty($ticket_closed_at)) { ?>
replyForm.addEventListener('submit', function (e) {
if (!replyComposer.classList.contains('show')) {
e.preventDefault();
return;
}
const problem = replyToCheck();
if (problem) {
e.preventDefault();
replyToProblem.textContent = problem;
replyToProblem.hidden = false;
replyTo.scrollIntoView({ block: 'nearest' });
}
});
}