Undo in Admin after archiving tags and categories (one or several), canned responses, ticket, project, document, vendor, license and contract templates, roles, payment methods with any default they were, SLAs, custom links, portal announcements and agent banners, and after archiving a tax or switching off a ticket status when nothing was moved; payment methods archive without asking first and the SLA archive message names the SLA

This commit is contained in:
johnnyq
2026-11-20 16:53:11 -05:00
parent 651a4d1306
commit 8d9d15d6f7
20 changed files with 133 additions and 8 deletions

View File

@@ -188,7 +188,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
- Projects: a project has a Timeline - its booked tickets as bars in time, from its start to its due date, with now marked. A booking that is past and still open shows in amber, and two of the same agent's at the same time are flagged. Its open tickets with no booking are listed under it, with Book.
- Pickers: long lists of clients, contacts, vendors, categories, locations, assets, technicians, projects, accounts and products start with the last five you picked in a field like it, under Recent, above All. Typing searches everything as before.
- Archived: Tools > Archived lists what was archived lately - clients, contacts, locations, vendors, assets, credentials, documents, files, domains, certificates, networks, racks, services, licenses, projects and products - newest first, with who archived each one, to restore one or several at once. The last 30 days to start with, or pick another time, a kind or a client.
- Undo: archiving no longer asks "Are you sure?" - the message that says it's done has Undo instead, as do moves and changes to several records at once. The message stays up for ten seconds, or as long as the pointer is on it, and only its x closes it - set apart from Undo, so a click that just misses Undo doesn't lose it. Undo covers archiving one or several of every kind Archived lists, bringing back what archiving took with it - a contact's roles, a document's links, a client's recurring invoices; moving files, documents and folders, dragged or with Move; dragging kit on a rack; and the bulk changes to a ticket's priority, category or project, an asset's location, physical location, contact or status, a contact's location, department, phone number or roles, a client's industry or referral, a project's manager or due date, an invoice's or product's category, and an IP address's type. Undo works once and only for whoever made the change, and leaves anything changed again since as it is, saying how many. Archiving a client still asks first, and has no Undo when it withdrew a contract out for signing.
- Undo: archiving no longer asks "Are you sure?" - the message that says it's done has Undo instead, as do moves and changes to several records at once. The message stays up for ten seconds, or as long as the pointer is on it, and only its x closes it - set apart from Undo, so a click that just misses Undo doesn't lose it. Undo covers archiving one or several of every kind Archived lists, bringing back what archiving took with it - a contact's roles, a document's links, a client's recurring invoices; moving files, documents and folders, dragged or with Move; dragging kit on a rack; and the bulk changes to a ticket's priority, category or project, an asset's location, physical location, contact or status, a contact's location, department, phone number or roles, a client's industry or referral, a project's manager or due date, an invoice's or product's category, and an IP address's type. In Admin, Undo is on archiving tags and categories (one or several), canned responses, ticket, project, document, vendor, license and contract templates, roles, payment methods (a default it stopped being comes back with it), SLAs, custom links, portal announcements and banners, on archiving a tax, and on switching off a ticket status - those last two when nothing was moved to another. A payment method is archived without asking first now. Undo works once and only for whoever made the change, and leaves anything changed again since as it is, saying how many. Archiving a client still asks first, and has no Undo when it withdrew a contract out for signing.
- Client portal: one Search box in the top bar looks through the client's tickets - their subject, number and what was said on them - the documents you share with them and your Help articles at once, the first eight of each with See all. Nothing found offers Ask us, and a search with no Help article goes on the Knowledge Base's Not found list.
- Client portal: a ticket says when to expect your first reply - We'll reply by and the time, from its SLA - until you reply, while it isn't on hold and the time is still ahead. New ticket says how soon you reply to each priority, beside the priority and under What happens next. Without SLAs nothing is promised.
- Users: Invite several, beside New User, invites a list of people at once with one role (and office, and whether two-step sign-in is required) - one per line, as a name and email or just the email. Each gets their own invite email; anyone who already has an account is skipped and named. It needs email set up.
@@ -269,7 +269,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
### Developer Updates
- Leaving a page: `js/app.js`'s `beforeunload` guard for TinyMCE editors skips one whose element has `data-draft-kept="1"` - the ticket reply when `ticket_reply_drafts` is in use, which `agent/js/ticket_reply_extras.js` keeps with `sendBeacon` on `pagehide`. `js/inline_notes.js` no longer warns either: on `pagehide` it posts an unsaved note to its `ajax.php` endpoint with `sendBeacon`. The document editor page, the profile signature and the settings pages still ask, as nothing keeps what was typed there.
- Undo: `functions/undo.php`. A handler that can be put back calls `undoOffer()` after its `flashAlert()` with the message for afterwards and the steps that put it back; the steps are kept in `\$_SESSION['undo']` under a random token for `UNDO_SECONDS` (600, the last `UNDO_KEEP` of them), and `\$_SESSION['alert_undo']` gives the next message an Undo button (`includes/inc_alert_feedback.php`, `.itflow-toast-undo`) linking to `agent/post.php?undo=` with the CSRF token (`agent/post/undo.php`, `undoApply()`), which checks the user, the age and the permission level again. `flashAlert()` clears `alert_undo`, so only the message it was offered on carries it. `undoSnapshot()` before a change and `undoOfferChanges()` after offer the columns back as `set` steps, each only if the row still holds what the change wrote; `undoArchiveBefore()` / `undoOfferArchive()` do the same for archiving through `archivedRestore()`, with a contact's role flags, a document's link rows and a client's running recurring invoices. Steps that need their side effects go through their own code: `ticket_priority` (`ticketChangePriority()`), `ticket_category` and `ticket_project` (ticket history), `asset_status` (asset history), `folder` (no loop) and `rack` (`rackUnitConflict()`). The archive links and bulk Archive buttons of those kinds lose `confirm-link`; clients keep theirs.
- Undo: `functions/undo.php`. A handler that can be put back calls `undoOffer()` after its `flashAlert()` with the message for afterwards and the steps that put it back; the steps are kept in `\$_SESSION['undo']` under a random token for `UNDO_SECONDS` (600, the last `UNDO_KEEP` of them), and `\$_SESSION['alert_undo']` gives the next message an Undo button (`includes/inc_alert_feedback.php`, `.itflow-toast-undo`) linking to `agent/post.php?undo=` with the CSRF token (`agent/post/undo.php`, `undoApply()`), which checks the user, the age and the permission level again. `flashAlert()` clears `alert_undo`, so only the message it was offered on carries it. `undoSnapshot()` before a change and `undoOfferChanges()` after offer the columns back as `set` steps, each only if the row still holds what the change wrote; `undoArchiveBefore()` / `undoOfferArchive()` do the same for archiving through `archivedRestore()`, with a contact's role flags, a document's link rows and a client's running recurring invoices. Steps that need their side effects go through their own code: `ticket_priority` (`ticketChangePriority()`), `ticket_category` and `ticket_project` (ticket history), `asset_status` (asset history), `folder` (no loop) and `rack` (`rackUnitConflict()`). The archive links and bulk Archive buttons of those kinds lose `confirm-link`; clients keep theirs. Records that aren't on Archived - the admin pages' - use `undoArchivedBefore()` (the rows of an `X_id` not yet archived in `X_archived_at`) and `undoOfferArchived()` (`set` steps, Admin at level 3 unless the caller names a module), with snapshots of what else the archive changed - the payment method and tax defaults in `settings` - as `also` steps, which aren't counted. `undoOffer()` takes a `$plural` of `y|ies` for both endings, and `UNDO_KEEP_UPDATED` names the tables whose updated time a `set` step leaves as it was (documents, license and vendor templates). The payment method archive link loses `confirm-link`.
- Database 3.6.7: `rmm_providers` (`rmm_provider_kind` - `tactical` for now - the name it goes by, `api_url`, `web_url`, `api_key`, `api_secret` for an RMM that signs in with an id and a secret, and how the last sync went), `rmm_clients` (each provider's clients, `rmm_client_client_id` 0 until linked, `rmm_client_removed_at` once the RMM stops listing one) and `rmm_devices` (one a device: its asset, what the RMM last said - status, last seen, signed-in user, reboot, patches, checks - and `rmm_device_synced`, JSON of what the sync last wrote to the asset). Re-runnable, like 3.6.0.
- `functions/rmm.php` is what every RMM shares. A kind is an entry in `rmmProviderKinds()` and a file of its own - `functions/rmm_tactical.php` for Tactical. It names the connection fields it uses, with their labels and help (the window and its checks are built from them), hints added to errors, the device links it offers, and four functions: `clients`, `devices` (each device in the shape described above `rmmDeviceFacts()`), `dashboard` and `device_urls`. `rmmHttp()` makes the request and turns a refused key, a redirect, a 404 or an answer that isn't JSON into a sentence for the person; `rmmTacticalRequest()` adds Tactical's `X-API-KEY`. Another RMM is a file like `rmm_tactical.php`, a line in `rmmProviderKinds()` and a `require_once` in `functions.php`.
- `rmmSync($provider, $only = 0, $max_seconds = 120)` asks the kind for its clients, then for each linked client's devices, longest since brought in first, and stops on time to pick up next run. `GET_LOCK('itflow_rmm_sync_<provider id>')` keeps each provider to one sync at a time. Devices and clients are marked removed by what the RMM listed, never by time, and only for a client whose list came back whole. One client failing doesn't hold up the rest or mark its devices removed. `rmmDeviceSave()` matches (`rmmAssetMatch()` - serial, then a name only one asset has, among this client's unarchived assets no other device of the provider has), makes (`rmmAssetCreate()`) and follows (`rmmAssetFollow()`). The follow rule: a field is written when it is empty or still holds what `rmm_device_synced` says was last written, and a blank never writes. Values go through `rmmText()` first - what `escapeSql()` would store - so a value compares the same on both sides and isn't written again every run. CPU and storage are asset details (`asset_specs`) and are only written for a type that has them. A device two providers have is one asset with a row from each, and each follows only what it wrote.

View File

@@ -210,8 +210,8 @@ $csrf = $_SESSION['csrf_token'];
<i class="fas fa-fw fa-box-open text-secondary me-2"></i>Restore
</a>
<?php } else { ?>
<a class="dropdown-item text-danger confirm-link" href="post.php?archive_payment_method=<?= $payment_method_id ?>&csrf_token=<?= $csrf ?>"
data-confirm-title="Archive <?= $name ?>?" data-confirm-text="It can't be picked any more<?= $is_default ? ', and stops being a default' : '' ?>. The records that used it keep it." data-confirm-button="Archive">
<a class="dropdown-item text-danger" href="post.php?archive_payment_method=<?= $payment_method_id ?>&csrf_token=<?= $csrf ?>"
title="It can't be picked any more<?= $is_default ? ', and stops being a default' : '' ?> - the records that used it keep it">
<i class="fas fa-fw fa-archive me-2"></i>Archive
</a>
<?php } ?>

View File

@@ -76,11 +76,15 @@ if (isset($_GET['archive_canned_response']) || isset($_GET['restore_canned_respo
$name = escapeSql($row['canned_response_name']);
// Archived, it isn't offered on tickets; restoring it is all it takes to offer it again
$undo = $archive ? undoArchivedBefore('canned_responses', 'canned_response_id', [$canned_response_id]) : null;
mysqli_query($mysqli, "UPDATE canned_responses SET canned_response_archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE canned_response_id = $canned_response_id");
logAudit("Canned Response", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " canned response $name", 0, $canned_response_id);
flashAlert("Canned response <strong>" . escapeHtml($row['canned_response_name']) . "</strong> " . ($archive ? 'archived' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
undoOfferArchived($undo, '', $row['canned_response_name']);
}
redirect();

View File

@@ -93,11 +93,13 @@ if (isset($_GET['archive_category'])) {
$row = categoryRow(intval($_GET['archive_category']));
if ($row && !$row['category_archived_at']) {
$undo = undoArchivedBefore('categories', 'category_id', [$row['category_id']]);
categoryArchive($row['category_id']);
$used = categoryUsageFor($row['category_id']);
logAudit("Category", "Archive", "$session_name archived " . $category_kind_word($row) . " " . escapeSql($row['category_name']), 0, intval($row['category_id']));
flashAlert(ucfirst($category_kind_word($row)) . " <strong>" . escapeHtml($row['category_name']) . "</strong> archived - "
. ($used['all'] ? "it stays on the " . escapeHtml(categoryUsageText($row, $used)) . " that have it, but isn't offered for new ones" : "it isn't offered for new ones"));
undoOfferArchived($undo, 'categor', $row['category_name'], [], 'y|ies');
}
redirect();
@@ -244,6 +246,7 @@ if (isset($_POST['bulk_archive_categories']) || isset($_POST['bulk_restore_categ
validateCSRFToken();
$archive = isset($_POST['bulk_archive_categories']);
$undo = $archive ? undoArchivedBefore('categories', 'category_id', $_POST['category_ids'] ?? []) : null;
$count = 0;
foreach (array_unique(array_map('intval', (array) ($_POST['category_ids'] ?? []))) as $category_id) {
$row = categoryRow($category_id);
@@ -260,6 +263,9 @@ if (isset($_POST['bulk_archive_categories']) || isset($_POST['bulk_restore_categ
}
flashAlert(($archive ? "Archived " : "Restored ") . $count . ($count === 1 ? " category" : " categories") . ($archive && $count ? " - they stay on their records, but aren't offered for new ones" : ''));
if ($undo) {
undoOfferArchived($undo, 'categor', '', [], 'y|ies');
}
redirect();

View File

@@ -88,10 +88,14 @@ if (isset($_GET['archive_contract_template']) || isset($_GET['restore_contract_t
$contract_template_id = intval($row['contract_template_id']);
// Archived, it isn't offered on a new contract; contracts started from it are unchanged
$undo = $archive ? undoArchivedBefore('contract_templates', 'contract_template_id', [$contract_template_id]) : null;
mysqli_query($mysqli, "UPDATE contract_templates SET contract_template_archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE contract_template_id = $contract_template_id");
logAudit("Contract Template", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " contract template " . escapeSql($row['contract_template_name']), 0, $contract_template_id);
flashAlert("Contract template <strong>" . escapeHtml($row['contract_template_name']) . "</strong> " . ($archive ? 'archived' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
undoOfferArchived($undo, '', $row['contract_template_name']);
}
redirect();

View File

@@ -68,6 +68,7 @@ if (isset($_GET['archive_custom_link']) || isset($_GET['restore_custom_link']))
$row = customLinkRow($archive ? $_GET['archive_custom_link'] : $_GET['restore_custom_link']);
if ($row) {
$custom_link_id = intval($row['custom_link_id']);
$undo = $archive ? undoArchivedBefore('custom_links', 'custom_link_id', [$custom_link_id]) : null;
if ($archive) {
mysqli_query($mysqli, "UPDATE custom_links SET custom_link_archived_at = NOW() WHERE custom_link_id = $custom_link_id");
} else {
@@ -76,6 +77,9 @@ if (isset($_GET['archive_custom_link']) || isset($_GET['restore_custom_link']))
}
logAudit("Custom Link", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " custom link " . escapeSql($row['custom_link_name']), 0, $custom_link_id);
flashAlert("Link <strong>" . escapeHtml($row['custom_link_name']) . "</strong> " . ($archive ? 'archived - it no longer shows in its menu' : 'restored'));
if ($undo) {
undoOfferArchived($undo, '', $row['custom_link_name']);
}
}
redirect();

View File

@@ -72,11 +72,15 @@ if (isset($_GET['archive_document_template']) || isset($_GET['restore_document_t
$document_template_id = intval($row['document_template_id']);
// Archived, it isn't offered for new documents; the ones made from it stay
$undo = $archive ? undoArchivedBefore('document_templates', 'document_template_id', [$document_template_id]) : null;
mysqli_query($mysqli, "UPDATE document_templates SET document_template_archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE document_template_id = $document_template_id");
logAudit("Document Template", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " document template " . escapeSql($row['document_template_name']), 0, $document_template_id);
flashAlert("Document template <strong>" . escapeHtml($row['document_template_name']) . "</strong> " . ($archive ? 'archived' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
undoOfferArchived($undo, '', $row['document_template_name']);
}
redirect();

View File

@@ -71,10 +71,15 @@ if (isset($_GET['archive_payment_method']) || isset($_GET['restore_payment_metho
$archive = isset($_GET['archive_payment_method']);
$row = paymentMethodRow($archive ? $_GET['archive_payment_method'] : $_GET['restore_payment_method']);
if ($row) {
$undo = $archive ? undoArchivedBefore('payment_methods', 'payment_method_id', [$row['payment_method_id']]) : null;
$defaults = $archive ? undoSnapshot('settings', 'company_id', [1], ['config_default_payment_method', 'config_default_expense_payment_method']) : null;
$cleared = paymentMethodSetArchived($row, $archive);
$no_longer = $cleared ? ' - no longer the default for ' . implode(' or ', array_map(function ($way) { return $way === 'in' ? 'payments' : 'expenses'; }, $cleared)) : '';
logAudit("Payment Method", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " payment method " . escapeSql($row['payment_method_name']) . $no_longer, 0, intval($row['payment_method_id']));
flashAlert("Payment method <strong>" . escapeHtml($row['payment_method_name']) . "</strong> " . ($archive ? "archived - it isn't offered any more$no_longer" : 'restored'));
if ($undo) {
undoOfferArchived($undo, '', $row['payment_method_name'], [$defaults]);
}
}
redirect();

View File

@@ -62,12 +62,16 @@ if (isset($_GET['archive_portal_announcement']) || isset($_GET['restore_portal_a
redirect();
}
$undo = $archive ? undoArchivedBefore('portal_announcements', 'announcement_id', [$announcement_id]) : null;
mysqli_query($mysqli, "UPDATE portal_announcements SET announcement_archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE announcement_id = $announcement_id");
$title = escapeSql($row['announcement_title']);
logAudit("Announcement", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " the client portal announcement $title", intval($row['announcement_client_id']), $announcement_id);
flashAlert("Announcement <strong>" . escapeHtml($row['announcement_title']) . "</strong> " . ($archive ? 'archived - the portal no longer shows it' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
undoOfferArchived($undo, '', $row['announcement_title']);
}
redirect();

View File

@@ -140,11 +140,15 @@ if (isset($_GET['archive_project_template']) || isset($_GET['restore_project_tem
$project_template_id = intval($archive ? $_GET['archive_project_template'] : $_GET['restore_project_template']);
$name = projectTemplateName($project_template_id);
$undo = $archive ? undoArchivedBefore('project_templates', 'project_template_id', [$project_template_id]) : null;
mysqli_query($mysqli, "UPDATE project_templates SET project_template_archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE project_template_id = $project_template_id");
logAudit("Project Template", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " project template " . escapeSql($name), 0, $project_template_id);
flashAlert("Project template <strong>" . escapeHtml($name) . "</strong> " . ($archive ? 'archived - it is no longer offered on New Project' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
undoOfferArchived($undo, '', $name);
}
redirect();

View File

@@ -78,11 +78,13 @@ if (isset($_GET['archive_role'])) {
redirect();
}
$undo = undoArchivedBefore('user_roles', 'role_id', [$row['role_id']]);
roleSetArchived($row['role_id'], true);
logAudit("User Role", "Archive", "$session_name archived role " . escapeSql($row['role_name']), 0, $row['role_id']);
flashAlert("Role <strong>" . escapeHtml($row['role_name']) . "</strong> archived");
undoOfferArchived($undo, '', $row['role_name']);
redirect();

View File

@@ -86,6 +86,8 @@ if (isset($_GET['archive_sla'])) {
validateCSRFToken();
$sla_id = intval($_GET['archive_sla']);
$sla_name = (string) getFieldById('slas', $sla_id, 'sla_name');
$undo = undoArchivedBefore('slas', 'sla_id', [$sla_id]);
mysqli_query($mysqli, "UPDATE slas SET sla_archived_at = NOW() WHERE sla_id = $sla_id");
@@ -94,7 +96,8 @@ if (isset($_GET['archive_sla'])) {
logAudit("SLA", "Archive", "$session_name archived SLA ID $sla_id");
flashAlert("SLA archived");
flashAlert($sla_name !== '' ? "SLA <strong>" . escapeHtml($sla_name) . "</strong> archived" : "SLA archived");
undoOfferArchived($undo, 'SLA', $sla_name);
redirect();

View File

@@ -224,12 +224,16 @@ if (isset($_GET['archive_software_template']) || isset($_GET['restore_software_t
$software_template_id = intval($row['software_template_id']);
// Archived, it isn't offered for new licenses; the ones made from it stay
$undo = $archive ? undoArchivedBefore('software_templates', 'software_template_id', [$software_template_id]) : null;
mysqli_query($mysqli, "UPDATE software_templates SET software_template_archived_at = " . ($archive ? 'NOW()' : 'NULL') . ", software_template_updated_at = software_template_updated_at
WHERE software_template_id = $software_template_id");
logAudit("Software Template", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " license template " . escapeSql($row['software_template_name']), 0, $software_template_id);
flashAlert("License template <strong>" . escapeHtml($row['software_template_name']) . "</strong> " . ($archive ? 'archived' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
undoOfferArchived($undo, '', $row['software_template_name']);
}
redirect();

View File

@@ -77,11 +77,13 @@ if (isset($_GET['archive_tag'])) {
$row = tagRow(intval($_GET['archive_tag']));
if ($row && !$row['tag_archived_at']) {
$undo = undoArchivedBefore('tags', 'tag_id', [$row['tag_id']]);
tagArchive($row['tag_id']);
$used = tagUsage($row['tag_id']);
logAudit("Tag", "Archive", "$session_name archived " . $tag_kind_word($row) . " tag " . escapeSql($row['tag_name']), 0, intval($row['tag_id']));
flashAlert("Tag <strong>" . escapeHtml($row['tag_name']) . "</strong> archived - "
. ($used ? "it stays on its " . $tag_records_text($used, $row['tag_type']) . ", but isn't offered for tagging" : "it isn't offered for tagging"));
undoOfferArchived($undo, 'tag', $row['tag_name']);
}
redirect();
@@ -171,6 +173,7 @@ if (isset($_POST['bulk_archive_tags']) || isset($_POST['bulk_restore_tags'])) {
validateCSRFToken();
$archive = isset($_POST['bulk_archive_tags']);
$undo = $archive ? undoArchivedBefore('tags', 'tag_id', $_POST['tag_ids'] ?? []) : null;
$count = 0;
foreach (array_unique(array_map('intval', (array) ($_POST['tag_ids'] ?? []))) as $tag_id) {
$row = tagRow($tag_id);
@@ -187,6 +190,9 @@ if (isset($_POST['bulk_archive_tags']) || isset($_POST['bulk_restore_tags'])) {
}
flashAlert(($archive ? "Archived " : "Restored ") . $count . ($count === 1 ? " tag" : " tags") . ($archive && $count ? " - they stay on their records, but aren't offered for tagging" : ''));
if ($undo) {
undoOfferArchived($undo, 'tag');
}
redirect();

View File

@@ -81,6 +81,8 @@ if (isset($_POST['archive_tax'])) {
}
$move_to = intval($_POST['move_to'] ?? 0);
$undo = undoArchivedBefore('taxes', 'tax_id', [$tax_id]);
$default = undoSnapshot('settings', 'company_id', [1], ['config_tax_default_id']);
$done = taxArchive($tax_id, $move_to);
$label = taxLabel($row);
$moved = taxMovedText($done['moved']);
@@ -91,6 +93,10 @@ if (isset($_POST['archive_tax'])) {
flashAlert("Tax <strong>" . escapeHtml($label) . "</strong> archived"
. ($moved !== '' ? " - $moved moved to " . escapeHtml($to) : '')
. ($done['was_default'] ? ". There is no default rate now" : ''), 'error');
// What was moved to another rate isn't put back - with nothing moved, Undo is all it takes
if (!$done['moved']) {
undoOfferArchived($undo, '', $label, [$default]);
}
redirect();

View File

@@ -123,6 +123,7 @@ if (isset($_POST['switch_off_ticket_status'])) {
redirect();
}
$undo = undoSnapshot('ticket_statuses', 'ticket_status_id', [$row['ticket_status_id']], ['ticket_status_active']);
[$ok, $moved] = ticketStatusSetActive($row, false, $move_to);
if (!$ok) {
flashAlert("ITFlow's own statuses can't be switched off.", 'error');
@@ -132,6 +133,10 @@ if (isset($_POST['switch_off_ticket_status'])) {
$to_name = $moved ? ticketStatusesAll()[$move_to]['ticket_status_name'] : '';
logAudit("Ticket Status", "Edit", "$session_name switched off ticket status " . escapeSql($name) . ($moved ? ", moving $moved tickets to " . escapeSql($to_name) : ''), 0, intval($row['ticket_status_id']));
flashAlert("Status <strong>" . escapeHtml($name) . "</strong> switched off" . ($moved ? " - $moved " . ($moved === 1 ? 'ticket' : 'tickets') . " moved to <strong>" . escapeHtml($to_name) . "</strong>" : ''));
// Tickets moved to another status stay there - with none moved, Undo switches it back on
if (!$moved) {
undoOfferChanges($undo, '<strong>' . escapeHtml($name) . '</strong> is switched on again', 'admin', 3);
}
redirect();

View File

@@ -81,11 +81,15 @@ if (isset($_GET['archive_ticket_template']) || isset($_GET['restore_ticket_templ
$name = escapeSql($row['ticket_template_name']);
// Archived, it isn't offered for new tickets - recurring tickets and project templates that use it keep it, so restoring it is all it takes
$undo = $archive ? undoArchivedBefore('ticket_templates', 'ticket_template_id', [$ticket_template_id]) : null;
mysqli_query($mysqli, "UPDATE ticket_templates SET ticket_template_archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE ticket_template_id = $ticket_template_id");
logAudit("Ticket Template", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " ticket template $name", 0, $ticket_template_id);
flashAlert("Ticket template <strong>" . escapeHtml($row['ticket_template_name']) . "</strong> " . ($archive ? 'archived' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
undoOfferArchived($undo, '', $row['ticket_template_name']);
}
redirect();

View File

@@ -98,12 +98,16 @@ if (isset($_GET['archive_vendor_template']) || isset($_GET['restore_vendor_templ
$vendor_template_id = intval($row['vendor_template_id']);
// Archived, it isn't offered for new vendors; the ones made from it stay
$undo = $archive ? undoArchivedBefore('vendor_templates', 'vendor_template_id', [$vendor_template_id]) : null;
mysqli_query($mysqli, "UPDATE vendor_templates SET vendor_template_archived_at = " . ($archive ? 'NOW()' : 'NULL') . ", vendor_template_updated_at = vendor_template_updated_at
WHERE vendor_template_id = $vendor_template_id");
logAudit("Vendor Template", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " vendor template " . escapeSql($row['vendor_template_name']), 0, $vendor_template_id);
flashAlert("Vendor template <strong>" . escapeHtml($row['vendor_template_name']) . "</strong> " . ($archive ? 'archived' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
undoOfferArchived($undo, '', $row['vendor_template_name']);
}
redirect();

View File

@@ -72,6 +72,7 @@ if (isset($_GET['archive_agent_banner']) || isset($_GET['restore_agent_banner'])
redirect();
}
$announcement_id = intval($row['announcement_id']);
$undo = $archive ? undoArchivedBefore('portal_announcements', 'announcement_id', [$announcement_id], 'announcement_client_id') : null;
mysqli_query($mysqli, "UPDATE portal_announcements SET announcement_archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE announcement_id = $announcement_id");
$title = escapeSql($row['announcement_title']);
@@ -79,6 +80,10 @@ if (isset($_GET['archive_agent_banner']) || isset($_GET['restore_agent_banner'])
logAudit("Announcement", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " the agent banner $title", intval($row['announcement_client_id']), $announcement_id);
flashAlert("Banner <strong>" . escapeHtml($row['announcement_title']) . "</strong> " . ($archive ? 'archived - nobody sees it now' : 'restored'), $archive ? 'error' : 'success');
if ($undo) {
$client_banner = intval($row['announcement_client_id']) > 0;
undoOfferArchived($undo, '', $row['announcement_title'], [], 's', $client_banner ? 'module_client' : 'admin', $client_banner ? 2 : 3);
}
redirect();

View File

@@ -21,15 +21,23 @@
* ['ticket_category', ticket id, before, after] with the ticket's history line, as the bulk change
* ['ticket_project', ticket id, before, after] the same
* ['asset_status', asset id, before, after] with the asset's history line
* ['also', step] a step that goes with the others, not counted
*/
const UNDO_SECONDS = 600;
const UNDO_KEEP = 5;
// Tables where putting a row back isn't an edit - its updated time stays as it was
const UNDO_KEEP_UPDATED = [
'documents' => 'document_updated_at',
'software_templates' => 'software_template_updated_at',
'vendor_templates' => 'vendor_template_updated_at',
];
/*
* Offer to undo what was just done - on the message flashAlert() set, so call it after that.
* $done is the message once it is undone (escapeHtml() any names in it): in it {n} is how many
* steps were put back, {s} the plural ending ($plural) when that isn't one, and {are} is or are.
* A $plural of 'y|ies' gives both endings ("categor{s}").
* $module and $level are the permission the change needed. The token, or '' when there is nothing to put back.
*/
function undoOffer(string $done, array $steps, string $module, int $level = 2, string $plural = 's'): string {
@@ -225,6 +233,43 @@ function undoOfferArchive(array ...$befores): string {
return undoOffer($done, $steps, $module);
}
/*
* Archiving something that isn't on Archived - a tag, a category, a template, a role, a payment
* method, and the like on the admin pages: which of $ids ($id_column is the table's X_id, archived
* in X_archived_at) aren't archived yet, before. Hand it to undoOfferArchived() after.
* $client_column, for one that can be a client's, has Undo check the agent can still get to it.
*/
function undoArchivedBefore(string $table, string $id_column, $ids, string $client_column = ''): array {
$column = substr($id_column, 0, -3) . '_archived_at';
$snapshot = undoSnapshot($table, $id_column, $ids, [$column], $client_column);
$snapshot['rows'] = array_filter($snapshot['rows'], function ($row) use ($column) {
return $row[$column] === null;
});
return $snapshot;
}
/*
* After archiving: Undo for what undoArchivedBefore() saw that is archived now - "Name is back"
* with $name, or "3 tags are back" ($label 'tag'; 'categor' with $plural 'y|ies'). $also are
* snapshots of what else the archive changed (the default it stopped being), put back with it.
* Admins only unless $module says who else.
*/
function undoOfferArchived(array $snapshot, string $label, string $name = '', array $also = [], string $plural = 's', string $module = 'admin', int $level = 3): string {
$steps = undoChangedSteps($snapshot);
if (!$steps) {
return '';
}
foreach ($also as $more) {
foreach (undoChangedSteps($more) as $step) {
$steps[] = ['also', $step];
}
}
$done = $name !== '' && count($snapshot['rows']) === 1
? '<strong>' . escapeHtml($name) . '</strong> is back'
: '<strong>{n}</strong> ' . $label . '{s} {are} back';
return undoOffer($done, $steps, $module, $level, $plural);
}
/** A value as SQL - NULL stays NULL. Values come from the database, never from the request. */
function undoSqlValue($value): string {
global $mysqli;
@@ -263,7 +308,9 @@ function undoApply(string $token): array {
if (!$done && $missed) {
return [false, 'Nothing was put back - it has changed since.'];
}
$message = str_replace(['{n}', '{s}', '{are}'], [$done, $done === 1 ? '' : ($offer['plural'] ?? 's'), $done === 1 ? 'is' : 'are'], $offer['done']);
$plural = (string) ($offer['plural'] ?? 's');
[$one, $many] = strpos($plural, '|') !== false ? explode('|', $plural, 2) : ['', $plural];
$message = str_replace(['{n}', '{s}', '{are}'], [$done, $done === 1 ? $one : $many, $done === 1 ? 'is' : 'are'], $offer['done']);
logAudit('Undo', 'Undo', "$session_name undid a change: " . html_entity_decode(strip_tags($message), ENT_QUOTES));
return [true, 'Undone - ' . $message . ($missed ? " - $missed had changed since, and " . ($missed === 1 ? 'was left as it is' : 'were left as they are') : '')];
}
@@ -287,12 +334,16 @@ function undoStep(array $step) {
$sets[] = "$column = " . undoSqlValue($value);
$still[] = "$column <=> " . undoSqlValue($after[$column]);
}
if ($table === 'documents') {
$sets[] = 'document_updated_at = document_updated_at'; // a move isn't an edit
if (isset(UNDO_KEEP_UPDATED[$table])) {
$sets[] = UNDO_KEEP_UPDATED[$table] . ' = ' . UNDO_KEEP_UPDATED[$table]; // a move or a restore isn't an edit
}
mysqli_query($mysqli, "UPDATE $table SET " . implode(', ', $sets) . " WHERE $id_column = " . intval($id) . " AND " . implode(' AND ', $still));
return mysqli_affected_rows($mysqli) > 0;
case 'also':
undoStep($step[1]);
return null;
case 'rows':
[, $table, $rows] = $step;
foreach ($rows as $row) {