diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa4bd586..e6de8e970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow. ### Upgrading -Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's twenty-eight database changes (3.6.6 to 3.9.4) in the same pass. They add four tables for RMM providers and their alerts, a setting for device actions and one for API keys in the address, a column marking the emails the mail queue clears once sent, a column and a table for agent banners, two columns for ticket statuses with a setting for the "ticket assigned" message, a column on tickets with a setting for the "no response" message, a table for who a ticket's email replies go to, the settings for Quick tickets, columns on recurring tickets and recurring invoices for booking and making them ahead, two columns on ticket replies for when their time was worked, the settings for cron alerts, a table and a setting for labor costs, a column on users for API / service accounts, columns on tickets and recurring tickets for how long booked work takes, a column each on recurring tickets and calendar events for repeating on a weekday, three columns on agents' settings for their calendar color, what their calendar shows and their own time zone, and a table for the agents helping on a ticket, three columns on quote lines with three settings for what a quote line costs and how it is priced, a column each on quote lines and products with a setting for their pictures, two settings and a column on tickets for keeping project tickets apart, a table for your other offices with a column each on agents and invites for which one they work from, three settings for the emails and notices your agents get about their tickets, three settings for sending and reading Microsoft 365 mail through Microsoft Graph, five tables, four settings and a role permission for purchase orders, and five settings for switching Dispatch, Bookkeeping, Purchasing, Trips and the calendar on and off, and change nothing that is there. Nothing else is required. Purchasing starts off - switch it on in Admin > Settings > Modules. This release has twelve security fixes - see Security. On Apache, ITFlow's own .htaccess now sends security headers: if you show ITFlow inside a frame on another website, it will now refuse to load there. On nginx, add the same headers to your server block if you want them - see Developer Updates. +Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's twenty-nine database changes (3.6.6 to 3.9.5) in the same pass. They add four tables for RMM providers and their alerts, a setting for device actions and one for API keys in the address, a column marking the emails the mail queue clears once sent, a column and a table for agent banners, two columns for ticket statuses with a setting for the "ticket assigned" message, a column on tickets with a setting for the "no response" message, a table for who a ticket's email replies go to, the settings for Quick tickets, columns on recurring tickets and recurring invoices for booking and making them ahead, two columns on ticket replies for when their time was worked, the settings for cron alerts, a table and a setting for labor costs, a column on users for API / service accounts, columns on tickets and recurring tickets for how long booked work takes, a column each on recurring tickets and calendar events for repeating on a weekday, three columns on agents' settings for their calendar color, what their calendar shows and their own time zone, and a table for the agents helping on a ticket, three columns on quote lines with three settings for what a quote line costs and how it is priced, a column each on quote lines and products with a setting for their pictures, two settings and a column on tickets for keeping project tickets apart, a table for your other offices with a column each on agents and invites for which one they work from, three settings for the emails and notices your agents get about their tickets, three settings for sending and reading Microsoft 365 mail through Microsoft Graph, five tables, four settings and a role permission for purchase orders, five settings for switching Dispatch, Bookkeeping, Purchasing, Trips and the calendar on and off, and three tables and a setting for the Knowledge Base, and change nothing that is there. Nothing else is required. Purchasing starts off - switch it on in Admin > Settings > Modules. This release has twelve security fixes - see Security. On Apache, ITFlow's own .htaccess now sends security headers: if you show ITFlow inside a frame on another website, it will now refuse to load there. On nginx, add the same headers to your server block if you want them - see Developer Updates. ### Security @@ -132,6 +132,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - Client menu: Calendar, Racks, Certificates, Shared Links, Services and Trips stay out of a client's menu while the client has none of them, when two or more are empty, so the menu is shorter for most clients. More, at the bottom of the menu, shows them - to add a client's first rack, say. Each comes back as soon as the client has one, and always shows on its own page. - Admin menu: eight entries shorter, nine with texting set up. Templates is one entry under Lists & Templates, with Tickets, Projects, Canned Responses, Contracts, Vendors, Licenses and Documents as tabs across its pages. Logs, under Maintenance, has Audit Logs, Sign-ins and App Logs as tabs. Text Messaging has Setup and, once texting is set up, Messages, with the texts that failed to send in the last week counted on the menu. Where ITFlow points you to one of these pages, it now says where it is - Admin > Templates > Tickets, say. - Across clients: All Client Docs has moved from the main menu to the top bar, beside the menu button, and is now called Across clients. It opens a menu of what you can see for every client together - contacts, locations, assets, credentials and the rest - and takes you straight to the one you pick, instead of always to Contacts. Once you're there, the side menu is the same as before, headed Back | Across clients. On a phone it's just the icon, and the search box beside it narrows to fit. +- Knowledge Base: one place for the how-tos, fixes and policies your team gives again and again, written once for every client - Knowledge Base, in the main menu. Each article has a title, a line saying what it's for, a category, tags and its content, written in the same editor as documents, pictures and all. An article is a draft until it's published. Every edit keeps what it said before, to look at or put back from its Versions. Say how often an article is reviewed - every year, to start with - and it shows Review due once it's that long since it was written, edited or marked reviewed. The list searches titles, summaries, the words in the articles and their tags, filters by category and tag, and has All, Published, Drafts, Review due and Archived views. Ticked articles can be put in a category, published, made drafts, archived, restored or deleted. Articles can be copied, and Copy to Knowledge Base on a client's document makes a draft article from it. Search everywhere finds articles too. Admins set up the categories, with an icon and color each, in Admin > Categories > Knowledge Base, and the tags in Admin > Tags. Reading the Knowledge Base takes Support; writing in it takes Support Edit, and deleting Support Full. Admin > Settings > Modules switches it off. - Modules: Admin > Settings > Modules has five more switches, to take out of ITFlow what your business doesn't use - Dispatch, which comes with Ticketing; Bookkeeping, Purchasing and Trips, which come with invoicing; and the Calendar. Each part is off while the one it comes with is, and shows faded under it. Switched off, a part leaves the menus, the dashboard, search, the reports and the API, and its pages and windows say it is off. Nothing in it is deleted - switched back on, it is all there. Dispatch, Bookkeeping, Trips and the Calendar start on, as before. - Bookkeeping: switch it off when your books are kept somewhere else. Accounts, Transactions, Transfers, Reconcile and the statement imports go, and nobody picks an account: payments and income go into the default payment account, and expenses come out of the default expense account - both in Admin > Settings > Defaults, or the first account when there's no default. An expense is paid by The business or Paid personally. The dashboard leaves out the account balances, and the Tax Package its Accounts section. - Calendar: switched off, the calendar, its events and the calendar feeds go - a subscribed calendar stops getting them. Tickets are still booked from the ticket and on Dispatch, and booked work still shows on the dashboard. @@ -284,6 +285,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - Leads: `clientNotLeadSql($keep)` (`functions/auth.php`) leaves leads out of a query on `clients`, keeping the client or clients in `$keep` - the record's own, or the lead whose page it is. It's on the client pickers in the record windows, bulk edits, Dispatch, the client switcher (`get_active_clients`), the transaction filter and export, the report client picker and coverage, the template reports and use windows, agent banners, portal announcements, RMM client linking and suggestions, and managed instances. The invoice and recurring invoice picker in `billing_form.php` leaves them out; the quote picker and `quote_copy.php` order them after the clients with `data-description="Lead"`. Global search orders a lead after the clients that match and puts Lead in its subtitle. Filters built from existing records, imports and client permissions still list leads. - Modules: database 3.9.4 adds `config_module_enable_purchasing` (0, or 1 when `order_items` or `purchase_orders` has a row), `config_module_enable_bookkeeping`, `config_module_enable_dispatch`, `config_module_enable_trips` and `config_module_enable_calendar` (1) to `settings`. `load_global_settings.php` reads them in a second query only from 3.9.4, so an older database still loads. `moduleOn($module)` (`functions/settings.php`) answers for `itdoc`, `ticketing`, `accounting`, `portal` and the five - `dispatch` needs ticketing, and `purchasing`, `bookkeeping` and `trips` need accounting; a switch that isn't there yet counts as on - and `moduleOffText($module)` gives the message, naming the module it comes with when that is the one off. `settingsPageAvailable()`, `reportFeatureOn()`, `settingsStartPages()`, global search, both side menus, the dashboard and `poLevel()` use it; Modules marks each part `data-settings-depends-on` its parent's switch. Pages flash and redirect, windows return `error`, handlers refuse and the API answers 403 - the accounts, calendars, events and trips endpoints now require `load_global_settings.php` for it. In `functions/trips.php` each list and count is empty while trips are off and each change returns an error. `dispatchCan()` and `dispatchCanEdit()` are false while Dispatch is off. `agent/ajax.php` refuses the calendar's sources and moves (`calendar_day` stays, for the booking window), and `guest/guest_calendar_feed.php` answers not found. With bookkeeping off, `bookkeepingAccount($kind)` (`functions/accounts.php`) - the default payment or expense account, else the first open one - is posted in a hidden `account` in place of each account picker, the expense and income bulk edits lose Account, and the Tax Package leaves out its Accounts table and the Account column of its CSVs. - Across clients: `includes/top_nav.php` builds the menu, grouped as `agent/includes/client_overview_side_nav.php` groups its side menu - shown for `module_client` and `module_support`, Credentials only with `module_credential`, and the IT documentation pages only while `config_module_enable_itdoc` is on, in both. `agent/includes/inc_client_overview_all.php` sets `$nav_across_clients` before the top bar, which marks the open list, or the list one record belongs to (`asset.php` under Assets). The main menu's entry is gone, the search shortcut is renamed, and `.app-header .navbar-search` takes `min-width: 0` so the search box narrows on a phone rather than pushing the avatar off the bar. +- Knowledge Base: database 3.9.5 adds `kb_articles` (title, summary, content and its search text with a FULLTEXT index, `kb_article_category_id`, `kb_article_published_at` - none for a draft - `kb_article_review_months` and `kb_article_reviewed_at`/`_by`), `kb_article_versions`, `kb_article_tags` and `config_module_enable_kb` (on), which `load_global_settings.php` reads from 3.9.5. `functions/kb.php` has `kbOn()` (the switch, and the tables there), `kbCan($level)` on `module_support`, `kbClosedText()`, `kbArticleRow()`, `kbArticleFields()`, `kbArticleSave()` - which keeps the old words as a version and saves pasted images to `uploads/kb//` - `kbReviewState()` and `kbReviewDueSql()`, built like documents' review, `kbArticleFromDocument()` and `kbArticleDelete()`. Pages `agent/kb.php` and `agent/kb_article.php`, windows in `agent/modals/kb/`, handlers in `agent/post/kb.php`. Content is purified where it's shown, with `documentHtmlPurify()`. The `kb` category kind, the `kb_article` tag kind (type 13, with no client - `tagFilterOptionsHtml()`, `tagRecordsEditable()` and the tags API now allow a kind without one), the `Knowledge Base` audit log kind, a global search group with its shortcut, and the Modules switch, with `moduleOffText('kb')`. ## [26.11.8] diff --git a/admin/database_updates/3.9.5.php b/admin/database_updates/3.9.5.php new file mode 100644 index 000000000..a43ec85c3 --- /dev/null +++ b/admin/database_updates/3.9.5.php @@ -0,0 +1,68 @@ + isset($config_module_enable_purchasing) && moduleOn('purchasing'), 'Trips' => isset($config_module_enable_trips) && moduleOn('trips'), 'Calendar' => isset($config_module_enable_calendar) && moduleOn('calendar'), + 'Knowledge Base' => isset($config_module_enable_kb) && moduleOn('kb'), 'Client portal' => moduleOn('portal'), ])); $where = array_filter([$company['company_city'] ?? '', $company['company_country'] ?? '']); diff --git a/admin/settings_module.php b/admin/settings_module.php index 3a8dbcf89..8806ae607 100644 --- a/admin/settings_module.php +++ b/admin/settings_module.php @@ -29,12 +29,16 @@ $modules = [ 'config_module_enable_purchasing' => [$config_module_enable_purchasing ?? 0, 'fa-truck-loading', 'To Order, purchase orders to your vendors, and receiving into stock or client assets.', 'config_module_enable_accounting'], 'config_module_enable_trips' => [$config_module_enable_trips ?? 1, 'fa-route', 'Trips and mileage - logged, billed to clients and claimed back.', 'config_module_enable_accounting'], 'config_module_enable_calendar' => [$config_module_enable_calendar ?? 1, 'fa-calendar-alt', 'The calendar, its events and the calendar feeds. Tickets are still booked, and show on Dispatch.', ''], + 'config_module_enable_kb' => [$config_module_enable_kb ?? 1, 'fa-book', 'Knowledge Base articles - how-tos and fixes written once, for every client, with their categories, versions and reviews.', ''], 'config_client_portal_enable' => [$config_client_portal_enable, 'fa-user-shield', 'Where your clients\' contacts sign in to raise tickets, see their invoices and pay.', ''], ]; -// Before database 3.9.4 adds them, only the first four are there to switch +// Before database 3.9.4 adds them, only the first four are there to switch - and the Knowledge Base comes with 3.9.5 if (!isset($config_module_enable_calendar)) { $modules = array_intersect_key($modules, array_flip(['config_module_enable_itdoc', 'config_module_enable_ticketing', 'config_module_enable_accounting', 'config_client_portal_enable'])); } +if (!isset($config_module_enable_kb)) { + unset($modules['config_module_enable_kb']); +} echo settingsPageStart('module'); diff --git a/agent/document.php b/agent/document.php index 120be1e0d..bf7a8f245 100644 --- a/agent/document.php +++ b/agent/document.php @@ -134,6 +134,9 @@ $page_title = $row['document_name']; client portal + + Copy to Knowledge Base + Archive diff --git a/agent/includes/side_nav.php b/agent/includes/side_nav.php index 41ea91abb..3e825f765 100644 --- a/agent/includes/side_nav.php +++ b/agent/includes/side_nav.php @@ -81,6 +81,14 @@ + + + = 1) { ?> diff --git a/agent/kb.php b/agent/kb.php new file mode 100644 index 000000000..e30b2b3f8 --- /dev/null +++ b/agent/kb.php @@ -0,0 +1,349 @@ + 0 ? (string) intval($category_filter) : ''); +$category_query = $category_filter === '' ? '' : 'AND kb_article_category_id = ' . ($category_filter === 'none' ? 0 : intval($category_filter)); + +// Tags +$tag_filter_ids = tagFilterIds(); +$tag_query = tagFilterSql('kb_article', $tag_filter_ids, 'kb_articles.kb_article_id'); + +// The search - the title, the summary, the words in it and its tags +$search_query = $q !== '' + ? "AND (kb_article_title LIKE '%$q%' OR kb_article_summary LIKE '%$q%' OR MATCH(kb_article_content_raw) AGAINST ('$q') " . tagSearchSql('kb_article', $q, 'kb_articles.kb_article_id') . ")" + : ''; + +$filters = "1 $search_query $category_query $tag_query"; +$joins = "LEFT JOIN categories ON category_id = kb_article_category_id AND kb_article_category_id > 0 + LEFT JOIN users updater ON updater.user_id = COALESCE(NULLIF(kb_article_updated_by, 0), kb_article_created_by)"; + +// The views +$shows = [ + '' => ['label' => 'All', 'icon' => 'fa-book', 'where' => 'kb_article_archived_at IS NULL', 'title' => 'Every article, published or not'], + 'published' => ['label' => 'Published', 'icon' => 'fa-check-circle', 'where' => 'kb_article_archived_at IS NULL AND kb_article_published_at IS NOT NULL', 'title' => 'Finished'], + 'drafts' => ['label' => 'Drafts', 'icon' => 'fa-pencil-alt', 'where' => 'kb_article_archived_at IS NULL AND kb_article_published_at IS NULL', 'title' => 'Not finished - publish one when it is'], + 'review' => ['label' => 'Review due', 'icon' => 'fa-calendar-check', 'where' => kbReviewDueSql(), 'title' => 'Published articles due for a read-through - an edit, or Mark reviewed, counts', 'alarm' => 'warning'], + 'archived' => ['label' => 'Archived', 'icon' => 'fa-archive', 'where' => 'kb_article_archived_at IS NOT NULL', 'title' => 'Kept, out of the way'], +]; +$show = isset($shows[$_GET['show'] ?? '']) ? ($_GET['show'] ?? '') : ''; + +// Only these sort - an unknown column would take the page down +$sortable_columns = [ + 'kb_article_title' => "kb_article_title $order", + 'category_name' => "kb_article_category_id = 0 $order, category_order $order, category_name $order, kb_article_title ASC", + 'kb_article_updated' => "COALESCE(kb_article_updated_at, kb_article_created_at) $order", +]; +if (!isset($sortable_columns[$sort])) { + $sort = 'kb_article_updated'; +} + +$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS kb_article_id, kb_article_title, kb_article_summary, kb_article_content, kb_article_category_id, + kb_article_published_at, kb_article_review_months, kb_article_reviewed_at, kb_article_created_at, kb_article_updated_at, kb_article_archived_at, + category_name, category_description, category_color, category_icon, category_type, updater.user_name AS updated_by_name + FROM kb_articles $joins + WHERE $filters AND {$shows[$show]['where']} + ORDER BY {$sortable_columns[$sort]}, kb_article_id DESC + LIMIT $record_from, $record_to"); + +$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); + +$articles = []; +while ($row = mysqli_fetch_assoc($sql)) { + $articles[intval($row['kb_article_id'])] = $row; +} +$article_tags = tagsForRecords('kb_article', array_keys($articles)); + +// How many of each, for the pills - over what the filters leave +$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', array_map(function ($s) { + return "COALESCE(SUM({$s['where']}), 0)"; +}, $shows)) . " FROM kb_articles $joins WHERE $filters")); +$counts = array_combine(array_keys($shows), array_map('intval', $row)); + +$filter_url = function ($changes) { + $params = array_merge($_GET, $changes); + unset($params['page']); + return '?' . http_build_query(array_filter($params, function ($v) { return $v !== '' && $v !== null && $v !== []; })); +}; + +$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) { + return '' + . $label . ($sort === $column ? " $order_icon" : '') . ''; +}; + +$add_url = 'modals/kb/kb_article_add.php' . (intval($category_filter) > 0 ? '?category=' . intval($category_filter) : ''); +$csrf = $_SESSION['csrf_token']; + +?> + +
+
+

