mirror of
https://github.com/itflow-org/itflow
synced 2026-03-02 20:04:53 +00:00
Fix Ticket Template auto filling
This commit is contained in:
@@ -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));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ ob_start();
|
|||||||
<form action="post.php" method="post" autocomplete="off">
|
<form action="post.php" method="post" autocomplete="off">
|
||||||
<!-- Hidden/System fields -->
|
<!-- Hidden/System fields -->
|
||||||
<?php if ($client_id) { ?>
|
<?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 } ?>
|
||||||
<?php if ($project_id) { ?>
|
<?php if ($project_id) { ?>
|
||||||
<input type="hidden" name="project" value="<?php echo $project_id; ?>">
|
<input type="hidden" name="project" value="<?php echo $project_id; ?>">
|
||||||
@@ -297,32 +297,24 @@ ob_start();
|
|||||||
|
|
||||||
<!-- Ticket Templates -->
|
<!-- Ticket Templates -->
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
$(document).on('change', '#ticket_template_select', function () {
|
||||||
var templateSelect = $('#ticket_template_select');
|
const $opt = $(this).find(':selected');
|
||||||
var subjectInput = document.getElementById('subjectInput');
|
const templateSubject = $opt.data('subject') || '';
|
||||||
var detailsInput = document.getElementById('detailsInput');
|
const templateDetails = $opt.data('details') || '';
|
||||||
|
|
||||||
templateSelect.on('select2:select', function(e) {
|
$('#subjectInput').val(templateSubject);
|
||||||
var selectedOption = e.params.data.element;
|
|
||||||
var templateSubject = selectedOption.getAttribute('data-subject');
|
|
||||||
var templateDetails = selectedOption.getAttribute('data-details');
|
|
||||||
|
|
||||||
// Update Subject
|
if (window.tinymce) {
|
||||||
subjectInput.value = templateSubject || '';
|
const editor = tinymce.get('detailsInput');
|
||||||
|
if (editor) {
|
||||||
// Update Details
|
editor.setContent(templateDetails);
|
||||||
if (typeof tinymce !== 'undefined') {
|
} else {
|
||||||
var editor = tinymce.get('detailsInput');
|
$('#detailsInput').val(templateDetails);
|
||||||
if (editor) {
|
}
|
||||||
editor.setContent(templateDetails || '');
|
} else {
|
||||||
} else {
|
$('#detailsInput').val(templateDetails);
|
||||||
detailsInput.value = templateDetails || '';
|
}
|
||||||
}
|
});
|
||||||
} else {
|
|
||||||
detailsInput.value = templateDetails || '';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Ticket Client/Contact JS -->
|
<!-- Ticket Client/Contact JS -->
|
||||||
|
|||||||
@@ -51,4 +51,3 @@ $data = "otpauth://totp/ITFlow:$session_email?secret=$token";
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user