mirror of
https://github.com/itflow-org/itflow
synced 2026-12-05 18:49:01 +00:00
Document templates stage 3, the client standard and reviews: a template can be required of every client or of clients with chosen tags (leads and archived clients not counted, a template whose tags are all deleted applying to none rather than every client), with the template page showing how many clients have one with a bar and the clients missing it each with a Make button, and Admin > Document Templates showing the count on each template plus a new Required tab; documents can be made for several clients at once, one per client filled in from its own client, for a picked list or every missing client in one click, with fill-ins answered once for all and clients that already have one skipped unless unticked; a client's Files shows the documents it should have along the top opening Document from template with that template picked, the client overview shows a Documents missing count, and Document from template lists missing templates first; a template can ask for its documents to be reviewed every 3 or 6 months, yearly or every 2 years, a document falling due when not edited or marked reviewed for that long, with the document page showing Review due or the next review and who did the last one plus a Mark reviewed button, a Review due tab in Files listing due documents across every folder, and due counts on the client overview, the template page and the list, documents from archived templates not counted; an older document written before its template can be linked to it from its page so it counts and follows the template's review schedule; migration 3.2.7; fixed tag merges dropping merged tags from ticket templates (merges now carry over ticket template and required template tags) and deleting a document template marking all its documents as updated; changelog updated
This commit is contained in:
16
CHANGELOG.md
16
CHANGELOG.md
@@ -6,9 +6,9 @@ This file documents all notable changes made to ITFlow.
|
||||
|
||||
### Upgrading to 26.11
|
||||
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's database changes (2.7.9 to 3.2.6) in the same pass. Nothing else is required.
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's database changes (2.7.9 to 3.2.7) in the same pass. Nothing else is required.
|
||||
|
||||
This is a big release, with forty-seven database steps in one go, so take a backup before you update.
|
||||
This is a big release, with forty-eight database steps in one go, so take a backup before you update.
|
||||
|
||||
The update also clears out data that deletes in older versions left behind, all of it pointing at something that no longer exists:
|
||||
|
||||
@@ -47,7 +47,7 @@ Ticket templates keep their subject, details and tasks, and set nothing else on
|
||||
|
||||
Canned responses keep their names, what they say and their ticket category. One whose category had been deleted is offered on every ticket, as it already was. How often each is used is counted from the update. None sets up anything on the reply until you give it a reply type, status or time worked on its Sets up tab. Every canned response already there is shared - offered to everyone, as before - and none has files.
|
||||
|
||||
Document templates keep their names, summaries, what they say and their images. Words in square brackets already in them are now filled in when a document is made: [Client], [Site], [Contact], [Date] and the others listed under New Features (Document templates) from the client, and the rest are asked for as plain, optional text until you set them up on the template's page - one left blank stays as it is, as before. Documents made from a template before the update don't know which one, so a template's use is counted from the update.
|
||||
Document templates keep their names, summaries, what they say and their images. Words in square brackets already in them are now filled in when a document is made: [Client], [Site], [Contact], [Date] and the others listed under New Features (Document templates) from the client, and the rest are asked for as plain, optional text until you set them up on the template's page - one left blank stays as it is, as before. Documents made from a template before the update don't know which one, so a template's use is counted from the update - link one to its template from the document's page to count it. No template is required of anyone, and none asks for reviews, until you set it up on its page.
|
||||
|
||||
If you are coming from 26.08 or earlier, the database update still has to be run from the command line one time, exactly as described under Upgrading to 26.09 below.
|
||||
|
||||
@@ -532,6 +532,11 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- Tables of records: [Contacts], [Locations], [Assets], [Servers], [Workstations], [Network Equipment], [Printers], [Networks], [Domains], [Certificates], [Licenses], [Vendors] and [Services] put in the client's records as they are when the document is made. On a line of its own, it becomes a table - [Networks] gives each network's VLAN, network, gateway, DHCP range and site, [Servers] each server's operating system, IP address and site, [Domains] each domain's registrar and expiry - with an empty row to fill in when the client has none. In a sentence, it becomes their names. The template's page lists the ones it uses and what each holds, and the template window has a button for each placeholder and table to put it in.
|
||||
- Fill-ins: any other word in square brackets in a template's name, summary or what it says - [Firewall Model], [Review Date] - is asked for when a document is made, and the answer goes in wherever it is. On the template's page each fill-in can be made a date, a choice from a list or yes or no, made required, given a line of help, renamed (it is renamed in the template too) and dragged into the order it is asked in. One left blank stays for whoever writes the document.
|
||||
- New document for a client and Document from template show what the template fills in from the client, the site and contact to use, and its fill-ins, as soon as the template and client are picked. A copied template keeps its fill-ins.
|
||||
- Who should have one: a template can be required of every client, or of the clients with a client tag you pick (Managed, say) - leads and archived clients don't count. Its page shows how many of them have a document from it, with a bar, and lists the ones missing one, each with a Make button. Admin > Document Templates shows how many have one on each, and a Required tab lists them.
|
||||
- Several clients at once: New documents for several clients, from the template's page or the list, makes one for each client picked - or for every client missing one - each filled in from its own client with its primary site and contact, the fill-ins answered once for all of them. Clients that already have one are skipped unless you say otherwise.
|
||||
- A client's Files lists the documents it should have across the top, each a click from making it, and its overview counts them under Documents missing. Document from template lists them first.
|
||||
- Reviews: a template can say its documents are reviewed every 3 or 6 months, every year or every 2 years. A document is due once it hasn't been edited or marked as reviewed for that long. It says so on its page - Review due, or when the next review is and who last reviewed it - with Mark reviewed. Files marks the ones due and has a Review due tab listing them from every folder, the client's overview counts them, and the template's page and the list show how many are due. An archived template's documents aren't counted.
|
||||
- A document can be linked to the template it counts as made from, from its page - one written before the template, say - so it counts towards who has one and is reviewed as the template says. What it says stays as it is.
|
||||
- Templates can be copied (with their images), archived - not offered for new documents, while the documents made from them stay - restored and deleted. A document made from one remembers it, and its page says which template it came from.
|
||||
- The template window has its icons, a summary, and Draft it with AI when an AI model is set up for documentation. A draft uses the placeholders and tables of records, and puts what else is to be filled in in square brackets.
|
||||
- Document from template in a client's Files shows each template's summary and how often it has been used. A document made with no name or summary given takes the template's.
|
||||
@@ -1407,6 +1412,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- Removed: `agent/post/tag.php` and `agent/post/tag_model.php` (an older copy of `add_tag` for the window opened from agent pages), and `admin/post/tag_model.php`.
|
||||
- `agent/modals/billing/billing_item.php` adds a seat line's own tag to its choices when `recurringSeatOptions()` leaves it out.
|
||||
- CSS: `.itflow-tag` takes its text color from `--itflow-tag-text`; new `.itflow-tag-swatch`, `.itflow-tag-icons`, `.itflow-tag-icon` and `.itflow-tag-icon-custom`.
|
||||
- `tagMerge()` moves `ticket_template_tags` and `document_template_required_tags` to the tag merged into, so a ticket template keeps its tags and a document template the clients it is required of.
|
||||
- Tags on more records (database 3.1.4):
|
||||
- New link tables `software_tags`, `service_tags`, `network_tags`, `document_tags` and `ticket_tags` - the record's id and `tag_id`, keyed together and on `tag_id`, gone with the record or the tag. Tag types 8 (license - the `software` table), 9 (service), 10 (network), 11 (document) and 12 (ticket) in `tagKinds()` and `tagEntity()`; a document's `client_list` says its list (`files.php`) is one client's.
|
||||
- `functions/tags.php`: `tagListUrl($entity, $tag)` (the list filtered by it, or `global_search.php?query=` for documents), `tagFilterIds()` (`?tags[]=`), `tagFilterSql($entity, $ids, $column)`, `tagSearchSql($entity, $like, $column)`, `tagFilterOptionsHtml($entity, $selected, $records_where)` (the tags on records the list can show, its client joined, plus those chosen), `tagOptionHtml($row, $selected)`, `tagFormFieldHtml($entity, $selected, $placeholder)` (with the `tags_form` marker), `tagIdsForRecord()`, `tagChangeText($before, $after)`, `tagExportFieldHtml()`, `tagNamesSql()` and `tagExportFilterSql($entity, $values, &$summary, $column)`, `tagMatchSql()` and `tagSearchSpec()` for search, and `tagApiRow()` and `tagApiAttach($rows, $entity)` for the API. `tagsForRecords()` adds `tag_type`.
|
||||
@@ -1517,6 +1523,10 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- `functions/document_template.php`: `DOCUMENT_TEMPLATE_PLACEHOLDERS`, `DOCUMENT_TEMPLATE_SITE_PLACEHOLDERS`, `DOCUMENT_TEMPLATE_CONTACT_PLACEHOLDERS` and `DOCUMENT_TEMPLATE_TABLES` (name => what it is); `documentFillInName()` ('' for a placeholder's or table's name), `documentTemplateWordKind($word)` (`placeholder`, `table` or `fill_in`), `documentTemplateBracketsInAll($template)` and `documentTemplateWordsUsed($template)` (its name, summary and content), `documentTemplateFillInRowsFor($ids)`, `documentTemplateFillIns($template, $rows)` and `documentTemplateFillInsFor($id)` (the ones set up, then the rest in the text as optional text, each with `in_text`) - in the shape `ticketFillInAnswers()` and `ticketFillInFieldHtml()` take - `documentTemplateRenameFillIn()`, `documentTemplateClientLocations()` and `documentTemplateClientContacts()` (primary first), `documentTemplateValues($client_id, $location_id, $contact_id, $agent_name)` (-1 for none, 0 or another client's for the primary), `documentTemplateTableData($table, $client_id)` (`head`, `rows`, `names`), `documentTemplateTableHtml()` and `documentTemplateFill($html, $client_id, $values)`. `documentTemplateMakeDocument()` takes `$location_id`, `$contact_id` and `$answers` (from `ticketFillInAnswers()`) after `$folder_id`, and fills the name, summary and content. `documentTemplateCopy()` copies the fill-ins, and `documentTemplateMarkBrackets()` marks each kind in its own color.
|
||||
- Admin: new `admin/modals/document_template/document_template_fill_in.php` (`document_template_id`, `name`). `admin/post/document_template.php`: `save_document_template_fill_in` (`original_name` to change or rename one), GET `delete_document_template_fill_in` (reset while still in the text, otherwise removed) and POST `reorder_document_template_fill_ins` (`names[]`, answers JSON). Audit entries are Document Template Edit.
|
||||
- `agent/ajax.php?document_template_asks=ID&client_id=ID` returns `{html}` - what the template fills in, the `location_id` and `contact_id` pickers when it uses them, and its fill-ins as `fill_ins[Name]` - or '' without Support at edit level, access to the client, or a live template. `add_document_from_template` checks the answers with `ticketFillInAnswers()` and takes `location_id` and `contact_id`. New `agent/js/document_template_make.js` draws it in a form with `data-doc-template-make`, from its `data-doc-template-select` and `data-doc-template-client` fields into `data-doc-template-asks`; new `js/document_template_form.js` puts a `data-document-template-word` button's word in the editor (on a line of its own with `data-document-template-table`). `ai_create_document_template` tells the model the placeholders and tables.
|
||||
- Database 3.2.7: `document_templates.document_template_required` (0 no one, 1 every client, 2 the clients with one of its tags - none with no tags left) and `document_template_review_months` (0 never); new `document_template_required_tags` (`document_template_id`, `tag_id` - client tags, deleted with either); `documents.document_reviewed_at` and `document_reviewed_by`.
|
||||
- `functions/document_template.php`: `DOCUMENT_TEMPLATE_REQUIRED_NONE`, `_EVERY` and `_TAGGED`, `DOCUMENT_TEMPLATE_REVIEW_MONTHS`; `documentTemplateRequiredTags($ids)`, `documentTemplateSetRequired($id, $required, $tag_ids, $months)` (leaves `document_template_updated_at` alone), `documentTemplateScopeSql($required, $tag_ids)` (SQL on clients - current, not leads), `documentTemplateScopeText()`, `documentTemplateCoverage($id, $required, $tag_ids)` (`clients`, `covered`, `missing`), `documentTemplatesMissingFor($client_id)`, `documentReviewedAtSql()` (the latest of made, edited and reviewed), `documentReviewDueSql()` (on documents joined to document_templates), `documentsReviewDue($where)`, `documentTemplateDueCounts($ids)`, `documentReviewState($row)` (`last`, `reviewed`, `next`, `due`) and `documentTemplateMakeForClients($id, $client_ids, $answers, $skip_existing)`. `documentTemplateRow()` returns the two new columns; `documentTemplateCopy()` copies the review period and tags, not required; `documentTemplateDelete()` no longer touches its documents' `document_updated_at`.
|
||||
- Admin: new `admin/modals/document_template/document_template_required.php` (`id`) and `document_template_use_several.php` (`id`, `missing=1`); `document_template_use.php` takes `client_id`; `admin/document_templates.php` takes `show=required`. `admin/post/document_template.php`: `save_document_template_required` (`required`, `tags[]`, `review_months`) and `make_documents_from_template` (`client_ids[]`, `skip_existing`, `fill_ins`; a Document Create audit entry per document, under its client).
|
||||
- Agent: `agent/post/document.php` - GET `mark_document_reviewed` and POST `set_document_template` (`document_template_id`, 0 for none; an archived one only if it is already the one), both Support at edit level, neither touching `document_updated_at`; new `agent/modals/document/document_template_link.php` (`id`). `agent/files.php` takes `type=due`. `clientOverviewAttention()` adds Documents missing and Document reviews due.
|
||||
- Empty states: every list page sets `$empty_state` before including `includes/filter_footer.php`, and `includes/inc_empty_state.php` draws it with `emptyStateHtml()` (`functions/format.php`) when the list has nothing - `thing`, `icon`, `title`, `text`, `actions` (`label`, `modal` or `href`, `size`, `icon`; the first is the main one), `archived`, `view`, `filtered`, `views`, `match_title`, `match_text` and `clear_url`. `emptyStateFiltered($views)` reads the GET string for a search or filter, leaving out paging, sorting, `client_id`, `archived` and the page's views, and `emptyStateClearUrl($views)` is the page without them. `emptyStatePill($pills, $key, $default, $good)` builds a pill's own state from the page's pill array (`label`, `icon`, `title`, `alarm`), with a tick for `alarm` pills and the keys in `$good`. Files, notifications and racks call `emptyStateHtml()` in their own layout; files and notifications set `$empty_state_skip`. A page that sets nothing still gets one, worded from `$page_title`. The pages' own empty blocks (`.itflow-asset-empty` under a list) are gone. CSS: `.itflow-empty-state` with `-icon` (a grey round tile), `-title`, `-text` and `-actions`.
|
||||
- Removed unused helpers and files:
|
||||
- `credentialLink()`, `projectLink()`, `linkCounts()`, `assetContactAvatar()`, `clientOverviewPersonInitials()`, and `getMonthlyTax()` and `getQuarterlyTax()`, which looked a tax up by name
|
||||
|
||||
29
admin/database_updates/3.2.7.php
Normal file
29
admin/database_updates/3.2.7.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Database update to version 3.2.7 (from 3.2.6)
|
||||
* Included by admin/database_updates.php - do not access directly
|
||||
*/
|
||||
|
||||
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
|
||||
|
||||
// Who should have a document from a template - 0 no one in particular, 1
|
||||
// every client, 2 the clients with one of its tags - and how often the
|
||||
// documents made from it are reviewed, in months (0 never)
|
||||
mysqli_query($mysqli, "ALTER TABLE `document_templates`
|
||||
ADD `document_template_required` tinyint(1) NOT NULL DEFAULT 0 AFTER `document_template_content`,
|
||||
ADD `document_template_review_months` int(11) NOT NULL DEFAULT 0 AFTER `document_template_required`");
|
||||
|
||||
mysqli_query($mysqli, "CREATE TABLE `document_template_required_tags` (
|
||||
`document_template_id` int(11) NOT NULL,
|
||||
`tag_id` int(11) NOT NULL,
|
||||
PRIMARY KEY (`document_template_id`,`tag_id`),
|
||||
KEY `tag_id` (`tag_id`),
|
||||
CONSTRAINT `document_template_required_tags_ibfk_1` FOREIGN KEY (`document_template_id`) REFERENCES `document_templates` (`document_template_id`) ON DELETE CASCADE,
|
||||
CONSTRAINT `document_template_required_tags_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`tag_id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
|
||||
|
||||
// When a document was last marked as reviewed, and by whom - an edit counts as a review too
|
||||
mysqli_query($mysqli, "ALTER TABLE `documents`
|
||||
ADD `document_reviewed_at` datetime DEFAULT NULL AFTER `document_accessed_at`,
|
||||
ADD `document_reviewed_by` int(11) NOT NULL DEFAULT 0 AFTER `document_updated_by`");
|
||||
@@ -6,7 +6,9 @@
|
||||
* 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
|
||||
* them - and who should have one: the clients missing one, and the documents
|
||||
* made from it due a review. A new document can be made from here for a
|
||||
* client, for several at once, or for every client missing one. The work is in
|
||||
* functions/document_template.php.
|
||||
*/
|
||||
|
||||
@@ -42,8 +44,20 @@ while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$recent[] = $row;
|
||||
}
|
||||
|
||||
// Who should have one, and the documents due a review - neither counts while it is archived
|
||||
$required = intval($template['document_template_required']);
|
||||
$required_tags = documentTemplateRequiredTags([$document_template_id])[$document_template_id];
|
||||
$scope_text = documentTemplateScopeText($required, $required_tags);
|
||||
$coverage = $required && !$archived ? documentTemplateCoverage($document_template_id, $required, array_keys($required_tags)) : null;
|
||||
$review_months = intval($template['document_template_review_months']);
|
||||
$due = $review_months && !$archived ? documentsReviewDue("documents.document_template_id = $document_template_id") : [];
|
||||
$list_limit = 8;
|
||||
$missing_text = $coverage && $coverage['missing'] ? (count($coverage['missing']) === 1 ? 'the client missing one' : 'the ' . count($coverage['missing']) . ' clients missing one') : '';
|
||||
|
||||
$edit_url = "modals/document_template/document_template_edit.php?id=$document_template_id";
|
||||
$use_url = "modals/document_template/document_template_use.php?id=$document_template_id";
|
||||
$several_url = "modals/document_template/document_template_use_several.php?id=$document_template_id";
|
||||
$required_url = "modals/document_template/document_template_required.php?id=$document_template_id";
|
||||
|
||||
?>
|
||||
|
||||
@@ -68,7 +82,16 @@ $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-size="lg" data-modal-url="<?= $use_url ?>"><i class="fas fa-fw fa-plus me-2"></i>New document for a client</button>
|
||||
<div class="btn-group">
|
||||
<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>
|
||||
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-label="More ways to make one"></button>
|
||||
<div class="dropdown-menu dropdown-menu-end">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="<?= $several_url ?>"><i class="fas fa-fw fa-copy me-2"></i>For several clients</a>
|
||||
<?php if ($coverage && $coverage['missing']) { ?>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="<?= $several_url ?>&missing=1"><i class="fas fa-fw fa-clipboard-check me-2"></i>For <?= $missing_text ?></a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
<?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">
|
||||
@@ -105,6 +128,82 @@ $use_url = "modals/document_template/document_template_use.php?id=$document_temp
|
||||
|
||||
<div class="col-lg-4 d-print-none">
|
||||
|
||||
<div class="card mb-3" id="documentTemplateCoverage">
|
||||
<div class="card-header d-flex align-items-center">
|
||||
<h3 class="card-title mb-0"><i class="fas fa-fw fa-clipboard-check me-2"></i>Who should have one</h3>
|
||||
<button type="button" class="btn btn-sm btn-link ms-auto ajax-modal" data-modal-url="<?= $required_url ?>"><i class="fas fa-edit me-1"></i>Set up</button>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<?php if (!$required && !$review_months) { ?>
|
||||
<div class="small text-secondary">No one in particular, and no reviews. Set it up to see which clients are missing one - every client, or the ones with a tag - and which documents made from it are due a review.</div>
|
||||
<?php } elseif ($archived) { ?>
|
||||
<div class="small text-secondary"><?= escapeHtml($scope_text ?: 'No one in particular') ?><?= $review_months ? ', reviewed ' . lcfirst(DOCUMENT_TEMPLATE_REVIEW_MONTHS[$review_months]) : '' ?> - not counted while it is archived.</div>
|
||||
<?php } else { ?>
|
||||
<?php if ($coverage) {
|
||||
$percent = $coverage['clients'] ? intval(floor($coverage['covered'] * 100 / $coverage['clients'])) : 100;
|
||||
?>
|
||||
<div class="d-flex align-items-baseline gap-2 mb-1">
|
||||
<span class="fw-semibold min-w-0 text-truncate" title="<?= escapeHtml($scope_text) ?>"><?= escapeHtml($scope_text) ?></span>
|
||||
<span class="ms-auto small text-nowrap"><strong><?= $coverage['covered'] ?></strong> of <?= $coverage['clients'] ?> have one</span>
|
||||
</div>
|
||||
<div class="progress mb-2" role="progressbar" aria-label="Clients with one" aria-valuenow="<?= $percent ?>" aria-valuemin="0" aria-valuemax="100" style="height: 6px;">
|
||||
<div class="progress-bar <?= $coverage['missing'] ? 'bg-warning' : 'bg-success' ?>" style="width: <?= $percent ?>%"></div>
|
||||
</div>
|
||||
<?php if ($coverage['missing']) { ?>
|
||||
<div class="small text-secondary mb-1">Missing one</div>
|
||||
<div class="list-group list-group-flush small mb-2">
|
||||
<?php foreach (array_slice($coverage['missing'], 0, $list_limit, true) as $client_id => $client_name) { ?>
|
||||
<div class="list-group-item px-0 d-flex align-items-center gap-2" data-missing-client-id="<?= $client_id ?>">
|
||||
<i class="fas fa-fw fa-user text-secondary"></i>
|
||||
<a class="min-w-0 flex-grow-1 text-truncate text-reset" href="/agent/files.php?client_id=<?= $client_id ?>"><?= escapeHtml($client_name) ?></a>
|
||||
<button type="button" class="btn btn-sm btn-light py-0 ajax-modal" data-modal-size="lg" data-modal-url="<?= $use_url ?>&client_id=<?= $client_id ?>"><i class="fas fa-plus me-1"></i>Make</button>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php if (count($coverage['missing']) > $list_limit) { ?>
|
||||
<div class="small text-secondary mb-2">and <?= count($coverage['missing']) - $list_limit ?> more</div>
|
||||
<?php } ?>
|
||||
<button type="button" class="btn btn-sm btn-outline-primary ajax-modal" data-modal-size="lg" data-modal-url="<?= $several_url ?>&missing=1"><i class="fas fa-copy me-1"></i>Make for <?= $missing_text ?></button>
|
||||
<?php } elseif ($coverage['clients']) { ?>
|
||||
<div class="small text-success"><i class="fas fa-fw fa-check me-1"></i>Every one of them has one.</div>
|
||||
<?php } else { ?>
|
||||
<div class="small text-secondary"><?= $required === DOCUMENT_TEMPLATE_REQUIRED_TAGGED ? ($required_tags ? 'No clients have ' . (count($required_tags) === 1 ? 'that tag' : 'those tags') : 'No tags picked, so no clients') : 'No clients yet' ?>.</div>
|
||||
<?php } ?>
|
||||
<?php } else { ?>
|
||||
<div class="small text-secondary">No one in particular.</div>
|
||||
<?php } ?>
|
||||
|
||||
<div class="border-top mt-3 pt-3 small">
|
||||
<?php if (!$review_months) { ?>
|
||||
<span class="text-secondary"><i class="fas fa-fw fa-calendar-check me-1"></i>The documents made from it aren't reviewed.</span>
|
||||
<?php } else { ?>
|
||||
<div class="d-flex align-items-baseline gap-2 mb-1">
|
||||
<span><i class="fas fa-fw fa-calendar-check me-1 text-secondary"></i>Reviewed <?= lcfirst(DOCUMENT_TEMPLATE_REVIEW_MONTHS[$review_months]) ?></span>
|
||||
<span class="ms-auto text-nowrap <?= $due ? 'text-warning-emphasis fw-semibold' : 'text-success' ?>"><?= $due ? count($due) . ' due' : '<i class="fas fa-check me-1"></i>None due' ?></span>
|
||||
</div>
|
||||
<?php if ($due) { ?>
|
||||
<div class="list-group list-group-flush">
|
||||
<?php foreach (array_slice($due, 0, $list_limit, true) as $due_id => $due_row) { ?>
|
||||
<a class="list-group-item list-group-item-action px-0 d-flex align-items-center gap-2" href="/agent/document.php?client_id=<?= intval($due_row['document_client_id']) ?>&document_id=<?= $due_id ?>" data-due-document-id="<?= $due_id ?>">
|
||||
<i class="fas fa-fw fa-file-alt text-secondary"></i>
|
||||
<span class="min-w-0 flex-grow-1">
|
||||
<span class="d-block text-truncate"><?= escapeHtml($due_row['client_name']) ?></span>
|
||||
<span class="d-block text-truncate text-secondary"><?= escapeHtml($due_row['document_name']) ?></span>
|
||||
</span>
|
||||
<span class="text-secondary text-nowrap" title="Last edited or reviewed <?= escapeHtml($due_row['reviewed_at']) ?>"><?= timeAgo($due_row['reviewed_at']) ?></span>
|
||||
</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php if (count($due) > $list_limit) { ?>
|
||||
<div class="text-secondary mt-1">and <?= count($due) - $list_limit ?> more</div>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mb-3">
|
||||
<div class="card-header"><h3 class="card-title mb-0"><i class="fas fa-fw fa-chart-line me-2"></i>Used</h3></div>
|
||||
<div class="card-body">
|
||||
|
||||
@@ -6,8 +6,11 @@
|
||||
* 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.
|
||||
* made from it stay - or deleted. One that every client, or the clients with
|
||||
* a tag, should have shows how many of them do, and one whose documents are
|
||||
* reviewed shows how many are due; the Required tab lists those it is
|
||||
* required of. The work is in functions/document_template.php; the handlers
|
||||
* are admin/post/document_template.php.
|
||||
*/
|
||||
|
||||
// Default Column Sortby Filter
|
||||
@@ -16,7 +19,15 @@ $order = "ASC";
|
||||
|
||||
require_once "includes/inc_all_admin.php";
|
||||
|
||||
$show_archived = ($_GET['show'] ?? '') === 'archived';
|
||||
// '' current, 'required' the current ones someone should have, 'archived'
|
||||
$show = (string) ($_GET['show'] ?? '');
|
||||
$show = in_array($show, ['required', 'archived'], true) ? $show : '';
|
||||
$show_archived = $show === 'archived';
|
||||
$show_where = [
|
||||
'' => 'document_template_archived_at IS NULL',
|
||||
'required' => 'document_template_archived_at IS NULL AND document_template_required > 0',
|
||||
'archived' => 'document_template_archived_at IS NOT NULL',
|
||||
][$show];
|
||||
|
||||
// Only these sort - an unknown column would take the page down
|
||||
$sortable_columns = [
|
||||
@@ -33,7 +44,7 @@ $search = "(document_template_name LIKE '%$q%' OR document_template_description
|
||||
|
||||
// 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,
|
||||
document_template_required, document_template_review_months, 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
|
||||
@@ -43,20 +54,36 @@ $sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS document_templates.docu
|
||||
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') . "
|
||||
WHERE $search AND $show_where
|
||||
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"));
|
||||
$rows = [];
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$rows[intval($row['document_template_id'])] = $row;
|
||||
}
|
||||
|
||||
// Who should have them and how many do, and the documents due a review - for the ones on show
|
||||
$required_tags = documentTemplateRequiredTags(array_keys($rows));
|
||||
$due_counts = documentTemplateDueCounts(array_keys($rows));
|
||||
$coverage = [];
|
||||
foreach ($rows as $template_id => $row) {
|
||||
if (intval($row['document_template_required']) && !$row['document_template_archived_at']) {
|
||||
$coverage[$template_id] = documentTemplateCoverage($template_id, $row['document_template_required'], array_keys($required_tags[$template_id]));
|
||||
}
|
||||
}
|
||||
|
||||
$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),
|
||||
COALESCE(SUM(document_template_archived_at IS NULL AND document_template_required > 0), 0) FROM document_templates WHERE $search"));
|
||||
|
||||
$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) {
|
||||
return '<a class="text-reset text-decoration-none" href="?' . escapeHtml($url_query_strings_sort) . '&sort=' . $column . '&order=' . $disp . '">'
|
||||
. $label . ($sort === $column ? " $order_icon" : '') . '</a>';
|
||||
};
|
||||
$pill_url = function ($archived) {
|
||||
return '?' . http_build_query(array_filter(['q' => $_GET['q'] ?? '', 'show' => $archived ? 'archived' : '']));
|
||||
$pill_url = function ($view) {
|
||||
return '?' . http_build_query(array_filter(['q' => $_GET['q'] ?? '', 'show' => $view]));
|
||||
};
|
||||
|
||||
$add_url = 'modals/document_template/document_template_add.php';
|
||||
@@ -73,7 +100,7 @@ $csrf = $_SESSION['csrf_token'];
|
||||
</div>
|
||||
<div class="card-header py-3">
|
||||
<form autocomplete="off">
|
||||
<?php if ($show_archived) { ?><input type="hidden" name="show" value="archived"><?php } ?>
|
||||
<?php if ($show !== '') { ?><input type="hidden" name="show" value="<?= $show ?>"><?php } ?>
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col-lg-5 col-md-6">
|
||||
<div class="input-group">
|
||||
@@ -84,8 +111,9 @@ $csrf = $_SESSION['csrf_token'];
|
||||
</div>
|
||||
</form>
|
||||
<nav class="nav nav-pills itflow-asset-tabs mt-2">
|
||||
<a class="nav-link <?= $show_archived ? '' : 'active' ?>" href="<?= escapeHtml($pill_url(false)) ?>"><i class="fas fa-fw fa-file-alt me-1"></i>Active <span class="badge rounded-pill ms-1"><?= intval($counts[0]) ?></span></a>
|
||||
<a class="nav-link <?= $show_archived ? 'active' : '' ?>" href="<?= escapeHtml($pill_url(true)) ?>" title="Not offered for new documents - the documents made from them stay"><i class="fas fa-fw fa-archive me-1"></i>Archived <span class="badge rounded-pill ms-1"><?= intval($counts[1]) ?></span></a>
|
||||
<a class="nav-link <?= $show === '' ? 'active' : '' ?>" href="<?= escapeHtml($pill_url('')) ?>"><i class="fas fa-fw fa-file-alt me-1"></i>Active <span class="badge rounded-pill ms-1"><?= intval($counts[0]) ?></span></a>
|
||||
<a class="nav-link <?= $show === 'required' ? 'active' : '' ?>" href="<?= escapeHtml($pill_url('required')) ?>" title="Every client, or the clients with a tag, should have one"><i class="fas fa-fw fa-clipboard-check me-1"></i>Required <span class="badge rounded-pill ms-1"><?= intval($counts[2]) ?></span></a>
|
||||
<a class="nav-link <?= $show_archived ? 'active' : '' ?>" href="<?= escapeHtml($pill_url('archived')) ?>" title="Not offered for new documents - the documents made from them stay"><i class="fas fa-fw fa-archive me-1"></i>Archived <span class="badge rounded-pill ms-1"><?= intval($counts[1]) ?></span></a>
|
||||
</nav>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
@@ -100,8 +128,7 @@ $csrf = $_SESSION['csrf_token'];
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$template_id = intval($row['document_template_id']);
|
||||
<?php foreach ($rows as $template_id => $row) {
|
||||
$name = escapeHtml($row['document_template_name']);
|
||||
$used_count = intval($row['used_count']);
|
||||
$client_count = intval($row['client_count']);
|
||||
@@ -121,6 +148,25 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<?php if ($row['document_template_archived_at']) { ?>
|
||||
<div class="small text-body-secondary"><i class="fas fa-fw fa-archive me-1"></i>Archived <?= date('M j, Y', strtotime($row['document_template_archived_at'])) ?></div>
|
||||
<?php } ?>
|
||||
<?php if (isset($coverage[$template_id]) || ($row['document_template_review_months'] && !$row['document_template_archived_at'])) {
|
||||
$reach = $coverage[$template_id] ?? null;
|
||||
$due_count = $due_counts[$template_id] ?? 0;
|
||||
?>
|
||||
<div class="d-flex flex-wrap gap-1 mt-1 itflow-document-template-reach">
|
||||
<?php if ($reach) { ?>
|
||||
<a class="badge rounded-pill fw-normal text-decoration-none <?= $reach['missing'] ? 'bg-warning-subtle text-warning-emphasis border border-warning-subtle' : 'bg-success-subtle text-success-emphasis border border-success-subtle' ?>"
|
||||
href="document_template.php?document_template_id=<?= $template_id ?>#documentTemplateCoverage" title="<?= escapeHtml(documentTemplateScopeText($row['document_template_required'], $required_tags[$template_id])) ?>">
|
||||
<i class="fas fa-clipboard-check me-1"></i><?= $reach['covered'] ?> of <?= $reach['clients'] ?> clients
|
||||
</a>
|
||||
<?php } ?>
|
||||
<?php if ($row['document_template_review_months'] && !$row['document_template_archived_at']) { ?>
|
||||
<a class="badge rounded-pill fw-normal text-decoration-none <?= $due_count ? 'bg-warning-subtle text-warning-emphasis border border-warning-subtle' : 'text-bg-light border' ?>"
|
||||
href="document_template.php?document_template_id=<?= $template_id ?>#documentTemplateCoverage" title="Reviewed <?= lcfirst(DOCUMENT_TEMPLATE_REVIEW_MONTHS[intval($row['document_template_review_months'])] ?? '') ?>">
|
||||
<i class="fas fa-calendar-check me-1"></i><?= $due_count ? $due_count . ' due a review' : 'Reviews up to date' ?>
|
||||
</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<!-- On smaller screens its use comes along under the name -->
|
||||
<div class="d-md-none small text-body-secondary mt-1"><?= $used_count ? $used_text . ' · last ' . timeAgo($row['last_used']) : 'Not used yet' ?></div>
|
||||
</div>
|
||||
@@ -143,7 +189,13 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<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>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/document_template/document_template_use_several.php?id=<?= $template_id ?>">
|
||||
<i class="fas fa-fw fa-copy text-secondary me-2"></i>For several clients
|
||||
</a>
|
||||
<?php } ?>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/document_template/document_template_required.php?id=<?= $template_id ?>">
|
||||
<i class="fas fa-fw fa-clipboard-check text-secondary me-2"></i>Who should have one
|
||||
</a>
|
||||
<a class="dropdown-item" href="post.php?copy_document_template=<?= $template_id ?>&csrf_token=<?= $csrf ?>" title="A new template starting from this one">
|
||||
<i class="fas fa-fw fa-copy text-secondary me-2"></i>Copy
|
||||
</a>
|
||||
@@ -176,6 +228,7 @@ $csrf = $_SESSION['csrf_token'];
|
||||
'filtered' => $q !== '',
|
||||
'archived' => $show_archived,
|
||||
'views' => ['show'],
|
||||
'view' => $show === 'required' ? ['title' => 'No required templates', 'text' => 'Set one up from its page - <strong>Who should have one</strong> - to see which clients are missing a document from it.'] : null,
|
||||
'text' => 'A document ready to start a client\'s from - a runbook, a build sheet, a plan. Agents start one from <strong>Document from template</strong> in a client\'s Files. Maintenance > Starter Content has a set for an MSP.',
|
||||
'actions' => [['label' => 'New Document Template', 'modal' => $add_url, 'size' => 'xl']],
|
||||
];
|
||||
|
||||
103
admin/modals/document_template/document_template_required.php
Normal file
103
admin/modals/document_template/document_template_required.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Who should have a document from a template - no one in particular, every
|
||||
* client, or the clients with one of the client tags picked - and how often
|
||||
* the documents made from it are reviewed. ?id= the template. Saved by
|
||||
* save_document_template_required (admin/post/document_template.php); the
|
||||
* template's page then shows the clients missing one and the documents due a
|
||||
* review.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$template = documentTemplateRow($_GET['id'] ?? 0);
|
||||
|
||||
ob_start();
|
||||
|
||||
if (!$template) { ?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fa fa-fw fa-clipboard-check me-2"></i>Who should have one</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 document template no longer exists.</div>
|
||||
<?php } else {
|
||||
$document_template_id = intval($template['document_template_id']);
|
||||
$required = intval($template['document_template_required']);
|
||||
$tag_ids = array_keys(documentTemplateRequiredTags([$document_template_id])[$document_template_id]);
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title text-truncate"><i class="fa fa-fw fa-clipboard-check me-2"></i>Who should have one <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="<?= $document_template_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Every one of these should have a document from it</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-users"></i></span>
|
||||
<select class="form-select select2" name="required" id="documentTemplateRequired">
|
||||
<option value="<?= DOCUMENT_TEMPLATE_REQUIRED_NONE ?>" <?= $required === DOCUMENT_TEMPLATE_REQUIRED_NONE ? 'selected' : '' ?>>No one in particular</option>
|
||||
<option value="<?= DOCUMENT_TEMPLATE_REQUIRED_EVERY ?>" <?= $required === DOCUMENT_TEMPLATE_REQUIRED_EVERY ? 'selected' : '' ?>>Every client</option>
|
||||
<option value="<?= DOCUMENT_TEMPLATE_REQUIRED_TAGGED ?>" <?= $required === DOCUMENT_TEMPLATE_REQUIRED_TAGGED ? 'selected' : '' ?>>Clients with a tag</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-text">Leads and archived clients don't count. The template's page and each client's Files show who is missing one.</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3" id="documentTemplateRequiredTags" <?= $required === DOCUMENT_TEMPLATE_REQUIRED_TAGGED ? '' : 'hidden' ?>>
|
||||
<label class="form-label">Client tags</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-tags"></i></span>
|
||||
<select class="form-select select2" name="tags[]" multiple data-placeholder="- Pick client tags -">
|
||||
<?php foreach (tagOptionsFor('client', $tag_ids) as $tag) { ?>
|
||||
<?= tagOptionHtml($tag, $tag_ids) ?>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-text">Clients with any of them - Managed, say.</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-1">
|
||||
<label class="form-label">Review the documents made from it</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-calendar-check"></i></span>
|
||||
<select class="form-select select2" name="review_months">
|
||||
<?php foreach (DOCUMENT_TEMPLATE_REVIEW_MONTHS as $months => $label) { ?>
|
||||
<option value="<?= $months ?>" <?= intval($template['document_template_review_months']) === $months ? 'selected' : '' ?>><?= $label ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-text">A document is due once it hasn't been edited or marked as reviewed for that long - shown on it, in its client's Files and here.</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="save_document_template_required" 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 tags are for "Clients with a tag" only
|
||||
var required = document.getElementById('documentTemplateRequired');
|
||||
var tags = document.getElementById('documentTemplateRequiredTags');
|
||||
if (required && tags) {
|
||||
required.addEventListener('change', function () {
|
||||
tags.hidden = required.value !== '<?= DOCUMENT_TEMPLATE_REQUIRED_TAGGED ?>';
|
||||
});
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php }
|
||||
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
@@ -6,12 +6,14 @@
|
||||
* 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).
|
||||
* agent/post/document.php). &client_id= picks the client - from the
|
||||
* template's list of clients missing one.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$template = documentTemplateRow($_GET['id'] ?? 0);
|
||||
$picked_client_id = intval($_GET['client_id'] ?? 0);
|
||||
|
||||
ob_start();
|
||||
|
||||
@@ -41,7 +43,7 @@ if (!$template || $template['document_template_archived_at']) { ?>
|
||||
<?php
|
||||
$sql_clients = mysqli_query($mysqli, "SELECT client_id, client_name FROM clients WHERE client_archived_at IS NULL ORDER BY client_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql_clients)) { ?>
|
||||
<option value="<?= intval($row['client_id']) ?>"><?= escapeHtml($row['client_name']) ?></option>
|
||||
<option value="<?= intval($row['client_id']) ?>" <?= intval($row['client_id']) === $picked_client_id ? 'selected' : '' ?>><?= escapeHtml($row['client_name']) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* New documents from a template for several clients at once - from the
|
||||
* template's page. Each is filled in from its own client - its primary site
|
||||
* and contact - and the fill-ins are answered once, for all of them. Each goes
|
||||
* in its client's Files at the top level. ?id= the template; &missing=1 picks
|
||||
* the clients it is required of that have none yet. Made by
|
||||
* make_documents_from_template (admin/post/document_template.php).
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$template = documentTemplateRow($_GET['id'] ?? 0);
|
||||
|
||||
ob_start();
|
||||
|
||||
if (!$template || $template['document_template_archived_at']) { ?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fa fa-fw fa-copy me-2"></i>New documents</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 document template is gone or archived.</div>
|
||||
<?php } else {
|
||||
$document_template_id = intval($template['document_template_id']);
|
||||
$picked = [];
|
||||
if (!empty($_GET['missing'])) {
|
||||
$tag_ids = array_keys(documentTemplateRequiredTags([$document_template_id])[$document_template_id]);
|
||||
$picked = array_keys(documentTemplateCoverage($document_template_id, $template['document_template_required'], $tag_ids)['missing']);
|
||||
}
|
||||
$used = documentTemplateWordsUsed($template);
|
||||
$from_client = array_values(array_intersect(array_keys($used['placeholder']), array_merge(DOCUMENT_TEMPLATE_SITE_PLACEHOLDERS, DOCUMENT_TEMPLATE_CONTACT_PLACEHOLDERS)));
|
||||
$fill_ins = documentTemplateFillInsFor($document_template_id);
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title text-truncate"><i class="fa fa-fw fa-copy me-2"></i>New documents 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="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="document_template_id" value="<?= $document_template_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Clients <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-users"></i></span>
|
||||
<select class="form-select select2" name="client_ids[]" multiple required data-placeholder="- Pick clients -">
|
||||
<?php
|
||||
$sql_clients = mysqli_query($mysqli, "SELECT client_id, client_name, client_lead FROM clients WHERE client_archived_at IS NULL ORDER BY client_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql_clients)) { ?>
|
||||
<option value="<?= intval($row['client_id']) ?>" <?= in_array(intval($row['client_id']), $picked, true) ? 'selected' : '' ?>><?= escapeHtml($row['client_name']) ?><?= intval($row['client_lead']) ? ' (lead)' : '' ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php if (!empty($_GET['missing'])) { ?>
|
||||
<div class="form-text"><?= $picked ? 'The ' . count($picked) . ' client' . (count($picked) === 1 ? '' : 's') . ' it is required of with none yet.' : 'Every client it is required of has one.' ?></div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="form-check form-switch mb-3">
|
||||
<input class="form-check-input" type="checkbox" role="switch" name="skip_existing" value="1" id="documentTemplateSkipExisting" checked>
|
||||
<label class="form-check-label" for="documentTemplateSkipExisting">Skip clients that already have one</label>
|
||||
</div>
|
||||
|
||||
<?php if ($used['placeholder'] || $used['table']) { ?>
|
||||
<div class="small text-secondary mb-3">
|
||||
<i class="fas fa-fw fa-magic me-1"></i>Each is filled in from its own client: <span class="font-monospace"><?= escapeHtml(implode(' ', array_map(function ($word) {
|
||||
return "[$word]";
|
||||
}, array_values($used['placeholder'] + $used['table'])))) ?></span><?= $from_client ? ' - with its primary site and contact' : '' ?>.
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($fill_ins) { ?>
|
||||
<div class="border rounded p-3 bg-body-tertiary mb-3">
|
||||
<div class="fw-semibold mb-1"><i class="fas fa-fw fa-pen-square text-secondary me-1"></i>It asks for</div>
|
||||
<div class="small text-secondary mb-2">Answered once, for every one of them. Leave one blank to fill it in on each document later.</div>
|
||||
<div class="row g-2">
|
||||
<?php foreach ($fill_ins as $fill_in) { ?>
|
||||
<div class="col-md-6"><?= ticketFillInFieldHtml($fill_in, '') ?></div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<small class="text-secondary">Each goes in its client's Files at the top level, named and summed up as the template is.</small>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="make_documents_from_template" class="btn btn-primary text-bold"><i class="fa fa-check me-2"></i>Create</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fa fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php }
|
||||
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
/*
|
||||
* Document templates (Admin > Document Templates) - making, changing,
|
||||
* 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
|
||||
* copying, archiving and deleting them, with their images, setting up what
|
||||
* they ask for when a document is made (their fill-ins), who should have a
|
||||
* document from one and how often those are reviewed, and making documents
|
||||
* from one for several clients at once. The work is in
|
||||
* functions/document_template.php; a document is made from one by
|
||||
* add_document_from_template (agent/post/document.php).
|
||||
*/
|
||||
@@ -222,3 +224,69 @@ if (isset($_POST['reorder_document_template_fill_ins'])) {
|
||||
exit;
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['save_document_template_required'])) {
|
||||
|
||||
// Who should have a document from it, and how often those documents are reviewed
|
||||
validateCSRFToken();
|
||||
|
||||
$row = documentTemplateForRequest($_POST['document_template_id'] ?? 0);
|
||||
$document_template_id = intval($row['document_template_id']);
|
||||
$required = intval($_POST['required'] ?? 0);
|
||||
$tag_ids = array_map('intval', (array) ($_POST['tags'] ?? []));
|
||||
if ($required === DOCUMENT_TEMPLATE_REQUIRED_TAGGED && !array_filter($tag_ids)) {
|
||||
flashAlert('Pick the client tags - or Every client.', 'error');
|
||||
redirect();
|
||||
}
|
||||
documentTemplateSetRequired($document_template_id, $required, $required === DOCUMENT_TEMPLATE_REQUIRED_TAGGED ? $tag_ids : [], $_POST['review_months'] ?? 0);
|
||||
|
||||
$saved = documentTemplateRow($document_template_id);
|
||||
$scope = documentTemplateScopeText($saved['document_template_required'], documentTemplateRequiredTags([$document_template_id])[$document_template_id]);
|
||||
$months = intval($saved['document_template_review_months']);
|
||||
$said = ($scope !== '' ? "required of " . lcfirst($scope) : 'not required of anyone') . ($months ? ", reviewed " . lcfirst(DOCUMENT_TEMPLATE_REVIEW_MONTHS[$months]) : ', with no reviews');
|
||||
|
||||
logAudit("Document Template", "Edit", "$session_name set document template " . escapeSql($row['document_template_name']) . " " . escapeSql($said), 0, $document_template_id);
|
||||
|
||||
flashAlert("<strong>" . escapeHtml($row['document_template_name']) . "</strong> is " . escapeHtml($said));
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['make_documents_from_template'])) {
|
||||
|
||||
// One for each client picked, filled in from each - the fill-ins answered once for all
|
||||
validateCSRFToken();
|
||||
|
||||
$row = documentTemplateForRequest($_POST['document_template_id'] ?? 0);
|
||||
$document_template_id = intval($row['document_template_id']);
|
||||
if ($row['document_template_archived_at']) {
|
||||
flashAlert('That template is archived - restore it to make documents from it.', 'error');
|
||||
redirect();
|
||||
}
|
||||
$client_ids = array_values(array_filter(array_map('intval', (array) ($_POST['client_ids'] ?? []))));
|
||||
if (!$client_ids) {
|
||||
flashAlert('Pick the clients to make one for.', 'error');
|
||||
redirect();
|
||||
}
|
||||
$answers = ticketFillInAnswers(documentTemplateFillInsFor($document_template_id), $_POST['fill_ins'] ?? [], $fill_in_error);
|
||||
if ($answers === null) {
|
||||
flashAlert($fill_in_error, 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
$result = documentTemplateMakeForClients($document_template_id, $client_ids, $answers, !empty($_POST['skip_existing']));
|
||||
|
||||
foreach ($result['made'] as $client_id => $document_id) {
|
||||
$document_name = (string) getFieldById('documents', $document_id, 'document_name');
|
||||
logAudit("Document", "Create", "$session_name created document " . escapeSql($document_name) . " from template " . escapeSql($row['document_template_name']), $client_id, $document_id);
|
||||
}
|
||||
|
||||
$made = count($result['made']);
|
||||
$skipped = count($result['skipped']);
|
||||
flashAlert(($made ? "Made $made document" . ($made === 1 ? '' : 's') : 'Made no documents') . " from <strong>" . escapeHtml($row['document_template_name']) . "</strong>"
|
||||
. ($skipped ? " - skipped " . ($skipped === 1 ? escapeHtml(reset($result['skipped'])) . ", which already has one" : "$skipped clients that already have one") : ''), $made ? 'success' : 'warning');
|
||||
|
||||
redirect("document_template.php?document_template_id=$document_template_id");
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
* a header card with the actions, the content beside a column of what goes
|
||||
* with it.
|
||||
*
|
||||
* When its template says how often documents made from it are reviewed, it
|
||||
* says when it is next due - or that it is due - and can be marked as
|
||||
* reviewed; it can be linked to the template it came from after the fact.
|
||||
*
|
||||
* Open to anyone with access to the client, like the files list; changing it
|
||||
* takes support write access, like the handlers behind it.
|
||||
*/
|
||||
@@ -15,11 +19,13 @@ require_once "includes/inc_all_client.php";
|
||||
$document_id = intval($_GET['document_id'] ?? 0);
|
||||
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_archived_at, document_client_visible, document_content, document_created_at,
|
||||
document_description, document_folder_id, document_name, document_updated_at, document_template_id, document_template_name,
|
||||
creator.user_name AS created_by_name, updater.user_name AS updated_by_name
|
||||
document_description, document_folder_id, document_name, document_updated_at, document_reviewed_at, document_template_id, document_template_name,
|
||||
document_template_review_months, document_template_archived_at,
|
||||
creator.user_name AS created_by_name, updater.user_name AS updated_by_name, reviewer.user_name AS reviewed_by_name
|
||||
FROM documents
|
||||
LEFT JOIN users creator ON document_created_by = creator.user_id
|
||||
LEFT JOIN users updater ON document_updated_by = updater.user_id
|
||||
LEFT JOIN users reviewer ON document_reviewed_by = reviewer.user_id AND document_reviewed_by > 0
|
||||
LEFT JOIN document_templates USING (document_template_id)
|
||||
WHERE document_client_id = $client_id AND document_id = $document_id
|
||||
LIMIT 1"));
|
||||
@@ -40,6 +46,9 @@ $document_visible = intval($row['document_client_visible']);
|
||||
$document_updated_at = $row['document_updated_at'] ?: $row['document_created_at'];
|
||||
$document_updated_by = escapeHtml($row['updated_by_name'] ?: $row['created_by_name']);
|
||||
|
||||
// Its review, when its template asks for one - not for an archived document
|
||||
$review = !$document_archived && $row['document_template_name'] !== null ? documentReviewState($row) : null;
|
||||
|
||||
$folder_tree = folderTree($client_id);
|
||||
$document_folder_id = isset($folder_tree[intval($row['document_folder_id'])]) ? intval($row['document_folder_id']) : 0;
|
||||
$folder_path = folderPath($folder_tree, $document_folder_id);
|
||||
@@ -79,6 +88,7 @@ $page_title = $row['document_name'];
|
||||
<span class="text-break"><?= $document_name ?></span>
|
||||
<?php if ($document_archived) { ?><span class="badge rounded-pill text-bg-secondary fw-normal fs-6">Archived</span><?php } ?>
|
||||
<?php if ($document_visible) { ?><span class="badge rounded-pill bg-info-subtle text-info-emphasis fw-normal fs-6" title="Technical contacts can read this in the client portal">Portal</span><?php } ?>
|
||||
<?php if ($review && $review['due']) { ?><span class="badge rounded-pill bg-warning-subtle text-warning-emphasis border border-warning-subtle fw-normal fs-6" title="Due <?= date('M j, Y', strtotime($review['next'])) ?>"><i class="fas fa-calendar-check me-1"></i>Review due</span><?php } ?>
|
||||
</h1>
|
||||
<?php if ($document_description) { ?>
|
||||
<div class="text-body-secondary"><?= $document_description ?></div>
|
||||
@@ -182,9 +192,37 @@ $page_title = $row['document_name'];
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Where it lives -->
|
||||
<!-- Where it lives, and where it came from -->
|
||||
<div class="card mb-3">
|
||||
<div class="card-body">
|
||||
<div class="itflow-glance-item" title="Template">
|
||||
<i class="fas fa-fw fa-puzzle-piece"></i>
|
||||
<span class="min-w-0 d-flex align-items-center">
|
||||
<?php if ($row['document_template_name'] !== null) { ?>
|
||||
<span class="text-truncate">From <?= $session_is_admin ? '<a href="/admin/document_template.php?document_template_id=' . intval($row['document_template_id']) . '">' . escapeHtml($row['document_template_name']) . '</a>' : escapeHtml($row['document_template_name']) ?></span>
|
||||
<?php } else { ?>
|
||||
<span class="text-body-secondary">Not from a template</span>
|
||||
<?php } ?>
|
||||
<?php if ($can_edit && !$document_archived) { ?>
|
||||
<a class="itflow-copy ajax-modal" href="#" data-modal-url="modals/document/document_template_link.php?id=<?= $document_id ?>" title="Change the template it counts as made from"><i class="fas fa-pen"></i></a>
|
||||
<?php } ?>
|
||||
</span>
|
||||
</div>
|
||||
<?php if ($review) { ?>
|
||||
<div class="itflow-glance-item<?= $review['due'] ? ' text-warning-emphasis' : '' ?>" title="Review">
|
||||
<i class="fas fa-fw fa-calendar-check"></i>
|
||||
<span class="min-w-0">
|
||||
<span class="d-block"><?= $review['due'] ? '<strong>Review due</strong> since ' . date('M j, Y', strtotime($review['next'])) : 'Next review ' . date('M j, Y', strtotime($review['next'])) ?></span>
|
||||
<span class="d-block small text-body-secondary">
|
||||
<?= $review['reviewed'] ? 'Reviewed' : (strtotime($review['last']) === strtotime($row['document_created_at']) ? 'Made' : 'Edited') ?> <?= timeAgo($review['last']) ?><?= $review['reviewed'] && $row['reviewed_by_name'] !== null ? ' by ' . escapeHtml($row['reviewed_by_name']) : '' ?>
|
||||
· <?= lcfirst(DOCUMENT_TEMPLATE_REVIEW_MONTHS[intval($row['document_template_review_months'])] ?? '') ?>
|
||||
</span>
|
||||
<?php if ($can_edit) { ?>
|
||||
<a class="btn btn-sm <?= $review['due'] ? 'btn-warning' : 'btn-light border' ?> mt-2" href="post.php?mark_document_reviewed=<?= $document_id ?>&csrf_token=<?= $csrf ?>"><i class="fas fa-check me-1"></i>Mark reviewed</a>
|
||||
<?php } ?>
|
||||
</span>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<div class="itflow-glance-item" title="Folder">
|
||||
<i class="fas fa-fw fa-folder"></i>
|
||||
<span class="min-w-0 d-flex align-items-center">
|
||||
|
||||
@@ -12,6 +12,11 @@
|
||||
* Documents can be tagged; filtering by a tag shows the documents with it -
|
||||
* from every folder at the top level, like a search.
|
||||
*
|
||||
* Documents due a review (their template says how often) are marked, and the
|
||||
* Review due tab lists them from every folder. The document templates the
|
||||
* client should have a document from, and has none, are listed across the
|
||||
* top, each a click from making one.
|
||||
*
|
||||
* Open to anyone with access to the client (contracts and the like live here),
|
||||
* changes need support write access, like the handlers behind them.
|
||||
*/
|
||||
@@ -35,9 +40,9 @@ if (!isset($folder_tree[$folder_id])) {
|
||||
$folder_path = folderPath($folder_tree, $folder_id);
|
||||
$folder_name = $folder_id ? escapeHtml($folder_tree[$folder_id]['folder_name']) : 'Top level';
|
||||
|
||||
// '' everything, 'file' uploads only, 'document' created documents only
|
||||
// '' everything, 'file' uploads only, 'document' created documents only, 'due' documents due a review
|
||||
$type_filter = $_GET['type'] ?? '';
|
||||
if (!in_array($type_filter, ['file', 'document'], true)) {
|
||||
if (!in_array($type_filter, ['file', 'document', 'due'], true)) {
|
||||
$type_filter = '';
|
||||
}
|
||||
|
||||
@@ -55,7 +60,7 @@ $search_everywhere = $search !== '' && $folder_id === 0;
|
||||
// Tags - documents with any of them, and no uploads, which have none. At the
|
||||
// top level that is in every folder, like a search
|
||||
$tag_filter_ids = tagFilterIds();
|
||||
$look_everywhere = $search_everywhere || ($tag_filter_ids && $folder_id === 0);
|
||||
$look_everywhere = $search_everywhere || ($tag_filter_ids && $folder_id === 0) || $type_filter === 'due';
|
||||
|
||||
/** This page's URL with some parameters changed - the rest (folder, type, view, search, archived) ride along. */
|
||||
function filesUrl($changes = []) {
|
||||
@@ -80,11 +85,16 @@ foreach ([['files', 'file'], ['documents', 'document']] as [$table, $prefix]) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Documents due a review, and the required templates the client has no document from ----
|
||||
$due_documents = $archived ? [] : documentsReviewDue("documents.document_client_id = $client_id");
|
||||
$missing_templates = $archived ? [] : documentTemplatesMissingFor($client_id);
|
||||
|
||||
// ---- What is on show: files and documents, merged and sorted here ----
|
||||
$search_sql = escapeSql($search);
|
||||
$file_where = "file_client_id = $client_id AND file_$archive_query" . ($look_everywhere ? '' : " AND file_folder_id = $folder_id") . ($tag_filter_ids ? ' AND 0' : '');
|
||||
$file_where = "file_client_id = $client_id AND file_$archive_query" . ($look_everywhere ? '' : " AND file_folder_id = $folder_id") . ($tag_filter_ids || $type_filter === 'due' ? ' AND 0' : '');
|
||||
$document_where = "document_client_id = $client_id AND document_$archive_query" . ($look_everywhere ? '' : " AND document_folder_id = $folder_id")
|
||||
. ' ' . tagFilterSql('document', $tag_filter_ids, 'documents.document_id');
|
||||
. ' ' . tagFilterSql('document', $tag_filter_ids, 'documents.document_id')
|
||||
. ($type_filter === 'due' ? ' AND documents.document_id IN (' . ($due_documents ? implode(',', array_keys($due_documents)) : '0') . ')' : '');
|
||||
if ($search !== '') {
|
||||
$file_where .= " AND (file_name LIKE '%$search_sql%' OR file_ext LIKE '%$search_sql%' OR file_description LIKE '%$search_sql%')";
|
||||
$document_where .= " AND (MATCH(document_content_raw) AGAINST ('$search_sql') OR document_name LIKE '%$search_sql%' OR document_description LIKE '%$search_sql%'
|
||||
@@ -337,21 +347,42 @@ $preview = []; // the grid's previewer payload
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Documents, uploads or both -->
|
||||
<!-- Documents, uploads or both - or the documents due a review, from every folder -->
|
||||
<div class="card-header py-2 itflow-asset-tabs">
|
||||
<nav class="nav nav-pills flex-nowrap overflow-auto">
|
||||
<a class="nav-link text-nowrap <?= $type_filter === '' ? 'active' : '' ?>" href="<?= escapeHtml(filesUrl(['type' => ''])) ?>">
|
||||
All <span class="badge rounded-pill ms-1"><?= $type_counts['file'] + $type_counts['document'] ?></span>
|
||||
All<?php if ($type_filter !== 'due') { ?> <span class="badge rounded-pill ms-1"><?= $type_counts['file'] + $type_counts['document'] ?></span><?php } ?>
|
||||
</a>
|
||||
<a class="nav-link text-nowrap <?= $type_filter === 'document' ? 'active' : '' ?>" href="<?= escapeHtml(filesUrl(['type' => 'document'])) ?>">
|
||||
<i class="fas fa-fw fa-file-alt me-1"></i>Documents <span class="badge rounded-pill ms-1"><?= $type_counts['document'] ?></span>
|
||||
<i class="fas fa-fw fa-file-alt me-1"></i>Documents<?php if ($type_filter !== 'due') { ?> <span class="badge rounded-pill ms-1"><?= $type_counts['document'] ?></span><?php } ?>
|
||||
</a>
|
||||
<a class="nav-link text-nowrap <?= $type_filter === 'file' ? 'active' : '' ?>" href="<?= escapeHtml(filesUrl(['type' => 'file'])) ?>">
|
||||
<i class="fas fa-fw fa-paperclip me-1"></i>Uploads <span class="badge rounded-pill ms-1"><?= $type_counts['file'] ?></span>
|
||||
<i class="fas fa-fw fa-paperclip me-1"></i>Uploads<?php if ($type_filter !== 'due') { ?> <span class="badge rounded-pill ms-1"><?= $type_counts['file'] ?></span><?php } ?>
|
||||
</a>
|
||||
<?php if ($due_documents || $type_filter === 'due') { ?>
|
||||
<a class="nav-link text-nowrap <?= $type_filter === 'due' ? 'active' : '' ?>" href="<?= escapeHtml(filesUrl(['type' => 'due'])) ?>" title="Documents their template says are due a review - from every folder">
|
||||
<i class="fas fa-fw fa-calendar-check me-1"></i>Review due <span class="badge rounded-pill ms-1 <?= $due_documents ? 'text-bg-warning' : '' ?>"><?= count($due_documents) ?></span>
|
||||
</a>
|
||||
<?php } ?>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
<?php if ($missing_templates) { ?>
|
||||
<!-- The document templates this client should have a document from, and hasn't -->
|
||||
<div class="card-header py-2 bg-warning-subtle d-flex flex-wrap align-items-center gap-2 itflow-files-missing">
|
||||
<span class="small text-warning-emphasis fw-semibold"><i class="fas fa-fw fa-clipboard-check me-1"></i><?= count($missing_templates) === 1 ? 'A document it should have' : 'Documents it should have' ?>:</span>
|
||||
<?php foreach ($missing_templates as $missing_id => $missing_name) { ?>
|
||||
<?php if ($can_edit) { ?>
|
||||
<button type="button" class="btn btn-sm btn-light border py-0 ajax-modal" data-modal-size="lg" data-modal-url="modals/document/document_add_from_template.php?client_id=<?= $client_id ?>&document_template_id=<?= $missing_id ?>" title="Make it from the template" data-missing-template-id="<?= $missing_id ?>">
|
||||
<i class="fas fa-plus me-1 text-secondary"></i><?= escapeHtml($missing_name) ?>
|
||||
</button>
|
||||
<?php } else { ?>
|
||||
<span class="badge rounded-pill text-bg-light border fw-normal" data-missing-template-id="<?= $missing_id ?>"><?= escapeHtml($missing_name) ?></span>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<div class="card-header py-3">
|
||||
<form autocomplete="off">
|
||||
<input type="hidden" name="client_id" value="<?= $client_id ?>">
|
||||
@@ -492,10 +523,13 @@ $preview = []; // the grid's previewer payload
|
||||
'archived_title' => 'Nothing archived ' . ($folder_id ? 'in ' . $folder_name : 'here'),
|
||||
'title' => $folder_id ? $folder_name . ' is empty' : 'No files or documents yet',
|
||||
'text' => $can_edit ? 'Drop files here to upload them, or start a document.' : '',
|
||||
'view' => $type_filter && !$folder_id ? [
|
||||
'view' => $type_filter === 'due' ? [
|
||||
'title' => 'Nothing due a review',
|
||||
'text' => 'Every document is within the review its template asks for.',
|
||||
] : ($type_filter && !$folder_id ? [
|
||||
'title' => $type_filter === 'document' ? 'No documents yet' : 'No uploads yet',
|
||||
'text' => $can_edit ? ($type_filter === 'document' ? 'Write one here - a procedure, a how-to, notes on the site.' : 'Drop files here to upload them.') : '',
|
||||
] : null,
|
||||
] : null),
|
||||
'actions' => $can_edit ? [
|
||||
['label' => 'Upload files', 'modal' => $upload_url, 'icon' => 'fa-cloud-upload-alt'],
|
||||
['label' => 'New document', 'modal' => "modals/document/document_add.php?client_id=$client_id&folder_id=$folder_id", 'size' => 'xl', 'icon' => 'fa-file-alt'],
|
||||
@@ -578,6 +612,9 @@ $preview = []; // the grid's previewer payload
|
||||
<a class="fw-semibold small d-block text-truncate text-body ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/file/file.php?id=<?= $id ?>" title="<?= $name ?>"><?= $name ?></a>
|
||||
<?php } ?>
|
||||
<div class="small text-body-secondary text-truncate"><?= escapeHtml($meta) ?> · <?= timeAgo($item['updated']) ?></div>
|
||||
<?php if ($item['kind'] === 'document' && isset($due_documents[$id])) { ?>
|
||||
<span class="badge rounded-pill bg-warning-subtle text-warning-emphasis border border-warning-subtle fw-normal mt-1"><i class="fas fa-calendar-check me-1"></i>Review due</span>
|
||||
<?php } ?>
|
||||
<?php if ($item['kind'] === 'document' && !empty($documents_tags[$id])) { ?>
|
||||
<div class="mt-1"><?= tagPills('document', $id, $documents_tags[$id], "files.php?client_id=$client_id&", false) ?></div>
|
||||
<?php } ?>
|
||||
@@ -642,6 +679,9 @@ $preview = []; // the grid's previewer payload
|
||||
<?php if (!empty($row['document_client_visible'])) { ?>
|
||||
<span class="badge rounded-pill bg-info-subtle text-info-emphasis fw-normal" title="Technical contacts can read this in the client portal">Portal</span>
|
||||
<?php } ?>
|
||||
<?php if (isset($due_documents[$id])) { ?>
|
||||
<span class="badge rounded-pill bg-warning-subtle text-warning-emphasis border border-warning-subtle fw-normal" title="Last edited or reviewed <?= escapeHtml(date('M j, Y', strtotime($due_documents[$id]['reviewed_at']))) ?> - <?= escapeHtml($due_documents[$id]['document_template_name']) ?> is reviewed <?= lcfirst(DOCUMENT_TEMPLATE_REVIEW_MONTHS[intval($due_documents[$id]['document_template_review_months'])] ?? '') ?>"><i class="fas fa-calendar-check me-1"></i>Review due</span>
|
||||
<?php } ?>
|
||||
<?php } else { ?>
|
||||
<a class="fw-semibold text-body stretched-link text-break ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/file/file.php?id=<?= $id ?>"><?= $name ?></a>
|
||||
<?php } ?>
|
||||
|
||||
@@ -26,6 +26,11 @@ while ($row = mysqli_fetch_assoc($sql)) {
|
||||
}
|
||||
$usage = documentTemplateUsage(array_keys($templates));
|
||||
$picked = intval($_GET['document_template_id'] ?? 0);
|
||||
// The ones this client should have a document from, and hasn't - marked, and first
|
||||
$missing = documentTemplatesMissingFor($client_id);
|
||||
uksort($templates, function ($a, $b) use ($missing, $templates) {
|
||||
return [!isset($missing[$a]), strtolower($templates[$a]['document_template_name'])] <=> [!isset($missing[$b]), strtolower($templates[$b]['document_template_name'])];
|
||||
});
|
||||
|
||||
ob_start();
|
||||
|
||||
@@ -58,8 +63,8 @@ ob_start();
|
||||
<option value="">- Pick a template -</option>
|
||||
<?php foreach ($templates as $template_id => $template) {
|
||||
$used = $usage[$template_id]['count'] ?? 0;
|
||||
$about = trim(implode(' · ', array_filter([trim((string) $template['document_template_description']), $used ? 'used ' . $used . ' time' . ($used === 1 ? '' : 's') : '']))); ?>
|
||||
<option value="<?= $template_id ?>"<?= $template_id === $picked ? ' selected' : '' ?><?= selectOptionData(['icon' => 'fas fa-file-alt', 'description' => $about]) ?>><?= escapeHtml($template['document_template_name']) ?></option>
|
||||
$about = trim(implode(' · ', array_filter([isset($missing[$template_id]) ? 'this client should have one' : '', trim((string) $template['document_template_description']), $used ? 'used ' . $used . ' time' . ($used === 1 ? '' : 's') : '']))); ?>
|
||||
<option value="<?= $template_id ?>"<?= $template_id === $picked ? ' selected' : '' ?><?= selectOptionData(['icon' => isset($missing[$template_id]) ? 'fas fa-clipboard-check' : 'fas fa-file-alt', 'description' => $about]) ?>><?= escapeHtml($template['document_template_name']) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
72
agent/modals/document/document_template_link.php
Normal file
72
agent/modals/document/document_template_link.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* The template a document counts as made from - for one written by hand
|
||||
* before the template, or made from another, so it counts towards who has one
|
||||
* and is reviewed as the template says. Saved by set_document_template
|
||||
* (agent/post/document.php).
|
||||
*/
|
||||
|
||||
require_once '../../../includes/modal_header.php';
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$document_id = intval($_GET['id'] ?? 0);
|
||||
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_client_id, document_name, document_template_id FROM documents WHERE document_id = $document_id LIMIT 1"));
|
||||
if (!$row) {
|
||||
echo json_encode(['error' => 'That document no longer exists.']);
|
||||
exit;
|
||||
}
|
||||
$client_id = intval($row['document_client_id']);
|
||||
enforceClientAccess();
|
||||
$current_id = intval($row['document_template_id']);
|
||||
|
||||
// The current templates, and the one it is from even if archived since
|
||||
$templates = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT document_template_id, document_template_name, document_template_description, document_template_archived_at, document_template_required, document_template_review_months
|
||||
FROM document_templates WHERE document_template_archived_at IS NULL OR document_template_id = $current_id ORDER BY document_template_name ASC");
|
||||
while ($template = mysqli_fetch_assoc($sql)) {
|
||||
$templates[intval($template['document_template_id'])] = $template;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title text-truncate"><i class="fas fa-fw fa-puzzle-piece me-2"></i>Template for <strong><?= escapeHtml($row['document_name']) ?></strong></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_id" value="<?= $document_id ?>">
|
||||
<div class="modal-body">
|
||||
|
||||
<label class="form-label">Counts as made from</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-puzzle-piece"></i></span>
|
||||
<select class="form-select select2" name="document_template_id">
|
||||
<option value="0">- Not from a template -</option>
|
||||
<?php foreach ($templates as $template_id => $template) {
|
||||
$about = array_filter([
|
||||
$template['document_template_required'] ? 'required' : '',
|
||||
$template['document_template_review_months'] ? 'reviewed ' . lcfirst(DOCUMENT_TEMPLATE_REVIEW_MONTHS[intval($template['document_template_review_months'])] ?? '') : '',
|
||||
$template['document_template_archived_at'] ? 'archived' : '',
|
||||
]);
|
||||
?>
|
||||
<option value="<?= $template_id ?>" <?= $template_id === $current_id ? 'selected' : '' ?> data-description="<?= escapeHtml(trim($template['document_template_description'] . ($about ? ' · ' . implode(', ', $about) : ''), ' ·')) ?>"><?= escapeHtml($template['document_template_name']) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-text">What it says stays as it is - it counts towards the clients that should have one, and is reviewed as the template says.</div>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="set_document_template" 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>
|
||||
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
@@ -233,6 +233,62 @@ if (isset($_POST['move_document'])) {
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['mark_document_reviewed'])) {
|
||||
|
||||
// Looked over and still right - due again a review period from now
|
||||
validateCSRFToken();
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$row = documentForHandler($_GET['mark_document_reviewed']);
|
||||
$document_id = intval($row['document_id']);
|
||||
if ($row['document_archived_at']) {
|
||||
flashAlert('That document is archived.', 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
mysqli_query($mysqli, "UPDATE documents SET document_reviewed_at = NOW(), document_reviewed_by = " . intval($session_user_id) . ", document_updated_at = document_updated_at WHERE document_id = $document_id");
|
||||
|
||||
logAudit("Document", "Edit", "$session_name marked document " . escapeSql($row['document_name']) . " as reviewed", $client_id, $document_id);
|
||||
|
||||
$review = documentReviewState(mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_created_at, document_updated_at, document_reviewed_at, document_template_review_months, document_template_archived_at
|
||||
FROM documents LEFT JOIN document_templates USING (document_template_id) WHERE document_id = $document_id")));
|
||||
|
||||
flashAlert("Document <strong>" . escapeHtml($row['document_name']) . "</strong> marked as reviewed" . ($review ? " - next due " . date('M j, Y', strtotime($review['next'])) : ''));
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['set_document_template'])) {
|
||||
|
||||
// The template it counts as made from - what it says stays as it is
|
||||
validateCSRFToken();
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$row = documentForHandler($_POST['document_id']);
|
||||
$document_id = intval($row['document_id']);
|
||||
$current_id = intval(getFieldById('documents', $document_id, 'document_template_id'));
|
||||
|
||||
// A current template, or the one it is from already - or none
|
||||
$template_id = intval($_POST['document_template_id'] ?? 0);
|
||||
$template = $template_id ? documentTemplateRow($template_id) : null;
|
||||
if ($template_id && (!$template || ($template['document_template_archived_at'] && $template_id !== $current_id))) {
|
||||
flashAlert('That template is gone or archived.', 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
mysqli_query($mysqli, "UPDATE documents SET document_template_id = $template_id, document_updated_at = document_updated_at WHERE document_id = $document_id");
|
||||
|
||||
logAudit("Document", "Edit", "$session_name " . ($template ? "set document " . escapeSql($row['document_name']) . " as made from template " . escapeSql($template['document_template_name']) : "set document " . escapeSql($row['document_name']) . " as not from a template"), $client_id, $document_id);
|
||||
|
||||
flashAlert("Document <strong>" . escapeHtml($row['document_name']) . "</strong> " . ($template ? "counts as made from <strong>" . escapeHtml($template['document_template_name']) . "</strong>" : "no longer counts as made from a template"));
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['rename_document'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
21
db.sql
21
db.sql
@@ -1293,6 +1293,23 @@ CREATE TABLE `document_template_fill_ins` (
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `document_template_required_tags`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `document_template_required_tags`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `document_template_required_tags` (
|
||||
`document_template_id` int(11) NOT NULL,
|
||||
`tag_id` int(11) NOT NULL,
|
||||
PRIMARY KEY (`document_template_id`,`tag_id`),
|
||||
KEY `tag_id` (`tag_id`),
|
||||
CONSTRAINT `document_template_required_tags_ibfk_1` FOREIGN KEY (`document_template_id`) REFERENCES `document_templates` (`document_template_id`) ON DELETE CASCADE,
|
||||
CONSTRAINT `document_template_required_tags_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`tag_id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `document_templates`
|
||||
--
|
||||
@@ -1305,6 +1322,8 @@ CREATE TABLE `document_templates` (
|
||||
`document_template_name` varchar(200) NOT NULL,
|
||||
`document_template_description` text DEFAULT NULL,
|
||||
`document_template_content` longtext NOT NULL,
|
||||
`document_template_required` tinyint(1) NOT NULL DEFAULT 0,
|
||||
`document_template_review_months` int(11) NOT NULL DEFAULT 0,
|
||||
`document_template_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
`document_template_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
|
||||
`document_template_archived_at` datetime DEFAULT NULL,
|
||||
@@ -1352,9 +1371,11 @@ CREATE TABLE `documents` (
|
||||
`document_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
|
||||
`document_archived_at` datetime DEFAULT NULL,
|
||||
`document_accessed_at` datetime DEFAULT NULL,
|
||||
`document_reviewed_at` datetime DEFAULT NULL,
|
||||
`document_folder_id` int(11) NOT NULL DEFAULT 0,
|
||||
`document_created_by` int(11) NOT NULL DEFAULT 0,
|
||||
`document_updated_by` int(11) NOT NULL DEFAULT 0,
|
||||
`document_reviewed_by` int(11) NOT NULL DEFAULT 0,
|
||||
`document_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`document_template_id` int(11) NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (`document_id`),
|
||||
|
||||
@@ -132,6 +132,20 @@ function clientOverviewAttention($client_id, $access, $renewals) {
|
||||
'detail' => 'Next ' . SERVICE_REVIEW_SOON_DAYS . ' days or overdue',
|
||||
'url' => "/agent/services.php?client_id=$client_id&show=review",
|
||||
];
|
||||
// Document templates it should have a document from - the strip across the top of its Files
|
||||
$items[] = [
|
||||
'count' => count(documentTemplatesMissingFor($client_id)),
|
||||
'label' => 'Documents missing', 'icon' => 'file-medical', 'tone' => 'warning',
|
||||
'detail' => 'Ones every client like it should have',
|
||||
'url' => "/agent/files.php?client_id=$client_id",
|
||||
];
|
||||
// The same rule as files.php?type=due
|
||||
$items[] = [
|
||||
'count' => count(documentsReviewDue("documents.document_client_id = $client_id")),
|
||||
'label' => 'Document reviews due', 'icon' => 'calendar-check', 'tone' => 'warning',
|
||||
'detail' => 'As their templates ask',
|
||||
'url' => "/agent/files.php?client_id=$client_id&type=due",
|
||||
];
|
||||
}
|
||||
|
||||
if ($access['credentials']) {
|
||||
|
||||
@@ -17,6 +17,14 @@
|
||||
* a list or yes or no, required or with help (document_template_fill_ins,
|
||||
* from database 3.2.6 on). One left unanswered stays in the document.
|
||||
*
|
||||
* A template can say who should have a document from it - every client, or
|
||||
* the ones with a client tag - and how often the documents made from it are
|
||||
* reviewed (database 3.2.7). Its page shows which clients are missing one and
|
||||
* which documents are due a review; a client's Files and overview say the
|
||||
* same for that client. A document is reviewed when it is edited or marked as
|
||||
* reviewed (documents.document_reviewed_at), and can be linked to the template
|
||||
* it came from after the fact, so one written by hand counts too.
|
||||
*
|
||||
* Shared by admin/document_templates.php, admin/document_template.php,
|
||||
* admin/modals/document_template/*, admin/post/document_template.php and the
|
||||
* New document from a template window and its handler on the agent side
|
||||
@@ -36,7 +44,7 @@ function documentTemplateRow($document_template_id) {
|
||||
return null;
|
||||
}
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_template_id, document_template_name, document_template_description, document_template_content,
|
||||
document_template_created_at, document_template_updated_at, document_template_archived_at, document_template_created_by, document_template_updated_by,
|
||||
document_template_required, document_template_review_months, document_template_created_at, document_template_updated_at, document_template_archived_at, document_template_created_by, document_template_updated_by,
|
||||
creator.user_name AS created_by_name, updater.user_name AS updated_by_name
|
||||
FROM document_templates
|
||||
LEFT JOIN users creator ON creator.user_id = document_template_created_by AND document_template_created_by > 0
|
||||
@@ -124,7 +132,11 @@ function documentTemplateCopyImages($document_template_id, $to_dir, $to_web, $ht
|
||||
return str_replace("/uploads/document_templates/$document_template_id/", $to_web, (string) $html);
|
||||
}
|
||||
|
||||
/* A new template starting from this one - "Copy of", with copies of its images and its fill-ins. Its id, or 0. */
|
||||
/*
|
||||
* A new template starting from this one - "Copy of", with copies of its images
|
||||
* and its fill-ins, and how often its documents are reviewed. A copy isn't
|
||||
* required of anyone until it is set up to be (its tags come along for that). Its id, or 0.
|
||||
*/
|
||||
function documentTemplateCopy($document_template_id) {
|
||||
global $mysqli, $session_user_id;
|
||||
$row = documentTemplateRow($document_template_id);
|
||||
@@ -133,8 +145,11 @@ function documentTemplateCopy($document_template_id) {
|
||||
}
|
||||
$user_id = intval($session_user_id ?? 0);
|
||||
mysqli_query($mysqli, "INSERT INTO document_templates SET document_template_name = '" . escapeSql(mb_substr('Copy of ' . $row['document_template_name'], 0, 200)) . "',
|
||||
document_template_description = '" . escapeSql((string) $row['document_template_description']) . "', document_template_content = '', document_template_created_by = $user_id");
|
||||
document_template_description = '" . escapeSql((string) $row['document_template_description']) . "', document_template_content = '',
|
||||
document_template_review_months = " . intval($row['document_template_review_months']) . ", document_template_created_by = $user_id");
|
||||
$new_id = intval(mysqli_insert_id($mysqli));
|
||||
mysqli_query($mysqli, "INSERT INTO document_template_required_tags (document_template_id, tag_id)
|
||||
SELECT $new_id, tag_id FROM document_template_required_tags WHERE document_template_id = " . intval($row['document_template_id']));
|
||||
$content = documentTemplateCopyImages($row['document_template_id'], documentTemplateDir($new_id), "/uploads/document_templates/$new_id/", $row['document_template_content']);
|
||||
mysqli_query($mysqli, "UPDATE document_templates SET document_template_content = '" . mysqli_real_escape_string($mysqli, $content) . "', document_template_updated_at = NULL WHERE document_template_id = $new_id");
|
||||
// And what it asks for
|
||||
@@ -149,7 +164,7 @@ function documentTemplateDelete($document_template_id) {
|
||||
global $mysqli;
|
||||
$document_template_id = intval($document_template_id);
|
||||
mysqli_query($mysqli, "DELETE FROM document_templates WHERE document_template_id = $document_template_id");
|
||||
mysqli_query($mysqli, "UPDATE documents SET document_template_id = 0 WHERE document_template_id = $document_template_id");
|
||||
mysqli_query($mysqli, "UPDATE documents SET document_template_id = 0, document_updated_at = document_updated_at WHERE document_template_id = $document_template_id");
|
||||
removeDirectory(documentTemplateDir($document_template_id));
|
||||
}
|
||||
|
||||
@@ -604,3 +619,240 @@ function documentTemplateFill($html, $client_id, array $values) {
|
||||
}
|
||||
return ticketPlaceholderFill($html, $values, true);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- who should have one, and reviews */
|
||||
|
||||
/** Who should have a document from a template (document_template_required). */
|
||||
const DOCUMENT_TEMPLATE_REQUIRED_NONE = 0;
|
||||
const DOCUMENT_TEMPLATE_REQUIRED_EVERY = 1;
|
||||
const DOCUMENT_TEMPLATE_REQUIRED_TAGGED = 2;
|
||||
|
||||
/** How often the documents made from a template are reviewed - months => what it says; 0 never. */
|
||||
const DOCUMENT_TEMPLATE_REVIEW_MONTHS = [0 => 'Never', 3 => 'Every 3 months', 6 => 'Every 6 months', 12 => 'Every year', 24 => 'Every 2 years'];
|
||||
|
||||
/** The client tags templates are required by - [template id => [tag id => tag name]], in name order. */
|
||||
function documentTemplateRequiredTags(array $document_template_ids) {
|
||||
global $mysqli;
|
||||
$ids = array_values(array_filter(array_map('intval', $document_template_ids)));
|
||||
$tags = array_fill_keys($ids, []);
|
||||
if (!$ids) {
|
||||
return $tags;
|
||||
}
|
||||
$sql = mysqli_query($mysqli, "SELECT document_template_id, tags.tag_id, tag_name FROM document_template_required_tags
|
||||
JOIN tags ON tags.tag_id = document_template_required_tags.tag_id
|
||||
WHERE document_template_id IN (" . implode(',', $ids) . ") ORDER BY tag_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$tags[intval($row['document_template_id'])][intval($row['tag_id'])] = (string) $row['tag_name'];
|
||||
}
|
||||
return $tags;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set who should have a document from a template - $required one of the
|
||||
* DOCUMENT_TEMPLATE_REQUIRED_ values, $tag_ids the client tags for TAGGED -
|
||||
* and how often its documents are reviewed. Not a change to what it says, so
|
||||
* when it last changed stays as it was.
|
||||
*/
|
||||
function documentTemplateSetRequired($document_template_id, $required, array $tag_ids, $review_months) {
|
||||
global $mysqli;
|
||||
$document_template_id = intval($document_template_id);
|
||||
$required = in_array(intval($required), [DOCUMENT_TEMPLATE_REQUIRED_EVERY, DOCUMENT_TEMPLATE_REQUIRED_TAGGED], true) ? intval($required) : DOCUMENT_TEMPLATE_REQUIRED_NONE;
|
||||
$review_months = isset(DOCUMENT_TEMPLATE_REVIEW_MONTHS[intval($review_months)]) ? intval($review_months) : 0;
|
||||
mysqli_query($mysqli, "UPDATE document_templates SET document_template_required = $required, document_template_review_months = $review_months,
|
||||
document_template_updated_at = document_template_updated_at WHERE document_template_id = $document_template_id");
|
||||
// Client tags only - any others are left out
|
||||
$tag_ids = array_values(array_unique(array_filter(array_map('intval', $tag_ids))));
|
||||
mysqli_query($mysqli, "DELETE FROM document_template_required_tags WHERE document_template_id = $document_template_id");
|
||||
if ($tag_ids) {
|
||||
mysqli_query($mysqli, "INSERT INTO document_template_required_tags (document_template_id, tag_id)
|
||||
SELECT $document_template_id, tag_id FROM tags WHERE tag_type = 1 AND tag_id IN (" . implode(',', $tag_ids) . ")");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The clients a template is required of, as SQL on clients: current clients,
|
||||
* not leads - every one, or the ones with any of its tags (none when it has
|
||||
* no tags left, or isn't required).
|
||||
*/
|
||||
function documentTemplateScopeSql($required, array $tag_ids) {
|
||||
$sql = "clients.client_archived_at IS NULL AND clients.client_lead = 0";
|
||||
if (intval($required) === DOCUMENT_TEMPLATE_REQUIRED_EVERY) {
|
||||
return $sql;
|
||||
}
|
||||
$tag_ids = array_values(array_filter(array_map('intval', $tag_ids)));
|
||||
if (intval($required) !== DOCUMENT_TEMPLATE_REQUIRED_TAGGED || !$tag_ids) {
|
||||
return '0';
|
||||
}
|
||||
return "$sql AND EXISTS (SELECT 1 FROM client_tags WHERE client_tags.client_id = clients.client_id AND client_tags.tag_id IN (" . implode(',', $tag_ids) . "))";
|
||||
}
|
||||
|
||||
/** Who a template is required of, in words - '' when it isn't. */
|
||||
function documentTemplateScopeText($required, array $tags) {
|
||||
if (intval($required) === DOCUMENT_TEMPLATE_REQUIRED_EVERY) {
|
||||
return 'Every client';
|
||||
}
|
||||
if (intval($required) !== DOCUMENT_TEMPLATE_REQUIRED_TAGGED) {
|
||||
return '';
|
||||
}
|
||||
return $tags ? 'Clients tagged ' . implode(' or ', $tags) : 'Clients with a tag - none picked';
|
||||
}
|
||||
|
||||
/*
|
||||
* How far a required template reaches - ['clients' => how many it is
|
||||
* required of, 'covered' => how many of them have a current document from
|
||||
* it, 'missing' => [client id => name] the rest, by name].
|
||||
*/
|
||||
function documentTemplateCoverage($document_template_id, $required, array $tag_ids) {
|
||||
global $mysqli;
|
||||
$document_template_id = intval($document_template_id);
|
||||
$coverage = ['clients' => 0, 'covered' => 0, 'missing' => []];
|
||||
$sql = mysqli_query($mysqli, "SELECT client_id, client_name, EXISTS (SELECT 1 FROM documents WHERE document_client_id = clients.client_id
|
||||
AND document_template_id = $document_template_id AND document_archived_at IS NULL) AS covered
|
||||
FROM clients WHERE " . documentTemplateScopeSql($required, $tag_ids) . " ORDER BY client_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$coverage['clients']++;
|
||||
if (intval($row['covered'])) {
|
||||
$coverage['covered']++;
|
||||
} else {
|
||||
$coverage['missing'][intval($row['client_id'])] = (string) $row['client_name'];
|
||||
}
|
||||
}
|
||||
return $coverage;
|
||||
}
|
||||
|
||||
/*
|
||||
* The required templates a client has no current document from - [template
|
||||
* id => name]. None for an archived client or a lead.
|
||||
*/
|
||||
function documentTemplatesMissingFor($client_id) {
|
||||
global $mysqli;
|
||||
$client_id = intval($client_id);
|
||||
$missing = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT document_template_id, document_template_name FROM document_templates
|
||||
WHERE document_template_archived_at IS NULL AND document_template_required IN (" . DOCUMENT_TEMPLATE_REQUIRED_EVERY . ", " . DOCUMENT_TEMPLATE_REQUIRED_TAGGED . ")
|
||||
AND EXISTS (SELECT 1 FROM clients WHERE clients.client_id = $client_id AND clients.client_archived_at IS NULL AND clients.client_lead = 0)
|
||||
AND (document_template_required = " . DOCUMENT_TEMPLATE_REQUIRED_EVERY . " OR EXISTS (SELECT 1 FROM document_template_required_tags rt
|
||||
JOIN client_tags ON client_tags.tag_id = rt.tag_id AND client_tags.client_id = $client_id
|
||||
WHERE rt.document_template_id = document_templates.document_template_id))
|
||||
AND NOT EXISTS (SELECT 1 FROM documents WHERE document_client_id = $client_id AND documents.document_template_id = document_templates.document_template_id AND document_archived_at IS NULL)
|
||||
ORDER BY document_template_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$missing[intval($row['document_template_id'])] = (string) $row['document_template_name'];
|
||||
}
|
||||
return $missing;
|
||||
}
|
||||
|
||||
/* When a document was last looked at - made, edited or marked as reviewed - as SQL on documents. */
|
||||
function documentReviewedAtSql() {
|
||||
return "GREATEST(documents.document_created_at, COALESCE(documents.document_updated_at, documents.document_created_at), COALESCE(documents.document_reviewed_at, documents.document_created_at))";
|
||||
}
|
||||
|
||||
/*
|
||||
* A document due a review, as SQL on documents joined to document_templates:
|
||||
* its template - a current one - says how often, and it has been longer than
|
||||
* that since it was last looked at.
|
||||
*/
|
||||
function documentReviewDueSql() {
|
||||
return "document_templates.document_template_archived_at IS NULL AND document_templates.document_template_review_months > 0
|
||||
AND " . documentReviewedAtSql() . " < NOW() - INTERVAL document_templates.document_template_review_months MONTH";
|
||||
}
|
||||
|
||||
/*
|
||||
* The current documents due a review, the longest overdue first - for one
|
||||
* template, or one client ($where: SQL on documents, document_templates and
|
||||
* clients). Each with document_id, document_name, document_client_id,
|
||||
* client_name, document_template_id, document_template_name, reviewed_at and
|
||||
* document_template_review_months.
|
||||
*/
|
||||
function documentsReviewDue($where) {
|
||||
global $mysqli;
|
||||
$due = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT documents.document_id, document_name, document_client_id, client_name, documents.document_template_id, document_template_name,
|
||||
" . documentReviewedAtSql() . " AS reviewed_at, document_template_review_months
|
||||
FROM documents
|
||||
JOIN document_templates ON document_templates.document_template_id = documents.document_template_id
|
||||
JOIN clients ON clients.client_id = documents.document_client_id AND clients.client_archived_at IS NULL
|
||||
WHERE documents.document_archived_at IS NULL AND " . documentReviewDueSql() . " AND ($where)
|
||||
ORDER BY reviewed_at ASC, documents.document_id ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$due[intval($row['document_id'])] = $row;
|
||||
}
|
||||
return $due;
|
||||
}
|
||||
|
||||
/** How many current documents from each template are due a review - [template id => count]; ones with none aren't in it. */
|
||||
function documentTemplateDueCounts(array $document_template_ids) {
|
||||
global $mysqli;
|
||||
$ids = array_values(array_filter(array_map('intval', $document_template_ids)));
|
||||
$counts = [];
|
||||
if (!$ids) {
|
||||
return $counts;
|
||||
}
|
||||
$sql = mysqli_query($mysqli, "SELECT documents.document_template_id, COUNT(*) FROM documents
|
||||
JOIN document_templates ON document_templates.document_template_id = documents.document_template_id
|
||||
JOIN clients ON clients.client_id = documents.document_client_id AND clients.client_archived_at IS NULL
|
||||
WHERE documents.document_template_id IN (" . implode(',', $ids) . ") AND documents.document_archived_at IS NULL AND " . documentReviewDueSql() . "
|
||||
GROUP BY documents.document_template_id");
|
||||
while ($row = mysqli_fetch_row($sql)) {
|
||||
$counts[intval($row[0])] = intval($row[1]);
|
||||
}
|
||||
return $counts;
|
||||
}
|
||||
|
||||
/*
|
||||
* Where a document stands for review, from its row (document_created_at,
|
||||
* document_updated_at, document_reviewed_at, and its template's
|
||||
* document_template_review_months and document_template_archived_at) -
|
||||
* ['last' => when it was last looked at, 'reviewed' => whether that was a
|
||||
* review rather than an edit, 'next' => when it is due, 'due' => bool], or
|
||||
* null when its template doesn't ask for reviews.
|
||||
*/
|
||||
function documentReviewState(array $row) {
|
||||
$months = intval($row['document_template_review_months'] ?? 0);
|
||||
if ($months <= 0 || !empty($row['document_template_archived_at'])) {
|
||||
return null;
|
||||
}
|
||||
$times = array_filter([$row['document_created_at'] ?? null, $row['document_updated_at'] ?? null, $row['document_reviewed_at'] ?? null]);
|
||||
$last = max(array_map('strtotime', $times));
|
||||
$next = strtotime("+$months months", $last);
|
||||
return [
|
||||
'last' => date('Y-m-d H:i:s', $last),
|
||||
'reviewed' => !empty($row['document_reviewed_at']) && strtotime($row['document_reviewed_at']) === $last,
|
||||
'next' => date('Y-m-d H:i:s', $next),
|
||||
'due' => $next < time(),
|
||||
];
|
||||
}
|
||||
|
||||
/*
|
||||
* Documents from a template for several clients at once, each filled in from
|
||||
* its own client (its primary site and contact), the answers to the fill-ins
|
||||
* the same for all ($answers, from ticketFillInAnswers()), at the top level of
|
||||
* its Files. With $skip_existing, a client with a current document from it is
|
||||
* passed over. ['made' => [client id => document id], 'skipped' => [client id
|
||||
* => name]] - only current clients.
|
||||
*/
|
||||
function documentTemplateMakeForClients($document_template_id, array $client_ids, array $answers, $skip_existing = true) {
|
||||
global $mysqli;
|
||||
$document_template_id = intval($document_template_id);
|
||||
$result = ['made' => [], 'skipped' => []];
|
||||
$client_ids = array_values(array_unique(array_filter(array_map('intval', $client_ids))));
|
||||
if (!$client_ids) {
|
||||
return $result;
|
||||
}
|
||||
$sql = mysqli_query($mysqli, "SELECT client_id, client_name, EXISTS (SELECT 1 FROM documents WHERE document_client_id = client_id
|
||||
AND document_template_id = $document_template_id AND document_archived_at IS NULL) AS has_one
|
||||
FROM clients WHERE client_archived_at IS NULL AND client_id IN (" . implode(',', $client_ids) . ") ORDER BY client_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$client_id = intval($row['client_id']);
|
||||
if ($skip_existing && intval($row['has_one'])) {
|
||||
$result['skipped'][$client_id] = (string) $row['client_name'];
|
||||
continue;
|
||||
}
|
||||
$document_id = documentTemplateMakeDocument($document_template_id, $client_id, '', '', 0, 0, 0, $answers);
|
||||
if (!$document_id) {
|
||||
break;
|
||||
}
|
||||
$result['made'][$client_id] = $document_id;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -661,8 +661,8 @@ function tagDelete($tag_id, &$error = '') {
|
||||
|
||||
/*
|
||||
* Merge tags into one of the same kind: every record with one of them gets
|
||||
* $into_id instead, recurring invoice lines counting by them count by it, and
|
||||
* they are deleted. How many records changed hands.
|
||||
* $into_id instead, recurring invoice lines counting by them count by it, the
|
||||
* ticket and document templates going by them go by it, and they are deleted. How many records changed hands.
|
||||
*/
|
||||
function tagMerge(array $from_ids, $into_id) {
|
||||
global $mysqli;
|
||||
@@ -690,6 +690,10 @@ function tagMerge(array $from_ids, $into_id) {
|
||||
$moved = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(DISTINCT {$config['link_record']}) FROM {$config['link_table']} WHERE {$config['link_tag']} IN ($from_list)"))[0]);
|
||||
mysqli_query($mysqli, "INSERT IGNORE INTO {$config['link_table']} ({$config['link_record']}, {$config['link_tag']})
|
||||
SELECT DISTINCT {$config['link_record']}, $into_id FROM {$config['link_table']} WHERE {$config['link_tag']} IN ($from_list)");
|
||||
// Templates that go by these tags - a ticket template's tags, the clients a document template is required of - go by the one they are merged into
|
||||
foreach (['ticket_template_tags' => 'ticket_template_id', 'document_template_required_tags' => 'document_template_id'] as $table => $column) {
|
||||
mysqli_query($mysqli, "INSERT IGNORE INTO $table ($column, tag_id) SELECT DISTINCT $column, $into_id FROM $table WHERE tag_id IN ($from_list)");
|
||||
}
|
||||
foreach (tagSeatLineItems($from_ids) as $item_id => $filter) {
|
||||
$filter['tag'] = $into_id;
|
||||
mysqli_query($mysqli, "UPDATE recurring_invoice_items SET item_seat_filter = '" . escapeSql(json_encode($filter)) . "' WHERE item_id = " . intval($item_id));
|
||||
|
||||
Reference in New Issue
Block a user