Knowledge Base

+
+
+ + + + + + + +
+
+
+ +
+
+ +
+
+
+ + +
+
+
+ +
+
+ +
+
+
+ + + +
+
+
+
+ + + +
+ +
+ +
+ + text-nowrap table-light"> + + + + + + + + + + + + + $row) { + $title = escapeHtml($row['kb_article_title']); + $url = "kb_article.php?kb_article_id=$kb_article_id"; + $excerpt = kbArticleExcerpt($row); + $updated_at = $row['kb_article_updated_at'] ?: $row['kb_article_created_at']; + $review = kbReviewState($row); + $category_html = $row['category_name'] !== null ? categoryChip($row) : '-'; + $tags_html = tagPills('kb_article', $kb_article_id, $article_tags[$kb_article_id] ?? [], 'kb.php?', $can_edit && !$row['kb_article_archived_at']); + ?> + + + + + + + + + + + + +
+
+ +
+
Review
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+ + +
+
Updated
+
+
+
+ +
+
+ + + + + + - + + + + + +
+
+
+ 'articles', + 'icon' => 'fa-book', + 'filtered' => $q !== '' || $category_filter !== '' || $tag_filter_ids, + 'archived' => $show === 'archived', + 'views' => ['show'], + 'view' => emptyStatePill($shows, $show, '', ['review']), + 'text' => 'Write down the fixes and how-tos your team gives again and again - setting up email on a phone, connecting to the VPN, the printer that needs a nudge - once, for every client.', + 'actions' => $can_edit ? [['label' => 'New Article', 'modal' => $add_url, 'size' => 'xl']] : [], + ]; + require_once "../includes/filter_footer.php"; + ?> +
+ + + +

