Fix Ticket Template auto filling

This commit is contained in:
johnnyq 2025-12-29 17:46:23 -05:00
parent f784b659e8
commit f2d4eb0486
3 changed files with 104 additions and 31 deletions

View File

@ -174,3 +174,85 @@ function populateVendorsDropdown(client_id) {
}
);
}
const TEMPLATE_AJAX_URL = "/agent/ajax.php";
function applyTemplateToForm(subject, details) {
const subjectEl = document.getElementById("subjectInput");
if (subjectEl) subjectEl.value = subject || "";
if (typeof tinymce !== "undefined") {
const ed = tinymce.get("detailsInput");
if (ed) {
ed.setContent(details || "");
return;
}
}
const detailsEl = document.getElementById("detailsInput");
if (detailsEl) detailsEl.value = details || "";
}
function loadTemplatesIfPresent() {
const select = document.getElementById("ticket_template_select");
if (!select) return;
// If already loaded, do nothing
if (select.dataset.loaded === "1") return;
// Mark as loading to prevent duplicate fetches
select.dataset.loaded = "1";
fetch(`${TEMPLATE_AJAX_URL}?get_ticket_templates=true`, { credentials: "same-origin" })
.then(r => r.json())
.then(data => {
select.innerHTML = `<option value="0">- Choose a Template -</option>`;
(data.templates || []).forEach(t => {
const opt = document.createElement("option");
opt.value = t.ticket_template_id;
opt.textContent = `${t.ticket_template_name} (${t.task_count} tasks)`;
select.appendChild(opt);
});
})
.catch(err => {
// allow retry if it failed
select.dataset.loaded = "0";
console.error("Template list load failed:", err);
});
}
// 1) Run once now (in case modal is already present)
loadTemplatesIfPresent();
// 2) Watch for AJAX-injected modal content
const ticketTemplateObserver = new MutationObserver(() => {
loadTemplatesIfPresent();
});
ticketTemplateObserver.observe(document.body, { childList: true, subtree: true });
// 3) When user changes template, fetch details
document.addEventListener("change", function (e) {
if (e.target && e.target.id === "ticket_template_select") {
const templateId = e.target.value;
if (!templateId || templateId === "0") {
applyTemplateToForm("", "");
return;
}
fetch(`${TEMPLATE_AJAX_URL}?get_ticket_template_details=true&ticket_template_id=${encodeURIComponent(templateId)}`, {
credentials: "same-origin"
})
.then(r => r.json())
.then(resp => {
if (resp.success !== "TRUE") {
applyTemplateToForm("", "");
return;
}
applyTemplateToForm(resp.subject, resp.details);
})
.catch(err => console.error("Template details load failed:", err));
}
});

View File

@ -18,7 +18,7 @@ ob_start();
<form action="post.php" method="post" autocomplete="off">
<!-- Hidden/System fields -->
<?php if ($client_id) { ?>
<input type="hidden" name="client" value="<?php echo $client_id; ?>>">
<input type="hidden" name="client" value="<?php echo $client_id; ?>">
<?php } ?>
<?php if ($project_id) { ?>
<input type="hidden" name="project" value="<?php echo $project_id; ?>">
@ -58,13 +58,13 @@ ob_start();
<option value="0">- Choose a Template -</option>
<?php
$sql_ticket_templates = mysqli_query($mysqli, "
SELECT tt.ticket_template_id,
SELECT tt.ticket_template_id,
tt.ticket_template_name,
tt.ticket_template_subject,
tt.ticket_template_subject,
tt.ticket_template_details,
COUNT(ttt.task_template_id) as task_count
FROM ticket_templates tt
LEFT JOIN task_templates ttt
LEFT JOIN task_templates ttt
ON tt.ticket_template_id = ttt.task_template_ticket_template_id
WHERE tt.ticket_template_archived_at IS NULL
GROUP BY tt.ticket_template_id
@ -189,7 +189,7 @@ ob_start();
<!-- Ticket client/contact -->
<?php if ($contact_id) { ?>
<input type="hidden" name="contact" value="<?php echo $contact_id; ?>">
<?php } else { ?>
<?php } else { ?>
<div class="tab-pane fade" id="pills-add-contacts">
<div class="form-group">
@ -297,32 +297,24 @@ ob_start();
<!-- Ticket Templates -->
<script>
document.addEventListener("DOMContentLoaded", function() {
var templateSelect = $('#ticket_template_select');
var subjectInput = document.getElementById('subjectInput');
var detailsInput = document.getElementById('detailsInput');
$(document).on('change', '#ticket_template_select', function () {
const $opt = $(this).find(':selected');
const templateSubject = $opt.data('subject') || '';
const templateDetails = $opt.data('details') || '';
templateSelect.on('select2:select', function(e) {
var selectedOption = e.params.data.element;
var templateSubject = selectedOption.getAttribute('data-subject');
var templateDetails = selectedOption.getAttribute('data-details');
$('#subjectInput').val(templateSubject);
// Update Subject
subjectInput.value = templateSubject || '';
// Update Details
if (typeof tinymce !== 'undefined') {
var editor = tinymce.get('detailsInput');
if (editor) {
editor.setContent(templateDetails || '');
} else {
detailsInput.value = templateDetails || '';
}
} else {
detailsInput.value = templateDetails || '';
}
});
});
if (window.tinymce) {
const editor = tinymce.get('detailsInput');
if (editor) {
editor.setContent(templateDetails);
} else {
$('#detailsInput').val(templateDetails);
}
} else {
$('#detailsInput').val(templateDetails);
}
});
</script>
<!-- Ticket Client/Contact JS -->

View File

@ -25,7 +25,7 @@ $data = "otpauth://totp/ITFlow:$session_email?secret=$token";
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token'] ?>">
<div class="modal-body">
<div class="text-center">
<img src='../../plugins/barcode/barcode.php?f=png&s=qr&d=<?php echo $data; ?>'>
<p><span class='text-secondary'>Secret:</span> <?php echo $token; ?>
@ -51,4 +51,3 @@ $data = "otpauth://totp/ITFlow:$session_email?secret=$token";
</div>
</div>
</div>