From 1a613eab7b96e84e8603b93f50a69f52fc16a3cf Mon Sep 17 00:00:00 2001 From: johnnyq Date: Wed, 14 Oct 2026 20:35:41 -0400 Subject: [PATCH] Files and documents overhaul: rebuilt files list with folder tree (counts, open/close remembered, phone collapse), current folder path and folder menu, All/Documents/Uploads tabs with counts, colour-coded type tiles, shared and portal pills, updated time and author, linked record counts, permission-gated row menus, search across every folder from the top level showing where each result lives, grid view with real previews (image thumbnails, PDF first page, document and text excerpts) and an arrow-key previewer with a Details button, drag and drop files onto the list to upload into the folder on show, bulk move/archive/restore/delete for files and documents together; document page rebuilt like the asset and contact pages with header actions (Edit, Share, PDF, Print, Rename, Move, Link, portal visibility, Archive, Delete), Linked card, folder and portal card and a History card where versions can be viewed, restored (current content kept as a version) or deleted, revision table moved to print only; new file quick look with preview, folder, uploader, share status and links; document quick look and version viewer redone; one link modal for every relation (files, documents, assets, contacts, licenses, vendors) with type tabs and multi-select, offering only the client's own unarchived, not yet linked records, old link modals now open it; upload modal with drop box, file list and size checks; new and edit document share one form with folder and client portal switch; template, rename, move, bulk move, visibility and folder add/rename/delete modals modernized with folder tree pickers; asset and contact pages show the same file and document tiles and open the file quick look; fixes: folder, contact and asset ownership checked on upload, new document, template, move and bulk move, links limited to records of the same client, linking an existing link no longer errors, deleting a folder now moves its files, documents and subfolders up instead of orphaning them, file delete confirmation no longer builds its name into inline JS, permission checks added to the upload, new document, folder, visibility and link modals, upload modal script error that skipped the size check, skipped uploads now reported, mime type read from the uploaded file, per-row link modals that broke grid view, search with quotes double escaped, document search text stored without tags and with words spaced, bulk messages with empty counts, visibility wording, file rename icon, PDF export redirecting after output and needing write access, file and document deletes now remove their links and skip missing files on disk, duplicate asset to file link handler removed; developer: new functions/links.php (relation registry, linkAdd, linkRemove, linkRemoveAll, linkedRecords, linkCandidates, linkGroupsHtml), post/link.php with link_items and unlink_item plus the legacy link and unlink names, functions/files.php gains fileTypeInfo, fileTypeTile, documentTile, folderTree, folderPath, folderSubtree, folderOptionsHtml, folderOwnedId, delete helpers, documentContentRaw and documentHtmlPurify, new agent/js/files.js, modals/link/link.php, modals/file/file.php, modals/folder/folder_delete.php and modals/document/document_form.php, removed unused document_bulk_move.php, document_add_file_relation.php and js/file_delete_modal.js --- agent/ajax.php | 12 +- agent/asset.php | 30 +- agent/contact.php | 10 +- agent/document.php | 573 ++--- agent/files.php | 1890 ++++++----------- agent/js/file_delete_modal.js | 5 - agent/js/files.js | 249 +++ agent/modals/asset/asset_link_document.php | 72 +- agent/modals/asset/asset_link_file.php | 77 +- .../modals/contact/contact_link_document.php | 72 +- agent/modals/contact/contact_link_file.php | 74 +- agent/modals/document/document_add.php | 54 +- .../document/document_add_file_relation.php | 44 - .../document/document_add_from_template.php | 118 +- agent/modals/document/document_bulk_move.php | 108 - agent/modals/document/document_edit.php | 42 +- .../document/document_edit_visibility.php | 46 +- agent/modals/document/document_form.php | 59 + agent/modals/document/document_link_asset.php | 73 +- .../modals/document/document_link_contact.php | 73 +- agent/modals/document/document_link_file.php | 76 +- .../document/document_link_software.php | 74 +- .../modals/document/document_link_vendor.php | 73 +- agent/modals/document/document_move.php | 94 +- agent/modals/document/document_rename.php | 36 +- .../modals/document/document_version_view.php | 74 +- agent/modals/document/document_view.php | 79 +- agent/modals/file/file.php | 139 ++ agent/modals/file/file_bulk_move.php | 138 +- agent/modals/file/file_delete.php | 60 +- agent/modals/file/file_link_asset.php | 78 +- agent/modals/file/file_move.php | 121 +- agent/modals/file/file_rename.php | 63 +- agent/modals/file/file_upload.php | 176 +- agent/modals/file/file_view.php | 59 +- agent/modals/folder/folder_add.php | 30 +- agent/modals/folder/folder_delete.php | 55 + agent/modals/folder/folder_rename.php | 41 +- agent/modals/link/link.php | 158 ++ agent/post/asset.php | 62 - agent/post/contact.php | 65 - agent/post/document.php | 890 ++------ agent/post/document_model.php | 12 +- agent/post/file.php | 676 ++---- agent/post/folder.php | 92 +- agent/post/link.php | 82 + css/itflow_custom.css | 460 ++++ functions.php | 1 + functions/files.php | 221 ++ functions/links.php | 331 +++ 50 files changed, 3745 insertions(+), 4452 deletions(-) delete mode 100644 agent/js/file_delete_modal.js create mode 100644 agent/js/files.js delete mode 100644 agent/modals/document/document_add_file_relation.php delete mode 100644 agent/modals/document/document_bulk_move.php create mode 100644 agent/modals/document/document_form.php create mode 100644 agent/modals/file/file.php create mode 100644 agent/modals/folder/folder_delete.php create mode 100644 agent/modals/link/link.php create mode 100644 agent/post/link.php create mode 100644 functions/links.php diff --git a/agent/ajax.php b/agent/ajax.php index 8eb50f044..282f55248 100644 --- a/agent/ajax.php +++ b/agent/ajax.php @@ -900,7 +900,8 @@ if (isset($_GET['get_canned_response'])) { * and agent/document.php do it. */ if (isset($_GET['get_document_content'])) { - enforceUserPermission('module_support'); + // Whoever can open the files list can preview its documents in the grid + enforceUserPermission('module_client'); $document_id = intval($_GET['get_document_content']); @@ -917,15 +918,8 @@ if (isset($_GET['get_document_content'])) { $client_id = intval($document_row['document_client_id']); enforceClientAccess(); - require_once "../libs/htmlpurifier/HTMLPurifier.standalone.php"; - - $document_purifier_config = HTMLPurifier_Config::createDefault(); - $document_purifier_config->set('Cache.DefinitionImpl', null); - $document_purifier_config->set('URI.AllowedSchemes', ['data' => true, 'src' => true, 'http' => true, 'https' => true]); - $document_purifier = new HTMLPurifier($document_purifier_config); - $response['name'] = $document_row['document_name']; - $response['content'] = $document_purifier->purify($document_row['document_content']); + $response['content'] = documentHtmlPurify($document_row['document_content']); } else { $response['name'] = ''; $response['content'] = ''; diff --git a/agent/asset.php b/agent/asset.php index 386e30821..ca0d782a2 100644 --- a/agent/asset.php +++ b/agent/asset.php @@ -356,7 +356,7 @@ if (mysqli_num_rows($sql) == 0) { Interface - + Document @@ -1039,7 +1039,7 @@ if (mysqli_num_rows($sql) == 0) { - @@ -1066,8 +1066,13 @@ if (mysqli_num_rows($sql) == 0) { ?> - -
+
+ +
+ +
+
+
@@ -1103,13 +1108,6 @@ if (mysqli_num_rows($sql) == 0) { 'file-pdf', 'gz' => 'file-archive', 'tar' => 'file-archive', 'zip' => 'file-archive', '7z' => 'file-archive', 'rar' => 'file-archive', - 'txt' => 'file-alt', 'md' => 'file-alt', 'msg' => 'envelope', 'doc' => 'file-word', 'docx' => 'file-word', 'odt' => 'file-word', - 'xls' => 'file-excel', 'xlsx' => 'file-excel', 'ods' => 'file-excel', 'csv' => 'file-csv', 'pptx' => 'file-powerpoint', 'odp' => 'file-powerpoint', - 'mp3' => 'file-audio', 'wav' => 'file-audio', 'ogg' => 'file-audio', 'mov' => 'file-video', 'mp4' => 'file-video', 'av1' => 'file-video', - 'jpg' => 'file-image', 'jpeg' => 'file-image', 'png' => 'file-image', 'gif' => 'file-image', 'webp' => 'file-image', 'bmp' => 'file-image', 'tif' => 'file-image', - ]; ?>
@@ -1125,13 +1123,17 @@ if (mysqli_num_rows($sql) == 0) { $file_id = intval($row['file_id']); $file_name = escapeHtml($row['file_name']); $file_description = escapeHtml($row['file_description']); - $file_icon = $file_icons[strtolower((string) $row['file_ext'])] ?? 'file'; $file_created_at = escapeHtml($row['file_created_at']); ?>
- -
+
+ +
+ +
+
+
diff --git a/agent/contact.php b/agent/contact.php index 6582de976..e0429da57 100644 --- a/agent/contact.php +++ b/agent/contact.php @@ -291,7 +291,7 @@ if (!$can_support) { - + Document @@ -767,7 +767,7 @@ if (!$can_support) { Link - @@ -780,7 +780,7 @@ if (!$can_support) { $document_id = intval($row['document_id']); $document_last = $row['document_updated_at'] ?: $row['document_created_at']; ?>
- +
@@ -820,9 +820,9 @@ if (!$can_support) {
- +
- +
· diff --git a/agent/document.php b/agent/document.php index a5652b599..7f091b76a 100644 --- a/agent/document.php +++ b/agent/document.php @@ -1,427 +1,236 @@ set('Cache.DefinitionImpl', null); // Disable cache by setting a non-existent directory or an invalid one -$purifier_config->set('URI.AllowedSchemes', ['data' => true, 'src' => true, 'http' => true, 'https' => true]); -$purifier = new HTMLPurifier($purifier_config); - -if (isset($_GET['document_id'])) { - $document_id = intval($_GET['document_id']); -} - -$folder_location = 0; - -$sql_document = mysqli_query($mysqli, "SELECT document_archived_at, document_client_visible, document_content, document_created_at, - document_created_by, document_description, document_folder_id, document_name, - document_updated_at, folder_name, user_name FROM documents - LEFT JOIN folders ON document_folder_id = folder_id - LEFT JOIN users ON document_created_by = user_id +$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_archived_at, document_client_visible, document_content, document_created_at, + document_description, document_folder_id, document_name, document_updated_at, + creator.user_name AS created_by_name, updater.user_name AS updated_by_name + FROM documents + LEFT JOIN users creator ON document_created_by = creator.user_id + LEFT JOIN users updater ON document_updated_by = updater.user_id WHERE document_client_id = $client_id AND document_id = $document_id - LIMIT 1" -); + LIMIT 1")); -if (mysqli_num_rows($sql_document) == 0) { - echo "

Nothing to see here

Go Back
"; +if (!$row) { + echo "

Nothing to see here

Back to files
"; require_once "../includes/footer.php"; exit(); } -$row = mysqli_fetch_assoc($sql_document); +$can_edit = lookupUserPermission('module_support') >= 2; +$can_delete = lookupUserPermission('module_support') >= 3; -$folder_name = escapeHtml($row['folder_name']); $document_name = escapeHtml($row['document_name']); $document_description = escapeHtml($row['document_description']); -$document_content = $purifier->purify($row['document_content']); -$document_created_by_id = intval($row['document_created_by']); -$document_created_by_name = escapeHtml($row['user_name']); -$document_created_at = escapeHtml($row['document_created_at']); -$document_updated_at = escapeHtml($row['document_updated_at']); -$document_archived_at = escapeHtml($row['document_archived_at']); -$document_folder_id = intval($row['document_folder_id']); -$document_client_visible = intval($row['document_client_visible']); +$document_archived = !empty($row['document_archived_at']); +$document_visible = intval($row['document_client_visible']); +$document_updated_at = $row['document_updated_at'] ?: $row['document_created_at']; +$document_updated_by = escapeHtml($row['updated_by_name'] ?: $row['created_by_name']); -// Override Tab Title // No Sanitizing needed as this var will opnly be used in the tab title +$folder_tree = folderTree($client_id); +$document_folder_id = isset($folder_tree[intval($row['document_folder_id'])]) ? intval($row['document_folder_id']) : 0; +$folder_path = folderPath($folder_tree, $document_folder_id); + +$versions = []; +$sql_versions = mysqli_query($mysqli, "SELECT document_version_id, document_version_name, document_version_description, document_version_created_at, user_name + FROM document_versions LEFT JOIN users ON document_version_created_by = user_id + WHERE document_version_document_id = $document_id + ORDER BY document_version_created_at DESC, document_version_id DESC"); +while ($version = mysqli_fetch_assoc($sql_versions)) { + $versions[] = $version; +} + +$csrf = $_SESSION['csrf_token']; + +// Tab title - used raw, the header escapes it $page_title = $row['document_name']; ?> + +
+
+ +
+

+ + Archived + Portal +

+ +
+ +
+ Updated + · Created + · earlier version +
+
+
+ + + + + + + PDF + + + + +
+
+
+
-
-
-
-
-
-
- -
- -
-
-
-
- Date: - -
- -
- Prepared By: - -
- -
-
-
+ +
+
+
+ This document is empty.' . ($can_edit && !$document_archived ? ' Start writing' : '') . '

' ?>
-
- -
-

Documentation Revision History

+
+ + +
+

Revision history

- - - - - - - + - + $version) { ?> - - - - - + + + + + - +
VersionDateNameDescriptionAuthor
VersionDateNameSummaryAuthor
-
+
-
-
-
- - - - - - +
+ + +
+
+

Linked

+ + Link + +
+
+
-
-
Related Items
-
- Files - -
- -
- - - - + +
+
+
+ + + + + + +
- -
- Contacts - -
- -
- - - - -
- -
- Assets - -
- - - -
- Licenses - -
- -
- - - - -
- -
- Vendors - -
- - - + +
+ + + + + + + +
+ +
- -
-
Portal Collaboration
- + +
+
+

History

- - -
-
Revisions
- -
"> - - - - - - - -
- +
    +
  • +
    Now
    +
    Updated
    +
  • + +
  • +
    + + + +
    + + · was "" +
    +
    + + + +
  • + + +
  • No earlier versions - each edit keeps the one before it here.
  • + +
diff --git a/agent/files.php b/agent/files.php index 2971c868f..6ff2e6968 100644 --- a/agent/files.php +++ b/agent/files.php @@ -1,1395 +1,687 @@ = 2; +$can_delete = lookupUserPermission('module_support') >= 3; + +$folder_tree = folderTree($client_id); + +// The folder on show - one of this client's, or the top level +$folder_id = intval($_GET['folder_id'] ?? 0); +if (!isset($folder_tree[$folder_id])) { $folder_id = 0; } +$folder_path = folderPath($folder_tree, $folder_id); +$folder_name = $folder_id ? escapeHtml($folder_tree[$folder_id]['folder_name']) : 'Top level'; -// Folder ID (used in forms/etc) -$get_folder_id = $folder_id; - -/* - * Type filter -- '' all, 'file' uploads only, 'document' created documents only. - * - * Whitelisted rather than trusted: it decides which of the two queries below run - * at all, so an unexpected value should mean "everything", not an empty page. - * - * Like the view mode, it rides along on every folder link - a filter that - * silently clears itself the moment you open a folder is worse than no filter. - */ +// '' everything, 'file' uploads only, 'document' created documents only $type_filter = $_GET['type'] ?? ''; if (!in_array($type_filter, ['file', 'document'], true)) { $type_filter = ''; } -/* - * Links for the filter buttons. Built from a copy with 'type' removed so the - * new value does not land next to the old one, and with 'page' removed because - * switching filter while on page 4 of the files would otherwise open page 4 of - * a shorter list and show nothing. - */ -$type_get_copy = $_GET; -unset($type_get_copy['type'], $type_get_copy['page'], $type_get_copy['sort'], $type_get_copy['order']); -$url_query_strings_type = http_build_query($type_get_copy); -if ($url_query_strings_type !== '') { - $url_query_strings_type .= '&'; -} +// 0 list, 1 grid +$view = intval($_GET['view'] ?? 0) === 1 ? 1 : 0; -/* - * View Mode -- 0 List, 1 Grid - * - * Carried on every folder link below. Without it, opening a folder dropped the - * parameter and the page fell back to list view, so picking grid only lasted - * until the next click. - */ -if (!empty($_GET['view'])) { - $view = intval($_GET['view']); -} else { - $view = 0; -} +// Whitelisted - anything else sorts by name +$sort = in_array($_GET['sort'] ?? '', ['name', 'type', 'size', 'updated'], true) ? $_GET['sort'] : 'name'; +$order = ($_GET['order'] ?? '') === 'DESC' ? 'DESC' : 'ASC'; -// Folder tree expanded state: 1 = expand all, 0 = collapsed (default) -if (isset($_GET['folders_expanded'])) { - $folders_expanded = intval($_GET['folders_expanded']); -} else { - $folders_expanded = 0; -} +// A search at the top level looks in every folder; inside a folder, just that one +$search = trim((string) ($_GET['q'] ?? '')); +$search_everywhere = $search !== '' && $folder_id === 0; -if (!isset($q)) { - $q = ''; -} - -// --------------------------------------------- -// Breadcrumbs: build full folder path -// --------------------------------------------- -$folder_path = []; -$breadcrumb_folder_id = $get_folder_id; - -while ($breadcrumb_folder_id > 0) { - $sql_folder = mysqli_query($mysqli, "SELECT folder_name, parent_folder FROM folders WHERE folder_id = $breadcrumb_folder_id AND folder_client_id = $client_id"); - if ($row_folder = mysqli_fetch_assoc($sql_folder)) { - $folder_name = escapeHtml($row_folder['folder_name']); - $parent_folder = intval($row_folder['parent_folder']); - - array_unshift($folder_path, [ - 'folder_id' => $breadcrumb_folder_id, - 'folder_name' => $folder_name - ]); - - $breadcrumb_folder_id = $parent_folder; - } else { - break; - } -} - -// --------------------------------------------- -// Helper: unified folder tree (no folder_location) -// --------------------------------------------- -function isAncestorFolder($folder_id, $current_folder_id, $client_id) { - global $mysqli; - - if ($current_folder_id == 0) { - return false; - } - if ($current_folder_id == $folder_id) { - return true; - } - - $result = mysqli_query($mysqli, "SELECT parent_folder FROM folders WHERE folder_id = $current_folder_id AND folder_client_id = $client_id"); - if ($row = mysqli_fetch_assoc($result)) { - $parent_folder_id = intval($row['parent_folder']); - return isAncestorFolder($folder_id, $parent_folder_id, $client_id); - } else { - return false; - } -} - -function displayFolders($parent_folder_id, $client_id, $indent = 0, $render_root = false) { - global $mysqli, $get_folder_id, $session_user_role, $archive_query, $archived, $num_root_items, $folders_expanded, $view, $type_filter; - - // Always render root (only once) - if ($parent_folder_id == 0 && $indent == 0) { - echo ''; } + return '?' . http_build_query($params); +} - $sql_folders = mysqli_query( - $mysqli, - "SELECT folder_id, folder_name FROM folders - WHERE parent_folder = $parent_folder_id - AND folder_client_id = $client_id - ORDER BY folder_name ASC" - ); - - while ($row = mysqli_fetch_assoc($sql_folders)) { - $folder_id = intval($row['folder_id']); - $folder_name = escapeHtml($row['folder_name']); - - $row_files = mysqli_fetch_assoc(mysqli_query( - $mysqli, - "SELECT COUNT('file_id') AS num - FROM files - WHERE file_folder_id = $folder_id - AND file_client_id = $client_id - AND file_$archive_query" - )); - $num_files = intval($row_files['num']); - - $row_docs = mysqli_fetch_assoc(mysqli_query( - $mysqli, - "SELECT COUNT('document_id') AS num - FROM documents - WHERE document_folder_id = $folder_id - AND document_client_id = $client_id - AND document_$archive_query" - )); - $num_docs = intval($row_docs['num']); - - $num_total = $num_files + $num_docs; - - $subfolder_result = mysqli_query( - $mysqli, - "SELECT COUNT(*) AS count - FROM folders - WHERE parent_folder = $folder_id - AND folder_client_id = $client_id" - ); - $subfolder_count = intval(mysqli_fetch_assoc($subfolder_result)['count']); - - // Active or ancestor of active folder = on active path - $on_active_path = ($get_folder_id == $folder_id) || isAncestorFolder($folder_id, $get_folder_id, $client_id); - - // Option C: indent with padding (no AdminLTE sidebar CSS required) - // Tune these numbers if you want tighter/looser indent - $indent_px = 12 * $indent; // 12px per level - - echo ''; +// ---- Folder counts: one grouped query each, not one per folder ---- +$folder_counts = []; +foreach ([['files', 'file'], ['documents', 'document']] as [$table, $prefix]) { + $sql = mysqli_query($mysqli, "SELECT {$prefix}_folder_id AS folder_id, COUNT(*) AS num FROM $table + WHERE {$prefix}_client_id = $client_id AND {$prefix}_$archive_query GROUP BY {$prefix}_folder_id"); + while ($row = mysqli_fetch_assoc($sql)) { + $count_folder = isset($folder_tree[intval($row['folder_id'])]) ? intval($row['folder_id']) : 0; + $folder_counts[$count_folder] = ($folder_counts[$count_folder] ?? 0) + intval($row['num']); } } - -// --------------------------------------------- -// DATA LOAD -// view=1 (grid) - every file in the folder, thumbnail or file-type icon -// view=0 (list) loads ALL files+documents, merges, sorts in PHP -// --------------------------------------------- +// ---- What is on show: files and documents, merged and sorted here ---- +$search_sql = escapeSql($search); +$file_where = "file_client_id = $client_id AND file_$archive_query" . ($search_everywhere ? '' : " AND file_folder_id = $folder_id"); +$document_where = "document_client_id = $client_id AND document_$archive_query" . ($search_everywhere ? '' : " AND document_folder_id = $folder_id"); +if ($search !== '') { + $file_where .= " AND (file_name LIKE '%$search_sql%' OR file_ext LIKE '%$search_sql%' OR file_description LIKE '%$search_sql%')"; + $document_where .= " AND (MATCH(document_content_raw) AGAINST ('$search_sql') OR document_name LIKE '%$search_sql%' OR document_description LIKE '%$search_sql%')"; +} $items = []; -$num_rows = [0]; +$type_counts = ['file' => 0, 'document' => 0]; -// Both views decide previewability from this, so it is set before the branch - -// it used to be assigned inside the grid arm only, which left the list arm -// passing null to in_array() -$inline_viewable_mime_types = getInlineViewableMimeTypes(); - -if ($view == 1) { - - /* - * GRID VIEW: files AND documents, same as the list view beside it. - * - * This used to be a single files-only query with SQL_CALC_FOUND_ROWS and a - * LIMIT. Two sources cannot be paginated by one LIMIT, so it now mirrors - * exactly what the list view does: fetch both unpaginated, merge, sort by - * name, and slice in PHP. That also fixes the grid quietly omitting every - * created document - they were only ever visible in list view. - */ - $safe_q = mysqli_real_escape_string($mysqli, $q); - - if ($get_folder_id == 0 && isset($_GET["q"])) { - $file_folder_snippet = ""; // search across all folders - $doc_folder_snippet = ""; - } else { - $file_folder_snippet = "AND file_folder_id = $folder_id"; - $doc_folder_snippet = "AND document_folder_id = $folder_id"; +$sql_files = mysqli_query($mysqli, "SELECT file_id, file_name, file_description, file_reference_name, file_ext, file_size, file_mime_type, + file_created_at, file_updated_at, file_archived_at, file_folder_id, user_name + FROM files LEFT JOIN users ON file_created_by = user_id + WHERE $file_where"); +while ($row = mysqli_fetch_assoc($sql_files)) { + $type_counts['file']++; + if ($type_filter === 'document') { + continue; } - - $file_search_snippet = ""; - $doc_search_snippet = ""; - if (!empty($q)) { - $file_search_snippet = "AND (file_name LIKE '%$safe_q%' OR file_ext LIKE '%$safe_q%' OR file_description LIKE '%$safe_q%')"; - $doc_search_snippet = "AND (MATCH(document_content_raw) AGAINST ('$safe_q') OR document_name LIKE '%$safe_q%')"; - } - - // Skipped outright when the type filter excludes them - $sql_grid_files = false; - if ($type_filter !== 'document') { - $sql_grid_files = mysqli_query( - $mysqli, - "SELECT files.*, users.user_name - FROM files - LEFT JOIN users ON file_created_by = user_id - WHERE file_client_id = $client_id - AND file_$archive_query - $file_folder_snippet - $file_search_snippet" - ); - } - - $sql_grid_documents = false; - if ($type_filter !== 'file') { - $sql_grid_documents = mysqli_query( - $mysqli, - "SELECT documents.*, users.user_name - FROM documents - LEFT JOIN users ON document_created_by = user_id - WHERE document_client_id = $client_id - AND document_$archive_query - $doc_folder_snippet - $doc_search_snippet" - ); - } - - while ($sql_grid_files && ($row = mysqli_fetch_assoc($sql_grid_files))) { - $row['grid_kind'] = 'file'; - $items[] = $row; - } - - while ($sql_grid_documents && ($row = mysqli_fetch_assoc($sql_grid_documents))) { - $row['grid_kind'] = 'document'; - $items[] = $row; - } - - usort($items, function ($a, $b) { - $a_name = strtolower($a['grid_kind'] === 'file' ? $a['file_name'] : $a['document_name']); - $b_name = strtolower($b['grid_kind'] === 'file' ? $b['file_name'] : $b['document_name']); - return strcmp($a_name, $b_name); - }); - - $num_rows = [count($items)]; - $items = array_slice($items, $record_from, $record_to); - -} else { - - // -------- LIST VIEW: build unified items[] -------- - - // Folder filter - if ($get_folder_id == 0 && isset($_GET["q"])) { - $file_folder_snippet = ""; // search across all folders - $doc_folder_snippet = ""; - } else { - $file_folder_snippet = "AND file_folder_id = $folder_id"; - $doc_folder_snippet = "AND document_folder_id = $folder_id"; - } - - // Search filters - $safe_q = mysqli_real_escape_string($mysqli, $q); - - $file_search_snippet = ""; - if (!empty($q)) { - $file_search_snippet = "AND (file_name LIKE '%$safe_q%' OR file_ext LIKE '%$safe_q%' OR file_description LIKE '%$safe_q%')"; - } - - $doc_search_snippet = ""; - if (!empty($q)) { - $doc_search_snippet = "AND (MATCH(document_content_raw) AGAINST ('$safe_q') OR document_name LIKE '%$safe_q%')"; - } - - // Files query (NO limit - we'll paginate in PHP). Skipped outright when the - // type filter excludes files, rather than fetching rows to throw away. - $sql_files = false; - if ($type_filter !== 'document') { - $sql_files = mysqli_query( - $mysqli, - "SELECT files.*, users.user_name - FROM files - LEFT JOIN users ON file_created_by = user_id - WHERE file_client_id = $client_id - AND file_$archive_query - $file_folder_snippet - $file_search_snippet" - ); - } - - // Documents query (NO limit - paginate in PHP) - $sql_documents = false; - if ($type_filter !== 'file') { - $sql_documents = mysqli_query( - $mysqli, - "SELECT documents.*, users.user_name - FROM documents - LEFT JOIN users ON document_created_by = user_id - WHERE document_client_id = $client_id - AND document_$archive_query - $doc_folder_snippet - $doc_search_snippet" - ); - } - - // Normalize FILES into $items - while ($sql_files && ($row = mysqli_fetch_assoc($sql_files))) { - $file_id = intval($row['file_id']); - $file_name = escapeHtml($row['file_name']); - $file_description = escapeHtml($row['file_description']); - $file_reference_name= escapeHtml($row['file_reference_name']); - $file_ext = escapeHtml($row['file_ext']); - $file_size = intval($row['file_size']); - $file_mime_type = escapeHtml($row['file_mime_type']); - $file_uploaded_by = escapeHtml($row['user_name']); - $file_created_at = escapeHtml($row['file_created_at']); - $file_archived_at = $row['file_archived_at']; - - // determine icon - if ($file_ext == 'pdf') { - $file_icon = "file-pdf"; - } elseif (in_array($file_ext, ['gz','tar','zip','7z','rar'])) { - $file_icon = "file-archive"; - } elseif (in_array($file_ext, ['txt','md'])) { - $file_icon = "file-alt"; - } elseif ($file_ext == 'msg') { - $file_icon = "envelope"; - } elseif (in_array($file_ext, ['doc','docx','odt'])) { - $file_icon = "file-word"; - } elseif (in_array($file_ext, ['xls','xlsx','ods'])) { - $file_icon = "file-excel"; - } elseif (in_array($file_ext, ['pptx','odp'])) { - $file_icon = "file-powerpoint"; - } elseif (in_array($file_ext, ['mp3','wav','ogg'])) { - $file_icon = "file-audio"; - } elseif (in_array($file_ext, ['mov','mp4','av1'])) { - $file_icon = "file-video"; - } elseif (in_array($file_ext, ['jpg','jpeg','png','gif','webp','bmp','tif'])) { - $file_icon = "file-image"; - } else { - $file_icon = "file"; - } - - $items[] = [ - 'kind' => 'file', - 'id' => $file_id, - 'name' => $file_name, - 'description' => $file_description, - 'reference_name' => $file_reference_name, - 'icon' => $file_icon, - 'ext' => $file_ext, - 'mime' => $file_mime_type, - 'size' => $file_size, - 'created_at' => $file_created_at, - 'created_by' => $file_uploaded_by, - 'archived_at' => $file_archived_at, - ]; - } - - // Normalize DOCUMENTS into $items - while ($sql_documents && ($row = mysqli_fetch_assoc($sql_documents))) { - $document_id = intval($row['document_id']); - $document_name = escapeHtml($row['document_name']); - $document_description = escapeHtml($row['document_description']); - $document_created_by_name = escapeHtml($row['user_name']); - $document_created_at = $row['document_created_at']; - $document_updated_at = $row['document_updated_at']; - $document_archived_at = $row['document_archived_at']; - - $items[] = [ - 'kind' => 'document', - 'id' => $document_id, - 'name' => $document_name, - 'description' => $document_description, - 'mime' => 'Document', - 'size' => null, - 'updated_at' => $document_updated_at, - 'created_by' => $document_created_by_name, - 'archived_at' => $document_archived_at, - ]; - } - - // Sort combined items - $sort = isset($_GET['sort']) ? $_GET['sort'] : 'name'; - $order = isset($_GET['order']) ? $_GET['order'] : 'ASC'; - - usort($items, function($a, $b) use ($sort, $order) { - $direction = ($order === 'DESC') ? -1 : 1; - - if ($sort == 'created') { - $valA = strtotime($a['created_at']); - $valB = strtotime($b['created_at']); - } elseif ($sort == 'type') { - $valA = strtolower($a['mime']); - $valB = strtolower($b['mime']); - } elseif ($sort == 'size') { - $valA = (int)($a['size'] ?? 0); - $valB = (int)($b['size'] ?? 0); - } else { - // default: name - $valA = strtolower($a['name']); - $valB = strtolower($b['name']); - } - - if ($valA == $valB) { - return 0; - } - - return ($valA < $valB) ? -1 * $direction : 1 * $direction; - }); - - // Total items (for pagination footer) - $total_items = count($items); - $num_rows = [$total_items]; - - // Apply pagination slice - $items = array_slice($items, $record_from, $record_to); + $info = fileTypeInfo($row['file_ext']); + $items[] = [ + 'kind' => 'file', + 'id' => intval($row['file_id']), + 'name' => $row['file_name'], + 'description' => $row['file_description'], + 'type' => $info['label'], + 'size' => intval($row['file_size']), + 'updated' => $row['file_updated_at'] ?: $row['file_created_at'], + 'by' => $row['user_name'], + 'folder_id' => intval($row['file_folder_id']), + 'archived' => $row['file_archived_at'], + 'row' => $row, + ]; } -// --------------------------------------------- -// Root folder count (for "/" badge) -// --------------------------------------------- -$row_root_files = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT COUNT('file_id') AS num FROM files WHERE file_folder_id = 0 AND file_client_id = $client_id AND file_$archive_query")); -$row_root_docs = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT COUNT('document_id') AS num FROM documents WHERE document_folder_id = 0 AND document_client_id = $client_id AND document_$archive_query")); -$num_root_items = intval($row_root_files['num']) + intval($row_root_docs['num']); +$sql_documents = mysqli_query($mysqli, "SELECT document_id, document_name, document_description, document_client_visible, + document_created_at, document_updated_at, document_archived_at, document_folder_id, + " . ($view === 1 ? 'document_content,' : '') . " COALESCE(updater.user_name, creator.user_name) AS user_name + FROM documents + LEFT JOIN users creator ON document_created_by = creator.user_id + LEFT JOIN users updater ON document_updated_by = updater.user_id + WHERE $document_where"); +while ($row = mysqli_fetch_assoc($sql_documents)) { + $type_counts['document']++; + if ($type_filter === 'file') { + continue; + } + $items[] = [ + 'kind' => 'document', + 'id' => intval($row['document_id']), + 'name' => $row['document_name'], + 'description' => $row['document_description'], + 'type' => 'Document', + 'size' => -1, + 'updated' => $row['document_updated_at'] ?: $row['document_created_at'], + 'by' => $row['user_name'], + 'folder_id' => intval($row['document_folder_id']), + 'archived' => $row['document_archived_at'], + 'row' => $row, + ]; +} + +usort($items, function ($a, $b) use ($sort, $order) { + if ($sort === 'type') { + $result = strcasecmp($a['type'], $b['type']); + } elseif ($sort === 'size') { + $result = $a['size'] <=> $b['size']; + } elseif ($sort === 'updated') { + $result = strcmp((string) $a['updated'], (string) $b['updated']); + } else { + $result = 0; + } + // Name breaks every tie, so the order never shuffles between page loads + $result = $result ?: strnatcasecmp($a['name'], $b['name']); + return $order === 'DESC' ? -$result : $result; +}); + +$num_rows = [count($items)]; +$items = array_slice($items, $record_from, $record_to); + +// ---- For the page on show only: shares and link counts, batched ---- +$page_ids = ['file' => [], 'document' => []]; +foreach ($items as $item) { + $page_ids[$item['kind']][] = $item['id']; +} +$shared = []; +foreach (['file' => 'File', 'document' => 'Document'] as $kind => $share_type) { + if (!$page_ids[$kind]) { + continue; + } + $sql = mysqli_query($mysqli, "SELECT item_related_id, item_recipient, item_expire_at FROM shared_items + WHERE item_client_id = $client_id AND item_type = '$share_type' AND item_active = 1 + AND (COALESCE(item_view_limit, 0) = 0 OR item_views < item_view_limit) AND item_expire_at > NOW() + AND item_related_id IN (" . implode(',', $page_ids[$kind]) . ")"); + while ($row = mysqli_fetch_assoc($sql)) { + $shared[$kind][intval($row['item_related_id'])] = $row; + } +} +$link_counts = []; +foreach (['file', 'document'] as $kind) { + if (!$page_ids[$kind]) { + continue; + } + $ids = implode(',', $page_ids[$kind]); + foreach (linkTypesFor($kind) as $to) { + $relation = linkRelation($kind, $to); + $to_entity = LINK_ENTITIES[$to]; + $sql = mysqli_query($mysqli, "SELECT l.{$kind}_id AS id, COUNT(*) AS num FROM {$relation['table']} l + JOIN {$to_entity['table']} t ON t.{$to_entity['id']} = l.{$to_entity['id']} AND t.{$to_entity['archived']} IS NULL + WHERE l.{$kind}_id IN ($ids) GROUP BY l.{$kind}_id"); + while ($row = mysqli_fetch_assoc($sql)) { + $link_counts[$kind][intval($row['id'])][$to] = intval($row['num']); + } + } +} + +/** The linked-record chips for one row: an icon and a count per kind. */ +function filesLinkChips($kind, $id) { + global $link_counts; + $html = ''; + foreach ($link_counts[$kind][$id] ?? [] as $to => $num) { + $entity = LINK_ENTITIES[$to]; + $html .= '' . $num . ''; + } + return $html; +} + +/** The โ‹ฎ menu for one file or document - the same in the list and the grid. */ +function filesItemMenu($item) { + global $client_id, $can_edit, $can_delete; + $id = $item['id']; + $csrf = $_SESSION['csrf_token']; + $archived = !empty($item['archived']); + $html = ''; + if ($item['kind'] === 'file') { + $html .= "Details"; + $html .= "Download"; + if (!$archived) { + $html .= "Share"; + } + if ($can_edit && !$archived) { + $html .= ''; + $html .= "Rename"; + $html .= "Move"; + $html .= "Link"; + } + $archive_name = 'file'; + } else { + $html .= "Open"; + $html .= "Quick view"; + if (!$archived) { + $html .= "Share"; + } + if ($can_edit && !$archived) { + $html .= ''; + $html .= "Edit"; + $html .= "Rename"; + $html .= "Move"; + $html .= "Link"; + } + $archive_name = 'document'; + } + if ($can_edit) { + $html .= ''; + if ($archived) { + $html .= "Restore"; + if ($can_delete) { + $html .= $item['kind'] === 'file' + ? "Delete" + : "Delete"; + } + } else { + $html .= "Archive"; + } + } + return $html; +} + +/** "in Folder / Sub" for a search result outside the folder on show. */ +function filesFolderNote($item) { + global $folder_tree, $folder_id, $client_id; + if ($item['folder_id'] === $folder_id || !isset($folder_tree[$item['folder_id']])) { + return ''; + } + $path = array_column(folderPath($folder_tree, $item['folder_id']), 'folder_name'); + return '' . escapeHtml($path ? implode(' / ', $path) : 'Top level') . ''; +} + +/** One folder and its subfolders in the tree. */ +function filesFolderTree($parent_id, $depth = 0) { + global $folder_tree, $folder_id, $folder_counts, $folder_path, $can_edit, $can_delete, $client_id; + $open_path = array_column($folder_path, 'folder_id'); + $html = ''; + foreach ($folder_tree[$parent_id]['children'] as $id) { + $folder = $folder_tree[$id]; + $has_children = (bool) $folder['children']; + $is_open = in_array($id, $open_path, true); + $count = $folder_counts[$id] ?? 0; + $name = escapeHtml($folder['folder_name']); + $menu = ''; + if ($can_edit) { + $menu = ''; + } + $html .= '
  • ' + . '
    ' + . ($has_children ? '' : '') + . '' . $name . '' + . ($count ? '' . $count . '' : '') . '' + . $menu . '
    ' + . ($has_children ? '
      ' . filesFolderTree($id, $depth + 1) . '
    ' : '') + . '
  • '; + } + return $html; +} + +$empty_state_skip = true; +$upload_url = "modals/file/file_upload.php?client_id=$client_id&folder_id=$folder_id"; +$search_label = $search_everywhere || $folder_id === 0 ? 'all folders' : $folder_name; +$preview = []; // the grid's previewer payload ?> -
    - -
    +
    +

    Files

    - -
    -
    + + - -
    -
    -

    Folders

    - - - - - - +
    +
    + + + + + +
    +
    +
    + + +
    -
    -
    +
    +
    - +
    + + +
    +
    + +
    +
    +
    + Folders + + + + + +
    +
    +
    - -
    +
    > - -
    - - - - - - -
    -
    -
    - - -
    -
    -
    -
    - - - -
    - -
    -
    -
    -
    -
    - - -
    + - + '; + action.appendChild(document.createTextNode(item.action)); + var details = el('modalDetails'); + details.classList.toggle('d-none', !item.details); + details.dataset.modalUrl = item.details || ''; + + // Blank the last one first, or a heavy PDF keeps rendering behind the next image + [image, frame].forEach(function (node) { + node.classList.add('d-none'); + node.removeAttribute('src'); + }); + [fallback, text, doc].forEach(function (node) { + node.classList.add('d-none'); + }); + doc.innerHTML = ''; + + if (item.kind === 'document') { + var requested = current; + doc.innerHTML = '

    Loading...

    '; + doc.classList.remove('d-none'); + fetch(item.preview, { headers: { 'X-Requested-With': 'XMLHttpRequest' }, credentials: 'same-origin' }) + .then(function (r) { return r.json(); }) + .then(function (data) { + if (requested !== current) { + return; + } + // HTMLPurifier output - the same the document page renders + doc.innerHTML = data.content || '

    This document is empty.

    '; + if (typeof prettyContent === 'function') { + prettyContent(); + } + }) + .catch(function () { + if (requested === current) { + doc.innerHTML = '

    Could not load this document.

    '; + } + }); + } else if (item.kind === 'image') { + image.src = item.preview; + image.alt = item.name; + image.classList.remove('d-none'); + } else if (item.kind === 'pdf' || item.kind === 'text') { + frame.src = item.preview; + frame.classList.remove('d-none'); + } else { + el('modalFallbackIcon').className = 'fas fa-' + item.icon + ' fa-4x text-secondary'; + fallback.classList.remove('d-none'); + } + + var single = items.length < 2; + el('modalPrev').classList.toggle('d-none', single); + el('modalNext').classList.toggle('d-none', single); + } + + function step(by) { + current = (current + by + items.length) % items.length; + draw(); + } + + if (modal && items.length) { + document.addEventListener('click', function (e) { + var tile = e.target.closest('[data-files-preview]'); + if (!tile) { + return; + } + e.preventDefault(); + current = parseInt(tile.dataset.filesPreview, 10) || 0; + draw(); + bootstrap.Modal.getOrCreateInstance(modal).show(); + }); + el('modalPrev').addEventListener('click', function () { step(-1); }); + el('modalNext').addEventListener('click', function () { step(1); }); + document.addEventListener('keydown', function (e) { + if (!modal.classList.contains('show') || items.length < 2) { + return; + } + if (e.key === 'ArrowRight') { + step(1); + } else if (e.key === 'ArrowLeft') { + step(-1); + } + }); + // Details is a quick look - close the previewer first so the two never stack + el('modalDetails').addEventListener('click', function () { + var url = this.dataset.modalUrl; + modal.addEventListener('hidden.bs.modal', function open() { + modal.removeEventListener('hidden.bs.modal', open); + var trigger = document.createElement('a'); + trigger.className = 'ajax-modal d-none'; + trigger.href = '#'; + trigger.dataset.modalUrl = url; + trigger.dataset.modalSize = 'lg'; + document.body.appendChild(trigger); + trigger.click(); + trigger.remove(); + }); + bootstrap.Modal.getInstance(modal).hide(); + }); + } + + // ---- Drop files on the list to upload them into the folder on show ---- + + var main = document.querySelector('[data-files-drop]'); + var overlay = document.querySelector('.itflow-files-dropzone'); + if (!main || !overlay) { + return; + } + var depth = 0; + + function carriesFiles(e) { + return e.dataTransfer && Array.prototype.indexOf.call(e.dataTransfer.types || [], 'Files') !== -1; + } + + main.addEventListener('dragenter', function (e) { + if (!carriesFiles(e)) { + return; + } + depth++; + overlay.hidden = false; + var box = main.getBoundingClientRect(), card = main.closest('.card').getBoundingClientRect(); + overlay.style.cssText = 'top:' + (box.top - card.top) + 'px;left:' + (box.left - card.left) + 'px;width:' + box.width + 'px;height:' + box.height + 'px'; + }); + main.addEventListener('dragover', function (e) { + if (carriesFiles(e)) { + e.preventDefault(); + e.dataTransfer.dropEffect = 'copy'; + } + }); + main.addEventListener('dragleave', function () { + depth = Math.max(0, depth - 1); + if (!depth) { + overlay.hidden = true; + } + }); + main.addEventListener('drop', function (e) { + if (!carriesFiles(e)) { + return; + } + e.preventDefault(); + depth = 0; + overlay.hidden = true; + var files = e.dataTransfer.files; + if (!files.length) { + return; + } + // Open the upload modal, then hand it the dropped files once it is on screen + var trigger = document.createElement('a'); + trigger.className = 'ajax-modal d-none'; + trigger.href = '#'; + trigger.dataset.modalUrl = main.dataset.filesDrop; + document.body.appendChild(trigger); + trigger.click(); + trigger.remove(); + var tries = 0; + (function handOver() { + var input = document.querySelector('.modal.show [data-upload-input]'); + if (input) { + var transfer = new DataTransfer(); + Array.prototype.forEach.call(files, function (file) { + transfer.items.add(file); + }); + input.files = transfer.files; + input.dispatchEvent(new Event('change', { bubbles: true })); + } else if (++tries < 50) { + setTimeout(handOver, 100); + } + })(); + }); +})(); diff --git a/agent/modals/asset/asset_link_document.php b/agent/modals/asset/asset_link_document.php index 73433732d..da732d580 100644 --- a/agent/modals/asset/asset_link_document.php +++ b/agent/modals/asset/asset_link_document.php @@ -1,68 +1,12 @@ - - -
    - - - - -
    - - - - -
    - - - - -
    - - - - -
    - - - - -
    - - +require __DIR__ . '/../link/link.php'; diff --git a/agent/modals/contact/contact_link_file.php b/agent/modals/contact/contact_link_file.php index 6527ef087..229ea3975 100644 --- a/agent/modals/contact/contact_link_file.php +++ b/agent/modals/contact/contact_link_file.php @@ -1,70 +1,12 @@ - - -
    - - - - -
    - -
    @@ -20,36 +35,7 @@ ob_start();