diff --git a/CHANGELOG.md b/CHANGELOG.md index 252f7ebb9..4c56c99bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -210,6 +210,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - Forms: the + beside a picker and the Cancel button look the same in every window, a vendor's What they are is Description as on other records, and a few notes under fields that only said where a value shows are gone. - Starter Content and the demo data are in US spelling - license, authorized, organization, fiber and the like. Where you loaded them before, nothing is added twice: the Authorised Contacts Only vendor tag and the Licence assigned canned response you have count as loaded, and bringing the starter canned responses up to date gives the response its new name. Bringing the starter ticket templates up to date has New Hire Onboarding ask for the License. Loading the demo data again, or removing it, still finds what an earlier load made. - Maintenance > Starter Content is laid out like Backup and Update: how much of the library you don't have yet, with Add all, at the top; each pack with how many it would add and how many of it you have, Pick which, its Add and its Update; the demo data under them; and the totals, how adding works and what to know about the demo data at the side. Update and Remove demo data ask first in the usual window. +- Maintenance > Starter Content: Clear out unused, beside a pack's counts, archives what you have from the pack that nothing uses yet - a category no ticket, invoice, expense or product has, a tag on nothing, a template nothing was made from, a canned response never put in a reply, a product on no invoice, quote or asset. A window lists them, all ticked, and says how many are in use and stay; Undo puts them back, and Add doesn't make them again. ### Bug Fixes @@ -415,6 +416,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - Look and feel: `.itflow-dash-rail` is a grid (`repeat(auto-fill, minmax(10rem, 1fr))`, `.itflow-dash-rail-clear` spans it; `.itflow-ov-stats` stays flex), and the attention items of `dashboardAttention()` and `clientOverviewAttention()` take `one`, used when the count is 1. `formatAssignee()` highlights unassigned in warning, `ticketBillableHtml()` draws not billable in `bg-body-secondary`, and `ticketSlaState()` gives `breached` for an open ticket with a `ticket_response_sla_met` or `ticket_resolution_sla_met` of 0 or a deadline in the past. `includes/filter_footer.php` links the first, last and neighboring pages with an ellipsis between, in place of every tenth page. The list pages render `tagPills()` only for a row with tags, and `agent/includes/inc_client_top_head.php` passes `$can_edit` false. `categoryAddButtonHtml()` and `quickAddButton()` are called with their default `btn-outline-secondary` everywhere. `js/app.js` focuses the dialog again on the next tick when AdminLTE's `shown.bs.modal` handler has put focus on its `.btn-close`. - Starter content: `starterRenamed()` in `admin/post/starter_content_model.php` maps a starter item's old name, keyed as `starterExistingNames()` keys it, to its new one. Tags and canned responses here under the old name count as present (`starterExistingRenamed()`, `starterCannedResponseNames()`), `starterUpdateCannedResponses()` renames one before replacing it, and `demoAttachTags()` falls back to the old tag name. `demoDataRenamed()` holds the demo products' old name and description and the errand trip's old purpose; `demoProductId()` finds a demo product either way for load and removal, and a load renames an old one in place. - `admin/starter_content.php` is rebuilt on the Maintenance page layout (a hero card, then the packs as a `list-group` sharing Get started's `.itflow-gs-pack-icon` and `.itflow-gs-pack-about`, and an At a glance card); `.itflow-starter-pack-cell` is gone. Update and Remove demo data are `confirm-link` buttons in place of `onsubmit` confirms. +- Starter content: `starterPackRecords()`, `starterPackHere()`, `starterPackUsed()`, `starterContentUnused()` and `starterClearUnused()` in `admin/post/starter_content_model.php`, `clear_starter_unused` in `admin/post/starter_content.php` (rechecked on post, with an Undo of the archive) and `admin/modals/starter_content/starter_clear.php`. A tag counts as used by any table with a `tag_id` or `*_tag_id` column (read from information_schema) or a seat count, a project template by its tickets' history line, and a category by `categoryUsage()` plus ticket templates, settings, RMM alerts, AI triage and child categories. `starterPickHtml()` takes `$open`; `js/starter_content.js` counts `data-starter-format="clear"` and sets up a pack in a window as it opens. ## [26.11.8] diff --git a/admin/includes/starter_content_pick.php b/admin/includes/starter_content_pick.php index 0d0983e8d..27131ed08 100644 --- a/admin/includes/starter_content_pick.php +++ b/admin/includes/starter_content_pick.php @@ -211,20 +211,26 @@ function starterMinutesText($minutes) { * A pack's Pick which - its items not here yet, each ticked, shown as they * will look with what they are for, and Preview where there is more to see. * $form is the id of the form the ticks go with, when they sit outside it (a - * table row's Add). $items from starterContentItems(). + * pack's Add). $items from starterContentItems(), or starterContentUnused()'s + * for Clear out unused, which shows the list open ($open) and without Preview, + * as Preview shows the library's version rather than the one here. */ -function starterPickHtml($pack, array $items, $form = '') { +function starterPickHtml($pack, array $items, $form = '', $open = false) { if (!$items) { return ''; } $form_attr = $form !== '' ? ' form="' . escapeHtml($form) . '"' : ''; - $previews = in_array($pack, starterPreviewPacks(), true); + $previews = !$open && in_array($pack, starterPreviewPacks(), true); $kind = $pack === 'tags' ? ' is-pills' : ''; ob_start(); ?> + +
+
Pick which + > -
+
@@ -255,7 +261,7 @@ function starterPickHtml($pack, array $items, $form = '') {
-
+ ' : '' ?> Starter Content (?pack=). + * Its items that are here and nothing uses (starterContentUnused()), every one + * ticked; Archive them (clear_starter_unused, admin/post/starter_content.php) + * archives the ones ticked. js/starter_content.js keeps the count in step. + */ + +require_once '../../includes/modal_header.php'; + +defined('FROM_STARTER_CONTENT') || define('FROM_STARTER_CONTENT', true); +require_once __DIR__ . '/../../post/starter_content_model.php'; +require_once __DIR__ . '/../../includes/starter_content_pick.php'; + +$pack = (string) ($_GET['pack'] ?? ''); +$packs = starterContentPacks(); +if (!isset($packs[$pack]) || !starterPackRecords($pack)) { + exit(''); +} +$pack_details = $packs[$pack]; +$unused = starterContentUnused($mysqli, $pack); +$items = $unused['items']; + +ob_start(); + +?> + + +
+ + + + +
+ +$count unused from " . escapeHtml($label)); + undoOfferChanges($before, "{n} from " . escapeHtml($label) . " put back", 'module_client'); + } else { + flashAlert("Nothing to archive - those are in use now, or already archived", 'info'); + } + + redirect(); + +} + if (isset($_POST['update_starter_ticket_templates'])) { // The starter ticket templates already here, brought up to this version diff --git a/admin/post/starter_content_model.php b/admin/post/starter_content_model.php index d713735d1..991f40142 100644 --- a/admin/post/starter_content_model.php +++ b/admin/post/starter_content_model.php @@ -358,6 +358,229 @@ function starterContentItems($mysqli, $pack) { return $items; } +// ------------------------------ +// starterPackRecords +// Where a pack's items land - the table, its id, name and archived columns, +// what narrows it to the pack's kind of record, and what a library row is +// matched on: kind and name, as Add finds them, and a product's code too, as +// an install may well have a line of its own by that name. +// ------------------------------ +function starterPackRecords($pack) { + $packs = [ + 'categories' => ['table' => 'categories', 'id' => 'category_id', 'name' => 'category_name', 'archived' => 'category_archived_at', 'kind' => 'category_type'], + 'tags' => ['table' => 'tags', 'id' => 'tag_id', 'name' => 'tag_name', 'archived' => 'tag_archived_at', 'kind' => 'tag_type'], + 'ticket_templates' => ['table' => 'ticket_templates', 'id' => 'ticket_template_id', 'name' => 'ticket_template_name', 'archived' => 'ticket_template_archived_at'], + 'project_templates' => ['table' => 'project_templates', 'id' => 'project_template_id', 'name' => 'project_template_name', 'archived' => 'project_template_archived_at'], + 'canned_responses' => ['table' => 'canned_responses', 'id' => 'canned_response_id', 'name' => 'canned_response_name', 'archived' => 'canned_response_archived_at', 'where' => 'canned_response_user_id = 0'], + 'vendor_templates' => ['table' => 'vendor_templates', 'id' => 'vendor_template_id', 'name' => 'vendor_template_name', 'archived' => 'vendor_template_archived_at'], + 'document_templates' => ['table' => 'document_templates', 'id' => 'document_template_id', 'name' => 'document_template_name', 'archived' => 'document_template_archived_at'], + 'products' => ['table' => 'products', 'id' => 'product_id', 'name' => 'product_name', 'archived' => 'product_archived_at', 'kind' => 'product_code'], + 'license_templates' => ['table' => 'software_templates', 'id' => 'software_template_id', 'name' => 'software_template_name', 'archived' => 'software_template_archived_at'], + ]; + return $packs[$pack] ?? null; +} + +/* A library row as starterPackHere() keys what is here - kind (a product's code)|name, lower cased. */ +function starterPackMatchKey($pack, $row) { + switch ($pack) { + case 'categories': + return mb_strtolower($row['type'] . '|' . $row['name']); + case 'tags': + return mb_strtolower($row[0] . '|' . $row[1]); + case 'products': + return mb_strtolower($row[2] . '|' . $row[0]); + } + return mb_strtolower(starterItemName($pack, $row)); +} + +// ------------------------------ +// starterPackHere +// The records here, not archived, that a pack's rows match - key as +// starterPackMatchKey() => record id. One kept under the name it had +// (starterRenamed()) is the same item. +// ------------------------------ +function starterPackHere($mysqli, $pack) { + $r = starterPackRecords($pack); + if (!$r) { + return []; + } + $kind = $r['kind'] ?? ''; + $sql = mysqli_query($mysqli, "SELECT {$r['id']}, {$r['name']}" . ($kind ? ", $kind" : '') . " FROM {$r['table']} + WHERE {$r['archived']} IS NULL" . (isset($r['where']) ? " AND {$r['where']}" : '') . " ORDER BY {$r['id']}"); + $here = []; + while ($row = mysqli_fetch_assoc($sql)) { + $key = mb_strtolower(($kind ? $row[$kind] . '|' : '') . $row[$r['name']]); + $here[$key] = $here[$key] ?? intval($row[$r['id']]); + } + foreach (starterRenamed($pack) as $old => $new) { + $type = strpos($old, '|') !== false ? substr($old, 0, strpos($old, '|') + 1) : ''; + $new_key = mb_strtolower($type . $new); + if (!isset($here[$new_key]) && isset($here[$old])) { + $here[$new_key] = $here[$old]; + } + } + return $here; +} + +// ------------------------------ +// starterPackUsed +// Of these record ids, the ones something uses - id => true. Used is +// anything that points at it: a category on a ticket, invoice, expense, +// product, budget or template, or with categories under it; a tag on any +// record, template or seat count; a ticket template a ticket, recurring +// ticket or project template was made from or uses, or with its request form +// on; a project template a project was raised from (its tickets' history says +// so); a canned response ever put in a reply; a vendor, document or license +// template something was made from; a product on a line, an asset, an order +// or a license template, or with stock or prices of its own. +// ------------------------------ +function starterPackUsed($mysqli, $pack, array $ids) { + $ids = array_values(array_unique(array_filter(array_map('intval', $ids)))); + if (!$ids) { + return []; + } + $in = implode(',', $ids); + $used = []; + $queries = []; + switch ($pack) { + case 'categories': + // What Admin > Categories counts for each kind, and what it doesn't + foreach (['Expense', 'Income', 'Referral', 'Ticket'] as $kind) { + foreach (array_keys(categoryUsage($kind)) as $category_id) { + $used[intval($category_id)] = true; + } + } + $queries = [ + "SELECT ticket_template_category_id FROM ticket_templates WHERE ticket_template_category_id IN ($in)", + "SELECT config_quick_ticket_category FROM settings WHERE config_quick_ticket_category IN ($in)", + "SELECT rmm_provider_alert_category_id FROM rmm_providers WHERE rmm_provider_alert_category_id IN ($in)", + "SELECT ai_triage_category_id FROM ai_ticket_triage WHERE ai_triage_category_id IN ($in)", + "SELECT category_parent FROM categories WHERE category_parent IN ($in) AND category_archived_at IS NULL", + ]; + break; + case 'tags': + // Every table that links a tag to something + $sql = mysqli_query($mysqli, "SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME <> 'tags' AND (COLUMN_NAME = 'tag_id' OR COLUMN_NAME LIKE '%\\_tag\\_id')"); + while ($row = mysqli_fetch_row($sql)) { + $queries[] = "SELECT `{$row[1]}` FROM `{$row[0]}` WHERE `{$row[1]}` IN ($in)"; + } + foreach (array_keys(tagSeatLineCounts()) as $tag_id) { + $used[intval($tag_id)] = true; + } + break; + case 'ticket_templates': + $queries = [ + "SELECT ticket_template_id FROM tickets WHERE ticket_template_id IN ($in)", + "SELECT recurring_ticket_ticket_template_id FROM recurring_tickets WHERE recurring_ticket_ticket_template_id IN ($in)", + "SELECT ticket_template_id FROM project_template_ticket_templates WHERE ticket_template_id IN ($in)", + "SELECT ticket_template_id FROM ticket_templates WHERE ticket_template_portal = 1 AND ticket_template_id IN ($in)", + ]; + break; + case 'project_templates': + // A project doesn't keep its template - the tickets it raised say which in their history + $names = []; + $sql = mysqli_query($mysqli, "SELECT project_template_id, project_template_name FROM project_templates WHERE project_template_id IN ($in)"); + while ($row = mysqli_fetch_row($sql)) { + $names[intval($row[0])] = $row[1]; + } + foreach ($names as $project_template_id => $name) { + $like = escapeSql(addcslashes("raised the ticket from project template $name", '%_')); + if (mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM ticket_history WHERE ticket_history_description LIKE '%$like' LIMIT 1"))) { + $used[$project_template_id] = true; + } + } + break; + case 'canned_responses': + $queries = ["SELECT canned_response_use_canned_response_id FROM canned_response_uses WHERE canned_response_use_canned_response_id IN ($in)"]; + break; + case 'vendor_templates': + $queries = [ + "SELECT vendor_template_id FROM vendors WHERE vendor_template_id IN ($in)", + "SELECT software_template_vendor_template_id FROM software_templates WHERE software_template_vendor_template_id IN ($in) AND software_template_archived_at IS NULL", + ]; + break; + case 'document_templates': + $queries = ["SELECT document_template_id FROM documents WHERE document_template_id IN ($in)"]; + break; + case 'products': + $queries = [ + "SELECT item_product_id FROM invoice_items WHERE item_product_id IN ($in)", + "SELECT item_product_id FROM quote_items WHERE item_product_id IN ($in)", + "SELECT item_product_id FROM recurring_invoice_items WHERE item_product_id IN ($in)", + "SELECT asset_product_id FROM assets WHERE asset_product_id IN ($in)", + "SELECT order_item_product_id FROM order_items WHERE order_item_product_id IN ($in)", + "SELECT po_item_product_id FROM po_items WHERE po_item_product_id IN ($in)", + "SELECT stock_product_id FROM product_stock WHERE stock_product_id IN ($in)", + "SELECT price_product_id FROM product_prices WHERE price_product_id IN ($in)", + "SELECT software_template_product_id FROM software_templates WHERE software_template_product_id IN ($in) AND software_template_archived_at IS NULL", + ]; + break; + case 'license_templates': + $queries = ["SELECT software_template_id FROM software WHERE software_template_id IN ($in)"]; + break; + } + foreach ($queries as $query) { + $sql = mysqli_query($mysqli, str_replace('SELECT ', 'SELECT DISTINCT ', $query)); + while ($sql && ($row = mysqli_fetch_row($sql))) { + $used[intval($row[0])] = true; + } + } + return array_intersect_key($used, array_flip($ids)); +} + +// ------------------------------ +// starterContentUnused +// A pack's items that are here and nothing uses - for Clear out unused, in +// starterContentItems()' shape with the record's 'id' - and how many of the +// pack that are here are in use: ['items' => [...], 'in_use' => n]. +// ------------------------------ +function starterContentUnused($mysqli, $pack) { + $here = starterPackHere($mysqli, $pack); + $found = []; + foreach (starterContentRows($pack) as $row) { + $id = $here[starterPackMatchKey($pack, $row)] ?? 0; + if ($id && !isset($found[$id])) { + $found[$id] = $row; + } + } + $used = starterPackUsed($mysqli, $pack, array_keys($found)); + $items = []; + foreach ($found as $id => $row) { + if (!isset($used[$id])) { + $items[] = ['key' => starterItemKey($pack, $row), 'name' => starterItemName($pack, $row), 'group' => starterItemGroup($pack, $row), 'row' => $row, 'id' => $id]; + } + } + return ['items' => $items, 'in_use' => count($found) - count($items)]; +} + +// ------------------------------ +// starterClearUnused +// Archives a pack's unused items - those of $only (starterItemKey()s) when +// given - checked again as it goes. Archived, not deleted: each comes back +// from its own list's Archived view. Add still counts an archived one as +// here, as it does any other, so it isn't made again beside it. +// Returns [record ids archived, undoSnapshot() of them before]. +// ------------------------------ +function starterClearUnused($mysqli, $pack, $only = null) { + $r = starterPackRecords($pack); + if (!$r) { + return [[], null]; + } + $ids = []; + foreach (starterContentUnused($mysqli, $pack)['items'] as $item) { + if ($only === null || in_array($item['key'], $only, true)) { + $ids[] = intval($item['id']); + } + } + if (!$ids) { + return [[], null]; + } + $before = undoSnapshot($r['table'], $r['id'], $ids, [$r['archived']]); + mysqli_query($mysqli, "UPDATE {$r['table']} SET {$r['archived']} = NOW() WHERE {$r['archived']} IS NULL AND {$r['id']} IN (" . implode(',', $ids) . ")"); + return [$ids, $before]; +} + // ------------------------------ // starterContentStatus // Per pack totals for the page - how many the library holds, how many are diff --git a/admin/starter_content.php b/admin/starter_content.php index 5d7936d4c..0e92cfdb2 100644 --- a/admin/starter_content.php +++ b/admin/starter_content.php @@ -6,6 +6,7 @@ * it is and the one thing to do at the top, the packs and the demo data under it, and at a glance * and how it works to the side. Pick which and Preview are includes/starter_content_pick.php, shared * with Get started's Starter content step; js/starter_content.js keeps the ticks and counts in step. + * Clear out unused, beside a pack's counts, opens modals/starter_content/starter_clear.php. */ require_once "includes/inc_all_admin.php"; @@ -17,17 +18,20 @@ require_once "includes/starter_content_pick.php"; $starter_content_packs = starterContentPacks(); -// Each pack's items not here yet - for Pick which, and how many Add brings +// Each pack's items not here yet - for Pick which, and how many Add brings - and how many of +// the ones here nothing uses, for Clear out unused $starter_content_items = []; $starter_content_status = []; -$total_in_library = $total_missing = 0; +$total_in_library = $total_missing = $total_unused = 0; foreach ($starter_content_packs as $pack => $pack_details) { $starter_content_items[$pack] = starterContentItems($mysqli, $pack); $total = starterContentTotal($pack); $missing = count($starter_content_items[$pack]); - $starter_content_status[$pack] = ['total' => $total, 'missing' => $missing, 'present' => $total - $missing]; + $unused = $total - $missing ? count(starterContentUnused($mysqli, $pack)['items']) : 0; + $starter_content_status[$pack] = ['total' => $total, 'missing' => $missing, 'present' => $total - $missing, 'unused' => $unused]; $total_in_library += $total; $total_missing += $missing; + $total_unused += $unused; } $total_present = $total_in_library - $total_missing; @@ -104,6 +108,14 @@ if (!$total_missing) { to add · of here + + + · + + +
@@ -228,6 +240,10 @@ if (!$total_missing) { To add None' ?> +
  • + Here but unused + +
  • Demo data @@ -244,6 +260,7 @@ if (!$total_missing) {
  • Adding never changes or removes anything. A name you already have is skipped, so a pack can be added again to bring back something deleted - never a duplicate.
  • Pick which, under a pack, lists what it would add as it will look, with a preview of each template. Untick what you don't want.
  • Update brings templates and canned responses added from an earlier version up to this one - it replaces what they hold.
  • +
  • Clear out unused archives what came from a pack and nothing uses yet - anything in use stays, and Undo puts them back.
  • Request forms come with the ticket templates, turned off. Canned responses come in shared. Products are priced as starting points - hardware and resold licensing at zero.
  • diff --git a/js/starter_content.js b/js/starter_content.js index 73e2814fd..e745ab747 100644 --- a/js/starter_content.js +++ b/js/starter_content.js @@ -13,6 +13,10 @@ * [data-starter-preview=pack] the item ([data-starter-key]) whole, in the * #starterPreview window * + * A count with data-starter-format="clear" counts what Clear out unused + * archives (admin/modals/starter_content/starter_clear.php). A pack in a window + * opened later is set up as the window opens. + * * Without it, the items can still be ticked and unticked and are sent as they * are; the server goes by them. */ @@ -21,9 +25,10 @@ function initPick(pack) { var items = Array.prototype.slice.call(pack.querySelectorAll('[data-starter-item]')); - if (!items.length) { + if (!items.length || pack.dataset.starterReady) { return; } + pack.dataset.starterReady = '1'; var check = pack.querySelector('[data-starter-pack-check]'); var counts = Array.prototype.slice.call(pack.querySelectorAll('[data-starter-count]')); var adds = Array.prototype.slice.call(pack.querySelectorAll('[data-starter-add]')); @@ -38,7 +43,9 @@ counts.forEach(function (count) { var short = count.dataset.starterFormat === 'short'; var more = count.dataset.starterMore === '1'; - if (short) { + if (count.dataset.starterFormat === 'clear') { + count.textContent = ticked === of ? of + ' to archive' : (ticked ? ticked + ' of ' + of + ' to archive' : 'None picked'); + } else if (short) { count.textContent = ticked === of ? String(of) : ticked + ' of ' + of; } else { count.textContent = ticked === of ? of + (more ? ' more to add' : ' to add') : (ticked ? ticked + ' of ' + of + ' to add' : 'None picked'); @@ -104,6 +111,9 @@ function init() { document.querySelectorAll('[data-starter-pick]').forEach(initPick); + document.addEventListener('shown.bs.modal', function (e) { + e.target.querySelectorAll('[data-starter-pick]').forEach(initPick); + }); document.addEventListener('click', function (e) { var button = e.target.closest('[data-starter-preview]'); if (button) {