Document Templates - documents ready to start a client's from: a * runbook, a build sheet, a plan. Each row shows what it is for, how many * documents have been made from it and for how many clients, when it was * last used and last changed. A template can be changed, copied as the start * of another, archived - not offered for new documents, while the documents * made from it stay - or deleted. The work is in functions/document_template.php; * the handlers are admin/post/document_template.php. */ // Default Column Sortby Filter $sort = "document_template_name"; $order = "ASC"; require_once "includes/inc_all_admin.php"; $show_archived = ($_GET['show'] ?? '') === 'archived'; // Only these sort - an unknown column would take the page down $sortable_columns = [ 'document_template_name' => "document_template_name $order", 'used_count' => "used_count $order, document_template_name ASC", 'last_used' => "last_used IS NULL, last_used $order, document_template_name ASC", 'changed_at' => "changed_at $order, document_template_name ASC", ]; if (!isset($sortable_columns[$sort])) { $sort = 'document_template_name'; } $search = "(document_template_name LIKE '%$q%' OR document_template_description LIKE '%$q%' OR document_template_content LIKE '%$q%')"; // Its use is the documents made from it that are not archived - as documentTemplateUsage() counts them $sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS document_templates.document_template_id, document_template_name, document_template_description, document_template_content, document_template_created_at, document_template_updated_at, document_template_archived_at, COALESCE(document_template_updated_at, document_template_created_at) AS changed_at, COALESCE(updater.user_name, creator.user_name) AS changed_by, COALESCE(used.used_count, 0) AS used_count, COALESCE(used.client_count, 0) AS client_count, used.last_used FROM document_templates LEFT JOIN users creator ON creator.user_id = document_template_created_by AND document_template_created_by > 0 LEFT JOIN users updater ON updater.user_id = document_template_updated_by AND document_template_updated_by > 0 LEFT JOIN ( SELECT document_template_id AS used_template_id, COUNT(*) AS used_count, COUNT(DISTINCT document_client_id) AS client_count, MAX(document_created_at) AS last_used FROM documents WHERE document_template_id > 0 AND document_archived_at IS NULL GROUP BY document_template_id ) used ON used.used_template_id = document_templates.document_template_id WHERE $search AND document_template_archived_at IS " . ($show_archived ? 'NOT NULL' : 'NULL') . " ORDER BY {$sortable_columns[$sort]}, document_templates.document_template_id ASC LIMIT $record_from, $record_to"); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); $counts = mysqli_fetch_row(mysqli_query($mysqli, "SELECT COALESCE(SUM(document_template_archived_at IS NULL), 0), COALESCE(SUM(document_template_archived_at IS NOT NULL), 0) FROM document_templates WHERE $search")); $sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) { return '' . $label . ($sort === $column ? " $order_icon" : '') . ''; }; $pill_url = function ($archived) { return '?' . http_build_query(array_filter(['q' => $_GET['q'] ?? '', 'show' => $archived ? 'archived' : ''])); }; $add_url = 'modals/document_template/document_template_add.php'; $csrf = $_SESSION['csrf_token']; ?>
| = $sort_link('document_template_name', 'Template') ?> | = $sort_link('used_count', 'Used') ?> | = $sort_link('last_used', 'Last used') ?> | = $sort_link('changed_at', 'Changed') ?> | |
|---|---|---|---|---|
|
= $name ?>
= escapeHtml($summary) ?>
Archived = date('M j, Y', strtotime($row['document_template_archived_at'])) ?>
= $used_count ? $used_text . ' · last ' . timeAgo($row['last_used']) : 'Not used yet' ?>
|
= $used_count ? escapeHtml($used_text) : 'Not yet' ?> | >= $row['last_used'] ? timeAgo($row['last_used']) : '-' ?> |
= timeAgo($row['changed_at']) ?>
= escapeHtml($row['changed_by']) ?>
|
|