mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 09:09:01 +00:00
240 lines
12 KiB
PHP
240 lines
12 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Your own wording for the emails about a ticket to clients - the 13 under "Tickets - to clients" in Admin > Settings >
|
|
* Mail > Previews, each changed there with Edit Wording (admin/modals/email_template/email_template_edit.php,
|
|
* admin/post/email_template.php). Database 4.3.4, table email_templates, by its preview's key.
|
|
*
|
|
* Each can have its own subject, heading, opening text and closing text - plain text with the words in square brackets
|
|
* filled in, as canned responses are: [Ticket], [Subject], [Contact], [First Name], [Client], [Agent], [Company],
|
|
* [Status], [Priority], and the email's own (EMAIL_TEMPLATES' extra) - [By], [When], [Task] and so on. A part not
|
|
* changed - NULL, or saved as ITFlow's own words - is ITFlow's own, as before. The opening and closing text can be
|
|
* left empty to leave them out; a blank line starts a new paragraph. The message, the summary, the button and the
|
|
* earlier messages are always there.
|
|
*
|
|
* The subject of every email about a ticket starts with its number in brackets - [TCK-123] - however it is worded, so
|
|
* a reply finds its ticket and the client's mail program keeps them together. To someone who isn't a contact, the
|
|
* "we have your email" answer keeps its fixed subject, and [Subject] is left empty in it: anyone can send an email
|
|
* that says it is from someone else, and their words would go to that person.
|
|
*
|
|
* Applied by emailTemplateApply() where each is made - ticketEmailContent(), ticketMailCcsAdded(),
|
|
* ticketMailColleaguesNew(), ticketMailTaskApproval() and ticketMailClosedReply() (functions/ticket.php) - and the
|
|
* subject by emailTemplateSubject(). The default words here are ITFlow's own, written with the placeholders - kept in
|
|
* step with those functions by hand.
|
|
*/
|
|
|
|
const EMAIL_TEMPLATE_PLACEHOLDERS = [
|
|
'Ticket' => 'its number - TCK-123',
|
|
'Subject' => "the ticket's subject",
|
|
'Contact' => "the contact's name",
|
|
'First Name' => "the contact's first name",
|
|
'Client' => "the client's name",
|
|
'Agent' => 'the agent it is assigned to',
|
|
'Company' => 'your company',
|
|
'Status' => "the ticket's status",
|
|
'Priority' => "the ticket's priority",
|
|
];
|
|
|
|
const EMAIL_TEMPLATES = [
|
|
'ticket_created' => [
|
|
'title' => 'We have opened a ticket for you',
|
|
'intro' => 'We have opened ticket [Ticket] for you. We will keep you posted here - to add anything, just reply to this email.',
|
|
],
|
|
'ticket_created_email' => [
|
|
'title' => 'We have your email',
|
|
'intro' => 'Thank you - it is ticket [Ticket] now, and we will reply to you here. To add anything, just reply to this email.',
|
|
'note' => 'To someone who isn\'t a contact, the subject is always [TCK-123] We have your email, and [Subject] is left out - anyone can send an email that says it is from someone else.',
|
|
],
|
|
'ticket_created_scheduled' => [
|
|
'title' => 'A scheduled ticket was opened for you',
|
|
'intro' => 'Ticket [Ticket] was opened for you, as scheduled. We will keep you posted here - to add anything, just reply to this email.',
|
|
],
|
|
'ticket_reply' => [
|
|
'title' => '[Subject]',
|
|
'intro' => 'There is a new reply on your ticket [Ticket].',
|
|
'after' => 'To answer, just reply to this email.',
|
|
'extra' => ['By' => 'who replied'],
|
|
],
|
|
'ticket_resolved' => [
|
|
'title' => 'Your ticket is resolved',
|
|
'intro' => "We have marked ticket [Ticket] resolved.\n\nIf that sorts it, there is nothing more to do - it closes by itself after [Closes After]. If not, reply to this email or reopen it and tell us what is still wrong.",
|
|
'extra' => ['By' => 'who resolved it', 'Closes After' => 'how long until it closes by itself - 3 days'],
|
|
'note' => 'Resolve - no response sends the agent\'s message in place of the opening text.',
|
|
],
|
|
'ticket_closed' => [
|
|
'title' => 'Your ticket is closed',
|
|
'intro' => 'Ticket [Ticket] is closed. We hope it was sorted the way you wanted.',
|
|
'after' => 'Need more help with this? Raise a new ticket - a reply to this email isn\'t acted on, as the ticket is closed.',
|
|
],
|
|
'ticket_client_reply' => [
|
|
'title' => '[Subject]',
|
|
'intro' => '[By] replied on ticket [Ticket].',
|
|
'after' => 'To answer, just reply to this email.',
|
|
'extra' => ['By' => 'who replied'],
|
|
],
|
|
'ticket_cc' => [
|
|
'title' => 'You were added to a ticket',
|
|
'intro' => '[By] added you to ticket [Ticket] for [Client], so you will get its updates. To add anything, just reply to this email.',
|
|
'extra' => ['By' => 'who added them'],
|
|
],
|
|
'ticket_colleague_new' => [
|
|
'title' => 'New ticket at [Client]',
|
|
'intro' => '[Raised By] raised a ticket at [Client].',
|
|
'extra' => ['Raised By' => 'who raised it - We, when it was your team'],
|
|
],
|
|
'ticket_approval' => [
|
|
'subject' => 'Ticket task approval required - [Subject]',
|
|
'title' => 'Your approval is needed',
|
|
'intro' => '[By] needs your approval for a task on ticket [Ticket] before the work goes ahead.',
|
|
'after' => 'Want to know more first? Just reply to this email.',
|
|
'extra' => ['By' => 'who asked', 'Task' => 'the task to approve'],
|
|
],
|
|
'ticket_scheduled' => [
|
|
'title' => 'Your ticket is booked in',
|
|
'intro' => 'We have booked the work on ticket [Ticket] for [When]. The calendar file with this email adds it to your calendar.',
|
|
'extra' => ['When' => 'when it is booked for', 'Where' => 'where - the site\'s address, or Remote'],
|
|
],
|
|
'ticket_schedule_cancelled' => [
|
|
'title' => 'The booked work is cancelled',
|
|
'intro' => 'The work booked on ticket [Ticket] is cancelled - the calendar file with this email takes it out of your calendar. We will be in touch to book it again.',
|
|
],
|
|
'ticket_closed_reply' => [
|
|
'title' => 'That ticket is closed',
|
|
'intro' => "You replied to ticket [Ticket] - \"[Subject]\" - which is closed. Your reply is kept with it, but the ticket isn't reopened, so nobody will act on it.\n\nTo raise a new ticket, send a new email to [Support Email].",
|
|
'extra' => ['Support Email' => 'the ticket mailbox\'s address'],
|
|
],
|
|
];
|
|
|
|
/** The parts of a template, and the longest each may be. */
|
|
const EMAIL_TEMPLATE_PARTS = ['subject' => 255, 'title' => 255, 'intro' => 5000, 'after' => 5000];
|
|
|
|
/** Whether the 4.3.4 update has run - email_templates is there. */
|
|
function emailTemplateReady() {
|
|
return function_exists('guestLinkReady') && guestLinkReady();
|
|
}
|
|
|
|
/** A template's own default words for $part - its subject, heading, opening or closing text - with the placeholders. */
|
|
function emailTemplateDefault($key, $part) {
|
|
if (!isset(EMAIL_TEMPLATES[$key])) {
|
|
return '';
|
|
}
|
|
if ($part === 'subject') {
|
|
return EMAIL_TEMPLATES[$key]['subject'] ?? '[Subject]';
|
|
}
|
|
return (string) (EMAIL_TEMPLATES[$key][$part] ?? '');
|
|
}
|
|
|
|
/** The wording saved for every template - key => [part => text, or null for ITFlow's own], read once. */
|
|
function emailTemplatesSaved($reload = false) {
|
|
global $mysqli;
|
|
static $saved = null;
|
|
if ($saved === null || $reload) {
|
|
$saved = [];
|
|
if (emailTemplateReady()) {
|
|
$sql = mysqli_query($mysqli, "SELECT email_template_key, email_template_subject, email_template_title, email_template_intro, email_template_after FROM email_templates");
|
|
while ($sql && ($row = mysqli_fetch_assoc($sql))) {
|
|
$saved[(string) $row['email_template_key']] = ['subject' => $row['email_template_subject'], 'title' => $row['email_template_title'],
|
|
'intro' => $row['email_template_intro'], 'after' => $row['email_template_after']];
|
|
}
|
|
}
|
|
}
|
|
return $saved;
|
|
}
|
|
|
|
/** Whether a template has any wording of your own. */
|
|
function emailTemplateEdited($key) {
|
|
return !empty(array_filter(emailTemplatesSaved()[$key] ?? [], function ($text) { return $text !== null; }));
|
|
}
|
|
|
|
/** The placeholders a template takes - name => what it becomes. */
|
|
function emailTemplatePlaceholders($key) {
|
|
return EMAIL_TEMPLATE_PLACEHOLDERS + (array) (EMAIL_TEMPLATES[$key]['extra'] ?? []);
|
|
}
|
|
|
|
/*
|
|
* The values for a ticket's placeholders - lower case name => raw text - with $extra (name => value) for the email's
|
|
* own. Every one a template takes is there, empty when there is nothing to put in, so none is left showing.
|
|
*/
|
|
function emailTemplateValues(array $ticket, array $extra = []) {
|
|
global $mysqli;
|
|
$values = ticketPlaceholderValues($ticket['ticket_id'] ?? 0);
|
|
$contact = trim((string) ($ticket['contact_name'] ?? $values['contact'] ?? ''));
|
|
$agent = trim((string) ($ticket['user_name'] ?? ''));
|
|
if ($agent === '' && intval($ticket['ticket_assigned_to'] ?? 0)) {
|
|
$agent = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT user_name FROM users WHERE user_id = " . intval($ticket['ticket_assigned_to'])))[0] ?? '');
|
|
}
|
|
$out = [
|
|
'ticket' => ticketReference($ticket),
|
|
'subject' => (string) ($ticket['ticket_subject'] ?? ''),
|
|
'contact' => $contact,
|
|
'first name' => $contact !== '' ? preg_split('/\s+/u', $contact)[0] : '',
|
|
'client' => (string) ($ticket['client_name'] ?? $values['client'] ?? ''),
|
|
'agent' => $agent,
|
|
'company' => (string) (billingCompany()['company_name'] ?? ''),
|
|
'status' => (string) ($ticket['ticket_status_name'] ?? ''),
|
|
'priority' => (string) ($ticket['ticket_priority'] ?? ''),
|
|
];
|
|
foreach ($extra as $name => $value) {
|
|
$out[strtolower($name)] = (string) $value;
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
/* $text with the placeholders of $values filled in, whatever their case - each name in square brackets. Raw text. */
|
|
function emailTemplateFill($text, array $values) {
|
|
if (!$values) {
|
|
return (string) $text;
|
|
}
|
|
$names = array_map(function ($name) {
|
|
return implode('\s+', array_map(function ($word) { return preg_quote($word, '/'); }, preg_split('/\s+/', $name)));
|
|
}, array_keys($values));
|
|
return preg_replace_callback('/\[(' . implode('|', $names) . ')\]/iu', function ($match) use ($values) {
|
|
return $values[strtolower(preg_replace('/\s+/u', ' ', $match[1]))] ?? $match[0];
|
|
}, (string) $text);
|
|
}
|
|
|
|
/* Paragraphs - a blank line between them - each trimmed, the empty ones dropped. */
|
|
function emailTemplateParagraphs($text) {
|
|
return array_values(array_filter(array_map('trim', preg_split('/\n\s*\n/', str_replace("\r", '', (string) $text))), function ($paragraph) {
|
|
return $paragraph !== '';
|
|
}));
|
|
}
|
|
|
|
/*
|
|
* $email (emailHtml()'s fields) with your own wording for template $key, where you changed it: the heading, the
|
|
* opening text (intro) and the closing text (after), filled in from $values (emailTemplateValues()). $skip names parts
|
|
* not to change. The subject goes in $email['template_subject'], for emailTemplateSubject().
|
|
*/
|
|
function emailTemplateApply($key, array $email, array $values, array $skip = []) {
|
|
$saved = emailTemplatesSaved()[$key] ?? null;
|
|
if (!$saved) {
|
|
return $email;
|
|
}
|
|
if ($saved['title'] !== null && trim($saved['title']) !== '' && !in_array('title', $skip, true)) {
|
|
$email['title'] = trim(emailTemplateFill($saved['title'], $values));
|
|
}
|
|
foreach (['intro', 'after'] as $part) {
|
|
if ($saved[$part] !== null && !in_array($part, $skip, true)) {
|
|
$email[$part] = emailTemplateParagraphs(emailTemplateFill($saved[$part], $values));
|
|
}
|
|
}
|
|
if ($saved['subject'] !== null && trim($saved['subject']) !== '' && !in_array('subject', $skip, true)) {
|
|
$email['template_subject'] = trim(preg_replace('/\s+/u', ' ', emailTemplateFill($saved['subject'], $values)));
|
|
}
|
|
return $email;
|
|
}
|
|
|
|
/*
|
|
* The subject of an email about a ticket ($ticket): your own (emailTemplateApply()'s template_subject in $email), with
|
|
* the ticket's number in brackets first when it hasn't it already - else $built_in. Takes template_subject out of $email.
|
|
*/
|
|
function emailTemplateSubject(array $ticket, array &$email, $built_in) {
|
|
$subject = trim((string) ($email['template_subject'] ?? ''));
|
|
unset($email['template_subject']);
|
|
if ($subject === '') {
|
|
return (string) $built_in;
|
|
}
|
|
$reference = '[' . ticketReference($ticket) . ']';
|
|
return mb_stripos($subject, $reference) !== false ? $subject : "$reference $subject";
|
|
}
|