Document templates stage 2, filled in from the client: placeholders [Client], [Website], [Site], [Address], [Site Phone], [Contact], [First Name], [Contact Email], [Contact Phone], [Agent], [Date], [Month], [Quarter] and [Year] are filled in across a template's name, summary and content when a document is made, using the client's primary site and contact unless another or none is picked; tables of records [Contacts], [Locations], [Assets], [Servers], [Workstations], [Network Equipment], [Printers], [Networks], [Domains], [Certificates], [Licenses], [Vendors] and [Services] put in the client's current records, as a table on a line of their own (an empty row when there are none) or as names in a sentence, all made safe; any other bracketed word is a fill-in asked for when a document is made, set up on the template page as text, date, list or yes/no, required or with help, renamed along with the template text, reset or removed, and dragged into order, with answers checked by ticketFillInAnswers and blanks left in the document; the template page gets Fill-ins and Filled in from the client cards with each kind of word marked in its own colour, and the editor gets a button for each placeholder and table to insert it at the cursor (js/document_template_form.js); New document for a client and Document from template draw the site and contact pickers and fill-ins once the template and client are picked (agent/ajax.php document_template_asks, agent/js/document_template_make.js) and open larger; copying a template copies its fill-ins; Draft it with AI is told the placeholders and tables; migration 3.2.6 adds document_template_fill_ins; fixed the fill-in add and reset messages showing raw HTML around the bracketed name on document and ticket templates; changelog updated

This commit is contained in:
johnnyq
2026-10-18 19:47:57 -04:00
parent 9fdc2f7032
commit 3ee183a09a
17 changed files with 1054 additions and 50 deletions

View File

