Files
itflow/admin/email_routing_rules.php

204 lines
14 KiB
PHP

<?php
/*
* Admin > Email Routing - email routing rules (functions/mail_route.php): an address or a domain, with words its subject
* has, whose new tickets by email go to a client and a contact, with a category, a priority, an agent and a ticket
* template. Each with why, who added it, and how many emails it routed and the last. New and edit in
* modals/email_routing_rule/email_routing_rule.php; the handlers are admin/post/email_routing_rule.php.
*/
// Default Column Sortby Filter
$sort = "routing_rule_match";
$order = "ASC";
require_once "includes/inc_all_admin.php";
if (!mailRouteReady()) { ?>
<div class="alert alert-warning">Email routing needs the database update - <a href="update.php" class="alert-link">Maintenance &gt; Update</a>.</div>
<?php require_once "../includes/footer.php";
return;
}
// Only these sort - an unknown column would take the page down
$sortable_columns = [
'routing_rule_match' => "routing_rule_match $order, routing_rule_subject ASC",
'client_name' => "client_name IS NULL, client_name $order, routing_rule_match ASC",
'routing_rule_matches' => "routing_rule_matches $order, routing_rule_match ASC",
'routing_rule_matched_at' => "routing_rule_matched_at IS NULL, routing_rule_matched_at $order, routing_rule_match ASC",
'routing_rule_created_at' => "routing_rule_created_at $order, routing_rule_match ASC",
];
if (!isset($sortable_columns[$sort])) {
$sort = 'routing_rule_match';
}
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS email_routing_rules.*, client_name, client_archived_at, contact_name, contact_archived_at, contact_client_id,
category_name, category_archived_at, agent.user_name AS agent_name, agent.user_status AS agent_status, agent.user_archived_at AS agent_archived_at,
ticket_template_name, ticket_template_archived_at, creator.user_name AS creator_name
FROM email_routing_rules
LEFT JOIN clients ON client_id = routing_rule_client_id
LEFT JOIN contacts ON contact_id = routing_rule_contact_id
LEFT JOIN categories ON category_id = routing_rule_category_id
LEFT JOIN users agent ON agent.user_id = routing_rule_assigned_to
LEFT JOIN ticket_templates ON ticket_template_id = routing_rule_ticket_template_id
LEFT JOIN users creator ON creator.user_id = routing_rule_created_by
WHERE (routing_rule_match LIKE '%$q%' OR routing_rule_subject LIKE '%$q%' OR routing_rule_note LIKE '%$q%' OR client_name LIKE '%$q%' OR contact_name LIKE '%$q%'
OR category_name LIKE '%$q%' OR agent.user_name LIKE '%$q%' OR ticket_template_name LIKE '%$q%')
ORDER BY {$sortable_columns[$sort]}
LIMIT $record_from, $record_to");
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
?>
<div class="card" data-routing-rules>
<div class="card-header bg-dark py-2">
<h3 class="card-title mt-2"><i class="fas fa-fw fa-route me-2"></i>Email Routing</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary ajax-modal" data-modal-size="lg" data-modal-url="modals/email_routing_rule/email_routing_rule.php">
<i class="fas fa-plus"></i><span class="d-none d-lg-inline ms-2">New Routing Rule</span>
</button>
</div>
</div>
<div class="card-header py-3">
<p class="small text-body-secondary mb-3">
A new ticket by email from these addresses and domains goes where the rule says - to its client, over the sender's own contact or domain, with its
category, priority, agent and template. Replies to a ticket still go on it, and <a href="email_ignored_senders.php">Ignored Senders</a> are set aside first.
The sender is answered and copied in only when they are the ticket's contact. When more than one rule takes an email, the most exact routes it: an address
before a domain, and a rule with subject words before one without. <a href="settings_ticket.php#section-email">Try an Email</a> shows what one would do.
</p>
<form autocomplete="off">
<div class="row g-2 align-items-center">
<div class="col-lg-5 col-md-6">
<div class="input-group">
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search routing rules..." title="An address, a domain, subject words, a client, a category, an agent, a template, or why">
<button class="btn btn-dark" title="Search"><i class="fas fa-search"></i></button>
</div>
</div>
</div>
</form>
</div>
<div>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-nowrap <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
<tr>
<th class="ps-3"><?= sortLink('routing_rule_match', 'Sender') ?></th>
<th><?= sortLink('client_name', 'Goes to') ?></th>
<th class="d-none d-md-table-cell">Sets</th>
<th><?= sortLink('routing_rule_matches', 'Routed') ?></th>
<th class="d-none d-lg-table-cell"><?= sortLink('routing_rule_matched_at', 'Last') ?></th>
<th class="d-none d-xl-table-cell"><?= sortLink('routing_rule_created_at', 'Added') ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($sql)) {
$id = intval($row['routing_rule_id']);
$match = (string) $row['routing_rule_match'];
$domain = strpos($match, '@') === false;
$edit = 'data-modal-size="lg" data-modal-url="modals/email_routing_rule/email_routing_rule.php?id=' . $id . '"';
$note = trim((string) $row['routing_rule_note']);
// What it names that isn't there any more - skipped when it routes, and said here
$gone = [];
$client_ok = intval($row['routing_rule_client_id']) && $row['client_name'] !== null && $row['client_archived_at'] === null;
if (intval($row['routing_rule_client_id']) && !$client_ok) {
$gone[] = 'Client archived or deleted';
}
$contact_ok = intval($row['routing_rule_contact_id']) && $row['contact_name'] !== null && $row['contact_archived_at'] === null
&& intval($row['contact_client_id']) === intval($row['routing_rule_client_id']);
if (intval($row['routing_rule_contact_id']) && !$contact_ok) {
$gone[] = 'Contact archived or deleted';
}
$sets = [];
if (intval($row['routing_rule_category_id'])) {
if ($row['category_name'] !== null && $row['category_archived_at'] === null) {
$sets[] = '<span class="badge text-bg-light border fw-normal" title="Category"><i class="fas fa-fw fa-layer-group me-1 text-body-secondary"></i>' . escapeHtml($row['category_name']) . '</span>';
} else {
$gone[] = 'Category archived or deleted';
}
}
if ((string) $row['routing_rule_priority'] !== '') {
$sets[] = '<span class="badge text-bg-' . ticketPriorityColor($row['routing_rule_priority']) . '" title="Priority">' . escapeHtml($row['routing_rule_priority']) . '</span>';
}
if (intval($row['routing_rule_assigned_to'])) {
if ($row['agent_name'] !== null && intval($row['agent_status']) === 1 && $row['agent_archived_at'] === null) {
$sets[] = '<span class="badge text-bg-light border fw-normal" title="Assigned to"><i class="fas fa-fw fa-user-check me-1 text-body-secondary"></i>' . escapeHtml($row['agent_name']) . '</span>';
} else {
$gone[] = 'Agent disabled or archived';
}
}
if (intval($row['routing_rule_ticket_template_id'])) {
if ($row['ticket_template_name'] !== null && $row['ticket_template_archived_at'] === null) {
$sets[] = '<span class="badge text-bg-light border fw-normal" title="Ticket template"><i class="fas fa-fw fa-life-ring me-1 text-body-secondary"></i>' . escapeHtml($row['ticket_template_name']) . '</span>';
} else {
$gone[] = 'Template archived or deleted';
}
}
?>
<tr data-routing-rule="<?= escapeHtml($match) ?>">
<td class="ps-3 min-w-0">
<a class="ajax-modal fw-semibold text-body text-break" href="#" <?= $edit ?>><?= escapeHtml($domain ? "@$match" : $match) ?></a>
<div class="small text-body-secondary text-break">
<?= $domain ? 'Everyone at this domain and its subdomains' : 'This address' ?><?php if ((string) $row['routing_rule_subject'] !== '') { ?>,
with <strong class="text-body">&ldquo;<?= escapeHtml($row['routing_rule_subject']) ?>&rdquo;</strong> in the subject<?php } ?>
</div>
<?php if ($note !== '') { ?>
<div class="small text-body-tertiary text-break"><i class="fas fa-fw fa-sticky-note me-1"></i><?= escapeHtml($note) ?></div>
<?php } ?>
</td>
<td class="small min-w-0">
<?php if ($client_ok) { ?>
<a class="text-body fw-semibold text-break" href="/agent/client_overview.php?client_id=<?= intval($row['routing_rule_client_id']) ?>"><?= escapeHtml($row['client_name']) ?></a>
<?php if ($contact_ok) { ?><div class="text-body-secondary text-break"><i class="fas fa-fw fa-user me-1"></i><?= escapeHtml($row['contact_name']) ?></div><?php } ?>
<?php } else { ?>
<span class="text-body-secondary" title="Its client is found from the sender, as for any email">Whoever it's from</span>
<?php } ?>
<div class="d-md-none mt-1 d-flex flex-wrap gap-1"><?= implode('', $sets) ?></div>
</td>
<td class="d-none d-md-table-cell">
<div class="d-flex flex-wrap gap-1"><?= $sets ? implode('', $sets) : '<span class="small text-body-tertiary">-</span>' ?></div>
<?php if ($gone) { ?>
<div class="small text-danger mt-1" data-routing-gone><i class="fas fa-fw fa-exclamation-triangle me-1"></i><?= escapeHtml(implode(', ', $gone)) ?> - skipped until it's fixed</div>
<?php } ?>
</td>
<td data-routing-count><?= number_format(intval($row['routing_rule_matches'])) ?> <span class="small text-body-secondary">ticket<?= intval($row['routing_rule_matches']) === 1 ? '' : 's' ?></span></td>
<td class="d-none d-lg-table-cell small text-nowrap text-body-secondary"><?= $row['routing_rule_matched_at'] ? escapeHtml(timeAgo($row['routing_rule_matched_at'])) : 'Never' ?></td>
<td class="d-none d-xl-table-cell small text-nowrap text-body-secondary">
<?= escapeHtml(date('M j, Y', strtotime((string) $row['routing_rule_created_at']))) ?>
<?php if ($row['creator_name'] !== null) { ?><div><?= escapeHtml($row['creator_name']) ?></div><?php } ?>
</td>
<td class="text-end pe-3">
<div class="dropdown dropstart">
<button class="btn btn-sm btn-light" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
<div class="dropdown-menu">
<a class="dropdown-item ajax-modal" href="#" <?= $edit ?>><i class="fas fa-fw fa-edit text-secondary me-2"></i>Edit</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-danger confirm-link" href="post.php?delete_routing_rule=<?= $id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>" data-routing-delete>
<i class="fas fa-fw fa-trash me-2"></i>Delete
</a>
</div>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<?php
$empty_state = [
'thing' => 'routing rules',
'icon' => 'fa-route',
'text' => 'Send the new tickets from an address or a domain to a client, with a category, priority, agent or template - a vendor\'s alerts about one client, a monitoring system, a form.',
'actions' => [['label' => 'New Routing Rule', 'modal' => 'modals/email_routing_rule/email_routing_rule.php', 'size' => 'lg']],
];
require_once "../includes/filter_footer.php";
?>
</div>
<?php
require_once "../includes/footer.php";