Nothing to see here

Back to the Knowledge Base"; + require_once "../includes/footer.php"; + exit(); +} + +$can_edit = kbCan(2); +$can_delete = kbCan(3); + +$title = escapeHtml($row['kb_article_title']); +$summary = escapeHtml((string) $row['kb_article_summary']); +$archived = !empty($row['kb_article_archived_at']); +$published = !empty($row['kb_article_published_at']); +$updated_at = $row['kb_article_updated_at'] ?: $row['kb_article_created_at']; +$updated_by = escapeHtml($row['updated_by_name'] ?: (string) $row['created_by_name']); +$review = kbReviewState($row); + +$versions = []; +$sql_versions = mysqli_query($mysqli, "SELECT kb_article_version_id, kb_article_version_title, kb_article_version_created_at, user_name + FROM kb_article_versions LEFT JOIN users ON user_id = kb_article_version_created_by AND kb_article_version_created_by > 0 + WHERE kb_article_version_kb_article_id = $kb_article_id + ORDER BY kb_article_version_created_at DESC, kb_article_version_id DESC"); +while ($version = mysqli_fetch_assoc($sql_versions)) { + $versions[] = $version; +} + +$csrf = $_SESSION['csrf_token']; +$edit_url = "modals/kb/kb_article_edit.php?id=$kb_article_id"; +$tags_display = tagPills('kb_article', $kb_article_id, tagsForRecord('kb_article', $kb_article_id), 'kb.php?', $can_edit && !$archived); + +// Tab title - used raw, the header escapes it +$page_title = $row['kb_article_title']; + +?> + + + + +
+
+ +
+

+ + +

+ +
+ + +
+ +
+ Updated + · Written + · earlier version +
+
+
+ + + + + Publish + + + + + + + +
+
+
+ +
+ + +
+
+
+ Nothing written yet.' . ($can_edit && !$archived ? ' Start writing' : '') . '

' ?> +
+
+
+ +
+ + +
+
+
+ + + + + + No category + + +
+
+ + + + Archived + + Published + + A draft - not finished + + +
+
+ + + + Review due since ' . date('M j, Y', strtotime($review['next'])) : 'Next review ' . date('M j, Y', strtotime($review['next'])) ?> + + + · + + + Mark reviewed + + 0 && !$archived) { ?> + Reviewed once it's published + + Not reviewed on a schedule + + +
+
+
+ + +
+
+

Versions

+
+
    +
  • +
    Now
    +
    Updated
    +
  • + +
  • +
    + + + +
    + + · was "" +
    +
    + + + +
  • + + +
  • No earlier versions - each edit keeps the one before it here.
  • + +
