Files
itflow/agent/modals/email_unprocessed/email_unprocessed_ticket.php

99 lines
5.3 KiB
PHP

<?php
/*
* Tickets > Unprocessed Email > Make a Ticket - the client it is for, and the contact: one of theirs, the sender made
* a contact there first, or nobody. Asked of the email parser (agent/post/email_unprocessed.php), which makes the
* ticket from the email as it would any other - its text, attachments and the rest. A sender who is a contact already
* (an email that failed) starts picked.
*/
require_once '../../../includes/modal_header.php';
enforceUserPermission('module_support', 2);
$row = mailUnprocessedReady() ? mailUnprocessedRow($_GET['id'] ?? 0) : null;
if (!$row || $row['email_unprocessed_done_at'] !== null) {
echo json_encode(['error' => $row ? 'That email was sorted already.' : "That email isn't there any more."]);
exit;
}
$id = intval($row['email_unprocessed_id']);
$from_email = trim((string) $row['email_unprocessed_from_email']);
$from_name = trim((string) $row['email_unprocessed_from_name']);
$can_add = filter_var($from_email, FILTER_VALIDATE_EMAIL) !== false;
// A sender who is a contact already starts picked - their client and them
$known = $from_email !== '' ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_id, contact_client_id FROM contacts
LEFT JOIN clients ON client_id = contact_client_id
WHERE contact_email = '" . escapeSql($from_email) . "' AND contact_archived_at IS NULL AND client_archived_at IS NULL " . clientScopeSql('contact_client_id') . "
ORDER BY contact_primary DESC, contact_id LIMIT 1")) : null;
$client_id = intval($known['contact_client_id'] ?? 0);
$contact_id = intval($known['contact_id'] ?? 0);
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fas fa-fw fa-plus me-2"></i>Make a Ticket</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="post.php" method="post" autocomplete="off" id="unprocessedTicketForm">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="hidden" name="email_unprocessed_id" value="<?= $id ?>">
<div class="modal-body">
<div class="alert alert-info small">
From <strong><?= escapeHtml($from_name !== '' ? $from_name : ($from_email !== '' ? $from_email : 'no sender address')) ?></strong><?= $from_name !== '' && $from_email !== '' ? ' &lt;' . escapeHtml($from_email) . '&gt;' : '' ?>
- <?= escapeHtml(trim((string) $row['email_unprocessed_subject']) !== '' ? $row['email_unprocessed_subject'] : '(no subject)') ?><br>
The ticket is made from the email, its attachments too, within a minute.
</div>
<div class="mb-3">
<label class="form-label">Client <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-user"></i></span>
<select class="form-select select2" name="client_id" id="unprocessedClientSelect" required>
<option value="">- Select a client -</option>
<?php
$sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation FROM clients WHERE client_archived_at IS NULL AND (client_lead = 0 OR client_id = $client_id) " . clientScopeSql('clients.client_id') . " ORDER BY client_name ASC");
while ($client = mysqli_fetch_assoc($sql)) { ?>
<option value="<?= intval($client['client_id']) ?>" <?= intval($client['client_id']) === $client_id ? 'selected' : '' ?><?= selectOptionClient($client['client_id'], $client['client_name'], $client['client_abbreviation']) ?>><?= escapeHtml($client['client_name']) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">Contact</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-user"></i></span>
<select class="form-select select2" name="contact_id" id="unprocessedContactSelect" data-selected="<?= $contact_id ?>">
<option value="0">- Nobody -</option>
</select>
<?= quickAddButton('contact', 0) ?>
</div>
</div>
<?php if ($can_add) { ?>
<div class="form-check" id="unprocessedAddContact">
<input class="form-check-input" type="checkbox" id="unprocessedAddContactCheck" name="make_contact" value="1" <?= $contact_id ? '' : 'checked' ?>>
<label class="form-check-label" for="unprocessedAddContactCheck">
Make <?= escapeHtml($from_email) ?> a contact at the client, for the ticket
</label>
<div class="form-text">So their replies go on the ticket, and they hear back about it.</div>
</div>
<?php } ?>
</div>
<div class="modal-footer">
<button type="submit" name="unprocessed_email_ticket" class="btn btn-primary fw-bold"><i class="fas fa-check me-2"></i>Make Ticket</button>
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
</div>
</form>
<script src="<?= staticFileUrl('/agent/js/email_unprocessed_modal.js') ?>"></script>
<?php
require_once '../../../includes/modal_footer.php';