@@ -0,0 +1,24 @@
<?php
/*
* ITFlow - Database update to version 3.2.6 (from 3.2.5)
* Included by admin/database_updates.php - do not access directly
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
// What a document template asks for when a document is made from it - its
// fill-ins, each answer going in wherever its [Name] is
mysqli_query($mysqli, "CREATE TABLE `document_template_fill_ins` (
`document_fill_in_id` int(11) NOT NULL AUTO_INCREMENT,
`document_fill_in_document_template_id` int(11) NOT NULL,
`document_fill_in_name` varchar(50) NOT NULL,
`document_fill_in_type` varchar(10) NOT NULL DEFAULT 'text',
`document_fill_in_options` text DEFAULT NULL,
`document_fill_in_required` tinyint(1) NOT NULL DEFAULT 0,
`document_fill_in_help` varchar(255) DEFAULT NULL,
`document_fill_in_order` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`document_fill_in_id`),
UNIQUE KEY `document_fill_in_template_name` (`document_fill_in_document_template_id`,`document_fill_in_name`),
CONSTRAINT `document_template_fill_ins_ibfk_1` FOREIGN KEY (`document_fill_in_document_template_id`) REFERENCES `document_templates` (`document_template_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");

View File

@@ -1,10 +1,12 @@
<?php
/*
* One document template - what it says, with the words in square brackets
* left to fill in marked, who made it and last changed it, and its use: how
* many documents have been made from it, for how many clients, and the latest
* of them. A new document for a client can be made from here. The work is in
* One document template - what it says, with its [words] marked: the
* placeholders filled in from the client, the tables of the client's records,
* and what it asks for when a document is made (its fill-ins: set up, dragged
* into order) - who made it and last changed it, and its use: how many
* documents have been made from it, for how many clients, and the latest of
* them. A new document for a client can be made from here. The work is in
* functions/document_template.php.
*/
@@ -23,7 +25,9 @@ $name = escapeHtml($template['document_template_name']);
$archived = !empty($template['document_template_archived_at']);
$usage = documentTemplateUsage([$document_template_id])[$document_template_id] ?? ['count' => 0, 'clients' => 0, 'last' => null];
$content = trim((string) $template['document_template_content']) !== '' ? documentTemplateMarkBrackets(documentHtmlPurify($template['document_template_content'])) : '';
$brackets = documentTemplateBracketsIn($template['document_template_content']);
$used = documentTemplateWordsUsed($template);
$fill_ins = documentTemplateFillIns($template, documentTemplateFillInRowsFor([$document_template_id])[$document_template_id]);
$fill_in_url = "modals/document_template/document_template_fill_in.php?document_template_id=$document_template_id";
$images = is_dir(documentTemplateDir($document_template_id)) ? count(glob(documentTemplateDir($document_template_id) . '/*')) : 0;
$csrf = $_SESSION['csrf_token'];
@@ -64,7 +68,7 @@ $use_url = "modals/document_template/document_template_use.php?id=$document_temp
</div>
<div class="d-flex flex-wrap gap-2 d-print-none">
<?php if (!$archived) { ?>
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="<?= $use_url ?>"><i class="fas fa-fw fa-plus me-2"></i>New document for a client</button>
<button type="button" class="btn btn-primary ajax-modal" data-modal-size="lg" data-modal-url="<?= $use_url ?>"><i class="fas fa-fw fa-plus me-2"></i>New document for a client</button>
<?php } ?>
<button type="button" class="btn btn-outline-secondary ajax-modal" data-modal-size="xl" data-modal-url="<?= $edit_url ?>"><i class="fas fa-fw fa-edit me-2"></i>Edit</button>
<div class="dropdown">
@@ -124,16 +128,84 @@ $use_url = "modals/document_template/document_template_use.php?id=$document_temp
</div>
</div>
<?php if ($brackets) { ?>
<div class="card mb-3">
<div class="card-header"><h3 class="card-title mb-0"><i class="fas fa-fw fa-pen-square me-2"></i>Left to fill in</h3></div>
<div class="card-body small">
<div class="d-flex flex-wrap gap-1 mb-2">
<?php foreach ($brackets as $word) { ?>
<span class="badge rounded-pill text-bg-light border font-monospace fw-normal">[<?= escapeHtml($word) ?>]</span>
<div class="card mb-3">
<div class="card-header d-flex align-items-center">
<h3 class="card-title mb-0"><i class="fas fa-fw fa-pen-square me-2"></i>Fill-ins</h3>
<span class="ms-auto small text-secondary me-2"><?= $fill_ins ? count($fill_ins) . ' asked for' : '' ?></span>
<button type="button" class="btn btn-sm btn-link ajax-modal" data-modal-url="<?= $fill_in_url ?>"><i class="fas fa-plus me-1"></i>Add</button>
</div>
<div class="card-body">
<?php if ($fill_ins) { ?>
<p class="small text-secondary">Asked for when a document is made from it - each answer goes in wherever its <span class="font-monospace">[Name]</span> is, and one left blank stays for whoever writes it. Drag to change the order they are asked in.</p>
<div class="list-group" id="documentTemplateFillIns">
<?php foreach ($fill_ins as $fill_in) {
$kind = TICKET_FILL_IN_TYPES[$fill_in['type']];
$about = [];
if ($fill_in['type'] === 'list') {
$about[] = escapeHtml(implode(' · ', $fill_in['options']));
}
if ($fill_in['help'] !== '') {
$about[] = escapeHtml($fill_in['help']);
}
?>
<div class="list-group-item d-flex align-items-center gap-2" data-fill-in-name="<?= escapeHtml($fill_in['name']) ?>">
<span class="drag-handle text-secondary" title="Drag to reorder" style="cursor: grab;"><i class="fas fa-fw fa-grip-vertical"></i></span>
<div class="min-w-0 flex-grow-1">
<div class="d-flex flex-wrap align-items-center gap-1">
<span class="fw-semibold me-1"><?= escapeHtml($fill_in['name']) ?></span>
<span class="badge rounded-pill text-bg-light border fw-normal"><i class="fas fa-fw <?= $kind['icon'] ?> me-1 text-secondary"></i><?= $kind['label'] ?></span>
<?php if ($fill_in['required']) { ?><span class="badge rounded-pill text-bg-light border fw-normal"><i class="fas fa-fw fa-asterisk me-1 text-danger"></i>Required</span><?php } ?>
<?php if (!$fill_in['in_text']) { ?><span class="badge rounded-pill text-bg-warning fw-normal" title="Not in what it says - asked for, but put in nowhere">Not in the text</span><?php } ?>
</div>
<?php if ($about) { ?><div class="small text-secondary text-truncate"><?= implode(' - ', $about) ?></div><?php } ?>
</div>
<div class="dropdown dropstart">
<button class="btn btn-light text-secondary btn-sm" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-ellipsis-v"></i></button>
<div class="dropdown-menu">
<a class="dropdown-item ajax-modal" href="#" data-modal-url="<?= $fill_in_url ?>&name=<?= urlencode($fill_in['name']) ?>"><i class="fas fa-fw fa-edit me-2"></i>Edit</a>
<?php if ($fill_in['id']) { ?>
<div class="dropdown-divider"></div>
<?php if ($fill_in['in_text']) { ?>
<a class="dropdown-item confirm-link" href="post.php?delete_document_template_fill_in=<?= $fill_in['id'] ?>&csrf_token=<?= $csrf ?>"
data-confirm-title="Reset <?= escapeHtml($fill_in['name']) ?>?" data-confirm-text="It is still in what it says, so it stays asked for - as plain, optional text." data-confirm-button="Reset"><i class="fas fa-fw fa-undo me-2"></i>Reset</a>
<?php } else { ?>
<a class="dropdown-item text-danger confirm-link" href="post.php?delete_document_template_fill_in=<?= $fill_in['id'] ?>&csrf_token=<?= $csrf ?>"
data-confirm-title="Remove <?= escapeHtml($fill_in['name']) ?>?" data-confirm-text="It is no longer asked for." data-confirm-button="Remove"><i class="fas fa-fw fa-trash-alt me-2"></i>Remove</a>
<?php } ?>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="text-secondary">Words in square brackets stay in each document made from it, for whoever writes it to fill in.</div>
<?php } else { ?>
<div class="text-secondary small">Nothing to ask. Put a word in square brackets in what it says - <span class="font-monospace">[Firewall Model]</span>, <span class="font-monospace">[Review Date]</span> - and it is asked for when a document is made from it, then filled in. Add one here to make it a date, a choice from a list or yes or no, or to make it required.</div>
<?php } ?>
</div>
</div>
<?php if ($used['placeholder'] || $used['table']) { ?>
<div class="card mb-3">
<div class="card-header"><h3 class="card-title mb-0"><i class="fas fa-fw fa-magic me-2"></i>Filled in from the client</h3></div>
<div class="card-body small">
<?php if ($used['placeholder']) { ?>
<div class="d-flex flex-wrap gap-1 mb-2">
<?php foreach ($used['placeholder'] as $key => $word) { ?>
<span class="badge rounded-pill text-bg-light border font-monospace fw-normal" title="<?= escapeHtml(ucfirst(array_change_key_case(DOCUMENT_TEMPLATE_PLACEHOLDERS)[$key] ?? '')) ?>">[<?= escapeHtml($word) ?>]</span>
<?php } ?>
</div>
<?php if (array_intersect(array_keys($used['placeholder']), DOCUMENT_TEMPLATE_SITE_PLACEHOLDERS) || array_intersect(array_keys($used['placeholder']), DOCUMENT_TEMPLATE_CONTACT_PLACEHOLDERS)) { ?>
<div class="text-secondary mb-2">The <?= implode(' and ', array_filter([array_intersect(array_keys($used['placeholder']), DOCUMENT_TEMPLATE_SITE_PLACEHOLDERS) ? 'site' : '', array_intersect(array_keys($used['placeholder']), DOCUMENT_TEMPLATE_CONTACT_PLACEHOLDERS) ? 'contact' : ''])) ?> - the client's primary, unless another is picked when the document is made.</div>
<?php } ?>
<?php } ?>
<?php if ($used['table']) { ?>
<div class="text-secondary mb-1">The client's records, as they are then</div>
<ul class="list-unstyled mb-0">
<?php foreach ($used['table'] as $key => $word) { ?>
<li class="mb-1"><span class="badge rounded-pill text-info-emphasis bg-info-subtle border border-info-subtle font-monospace fw-normal">[<?= escapeHtml($word) ?>]</span> <span class="text-secondary"><?= escapeHtml(array_change_key_case(DOCUMENT_TEMPLATE_TABLES)[$key] ?? '') ?></span></li>
<?php } ?>
</ul>
<?php } ?>
</div>
</div>
<?php } ?>
@@ -158,6 +230,29 @@ $use_url = "modals/document_template/document_template_use.php?id=$document_temp
</div>
<script src="../js/pretty_content.js"></script>
<script src="../libs/SortableJS/Sortable.min.js"></script>
<script>
(function () {
var list = document.getElementById('documentTemplateFillIns');
if (!list) {
return;
}
new Sortable(list, {
handle: '.drag-handle',
animation: 150,
onEnd: function () {
itflowPost('post.php', {
reorder_document_template_fill_ins: 1,
csrf_token: <?= json_encode($csrf) ?>,
document_template_id: <?= $document_template_id ?>,
names: Array.from(list.querySelectorAll('[data-fill-in-name]')).map(function (row) {
return row.dataset.fillInName;
})
});
}
});
})();
</script>
<?php
require_once "../includes/footer.php";

View File

@@ -140,7 +140,7 @@ $csrf = $_SESSION['csrf_token'];
<i class="fas fa-fw fa-edit text-secondary me-2"></i>Edit
</a>
<?php if (!$row['document_template_archived_at']) { ?>
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/document_template/document_template_use.php?id=<?= $template_id ?>">
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/document_template/document_template_use.php?id=<?= $template_id ?>">
<i class="fas fa-fw fa-plus text-secondary me-2"></i>New document for a client
</a>
<?php } ?>

View File

@@ -0,0 +1,118 @@
<?php
/*
* One of a document template's fill-ins - what is asked for when a document
* is made from it: its name (as it goes in square brackets), what kind of
* answer it takes, its choices for a list, whether it must be answered and a
* line of help. ?document_template_id= and, to change one, &name= - one in the
* text that isn't set up yet included. Saved by save_document_template_fill_in
* (admin/post/document_template.php).
*/
require_once '../../includes/modal_header.php';
$template = documentTemplateRow($_GET['document_template_id'] ?? 0);
$name = trim((string) ($_GET['name'] ?? ''));
$fill_in = null;
if ($template && $name !== '') {
foreach (documentTemplateFillInsFor($template['document_template_id']) as $one) {
if (strtolower($one['name']) === strtolower($name)) {
$fill_in = $one;
}
}
}
ob_start();
if (!$template || ($name !== '' && !$fill_in)) { ?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fa fa-fw fa-pen-square me-2"></i>Fill-in</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body text-center text-secondary py-5">That fill-in is gone - it may have been taken out of the template.</div>
<?php } else {
$type = $fill_in['type'] ?? 'text';
?>
<div class="modal-header bg-dark">
<h5 class="modal-title text-truncate"><i class="fa fa-fw fa-pen-square me-2"></i><?= $fill_in ? 'Fill-in' : 'New fill-in' ?> <small class="ms-1 opacity-75"><?= escapeHtml($template['document_template_name']) ?></small></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="hidden" name="document_template_id" value="<?= intval($template['document_template_id']) ?>">
<input type="hidden" name="original_name" value="<?= escapeHtml($fill_in['name'] ?? '') ?>">
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Name <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-pen-square"></i></span>
<input type="text" class="form-control" name="name" placeholder="Firewall Model" maxlength="41" pattern="[A-Za-z][A-Za-z0-9 _\-\/]{0,40}" value="<?= escapeHtml($fill_in['name'] ?? '') ?>" required autofocus>
</div>
<div class="form-text">
It goes in what the template says as <span class="font-monospace">[<?= escapeHtml($fill_in['name'] ?? 'Firewall Model') ?>]</span>, and the answer takes its place.
<?php if ($fill_in) { ?>Renaming it renames it there too.<?php } ?>
</div>
</div>
<div class="mb-3">
<label class="form-label">Answer</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-sliders-h"></i></span>
<select class="form-select select2" name="type" id="documentFillInType">
<?php foreach (TICKET_FILL_IN_TYPES as $key => $kind) { ?>
<option value="<?= $key ?>" <?= $type === $key ? 'selected' : '' ?> data-icon="fas <?= $kind['icon'] ?>"><?= $kind['label'] ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="mb-3" id="documentFillInChoices" <?= $type === 'list' ? '' : 'hidden' ?>>
<label class="form-label">Choices <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-list-ul"></i></span>
<textarea class="form-control" name="options" rows="4" placeholder="Critical&#10;High&#10;Normal"><?= escapeHtml(implode("\n", $fill_in['options'] ?? [])) ?></textarea>
</div>
<div class="form-text">One per line.</div>
</div>
<div class="mb-3">
<label class="form-label">Help</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-info-circle"></i></span>
<input type="text" class="form-control" name="help" maxlength="255" placeholder="Shown under it - as on the label on the back" value="<?= escapeHtml($fill_in['help'] ?? '') ?>">
</div>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" name="required" value="1" id="documentFillInRequired" <?= !empty($fill_in['required']) ? 'checked' : '' ?>>
<label class="form-check-label" for="documentFillInRequired">Must be answered to make a document from it</label>
</div>
</div>
<div class="modal-footer">
<button type="submit" name="save_document_template_fill_in" class="btn btn-primary text-bold"><i class="fa fa-check me-2"></i>Save</button>
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fa fa-times me-2"></i>Cancel</button>
</div>
</form>
<script>
(function () {
// The choices are for a list only
var type = document.getElementById('documentFillInType');
var choices = document.getElementById('documentFillInChoices');
if (type && choices) {
type.addEventListener('change', function () {
choices.hidden = type.value !== 'list';
});
}
})();
</script>
<?php }
require_once '../../../includes/modal_footer.php';

View File

@@ -5,6 +5,9 @@
* $template (documentTemplateRow(), or [] for a new one) before including it.
* Posts what documentTemplateFields() reads (functions/document_template.php).
* With an AI model set up for documentation, it can be drafted from a prompt.
* Under the editor, the placeholders filled in from the client and the tables
* of its records put themselves in where the cursor is
* (js/document_template_form.js).
*
* Should not be accessed directly.
*/
@@ -49,7 +52,20 @@ $ai_model = getAiModel('Documentation');
<label class="form-label" for="documentTemplateContent">What it says</label>
<textarea class="form-control tinymce" id="documentTemplateContent" name="content"><?= escapeHtml($t['document_template_content'] ?? '') ?></textarea>
</div>
<div class="small text-secondary">Words in square brackets - <span class="font-monospace">[Client]</span>, <span class="font-monospace">[Date]</span> - are left in the document for whoever writes it to fill in. Pasted images are kept with the template and copied into each document made from it.</div>
<div class="small text-secondary mb-1">Filled in from the client as a document is made - click one to put it where the cursor is:</div>
<div class="d-flex flex-wrap gap-1 mb-2">
<?php foreach (DOCUMENT_TEMPLATE_PLACEHOLDERS as $placeholder => $means) { ?>
<button type="button" class="btn btn-sm btn-light border font-monospace" data-document-template-word="<?= escapeHtml($placeholder) ?>" title="<?= escapeHtml(ucfirst($means)) ?>">[<?= escapeHtml($placeholder) ?>]</button>
<?php } ?>
</div>
<div class="small text-secondary mb-1">The client's records as a table, on a line of their own - as they are when the document is made:</div>
<div class="d-flex flex-wrap gap-1 mb-2">
<?php foreach (DOCUMENT_TEMPLATE_TABLES as $table => $holds) { ?>
<button type="button" class="btn btn-sm btn-light border font-monospace text-info-emphasis" data-document-template-word="<?= escapeHtml($table) ?>" data-document-template-table title="<?= escapeHtml(ucfirst($holds)) ?>">[<?= escapeHtml($table) ?>]</button>
<?php } ?>
</div>
<div class="small text-secondary">Any other word in square brackets - <span class="font-monospace">[Firewall Model]</span> - is asked for when a document is made, and set up on the template's page. Pasted images are kept with the template and copied into each document made from it.</div>
<script src="/js/document_template_form.js"></script>
<?php if ($ai_model) { ?>
<script>

View File

@@ -2,8 +2,11 @@
/*
* New document for a client from a document template - from Admin > Document
* Templates and the template's page. It goes in the client's Files, at the
* top level, and opens (add_document_from_template, agent/post/document.php).
* Templates and the template's page. When the client is picked, what the
* template asks for - the site and contact its placeholders come from, and its
* fill-ins - is drawn under it (agent/js/document_template_make.js). It goes in
* the client's Files, at the top level, and opens (add_document_from_template,
* agent/post/document.php).
*/
require_once '../../includes/modal_header.php';
@@ -24,16 +27,16 @@ if (!$template || $template['document_template_archived_at']) { ?>
<h5 class="modal-title text-truncate"><i class="fa fa-fw fa-file-alt me-2"></i>New document from <strong><?= escapeHtml($template['document_template_name']) ?></strong></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="/agent/post.php" method="post" autocomplete="off">
<form action="/agent/post.php" method="post" autocomplete="off" data-doc-template-make>
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="hidden" name="document_template_id" value="<?= intval($template['document_template_id']) ?>">
<input type="hidden" name="document_template_id" value="<?= intval($template['document_template_id']) ?>" data-doc-template-select>
<div class="modal-body">
<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="fa fa-fw fa-user"></i></span>
<select class="form-select select2" name="client_id" required>
<select class="form-select select2" name="client_id" required data-doc-template-client>
<option value="">- Pick a client -</option>
<?php
$sql_clients = mysqli_query($mysqli, "SELECT client_id, client_name FROM clients WHERE client_archived_at IS NULL ORDER BY client_name ASC");
@@ -44,6 +47,9 @@ if (!$template || $template['document_template_archived_at']) { ?>
</div>
</div>
<!-- What the template asks for, for the client picked - agent/js/document_template_make.js -->
<div class="mb-3" data-doc-template-asks hidden></div>
<div class="mb-3">
<label class="form-label">Name</label>
<div class="input-group">
@@ -67,6 +73,8 @@ if (!$template || $template['document_template_archived_at']) { ?>
</div>
</form>
<script src="/agent/js/document_template_make.js"></script>
<?php }
require_once '../../../includes/modal_footer.php';

View File

@@ -2,7 +2,8 @@
/*
* Document templates (Admin > Document Templates) - making, changing,
* copying, archiving and deleting them, with their images. The work is in
* copying, archiving and deleting them, with their images, and setting up
* what they ask for when a document is made (their fill-ins). The work is in
* functions/document_template.php; a document is made from one by
* add_document_from_template (agent/post/document.php).
*/
@@ -95,3 +96,129 @@ if (isset($_GET['delete_document_template'])) {
redirect(!empty($_GET['from_page']) ? 'document_templates.php' : null);
}
if (isset($_POST['save_document_template_fill_in'])) {
// A fill-in set up - a new one, or one in the text or already set up, changed
validateCSRFToken();
$row = documentTemplateForRequest($_POST['document_template_id'] ?? 0);
$document_template_id = intval($row['document_template_id']);
$original = trim((string) ($_POST['original_name'] ?? ''));
$name = documentFillInName($_POST['name'] ?? '');
if ($name === '') {
flashAlert("A fill-in's name is what goes in square brackets - a letter first, then letters, digits, spaces, _ - or /, up to 41 in all. [Client], [Site] and the others filled in from the client, and the tables of records, are taken.", 'error');
redirect();
}
$type = isset(TICKET_FILL_IN_TYPES[$_POST['type'] ?? '']) ? $_POST['type'] : 'text';
$options = $type === 'list' ? ticketFillInOptions($_POST['options'] ?? '') : [];
if ($type === 'list' && count($options) < 2) {
flashAlert('A list needs at least two choices - one per line.', 'error');
redirect();
}
$required = intval($_POST['required'] ?? 0) ? 1 : 0;
$help = trim(mb_substr(preg_replace('/\s+/', ' ', strip_tags((string) ($_POST['help'] ?? ''))), 0, 255));
// One name, one fill-in - adding one already in the text but not set up yet sets that one up
foreach (documentTemplateFillInsFor($document_template_id) as $fill_in) {
if ($original === '' && !$fill_in['id'] && strtolower($fill_in['name']) === strtolower($name)) {
$original = $fill_in['name'];
continue;
}
if (strtolower($fill_in['name']) === strtolower($name) && strtolower($fill_in['name']) !== strtolower($original)) {
flashAlert("<strong>" . escapeHtml($row['document_template_name']) . "</strong> already asks for <strong>" . escapeHtml($fill_in['name']) . "</strong>.", 'error');
redirect();
}
}
$existing = $original !== '' ? mysqli_fetch_row(mysqli_query($mysqli, "SELECT document_fill_in_id FROM document_template_fill_ins
WHERE document_fill_in_document_template_id = $document_template_id AND document_fill_in_name = '" . escapeSql($original) . "' LIMIT 1")) : null;
// Renamed: its [Name] in what the template says goes with it
if ($original !== '' && $original !== $name) {
documentTemplateRenameFillIn($document_template_id, $original, $name);
}
$set = "document_fill_in_name = '" . escapeSql($name) . "', document_fill_in_type = '$type', document_fill_in_options = " . ($options ? "'" . escapeSql(implode("\n", $options)) . "'" : 'NULL') . ",
document_fill_in_required = $required, document_fill_in_help = " . ($help !== '' ? "'" . escapeSql($help) . "'" : 'NULL');
if ($existing) {
mysqli_query($mysqli, "UPDATE document_template_fill_ins SET $set WHERE document_fill_in_id = " . intval($existing[0]));
} else {
$order = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COALESCE(MAX(document_fill_in_order), 0) + 1 FROM document_template_fill_ins WHERE document_fill_in_document_template_id = $document_template_id"))[0]);
mysqli_query($mysqli, "INSERT INTO document_template_fill_ins SET document_fill_in_document_template_id = $document_template_id, document_fill_in_order = $order, $set");
}
logAudit("Document Template", "Edit", "$session_name " . ($original === '' ? 'added' : 'changed') . " fill-in " . escapeSql($name) . " on document template " . escapeSql($row['document_template_name']), 0, $document_template_id);
flashAlert("Fill-in <strong>" . escapeHtml($name) . "</strong> saved" . ($original === '' ? " - put <code>[" . escapeHtml($name) . "]</code> in what it says where the answer goes" : ''));
redirect();
}
if (isset($_GET['delete_document_template_fill_in'])) {
// A fill-in's setup removed - gone, or, while its [Name] is still in the text, asked as plain optional text
validateCSRFToken();
$fill_in_id = intval($_GET['delete_document_template_fill_in']);
$fill_in = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_fill_in_name, document_fill_in_document_template_id FROM document_template_fill_ins WHERE document_fill_in_id = $fill_in_id"));
$row = documentTemplateForRequest($fill_in['document_fill_in_document_template_id'] ?? 0);
$document_template_id = intval($row['document_template_id']);
$name = (string) $fill_in['document_fill_in_name'];
mysqli_query($mysqli, "DELETE FROM document_template_fill_ins WHERE document_fill_in_id = $fill_in_id");
$still_asked = false;
foreach (documentTemplateFillInsFor($document_template_id) as $other) {
$still_asked = $still_asked || strtolower($other['name']) === strtolower($name);
}
logAudit("Document Template", "Edit", "$session_name removed fill-in " . escapeSql($name) . " from document template " . escapeSql($row['document_template_name']), 0, $document_template_id);
flashAlert($still_asked
? "Fill-in <strong>" . escapeHtml($name) . "</strong> reset - <code>[" . escapeHtml($name) . "]</code> is still in what it says, so it is asked for as plain, optional text"
: "Fill-in <strong>" . escapeHtml($name) . "</strong> removed", 'error');
redirect();
}
if (isset($_POST['reorder_document_template_fill_ins'])) {
// Dragged into order on the template's page - answers with JSON, as the page saves it in the background.
// Every one dragged keeps its place from now on, so each gets a row.
header('Content-Type: application/json');
if (!hash_equals((string) ($_SESSION['csrf_token'] ?? ''), (string) ($_POST['csrf_token'] ?? ''))) {
echo json_encode(['ok' => false, 'error' => 'The page is out of date - reload it']);
exit;
}
$row = documentTemplateRow($_POST['document_template_id'] ?? 0);
if (!$row) {
echo json_encode(['ok' => false, 'error' => 'That document template no longer exists']);
exit;
}
$document_template_id = intval($row['document_template_id']);
$fill_ins = [];
foreach (documentTemplateFillInsFor($document_template_id) as $fill_in) {
$fill_ins[strtolower($fill_in['name'])] = $fill_in;
}
$order = 0;
foreach ((array) ($_POST['names'] ?? []) as $name) {
$fill_in = $fill_ins[strtolower((string) $name)] ?? null;
if (!$fill_in) {
continue;
}
$order++;
if ($fill_in['id']) {
mysqli_query($mysqli, "UPDATE document_template_fill_ins SET document_fill_in_order = $order WHERE document_fill_in_id = " . intval($fill_in['id']));
} else {
mysqli_query($mysqli, "INSERT INTO document_template_fill_ins SET document_fill_in_document_template_id = $document_template_id, document_fill_in_name = '" . escapeSql($fill_in['name']) . "', document_fill_in_type = 'text', document_fill_in_order = $order");
}
}
logAudit("Document Template", "Edit", "$session_name reordered the fill-ins on document template " . escapeSql($row['document_template_name']), 0, $document_template_id);
echo json_encode(['ok' => true]);
exit;
}

View File

@@ -250,7 +250,7 @@ if (isset($_POST['save_ticket_template_fill_in'])) {
logAudit("Ticket Template", "Edit", "$session_name " . ($original === '' ? 'added' : 'changed') . " fill-in " . escapeSql($name) . " on ticket template $template_name", 0, $ticket_template_id);
flashAlert("Fill-in <strong>" . escapeHtml($name) . "</strong> saved" . ($original === '' ? " - put <span class='font-monospace'>[" . escapeHtml($name) . "]</span> in the text where the answer goes" : ''));
flashAlert("Fill-in <strong>" . escapeHtml($name) . "</strong> saved" . ($original === '' ? " - put <code>[" . escapeHtml($name) . "]</code> in the text where the answer goes" : ''));
redirect();
@@ -278,7 +278,7 @@ if (isset($_GET['delete_ticket_template_fill_in'])) {
logAudit("Ticket Template", "Edit", "$session_name removed fill-in " . escapeSql($name) . " from ticket template " . escapeSql($row['ticket_template_name']), 0, $ticket_template_id);
flashAlert($still_asked
? "Fill-in <strong>" . escapeHtml($name) . "</strong> reset - <span class='font-monospace'>[" . escapeHtml($name) . "]</span> is still in the text, so it is asked for as plain, optional text"
? "Fill-in <strong>" . escapeHtml($name) . "</strong> reset - <code>[" . escapeHtml($name) . "]</code> is still in the text, so it is asked for as plain, optional text"
: "Fill-in <strong>" . escapeHtml($name) . "</strong> removed", 'error');
redirect();