+
+ + + + +
+ +
+ + + + $kb_closed])); +} + +$kb_form_mode = 'add'; +$kb_form = [ + 'kb_article_category_id' => intval($_GET['category'] ?? 0), + 'kb_article_review_months' => 12, + 'kb_article_published_at' => date('Y-m-d H:i:s'), +]; + +ob_start(); + +?> + + + +
+ + + + + +
+ + $kb_closed])); +} + +$kb_article_ids = array_values(array_unique(array_filter(array_map('intval', (array) ($_GET['kb_article_ids'] ?? []))))); +$count = count($kb_article_ids); + +ob_start(); + +?> + + + +
+ + + + + + +
+ + $kb_closed])); +} + +$kb_form = kbArticleRow($_GET['id'] ?? 0); +if (!$kb_form || $kb_form['kb_article_archived_at']) { + exit(json_encode(['error' => $kb_form ? 'Restore the article to change it.' : 'That article no longer exists.'])); +} +$kb_form_mode = 'edit'; + +ob_start(); + +?> + + + +
+ + + + + + +
+ + + +
+
+ +
+ + > +
+
+
+ +
+ + +
+
+ +
+ +
+ + + +
+
+
+ +
+
+ +
+ + +
+
+ +
+
+ + > + +
+
+
+ + + + +
Saving keeps what it says now in the article's versions, so it can be put back.
+ diff --git a/agent/modals/kb/kb_article_version.php b/agent/modals/kb/kb_article_version.php new file mode 100644 index 000000000..39f619a3f --- /dev/null +++ b/agent/modals/kb/kb_article_version.php @@ -0,0 +1,74 @@ + $kb_closed])); +} + +$kb_article_version_id = intval($_GET['id'] ?? 0); + +$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT kb_article_version_id, kb_article_version_title, kb_article_version_summary, kb_article_version_content, + kb_article_version_created_at, kb_article_archived_at, user_name + FROM kb_article_versions + JOIN kb_articles ON kb_article_id = kb_article_version_kb_article_id + LEFT JOIN users ON user_id = kb_article_version_created_by AND kb_article_version_created_by > 0 + WHERE kb_article_version_id = $kb_article_version_id LIMIT 1")); +if (!$row) { + exit(json_encode(['error' => 'That version no longer exists.'])); +} + +$can_edit = kbCan(2) && empty($row['kb_article_archived_at']); +$can_delete = kbCan(3); + +ob_start(); + +?> + + + + + + + + + + title]. */ +function kbBulkArticles() { + global $mysqli; + $ids = array_values(array_unique(array_filter(array_map('intval', (array) ($_POST['kb_article_ids'] ?? []))))); + $articles = []; + if ($ids) { + $sql = mysqli_query($mysqli, "SELECT kb_article_id, kb_article_title FROM kb_articles WHERE kb_article_id IN (" . implode(',', $ids) . ")"); + while ($row = mysqli_fetch_assoc($sql)) { + $articles[intval($row['kb_article_id'])] = $row['kb_article_title']; + } + } + if (!$articles) { + flashAlert('No articles were ticked.', 'error'); + redirect(); + } + return $articles; +} + +if (isset($_POST['add_kb_article'])) { + + [$fields, $error] = kbArticleFields($_POST); + if ($error !== '') { + flashAlert(escapeHtml($error), 'error'); + redirect(); + } + + $kb_article_id = kbArticleSave($fields); + if (isset($_POST['tags_form'])) { + tagSetForRecord('kb_article', $kb_article_id, (array) ($_POST['tags'] ?? [])); + } + + logAudit("Knowledge Base", "Create", "$session_name wrote " . ($fields['published'] ? '' : 'a draft of ') . "article " . escapeSql($fields['title']), 0, $kb_article_id); + + flashAlert("Article " . escapeHtml($fields['title']) . " " . ($fields['published'] ? 'published' : 'saved as a draft')); + + redirect("kb_article.php?kb_article_id=$kb_article_id"); +} + +if (isset($_POST['edit_kb_article'])) { + + $row = kbArticleForHandler($_POST['kb_article_id'] ?? 0); + $kb_article_id = intval($row['kb_article_id']); + + [$fields, $error] = kbArticleFields($_POST); + if ($error !== '') { + flashAlert(escapeHtml($error), 'error'); + redirect(); + } + + kbArticleSave($fields, $row); + if (isset($_POST['tags_form'])) { + tagSetForRecord('kb_article', $kb_article_id, (array) ($_POST['tags'] ?? [])); + } + + $was = !empty($row['kb_article_published_at']); + $how = $fields['published'] === $was ? 'edited' : ($fields['published'] ? 'edited and published' : 'edited and made a draft of'); + logAudit("Knowledge Base", "Edit", "$session_name $how article " . escapeSql($fields['title']), 0, $kb_article_id); + + flashAlert("Article " . escapeHtml($fields['title']) . " saved" . ($fields['published'] === $was ? '' : ($fields['published'] ? ' and published' : ' as a draft'))); + + redirect(); +} + +if (isset($_GET['publish_kb_article']) || isset($_GET['draft_kb_article'])) { + + $publish = isset($_GET['publish_kb_article']); + $row = kbArticleForHandler($publish ? $_GET['publish_kb_article'] : $_GET['draft_kb_article']); + $kb_article_id = intval($row['kb_article_id']); + + mysqli_query($mysqli, "UPDATE kb_articles SET kb_article_published_at = " . ($publish ? 'COALESCE(kb_article_published_at, NOW())' : 'NULL') . " WHERE kb_article_id = $kb_article_id"); + + logAudit("Knowledge Base", $publish ? "Publish" : "Draft", "$session_name " . ($publish ? 'published' : 'made a draft of') . " article " . escapeSql($row['kb_article_title']), 0, $kb_article_id); + + flashAlert("Article " . escapeHtml($row['kb_article_title']) . " " . ($publish ? 'published' : 'is a draft again')); + + redirect(); +} + +if (isset($_GET['mark_kb_article_reviewed'])) { + + $row = kbArticleForHandler($_GET['mark_kb_article_reviewed']); + $kb_article_id = intval($row['kb_article_id']); + + mysqli_query($mysqli, "UPDATE kb_articles SET kb_article_reviewed_at = NOW(), kb_article_reviewed_by = " . intval($session_user_id) . " WHERE kb_article_id = $kb_article_id"); + + logAudit("Knowledge Base", "Review", "$session_name reviewed article " . escapeSql($row['kb_article_title']), 0, $kb_article_id); + + $months = intval($row['kb_article_review_months']); + flashAlert("Article " . escapeHtml($row['kb_article_title']) . " marked reviewed" . ($months ? ' - next due ' . date('M j, Y', strtotime("+$months months")) : '')); + + redirect(); +} + +if (isset($_GET['archive_kb_article']) || isset($_GET['restore_kb_article'])) { + + $archive = isset($_GET['archive_kb_article']); + $row = kbArticleForHandler($archive ? $_GET['archive_kb_article'] : $_GET['restore_kb_article'], false); + $kb_article_id = intval($row['kb_article_id']); + + mysqli_query($mysqli, "UPDATE kb_articles SET kb_article_archived_at = " . ($archive ? 'NOW()' : 'NULL') . " WHERE kb_article_id = $kb_article_id"); + + logAudit("Knowledge Base", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived' : 'restored') . " article " . escapeSql($row['kb_article_title']), 0, $kb_article_id); + + flashAlert("Article " . escapeHtml($row['kb_article_title']) . " " . ($archive ? 'archived' : 'restored'), $archive ? 'error' : 'success'); + + redirect(); +} + +if (isset($_GET['delete_kb_article'])) { + + $kb_article_id = intval($_GET['delete_kb_article']); + $title = kbArticleDelete($kb_article_id); + if ($title === null) { + flashAlert('That article no longer exists.', 'error'); + redirect('kb.php'); + } + + logAudit("Knowledge Base", "Delete", "$session_name deleted article " . escapeSql($title) . " and its versions", 0, $kb_article_id); + + flashAlert("Article " . escapeHtml($title) . " deleted", 'error'); + + // Not back to its page - it's gone + redirect(str_contains((string) ($_SERVER['HTTP_REFERER'] ?? ''), 'kb_article.php') ? 'kb.php' : null); +} + +if (isset($_GET['copy_kb_article'])) { + + $row = kbArticleForHandler($_GET['copy_kb_article']); + + // A new draft, with copies of its images and its tags + $kb_article_id = kbArticleSave([ + 'title' => mb_substr('Copy of ' . $row['kb_article_title'], 0, 200), + 'summary' => (string) $row['kb_article_summary'], + 'category_id' => intval($row['kb_article_category_id']), + 'review_months' => intval($row['kb_article_review_months']), + 'published' => false, + 'content' => '', + ]); + $content = (string) $row['kb_article_content']; + $from = kbArticleDir($row['kb_article_id']); + if (is_dir($from)) { + copyDirectory($from, kbArticleDir($kb_article_id)); + $content = str_replace('/uploads/kb/' . intval($row['kb_article_id']) . '/', "/uploads/kb/$kb_article_id/", $content); + } + mysqli_query($mysqli, "UPDATE kb_articles SET kb_article_content = '" . mysqli_real_escape_string($mysqli, $content) . "', + kb_article_content_raw = '" . escapeSql(kbArticleContentRaw('Copy of ' . $row['kb_article_title'], (string) $row['kb_article_summary'], $content)) . "' + WHERE kb_article_id = $kb_article_id"); + mysqli_query($mysqli, "INSERT INTO kb_article_tags (kb_article_id, tag_id) SELECT $kb_article_id, tag_id FROM kb_article_tags WHERE kb_article_id = " . intval($row['kb_article_id'])); + + logAudit("Knowledge Base", "Create", "$session_name copied article " . escapeSql($row['kb_article_title']) . " as a new draft", 0, $kb_article_id); + + flashAlert("A copy of " . escapeHtml($row['kb_article_title']) . " is ready as a draft"); + + redirect("kb_article.php?kb_article_id=$kb_article_id"); +} + +if (isset($_POST['restore_kb_article_version'])) { + + $kb_article_version_id = intval($_POST['kb_article_version_id'] ?? 0); + $version = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT kb_article_version_title, kb_article_version_summary, kb_article_version_content, kb_article_version_kb_article_id + FROM kb_article_versions WHERE kb_article_version_id = $kb_article_version_id")); + if (!$version) { + flashAlert('That version no longer exists.', 'error'); + redirect(); + } + $row = kbArticleForHandler($version['kb_article_version_kb_article_id']); + $kb_article_id = intval($row['kb_article_id']); + + // What it says now becomes a version itself + kbArticleSave([ + 'title' => $version['kb_article_version_title'], + 'summary' => (string) $version['kb_article_version_summary'], + 'category_id' => intval($row['kb_article_category_id']), + 'review_months' => intval($row['kb_article_review_months']), + 'published' => !empty($row['kb_article_published_at']), + 'content' => (string) $version['kb_article_version_content'], + ], $row); + + logAudit("Knowledge Base", "Edit", "$session_name put back an earlier version of article " . escapeSql($version['kb_article_version_title']), 0, $kb_article_id); + + flashAlert("The earlier version of " . escapeHtml($version['kb_article_version_title']) . " is back - what it said before is kept as a version"); + + redirect("kb_article.php?kb_article_id=$kb_article_id"); +} + +if (isset($_GET['delete_kb_article_version'])) { + + $kb_article_version_id = intval($_GET['delete_kb_article_version']); + $version = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT kb_article_version_title, kb_article_version_created_at, kb_article_version_kb_article_id + FROM kb_article_versions WHERE kb_article_version_id = $kb_article_version_id")); + if (!$version) { + flashAlert('That version no longer exists.', 'error'); + redirect(); + } + + mysqli_query($mysqli, "DELETE FROM kb_article_versions WHERE kb_article_version_id = $kb_article_version_id"); + + logAudit("Knowledge Base", "Edit", "$session_name deleted the version of " . date('M j, Y g:ia', strtotime($version['kb_article_version_created_at'])) . " of article " . escapeSql($version['kb_article_version_title']), 0, intval($version['kb_article_version_kb_article_id'])); + + flashAlert("Version deleted", 'error'); + + redirect("kb_article.php?kb_article_id=" . intval($version['kb_article_version_kb_article_id'])); +} + +if (isset($_POST['bulk_category_kb_articles'])) { + + $articles = kbBulkArticles(); + $category_id = kbCategoryId($_POST['category_id'] ?? 0); + + mysqli_query($mysqli, "UPDATE kb_articles SET kb_article_category_id = $category_id WHERE kb_article_id IN (" . implode(',', array_keys($articles)) . ")"); + + $category = $category_id ? categoryRow($category_id) : null; + foreach ($articles as $kb_article_id => $title) { + logAudit("Knowledge Base", "Edit", "$session_name put article " . escapeSql($title) . ($category ? " in category " . escapeSql($category['category_name']) : " in no category"), 0, $kb_article_id); + } + + $count = count($articles); + flashAlert("$count article" . ($count === 1 ? '' : 's') . ($category ? " put in " . escapeHtml($category['category_name']) . "" : " taken out of their category")); + + redirect(); +} + +if (isset($_POST['bulk_publish_kb_articles']) || isset($_POST['bulk_draft_kb_articles']) || isset($_POST['bulk_archive_kb_articles']) || isset($_POST['bulk_restore_kb_articles'])) { + + $articles = kbBulkArticles(); + $ids = implode(',', array_keys($articles)); + // What each does, on the ones it applies to + if (isset($_POST['bulk_publish_kb_articles'])) { + [$set, $only, $action, $verb] = ['kb_article_published_at = NOW()', 'kb_article_published_at IS NULL AND kb_article_archived_at IS NULL', 'Publish', 'published']; + } elseif (isset($_POST['bulk_draft_kb_articles'])) { + [$set, $only, $action, $verb] = ['kb_article_published_at = NULL', 'kb_article_published_at IS NOT NULL AND kb_article_archived_at IS NULL', 'Draft', 'made drafts']; + } elseif (isset($_POST['bulk_archive_kb_articles'])) { + [$set, $only, $action, $verb] = ['kb_article_archived_at = NOW()', 'kb_article_archived_at IS NULL', 'Archive', 'archived']; + } else { + [$set, $only, $action, $verb] = ['kb_article_archived_at = NULL', 'kb_article_archived_at IS NOT NULL', 'Restore', 'restored']; + } + + $changed = []; + $sql = mysqli_query($mysqli, "SELECT kb_article_id, kb_article_title FROM kb_articles WHERE kb_article_id IN ($ids) AND $only"); + while ($row = mysqli_fetch_assoc($sql)) { + $changed[intval($row['kb_article_id'])] = $row['kb_article_title']; + } + if ($changed) { + mysqli_query($mysqli, "UPDATE kb_articles SET $set WHERE kb_article_id IN (" . implode(',', array_keys($changed)) . ")"); + } + foreach ($changed as $kb_article_id => $title) { + logAudit("Knowledge Base", $action, "$session_name " . ($action === 'Draft' ? 'made a draft of' : $verb) . " article " . escapeSql($title), 0, $kb_article_id); + } + + $count = count($changed); + $left = count($articles) - $count; + flashAlert("$count article" . ($count === 1 ? '' : 's') . " $verb" . ($left ? " - $left already " . ($left === 1 ? 'was' : 'were') : ''), in_array($action, ['Archive'], true) ? 'error' : 'success'); + + redirect(); +} + +if (isset($_POST['bulk_delete_kb_articles'])) { + + $articles = kbBulkArticles(); + foreach ($articles as $kb_article_id => $title) { + kbArticleDelete($kb_article_id); + logAudit("Knowledge Base", "Delete", "$session_name deleted article " . escapeSql($title) . " and its versions", 0, $kb_article_id); + } + + $count = count($articles); + flashAlert("$count article" . ($count === 1 ? '' : 's') . " deleted", 'error'); + + redirect(); +} + +if (isset($_GET['copy_document_to_kb'])) { + + // The document's client must be one the agent can reach, as on its page + $document_id = intval($_GET['copy_document_to_kb']); + $document = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_name, document_client_id FROM documents WHERE document_id = $document_id")); + if (!$document) { + flashAlert('That document no longer exists.', 'error'); + redirect(); + } + $client_id = intval($document['document_client_id']); + enforceClientAccess(); + + $kb_article_id = kbArticleFromDocument($document_id); + + logAudit("Knowledge Base", "Create", "$session_name copied document " . escapeSql($document['document_name']) . " into the Knowledge Base as a draft", 0, $kb_article_id); + + flashAlert("" . escapeHtml($document['document_name']) . " is in the Knowledge Base as a draft - take out anything that's only about this client before you publish it"); + + redirect("kb_article.php?kb_article_id=$kb_article_id"); +} diff --git a/api/v1/tags/create.php b/api/v1/tags/create.php index 7e4255682..3cb6581eb 100644 --- a/api/v1/tags/create.php +++ b/api/v1/tags/create.php @@ -9,8 +9,8 @@ * Parameters (POST, JSON): * api_key required - Your API key, of an admin user * kind required - client, location, contact, credential, asset, vendor, - * domain, license, service, network, document or ticket - * (or its tag type, 1 to 12) + * domain, license, service, network, document, ticket or kb_article + * (or its tag type, 1 to 13) * name required - Unique within its kind, in any case * description optional * color optional - #rrggbb, else the next colour along diff --git a/api/v1/tags/read.php b/api/v1/tags/read.php index 8c09cdfb5..585fd89ab 100644 --- a/api/v1/tags/read.php +++ b/api/v1/tags/read.php @@ -9,8 +9,8 @@ * Parameters (GET): * api_key required - Your API key * kind optional - client, location, contact, credential, asset, vendor, - * domain, license, service, network, document or ticket - * (or its tag type, 1 to 12) + * domain, license, service, network, document, ticket or kb_article + * (or its tag type, 1 to 13) * record_id optional - With kind: that record's tags * tag_id optional - One tag (archived or not), with tag_record_ids - the * records with it this key can see @@ -37,7 +37,7 @@ if (isset($_GET['record_id'])) { $entity = tagApiKind($_GET['kind'] ?? '', 1); $config = tagEntity($entity); $record_id = intval($_GET['record_id']); - $record = mysqli_fetch_row(mysqli_query($mysqli, "SELECT {$config['id']} FROM {$config['table']} WHERE {$config['id']} = $record_id " . apiClientScopeSql($config['client']))); + $record = mysqli_fetch_row(mysqli_query($mysqli, "SELECT {$config['id']} FROM {$config['table']} WHERE {$config['id']} = $record_id " . ($config['client'] ? apiClientScopeSql($config['client']) : ''))); if (!$record) { $sql = false; require_once "../read_output.php"; @@ -66,7 +66,7 @@ if (isset($_GET['tag_id'])) { $tag_row['tag_record_ids'] = []; $records = mysqli_query($mysqli, "SELECT {$config['link_table']}.{$config['link_record']} FROM {$config['link_table']} JOIN {$config['table']} ON {$config['table']}.{$config['id']} = {$config['link_table']}.{$config['link_record']} - WHERE {$config['link_table']}.{$config['link_tag']} = " . intval($row['tag_id']) . " " . apiClientScopeSql($config['client']) . " + WHERE {$config['link_table']}.{$config['link_tag']} = " . intval($row['tag_id']) . " " . ($config['client'] ? apiClientScopeSql($config['client']) : '') . " ORDER BY {$config['link_table']}.{$config['link_record']}"); while ($record = mysqli_fetch_row($records)) { $tag_row['tag_record_ids'][] = intval($record[0]); diff --git a/api/v1/tags/update.php b/api/v1/tags/update.php index c22bfabb4..2bc4fd1dd 100644 --- a/api/v1/tags/update.php +++ b/api/v1/tags/update.php @@ -9,8 +9,8 @@ * Parameters (POST, JSON): * api_key required - Your API key * kind required - client, location, contact, credential, asset, vendor, - * domain, license, service, network, document or ticket - * (or its tag type, 1 to 12) + * domain, license, service, network, document, ticket or kb_article + * (or its tag type, 1 to 13) * record_id required - The record * tags required - Tag ids or names, as an array or comma-separated. * A name that is no tag of the kind yet is made when the diff --git a/db.sql b/db.sql index 547ab8b54..8b4a254e3 100644 --- a/db.sql +++ b/db.sql @@ -1925,6 +1925,72 @@ CREATE TABLE `invoices` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `kb_article_tags` +-- + +DROP TABLE IF EXISTS `kb_article_tags`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8mb4 */; +CREATE TABLE `kb_article_tags` ( + `kb_article_id` int(11) NOT NULL, + `tag_id` int(11) NOT NULL, + PRIMARY KEY (`kb_article_id`,`tag_id`), + KEY `tag_id` (`tag_id`), + CONSTRAINT `kb_article_tags_ibfk_1` FOREIGN KEY (`kb_article_id`) REFERENCES `kb_articles` (`kb_article_id`) ON DELETE CASCADE, + CONSTRAINT `kb_article_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 `kb_article_versions` +-- + +DROP TABLE IF EXISTS `kb_article_versions`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8mb4 */; +CREATE TABLE `kb_article_versions` ( + `kb_article_version_id` int(11) NOT NULL AUTO_INCREMENT, + `kb_article_version_title` varchar(200) NOT NULL, + `kb_article_version_summary` varchar(255) DEFAULT NULL, + `kb_article_version_content` longtext NOT NULL, + `kb_article_version_created_at` datetime NOT NULL DEFAULT current_timestamp(), + `kb_article_version_created_by` int(11) NOT NULL DEFAULT 0, + `kb_article_version_kb_article_id` int(11) NOT NULL, + PRIMARY KEY (`kb_article_version_id`), + KEY `kb_article_version_kb_article_id` (`kb_article_version_kb_article_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `kb_articles` +-- + +DROP TABLE IF EXISTS `kb_articles`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8mb4 */; +CREATE TABLE `kb_articles` ( + `kb_article_id` int(11) NOT NULL AUTO_INCREMENT, + `kb_article_title` varchar(200) NOT NULL, + `kb_article_summary` varchar(255) DEFAULT NULL, + `kb_article_content` longtext NOT NULL, + `kb_article_content_raw` longtext NOT NULL, + `kb_article_category_id` int(11) NOT NULL DEFAULT 0, + `kb_article_published_at` datetime DEFAULT NULL, + `kb_article_review_months` int(11) NOT NULL DEFAULT 0, + `kb_article_reviewed_at` datetime DEFAULT NULL, + `kb_article_reviewed_by` int(11) NOT NULL DEFAULT 0, + `kb_article_created_at` datetime NOT NULL DEFAULT current_timestamp(), + `kb_article_created_by` int(11) NOT NULL DEFAULT 0, + `kb_article_updated_at` datetime DEFAULT NULL, + `kb_article_updated_by` int(11) NOT NULL DEFAULT 0, + `kb_article_archived_at` datetime DEFAULT NULL, + PRIMARY KEY (`kb_article_id`), + KEY `kb_article_category_id` (`kb_article_category_id`), + FULLTEXT KEY `kb_article_content_raw` (`kb_article_content_raw`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + -- -- Table structure for table `location_documents` -- @@ -3602,6 +3668,7 @@ CREATE TABLE `settings` ( `config_module_enable_dispatch` tinyint(1) NOT NULL DEFAULT 1, `config_module_enable_trips` tinyint(1) NOT NULL DEFAULT 1, `config_module_enable_calendar` tinyint(1) NOT NULL DEFAULT 1, + `config_module_enable_kb` tinyint(1) NOT NULL DEFAULT 1, `config_theme` varchar(200) DEFAULT 'blue', `config_telemetry` tinyint(1) DEFAULT 0, `config_timezone` varchar(200) NOT NULL DEFAULT 'America/New_York', diff --git a/functions.php b/functions.php index 1d9347d84..91e079284 100644 --- a/functions.php +++ b/functions.php @@ -125,6 +125,7 @@ require_once __DIR__ . '/functions/settings.php'; require_once __DIR__ . '/functions/mail_queue.php'; require_once __DIR__ . '/functions/mail_graph.php'; require_once __DIR__ . '/functions/purchase_orders.php'; +require_once __DIR__ . '/functions/kb.php'; require_once __DIR__ . '/functions/cron_health.php'; require_once __DIR__ . '/functions/labor_cost.php'; require_once __DIR__ . '/functions/service_account.php'; diff --git a/functions/audit_log.php b/functions/audit_log.php index 983019b3c..395ca2762 100644 --- a/functions/audit_log.php +++ b/functions/audit_log.php @@ -41,6 +41,7 @@ function auditLogKinds() { 'Domain' => ['icon' => 'fa-globe', 'module' => 'module_support', 'table' => 'domains', 'id' => 'domain_id', 'name' => 'domain_name', 'client' => 'domain_client_id', 'url' => '/agent/domains.php?{client_q}open={id}'], 'Software' => ['icon' => 'fa-cube', 'module' => 'module_support', 'table' => 'software', 'id' => 'software_id', 'name' => 'software_name', 'client' => 'software_client_id', 'url' => '/agent/license.php?client_id={client}&software_id={id}'], 'Service' => ['icon' => 'fa-stream', 'module' => 'module_support', 'table' => 'services', 'id' => 'service_id', 'name' => 'service_name', 'client' => 'service_client_id', 'url' => '/agent/service.php?client_id={client}&service_id={id}'], + 'Knowledge Base' => ['icon' => 'fa-book', 'module' => 'module_support', 'table' => 'kb_articles', 'id' => 'kb_article_id', 'name' => 'kb_article_title', 'url' => '/agent/kb_article.php?kb_article_id={id}'], 'Document' => ['icon' => 'fa-file-alt', 'module' => 'module_support', 'table' => 'documents', 'id' => 'document_id', 'name' => 'document_name', 'client' => 'document_client_id', 'url' => '/agent/document.php?client_id={client}&document_id={id}'], 'File' => ['icon' => 'fa-file', 'module' => 'module_client', 'table' => 'files', 'id' => 'file_id', 'name' => 'file_name', 'client' => 'file_client_id', 'url' => '/agent/files.php?client_id={client}&open={id}'], 'Share' => ['icon' => 'fa-share-alt', 'module' => 'module_support', 'table' => 'shared_items', 'id' => 'item_id', 'name' => 'item_name', 'client' => 'item_client_id', 'url' => '/agent/shares.php?{client_q}share={id}'], diff --git a/functions/categories.php b/functions/categories.php index 7dc29a101..3489e87d5 100644 --- a/functions/categories.php +++ b/functions/categories.php @@ -129,6 +129,14 @@ function categoryKinds() { $use('contact_notes', 'contact_note_type', 'contact note', 'contact notes', null, 'contact_note_archived_at', 'contact_note_updated_at'), ], ], + 'kb' => [ + 'label' => 'Knowledge Base', 'one' => 'Knowledge Base category', 'many' => 'Knowledge Base categories', 'icon' => 'fa-book', + 'about' => 'How Knowledge Base articles are grouped - each has one, the Knowledge Base filters by them, and an article\'s icon is its category\'s.', + 'by' => 'id', 'ordered' => true, 'max' => 200, 'modules' => ['module_support'], + 'uses' => [ + $use('kb_articles', 'kb_article_category_id', 'article', 'articles', '/agent/kb.php?category={id}', 'kb_article_archived_at'), + ], + ], 'asset_note_type' => [ 'label' => 'Asset Note Type', 'one' => 'asset note type', 'many' => 'asset note types', 'icon' => 'fa-desktop', 'about' => 'What a note on an asset records - maintenance, a repair, an upgrade. Its icon marks the note.', diff --git a/functions/kb.php b/functions/kb.php new file mode 100644 index 000000000..6ed804f1a --- /dev/null +++ b/functions/kb.php @@ -0,0 +1,263 @@ + Settings > Modules (kbOn()), and + * comes with database 3.9.5. + * + * An article has a title, a line saying what it is for, a category (Admin > + * Categories, the 'kb' kind), tags (the kb_article kind) and its content - the + * editor's HTML, purified wherever it is shown (documentHtmlPurify()). Without + * a publish date it is a draft. Each edit keeps what it said before as a + * version. When it says how often, it is due for review that long after it was + * last made, edited or reviewed - an edit counts, as it does for documents. + * + * Reading takes Support (view), writing Support edit, deleting Support full. + */ + +const KB_REVIEW_MONTHS = [0 => 'Never', 3 => 'Every 3 months', 6 => 'Every 6 months', 12 => 'Every year', 24 => 'Every 2 years']; + +/** Is the Knowledge Base in use - switched on, and its tables there (database 3.9.5)? */ +function kbOn() { + return isset($GLOBALS['config_module_enable_kb']) && moduleOn('kb'); +} + +/** May the signed-in agent use the Knowledge Base at this level - 1 read, 2 write, 3 delete? */ +function kbCan($level = 1) { + return kbOn() && lookupUserPermission('module_support') >= intval($level); +} + +/** Why a page, window or handler is closed - the module off, or the agent's role - or '' when it's open. Plain text. */ +function kbClosedText($level = 1) { + if (!kbOn()) { + return isset($GLOBALS['config_module_enable_kb']) ? moduleOffText('kb') : 'The Knowledge Base needs the database update - ask an admin to run it from Maintenance > Update.'; + } + $has = intval(lookupUserPermission('module_support')); + return $has >= intval($level) ? '' : permissionRefusedText('module_support', $level, $has); +} + +/** One article with its category and the names of who made, changed and reviewed it - or null. */ +function kbArticleRow($kb_article_id) { + global $mysqli; + $kb_article_id = intval($kb_article_id); + return mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT kb_article_id, kb_article_title, kb_article_summary, kb_article_content, kb_article_category_id, + kb_article_published_at, kb_article_review_months, kb_article_reviewed_at, kb_article_reviewed_by, kb_article_created_at, kb_article_created_by, + kb_article_updated_at, kb_article_updated_by, kb_article_archived_at, + category_id, category_name, category_description, category_color, category_icon, category_type, category_archived_at, + creator.user_name AS created_by_name, updater.user_name AS updated_by_name, reviewer.user_name AS reviewed_by_name + FROM kb_articles + LEFT JOIN categories ON category_id = kb_article_category_id AND kb_article_category_id > 0 + LEFT JOIN users creator ON creator.user_id = kb_article_created_by AND kb_article_created_by > 0 + LEFT JOIN users updater ON updater.user_id = kb_article_updated_by AND kb_article_updated_by > 0 + LEFT JOIN users reviewer ON reviewer.user_id = kb_article_reviewed_by AND kb_article_reviewed_by > 0 + WHERE kb_article_id = $kb_article_id")) ?: null; +} + +/* + * An article's fields from a form - title (required), summary, category_id, + * review_months, published (on or off) and content - as [fields, error]. A + * category must be a Knowledge Base one; anything else counts as none. + */ +function kbArticleFields(array $in) { + $title = trim((string) ($in['title'] ?? '')); + if ($title === '') { + return [[], 'An article needs a title.']; + } + if (mb_strlen($title) > 200) { + return [[], 'Keep the title to 200 characters.']; + } + $summary = trim(preg_replace('/\s+/u', ' ', (string) ($in['summary'] ?? ''))); + if (mb_strlen($summary) > 255) { + return [[], 'Keep the summary to 255 characters.']; + } + $category_id = kbCategoryId($in['category_id'] ?? 0); + $review_months = intval($in['review_months'] ?? 0); + return [[ + 'title' => $title, + 'summary' => $summary, + 'category_id' => $category_id, + 'review_months' => isset(KB_REVIEW_MONTHS[$review_months]) ? $review_months : 0, + 'published' => !empty($in['published']), + 'content' => (string) ($in['content'] ?? ''), + ], '']; +} + +/** A Knowledge Base category's id - or 0 for none, or for one that isn't a Knowledge Base category. */ +function kbCategoryId($category_id) { + global $mysqli; + $category_id = intval($category_id); + return $category_id > 0 && mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM categories WHERE category_id = $category_id AND category_type = 'kb'")) ? $category_id : 0; +} + +/** What search runs over - the title, the summary and the text. Raw. */ +function kbArticleContentRaw($title, $summary, $html) { + return documentContentRaw(trim($title . ' ' . $summary), $html); +} + +/** Where an article's images live, on disk. */ +function kbArticleDir($kb_article_id) { + return dirname(__DIR__) . '/uploads/kb/' . intval($kb_article_id); +} + +/** Keep what an article says now as a version, before it changes. */ +function kbKeepVersion(array $row) { + global $mysqli; + $by = intval($row['kb_article_updated_by']) ?: intval($row['kb_article_created_by']); + $at = escapeSql($row['kb_article_updated_at'] ?: $row['kb_article_created_at']); + mysqli_query($mysqli, "INSERT INTO kb_article_versions SET + kb_article_version_title = '" . escapeSql($row['kb_article_title']) . "', + kb_article_version_summary = '" . escapeSql((string) $row['kb_article_summary']) . "', + kb_article_version_content = '" . mysqli_real_escape_string($mysqli, (string) $row['kb_article_content']) . "', + kb_article_version_created_at = '$at', + kb_article_version_created_by = $by, + kb_article_version_kb_article_id = " . intval($row['kb_article_id'])); +} + +/* + * Save an article - a new one ($row null), or a change to $row, which keeps + * what it said before as a version when its words change. Images pasted into + * the editor go into its folder. Its id. + */ +function kbArticleSave(array $f, $row = null) { + global $mysqli, $session_user_id; + $user_id = intval($session_user_id ?? 0); + if ($row === null) { + mysqli_query($mysqli, "INSERT INTO kb_articles SET kb_article_title = '" . escapeSql($f['title']) . "', kb_article_content = '', kb_article_content_raw = '', + kb_article_created_by = $user_id"); + $kb_article_id = intval(mysqli_insert_id($mysqli)); + $published_sql = $f['published'] ? 'NOW()' : 'NULL'; + } else { + $kb_article_id = intval($row['kb_article_id']); + if ($f['title'] !== $row['kb_article_title'] || $f['summary'] !== (string) $row['kb_article_summary'] || $f['content'] !== (string) $row['kb_article_content']) { + kbKeepVersion($row); + } + // Published stays when it was - its date is when it first went out + $published_sql = $f['published'] ? ($row['kb_article_published_at'] ? 'kb_article_published_at' : 'NOW()') : 'NULL'; + } + $content = saveBase64Images($f['content'], dirname(__DIR__) . '/uploads/kb/', 'uploads/kb/', $kb_article_id); + mysqli_query($mysqli, "UPDATE kb_articles SET + kb_article_title = '" . escapeSql($f['title']) . "', + kb_article_summary = " . ($f['summary'] !== '' ? "'" . escapeSql($f['summary']) . "'" : 'NULL') . ", + kb_article_content = '" . mysqli_real_escape_string($mysqli, $content) . "', + kb_article_content_raw = '" . escapeSql(kbArticleContentRaw($f['title'], $f['summary'], $content)) . "', + kb_article_category_id = " . intval($f['category_id']) . ", + kb_article_review_months = " . intval($f['review_months']) . ", + kb_article_published_at = $published_sql + " . ($row === null ? '' : ", kb_article_updated_at = NOW(), kb_article_updated_by = $user_id") . " + WHERE kb_article_id = $kb_article_id"); + return $kb_article_id; +} + +/* + * When a published article is next due for review - and whether it is - or + * null when it isn't reviewed (never, a draft, or archived). Like a + * document's: from when it was last made, edited or reviewed. + */ +function kbReviewState(array $row) { + $months = intval($row['kb_article_review_months'] ?? 0); + if ($months <= 0 || empty($row['kb_article_published_at']) || !empty($row['kb_article_archived_at'])) { + return null; + } + $times = array_filter([$row['kb_article_created_at'] ?? null, $row['kb_article_updated_at'] ?? null, $row['kb_article_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['kb_article_reviewed_at']) && strtotime($row['kb_article_reviewed_at']) === $last, + 'next' => date('Y-m-d H:i:s', $next), + 'due' => $next < time(), + ]; +} + +/* The articles due for review, as a WHERE condition - what kbReviewState() says, in SQL. */ +function kbReviewDueSql() { + return "(kb_article_archived_at IS NULL AND kb_article_published_at IS NOT NULL AND kb_article_review_months > 0 + AND GREATEST(kb_article_created_at, COALESCE(kb_article_updated_at, kb_article_created_at), COALESCE(kb_article_reviewed_at, kb_article_created_at)) + + INTERVAL kb_article_review_months MONTH < NOW())"; +} + +/** A line saying what an article is - its summary, or the start of its text. Raw. */ +function kbArticleExcerpt(array $row, $length = 160) { + $summary = trim((string) ($row['kb_article_summary'] ?? '')); + if ($summary !== '') { + return $summary; + } + $text = trim(preg_replace('/\s+/u', ' ', html_entity_decode(strip_tags(str_replace('<', ' <', (string) ($row['kb_article_content'] ?? ''))), ENT_QUOTES | ENT_HTML5, 'UTF-8'))); + return mb_strlen($text) > $length ? rtrim(mb_substr($text, 0, $length - 1)) . '…' : $text; +} + +/** An article's square icon - in its category's icon and colour, or a book in the accent colour. */ +function kbArticleTile(array $row, $size = '') { + $icon = categoryIcon($row['category_icon'] ?? '') ?: 'book'; + $color = categoryColor($row['category_color'] ?? ''); + return ''; +} + +/** The badges after an article's title - Draft, Archived, Review due. */ +function kbArticleBadges(array $row, $size = '') { + $fs = $size === 'lg' ? ' fs-6' : ''; + $html = ''; + if (!empty($row['kb_article_archived_at'])) { + $html .= 'Archived'; + } elseif (empty($row['kb_article_published_at'])) { + $html .= 'Draft'; + } + $review = kbReviewState($row); + if ($review && $review['due']) { + $html .= 'Review due'; + } + return $html; +} + +/* Delete an article for good, with its versions, tags and images. Its title, or null. */ +function kbArticleDelete($kb_article_id) { + global $mysqli; + $kb_article_id = intval($kb_article_id); + $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT kb_article_title FROM kb_articles WHERE kb_article_id = $kb_article_id")); + if (!$row) { + return null; + } + mysqli_query($mysqli, "DELETE FROM kb_articles WHERE kb_article_id = $kb_article_id"); + mysqli_query($mysqli, "DELETE FROM kb_article_versions WHERE kb_article_version_kb_article_id = $kb_article_id"); + mysqli_query($mysqli, "DELETE FROM kb_article_tags WHERE kb_article_id = $kb_article_id"); + removeDirectory(kbArticleDir($kb_article_id)); + return $row['kb_article_title']; +} + +/* + * A new draft article from a client's document - its name, description and + * content, with copies of its images. The caller has checked the document's + * client. The article's id, or 0. + */ +function kbArticleFromDocument($document_id) { + global $mysqli; + $document_id = intval($document_id); + $doc = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_name, document_description, document_content FROM documents WHERE document_id = $document_id")); + if (!$doc) { + return 0; + } + $kb_article_id = kbArticleSave([ + 'title' => mb_substr(trim($doc['document_name']), 0, 200), + 'summary' => mb_substr(trim(preg_replace('/\s+/u', ' ', (string) $doc['document_description'])), 0, 255), + 'category_id' => 0, + 'review_months' => 0, + 'published' => false, + 'content' => '', + ]); + $from = dirname(__DIR__) . "/uploads/documents/$document_id"; + $content = (string) $doc['document_content']; + if (is_dir($from)) { + copyDirectory($from, kbArticleDir($kb_article_id)); + $content = str_replace("/uploads/documents/$document_id/", "/uploads/kb/$kb_article_id/", $content); + } + mysqli_query($mysqli, "UPDATE kb_articles SET kb_article_content = '" . mysqli_real_escape_string($mysqli, $content) . "', + kb_article_content_raw = '" . escapeSql(kbArticleContentRaw($doc['document_name'], (string) $doc['document_description'], $content)) . "' + WHERE kb_article_id = $kb_article_id"); + return $kb_article_id; +} diff --git a/functions/search.php b/functions/search.php index 93f8b05c1..881e408c2 100644 --- a/functions/search.php +++ b/functions/search.php @@ -480,6 +480,30 @@ function globalSearchGroups($terms, $deep) { }, ]; + // The Knowledge Base - no client's, so in no client's scope + $kb_deep_match = $deep ? "OR MATCH(kb_article_content_raw) AGAINST ('$eq')" : ''; + $groups[] = [ + 'key' => 'kb_articles', 'label' => 'Knowledge Base', 'icon' => 'book', 'module' => 'module_support', 'enabled' => kbOn(), + 'select' => 'kb_article_id, kb_article_title, kb_article_summary, kb_article_content, kb_article_published_at, category_name, category_icon, category_color', + 'from' => 'kb_articles LEFT JOIN categories ON category_id = kb_article_category_id AND kb_article_category_id > 0', + 'where' => "kb_article_archived_at IS NULL + AND (kb_article_title LIKE '%$like%' OR kb_article_summary LIKE '%$like%' $kb_deep_match + " . tagSearchSql('kb_article', $like, 'kb_articles.kb_article_id') . ")", + 'scope' => null, + 'tags' => ['kb_article', 'kb_articles.kb_article_id'], + 'order' => "(kb_article_title LIKE '$like%') DESC, kb_article_published_at IS NULL, COALESCE(kb_article_updated_at, kb_article_created_at) DESC", + 'list_url' => '/agent/kb.php?' . http_build_query(['q' => $q]), + 'item' => function ($row) { + return [ + 'title' => $row['kb_article_title'], + 'subtitle' => implode(' · ', array_filter([$row['kb_article_published_at'] ? '' : 'Draft', kbArticleExcerpt($row, 120)])), + 'context' => $row['category_name'] ?? '', + 'url' => '/agent/kb_article.php?kb_article_id=' . intval($row['kb_article_id']), + 'tile' => ['tone' => 'document', 'icon' => categoryIcon($row['category_icon'] ?? '') ?: 'book', 'label' => 'Article'], + ]; + }, + ]; + $groups[] = [ 'key' => 'files', 'label' => 'Files', 'icon' => 'paperclip', 'module' => 'module_support', 'select' => 'file_id, file_name, file_ext, file_description, file_client_id, file_folder_id, client_name, folder_name', @@ -891,6 +915,7 @@ function globalSearchPages($raw_query) { ['Services', '/agent/services.php', $support], ['Vendors', '/agent/vendors.php', true], ['Calendar', '/agent/calendar.php', moduleOn('calendar')], + ['Knowledge Base', '/agent/kb.php', kbCan(1)], ['Tickets', '/agent/tickets.php', $support && $ticketing], ['Recurring Tickets', '/agent/recurring_tickets.php', $support && $ticketing], ['Projects', '/agent/projects.php', $support && $ticketing], diff --git a/functions/settings.php b/functions/settings.php index 7d3d9f105..344df2da2 100644 --- a/functions/settings.php +++ b/functions/settings.php @@ -64,7 +64,7 @@ function settingsPages() { 'identity' => ['group' => 'access', 'title' => 'Single sign-on', 'icon' => 'fa-fingerprint', 'url' => 'identity_providers.php', 'module' => 'portal', 'about' => 'Contacts signing in to the client portal with their Microsoft 365 account.'], 'module' => ['group' => 'system', 'title' => 'Modules', 'icon' => 'fa-cube', 'url' => 'settings_module.php', - 'about' => 'The parts of ITFlow in use - IT documentation, ticketing, invoicing, the calendar and the client portal, and the parts that come with them - and white-labelling.'], + 'about' => 'The parts of ITFlow in use - IT documentation, ticketing, invoicing, the calendar, the Knowledge Base and the client portal, and the parts that come with them - and white-labelling.'], 'logs' => ['group' => 'system', 'title' => 'Logs', 'icon' => 'fa-scroll', 'url' => 'settings_logs.php', 'about' => 'How long the audit log and app logs are kept, and sending them on to a syslog server or SIEM.'], 'telemetry' => ['group' => 'system', 'title' => 'Telemetry', 'icon' => 'fa-satellite-dish', 'url' => 'settings_telemetry.php', @@ -79,8 +79,8 @@ function settingsPageAvailable(array $page) { /* * Whether a part of ITFlow is in use (Admin > Settings > Modules) - itdoc, - * ticketing, accounting, portal, and purchasing, bookkeeping, dispatch, trips - * and calendar. Purchasing, bookkeeping and trips come with invoicing and + * ticketing, accounting, portal, and purchasing, bookkeeping, dispatch, trips, + * calendar and kb (the Knowledge Base). Purchasing, bookkeeping and trips come with invoicing and * dispatch with ticketing, so each is off while that is. Before the database * update that adds a switch, what it switches is on, as it was. */ @@ -106,7 +106,7 @@ function moduleOn($module) { * that comes with another, when that one is off. */ function moduleOffText($module) { - $names = ['dispatch' => 'Dispatch is', 'bookkeeping' => 'Bookkeeping is', 'trips' => 'Trips are', 'calendar' => 'The calendar is', 'purchasing' => 'Purchasing is']; + $names = ['dispatch' => 'Dispatch is', 'bookkeeping' => 'Bookkeeping is', 'trips' => 'Trips are', 'calendar' => 'The calendar is', 'purchasing' => 'Purchasing is', 'kb' => 'The Knowledge Base is']; $comes_with = ['dispatch' => 'ticketing', 'bookkeeping' => 'accounting', 'trips' => 'accounting', 'purchasing' => 'accounting']; $parent = $comes_with[$module] ?? ''; if ($parent && !moduleOn($parent)) { @@ -422,6 +422,7 @@ function settingsFields() { 'config_module_enable_purchasing' => ['page' => 'module', 'section' => 'Modules', 'label' => 'Purchasing', 'words' => 'module purchasing purchase orders procurement receiving'] + $on_off, 'config_module_enable_trips' => ['page' => 'module', 'section' => 'Modules', 'label' => 'Trips', 'words' => 'module trips mileage driving'] + $on_off, 'config_module_enable_calendar' => ['page' => 'module', 'section' => 'Modules', 'label' => 'Calendar', 'words' => 'module calendar events feed'] + $on_off, + 'config_module_enable_kb' => ['page' => 'module', 'section' => 'Modules', 'label' => 'Knowledge Base', 'words' => 'module knowledge base kb articles how-to help'] + $on_off, 'config_client_portal_enable' => ['page' => 'module', 'section' => 'Modules', 'label' => 'Client portal', 'words' => 'module portal clients'] + $on_off, 'config_whitelabel_key' => ['page' => 'module', 'section' => 'White-label', 'label' => 'White-label key', 'kind' => 'secret', 'words' => 'whitelabel white label powered by banner'], 'config_whitelabel_enabled' => ['page' => 'module', 'section' => 'White-label', 'label' => 'White-label', 'search' => false] + $on_off, diff --git a/functions/tags.php b/functions/tags.php index fe11b7c46..e8205b179 100644 --- a/functions/tags.php +++ b/functions/tags.php @@ -2,7 +2,7 @@ /* * Tags on clients, locations, contacts, credentials, assets, vendors, domains, - * licenses, services, networks, documents and tickets. + * licenses, services, networks, documents, tickets and Knowledge Base articles. * * Every list and detail page draws a record's tags with tagPills(): coloured * pills that open the list filtered to that tag, then a "+" pill that opens @@ -109,6 +109,14 @@ function tagEntity($entity) { 'table' => 'tickets', 'id' => 'ticket_id', 'client' => 'ticket_client_id', 'name' => "CONCAT(ticket_prefix, ticket_number, ' ', ticket_subject)", 'log' => 'Ticket', 'list' => 'tickets.php' ], + // Knowledge Base articles are no client's - 'client' is null + 'kb_article' => [ + 'title' => 'Article', 'plural' => 'Knowledge Base articles', 'icon' => 'fa-book', + 'type' => 13, 'label' => 'article', 'module' => 'module_support', + 'link_table' => 'kb_article_tags', 'link_record' => 'kb_article_id', 'link_tag' => 'tag_id', + 'table' => 'kb_articles', 'id' => 'kb_article_id', 'client' => null, 'name' => 'kb_article_title', + 'log' => 'Knowledge Base', 'list' => 'kb.php' + ], ]; return $entities[$entity] ?? null; @@ -374,7 +382,7 @@ function tagFilterOptionsHtml($entity, array $selected, $records_where = '1') { $selected = array_values(array_filter(array_map('intval', $selected))); $chosen_sql = $selected ? 'OR tag_id IN (' . implode(',', $selected) . ')' : ''; // The record's client is there too, for a list that leaves out archived clients - $client_join = $config['table'] === 'clients' ? '' : "LEFT JOIN clients ON clients.client_id = {$config['table']}.{$config['client']}"; + $client_join = $config['table'] === 'clients' || empty($config['client']) ? '' : "LEFT JOIN clients ON clients.client_id = {$config['table']}.{$config['client']}"; $sql = mysqli_query($mysqli, "SELECT tag_id, tag_name, tag_description, tag_color, tag_icon, tag_archived_at FROM tags WHERE tag_type = {$config['type']} AND ((tag_archived_at IS NULL AND EXISTS ( SELECT 1 FROM {$config['link_table']} JOIN {$config['table']} ON {$config['table']}.{$config['id']} = {$config['link_table']}.{$config['link_record']} $client_join @@ -408,7 +416,7 @@ function tagRecordsEditable($entity, array $record_ids) { $records = []; $sql = mysqli_query($mysqli, - "SELECT {$config['id']} AS record_id, {$config['client']} AS client_id, {$config['name']} AS record_name + "SELECT {$config['id']} AS record_id, " . ($config['client'] ?: '0') . " AS client_id, {$config['name']} AS record_name FROM {$config['table']} WHERE {$config['id']} IN (" . implode(',', $record_ids) . ")" ); while ($row = mysqli_fetch_assoc($sql)) { @@ -416,7 +424,7 @@ function tagRecordsEditable($entity, array $record_ids) { if (!$can_ours) { return false; } - } elseif (!$can_module || !hasClientAccess(intval($row['client_id']))) { + } elseif (!$can_module || ($config['client'] && !hasClientAccess(intval($row['client_id'])))) { return false; } $records[intval($row['record_id'])] = ['name' => $row['record_name'], 'client_id' => intval($row['client_id'])]; @@ -489,7 +497,7 @@ function tagPill(array $tag) { /* The seven kinds by tag type - type => the entity's key in tagEntity(). */ function tagKinds() { return [1 => 'client', 2 => 'location', 3 => 'contact', 4 => 'credential', 5 => 'asset', 6 => 'vendor', 7 => 'domain', - 8 => 'license', 9 => 'service', 10 => 'network', 11 => 'document', 12 => 'ticket']; + 8 => 'license', 9 => 'service', 10 => 'network', 11 => 'document', 12 => 'ticket', 13 => 'kb_article']; } /* "an asset tag", "a client tag" - for messages. */ diff --git a/includes/load_global_settings.php b/includes/load_global_settings.php index c68222957..104f6eeb5 100644 --- a/includes/load_global_settings.php +++ b/includes/load_global_settings.php @@ -149,6 +149,10 @@ if (version_compare((string) ($row['config_current_database_version'] ?? '0'), ' $$module_column = intval($module_on); } } +// The Knowledge Base - from database 3.9.5 +if (version_compare((string) ($row['config_current_database_version'] ?? '0'), '3.9.5', '>=')) { + $config_module_enable_kb = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_module_enable_kb FROM settings WHERE company_id = 1"))[0] ?? 1); +} $config_client_portal_require_mfa = intval($row['config_client_portal_require_mfa']); // The client record that represents this MSP's own company, or 0 for none diff --git a/uploads/kb/index.php b/uploads/kb/index.php new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/uploads/kb/index.php @@ -0,0 +1 @@ +