Files
itflow/agent/email_unprocessed.php

224 lines
15 KiB
PHP

<?php
/*
* Tickets > Unprocessed Email - email the ticket mailbox took but couldn't make a ticket from (functions/mail_unprocessed.php),
* and any kept before strangers' email was left in the inbox again. Each can
* be opened, made a ticket, added to a ticket, tried again or dismissed (agent/post/email_unprocessed.php); the first
* three are done by the email parser within a minute. The pills pick To sort, Sorted and Dismissed.
*/
// Default Column Sortby Filter
$sort = "email_unprocessed_received_at";
$order = "DESC";
require_once "includes/inc_all.php";
enforceUserPermission('module_support');
$page_title = 'Unprocessed Email';
$can_edit = lookupUserPermission('module_support') >= 2;
if (!mailUnprocessedReady()) {
echo '<div class="alert alert-info">This list arrives with the 4.3.2 database update - <a href="/admin/update.php">Maintenance &gt; Update</a>.</div>';
require_once "../includes/footer.php";
exit;
}
$tabs = [
'waiting' => ['To sort', 'fa-inbox', "email_unprocessed_done_at IS NULL"],
'sorted' => ['Sorted', 'fa-check', "email_unprocessed_done_at IS NOT NULL AND email_unprocessed_action <> 'dismiss'"],
'dismissed' => ['Dismissed', 'fa-times', "email_unprocessed_action = 'dismiss'"],
];
$show = (string) ($_GET['show'] ?? 'waiting');
$show = isset($tabs[$show]) ? $show : 'waiting';
// Only these sort - an unknown column would take the page down
$sortable_columns = [
'email_unprocessed_received_at' => "email_unprocessed_received_at $order",
'email_unprocessed_from_email' => "email_unprocessed_from_email $order, email_unprocessed_received_at DESC",
'email_unprocessed_subject' => "email_unprocessed_subject $order, email_unprocessed_received_at DESC",
];
if (!isset($sortable_columns[$sort])) {
$sort = 'email_unprocessed_received_at';
}
$search = "(email_unprocessed_from_email LIKE '%$q%' OR email_unprocessed_from_name LIKE '%$q%' OR email_unprocessed_subject LIKE '%$q%' OR email_unprocessed_preview LIKE '%$q%' OR email_unprocessed_reason LIKE '%$q%')";
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS email_unprocessed.*, ticket_prefix, ticket_number, ticket_client_id, user_name AS asked_by
FROM email_unprocessed
LEFT JOIN tickets ON ticket_id = email_unprocessed_ticket_id
LEFT JOIN users ON user_id = email_unprocessed_action_by
WHERE $search AND {$tabs[$show][2]}
ORDER BY {$sortable_columns[$sort]}, email_unprocessed_id DESC
LIMIT $record_from, $record_to");
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
// How many in each tab, as searched
$count_sql = [];
foreach ($tabs as $key => [, , $where]) {
$count_sql[] = "COALESCE(SUM($where), 0)";
}
$counts = array_combine(array_keys($tabs), array_map('intval', mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', $count_sql) . " FROM email_unprocessed WHERE $search"))));
$pill_url = function ($show) {
return '?' . http_build_query(array_filter(['q' => $_GET['q'] ?? '', 'show' => $show]));
};
$csrf = $_SESSION['csrf_token'];
?>
<div class="card">
<div class="card-header bg-dark py-2">
<h3 class="card-title mt-2"><i class="fas fa-fw fa-envelope-open-text me-2"></i>Unprocessed Email</h3>
<div class="card-tools">
<a class="btn btn-outline-light" href="tickets.php"><i class="fas fa-life-ring"></i><span class="d-none d-lg-inline ms-2">Tickets</span></a>
</div>
</div>
<div class="card-header py-3">
<form autocomplete="off">
<input type="hidden" name="show" value="<?= escapeHtml($show) ?>">
<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 unprocessed email..." title="Who it is from, its subject or what it says">
<button class="btn btn-dark" title="Search"><i class="fas fa-search"></i></button>
</div>
</div>
<div class="col-lg-7 col-md-6">
<div class="d-flex justify-content-end gap-2">
<?php if ($can_edit && $show === 'waiting') { ?>
<div class="dropdown" id="bulkActionButton" hidden>
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
<i class="fas fa-fw fa-layer-group me-2"></i>Bulk Action (<span id="selectedCount">0</span>)
</button>
<div class="dropdown-menu dropdown-menu-end">
<button class="dropdown-item" type="submit" form="bulkActions" name="bulk_unprocessed_email" value="retry" title="As any email is taken - within a minute">
<i class="fas fa-fw fa-redo-alt text-secondary me-2"></i>Try Again
</button>
<div class="dropdown-divider"></div>
<button class="dropdown-item" type="submit" form="bulkActions" name="bulk_unprocessed_email" value="dismiss">
<i class="fas fa-fw fa-times text-secondary me-2"></i>Dismiss
</button>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
</form>
<nav class="nav nav-pills itflow-asset-tabs flex-nowrap overflow-auto mt-2" aria-label="Which email">
<?php foreach ($tabs as $key => [$label, $icon]) { ?>
<a class="nav-link text-nowrap <?= $show === $key ? 'active' : '' ?>" href="<?= escapeHtml($pill_url($key)) ?>" data-unprocessed-tab="<?= $key ?>">
<i class="fas fa-fw <?= $icon ?> me-1"></i><?= $label ?> <span class="badge rounded-pill ms-1 <?= $key === 'waiting' && $show !== 'waiting' && $counts[$key] ? 'text-bg-warning' : '' ?>"><?= $counts[$key] ?></span>
</a>
<?php } ?>
</nav>
<div class="small text-body-secondary mt-2"><i class="fas fa-fw fa-info-circle me-1"></i>Email the ticket mailbox took but couldn't make a ticket from. A ticket made here, or a reply added, is made by the email parser within a minute. Sorted and dismissed email is kept <?= MAIL_UNPROCESSED_KEEP_DAYS ?> days.</div>
</div>
<form id="bulkActions" action="post.php" method="post">
<input type="hidden" name="csrf_token" value="<?= $csrf ?>">
<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>
<?php if ($can_edit && $show === 'waiting') { ?>
<th class="checkbox-column border-end">
<div class="form-check">
<input class="form-check-input" id="selectAllCheckbox" type="checkbox" title="Select all on this page">
</div>
</th>
<?php } ?>
<th><?= sortLink('email_unprocessed_from_email', 'From') ?></th>
<th class="d-none d-md-table-cell"><?= sortLink('email_unprocessed_subject', 'Subject') ?></th>
<th class="d-none d-lg-table-cell"><?= sortLink('email_unprocessed_received_at', 'Received') ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($sql)) {
$id = intval($row['email_unprocessed_id']);
$state = mailUnprocessedState($row);
$subject = trim((string) $row['email_unprocessed_subject']) !== '' ? escapeHtml($row['email_unprocessed_subject']) : '<span class="text-body-secondary">(no subject)</span>';
$from_name = trim((string) $row['email_unprocessed_from_name']);
$from_email = (string) $row['email_unprocessed_from_email'];
$view = 'data-modal-size="lg" data-modal-url="modals/email_unprocessed/email_unprocessed_view.php?id=' . $id . '"';
$waiting = $state['key'] === 'waiting';
?>
<tr data-unprocessed-id="<?= $id ?>" data-unprocessed-state="<?= $state['key'] ?>">
<?php if ($can_edit && $show === 'waiting') { ?>
<td class="checkbox-column bg-light border-end">
<?php if ($waiting) { ?>
<div class="form-check">
<input class="form-check-input bulk-select" type="checkbox" name="email_unprocessed_ids[]" value="<?= $id ?>" aria-label="Select email <?= $id ?>">
</div>
<?php } ?>
</td>
<?php } ?>
<td class="min-w-0">
<div class="text-break"><?= $from_name !== '' ? escapeHtml($from_name) . ' <span class="small text-body-secondary">' . escapeHtml($from_email) . '</span>' : ($from_email !== '' ? escapeHtml($from_email) : '<span class="text-body-secondary">No sender address</span>') ?></div>
<div class="d-md-none small"><a class="ajax-modal" href="#" <?= $view ?>><?= $subject ?></a></div>
<div class="small mt-1">
<span class="badge rounded-pill <?= $state['badge'] ?>" data-unprocessed-status><?= escapeHtml($state['label']) ?></span>
<?php if ($state['detail'] !== '') { ?><span class="text-body-secondary"><?= escapeHtml($state['detail']) ?></span><?php } ?>
<?php if (intval($row['email_unprocessed_ticket_id']) && $row['ticket_number'] !== null) { ?>
<a href="ticket.php?<?= intval($row['ticket_client_id']) ? 'client_id=' . intval($row['ticket_client_id']) . '&' : '' ?>ticket_id=<?= intval($row['email_unprocessed_ticket_id']) ?>" data-unprocessed-ticket><?= escapeHtml($row['ticket_prefix'] . $row['ticket_number']) ?></a>
<?php } ?>
<?php if ($row['asked_by'] !== null && $state['key'] !== 'waiting') { ?><span class="text-body-secondary">by <?= escapeHtml($row['asked_by']) ?></span><?php } ?>
</div>
</td>
<td class="d-none d-md-table-cell min-w-0">
<a class="ajax-modal fw-semibold text-body" href="#" <?= $view ?>><?= $subject ?></a>
<?php if (trim((string) $row['email_unprocessed_preview']) !== '') { ?>
<div class="small text-body-secondary text-truncate" style="max-width: 40rem"><?= escapeHtml($row['email_unprocessed_preview']) ?></div>
<?php } ?>
<?php if ($waiting && trim((string) $row['email_unprocessed_reason']) !== '') { ?>
<div class="small text-warning-emphasis" data-unprocessed-reason><?= escapeHtml($row['email_unprocessed_reason']) ?></div>
<?php } ?>
</td>
<td class="d-none d-lg-table-cell text-nowrap small" title="<?= escapeHtml(date('M j, Y g:i:s A', strtotime((string) $row['email_unprocessed_received_at']))) ?>">
<?= escapeHtml(date('M j, g:i A', strtotime((string) $row['email_unprocessed_received_at']))) ?>
<div class="text-body-secondary"><?= escapeHtml(timeAgo($row['email_unprocessed_received_at'])) ?></div>
</td>
<td class="text-end">
<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="#" <?= $view ?>><i class="fas fa-fw fa-envelope-open text-secondary me-2"></i>View</a>
<?php if ($can_edit && $waiting) { ?>
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/email_unprocessed/email_unprocessed_ticket.php?id=<?= $id ?>" data-unprocessed-make><i class="fas fa-fw fa-plus text-secondary me-2"></i>Make a Ticket</a>
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/email_unprocessed/email_unprocessed_reply.php?id=<?= $id ?>" data-unprocessed-add><i class="fas fa-fw fa-reply text-secondary me-2"></i>Add to a Ticket</a>
<a class="dropdown-item" href="post.php?unprocessed_email=retry&id=<?= $id ?>&csrf_token=<?= $csrf ?>" title="As any email is taken - after making them a contact, say" data-unprocessed-retry><i class="fas fa-fw fa-redo-alt text-secondary me-2"></i>Try Again</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="post.php?unprocessed_email=dismiss&id=<?= $id ?>&csrf_token=<?= $csrf ?>" data-unprocessed-dismiss><i class="fas fa-fw fa-times text-secondary me-2"></i>Dismiss</a>
<?php } ?>
<a class="dropdown-item" href="post.php?download_unprocessed_email=<?= $id ?>"><i class="fas fa-fw fa-download text-secondary me-2"></i>Download .eml</a>
</div>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</form>
<?php
$empty_state = [
'thing' => 'emails',
'views' => ['show'],
'icon' => 'fa-envelope-open-text',
'title' => ['waiting' => 'Nothing to sort', 'sorted' => 'Nothing sorted lately', 'dismissed' => 'Nothing dismissed lately'][$show],
'text' => 'Email the ticket mailbox took but couldn\'t make a ticket from waits here. Email from someone who isn\'t a contact, at a domain no client has, stays in the mailbox\'s inbox, flagged - unless tickets from unknown senders are on.',
];
?>
<?php require_once "../includes/filter_footer.php"; ?>
</div>
<script src="<?= staticFileUrl('/js/bulk_actions.js') ?>"></script>
<?php
require_once "../includes/footer.php";