Files drag and drop: files and documents drag from the list or grid onto a folder in the tree or the path above the list to move there, ticked ones together, and folders drag onto other folders or Top level; a closed folder held over opens, files from the computer dropped on a tree folder upload into it, folder moves use the new move_folder handler, and Bulk Action Move now leaves items already in the folder alone

This commit is contained in:
johnnyq
2026-11-19 14:19:38 -05:00
parent c20acc9ac1
commit 3cb42eaaa2
6 changed files with 373 additions and 43 deletions

View File

@@ -9,6 +9,10 @@
* details, what they are linked to); documents open their page. Files can be
* dropped straight onto the list to upload them into the folder on show.
*
* Files, documents and folders drag onto a folder in the tree or the path to
* move there - the ticked ones together (agent/js/files.js). Files from the
* computer dropped on a folder in the tree upload into it.
*
* Documents can be tagged; filtering by a tag shows the documents with it -
* from every folder at the top level, like a search.
*
@@ -274,6 +278,15 @@ function filesItemMenu($item) {
return $html;
}
/** What makes a file or document draggable onto a folder - nothing when it can't be moved from here. */
function filesDragAttr($item) {
global $can_edit, $archived;
if (!$can_edit || !empty($item['archived']) || $archived) {
return '';
}
return ' draggable="true" data-files-item="' . $item['kind'] . '" data-id="' . $item['id'] . '" data-folder-id="' . $item['folder_id'] . '" data-name="' . escapeHtml($item['name']) . '"';
}
/** "in Folder / Sub" for a search result outside the folder on show. */
function filesFolderNote($item) {
global $folder_tree, $folder_id, $client_id;
@@ -304,7 +317,8 @@ function filesFolderTree($parent_id, $depth = 0) {
. '</div></div>';
}
$html .= '<li class="itflow-folder' . ($is_open ? ' is-open' : '') . '" data-folder-id="' . $id . '">'
. '<div class="itflow-folder-row' . ($id === $folder_id ? ' active' : '') . '" style="--itflow-folder-depth: ' . $depth . '">'
. '<div class="itflow-folder-row' . ($id === $folder_id ? ' active' : '') . '" style="--itflow-folder-depth: ' . $depth . '"'
. ($can_edit ? ' draggable="true" data-folder-drag data-folder-drop="' . $id . '" data-name="' . $name . '"' : '') . '>'
. ($has_children ? '<button type="button" class="itflow-folder-toggle" data-folder-toggle aria-label="Show or hide subfolders"><i class="fas fa-chevron-right"></i></button>' : '<span class="itflow-folder-toggle"></span>')
. '<a class="itflow-folder-link" href="' . escapeHtml(filesUrl(['folder_id' => $id, 'q' => ''])) . '"><i class="fas fa-fw ' . ($id === $folder_id || $is_open ? 'fa-folder-open' : 'fa-folder') . ' me-2"></i><span class="text-truncate">' . $name . '</span>'
. ($count ? '<span class="badge rounded-pill ms-auto">' . $count . '</span>' : '') . '</a>'
@@ -449,7 +463,7 @@ $preview = []; // the grid's previewer payload
</form>
</div>
<div class="row g-0 itflow-files">
<div class="row g-0 itflow-files"<?= $can_edit ? ' data-files-move="' . $_SESSION['csrf_token'] . '"' . (!$archived ? ' data-files-upload="' . escapeHtml("modals/file/file_upload.php?client_id=$client_id&folder_id=") . '"' : '') : '' ?>>
<!-- Folders - a button on a phone, a column beside the list from lg up -->
<div class="col-lg-3 border-end itflow-files-tree">
@@ -469,7 +483,7 @@ $preview = []; // the grid's previewer payload
</div>
<ul class="list-unstyled mb-0 px-2 pb-3 itflow-folder-tree" data-client-id="<?= $client_id ?>">
<li class="itflow-folder is-open">
<div class="itflow-folder-row<?= $folder_id === 0 ? ' active' : '' ?>" style="--itflow-folder-depth: 0">
<div class="itflow-folder-row<?= $folder_id === 0 ? ' active' : '' ?>" style="--itflow-folder-depth: 0"<?= $can_edit ? ' data-folder-drop="0"' : '' ?>>
<span class="itflow-folder-toggle"></span>
<a class="itflow-folder-link" href="<?= escapeHtml(filesUrl(['folder_id' => 0, 'q' => ''])) ?>">
<i class="fas fa-fw fa-home me-2"></i><span class="text-truncate">Top level</span>
@@ -479,6 +493,9 @@ $preview = []; // the grid's previewer payload
</li>
<?= filesFolderTree(0) ?>
</ul>
<?php if ($can_edit) { ?>
<p class="small text-body-secondary px-3 pb-3 mb-0 d-none d-lg-block itflow-files-drag-hint"><i class="fas fa-fw fa-arrows-alt me-1"></i>Drag files, documents and folders onto a folder to move them.</p>
<?php } ?>
</div>
</div>
@@ -488,9 +505,9 @@ $preview = []; // the grid's previewer payload
<div class="d-flex align-items-center gap-2 px-3 py-2 border-bottom itflow-files-path">
<nav aria-label="Folder" class="min-w-0 me-auto">
<ol class="breadcrumb mb-0 flex-nowrap text-nowrap overflow-auto">
<li class="breadcrumb-item"><a href="<?= escapeHtml(filesUrl(['folder_id' => 0, 'q' => ''])) ?>"><i class="fas fa-home me-1"></i>Top level</a></li>
<li class="breadcrumb-item"><a href="<?= escapeHtml(filesUrl(['folder_id' => 0, 'q' => ''])) ?>"<?= $can_edit ? ' data-folder-drop="0"' : '' ?>><i class="fas fa-home me-1"></i>Top level</a></li>
<?php foreach ($folder_path as $crumb) { ?>
<li class="breadcrumb-item"><a href="<?= escapeHtml(filesUrl(['folder_id' => $crumb['folder_id'], 'q' => ''])) ?>"><?= escapeHtml($crumb['folder_name']) ?></a></li>
<li class="breadcrumb-item"><a href="<?= escapeHtml(filesUrl(['folder_id' => $crumb['folder_id'], 'q' => ''])) ?>"<?= $can_edit ? ' data-folder-drop="' . intval($crumb['folder_id']) . '"' : '' ?>><?= escapeHtml($crumb['folder_name']) ?></a></li>
<?php } ?>
<?php if ($search !== '') { ?>
<li class="breadcrumb-item active">Results for "<?= escapeHtml($search) ?>"</li>
@@ -589,7 +606,7 @@ $preview = []; // the grid's previewer payload
];
?>
<div class="col">
<div class="card h-100 mb-0 itflow-file-card <?= $item['kind'] === 'document' ? 'is-document' : '' ?>">
<div class="card h-100 mb-0 itflow-file-card <?= $item['kind'] === 'document' ? 'is-document' : '' ?>"<?= filesDragAttr($item) ?>>
<?php if ($can_edit) { ?>
<div class="form-check itflow-file-card-check">
<input class="form-check-input bulk-select" type="checkbox" name="<?= $item['kind'] ?>_ids[]" value="<?= $id ?>" aria-label="Select <?= $name ?>">
@@ -663,7 +680,7 @@ $preview = []; // the grid's previewer payload
$row = $item['row'];
$share = $shared[$item['kind']][$id] ?? null;
?>
<tr>
<tr<?= filesDragAttr($item) ?>>
<?php if ($can_edit) { ?>
<td class="checkbox-column bg-light border-end">
<div class="form-check">

View File

@@ -1,10 +1,44 @@
/*
* agent/files.php - the folder tree, the grid's previewer, and dropping files
* onto the list to upload them.
* agent/files.php - the folder tree, the grid's previewer, dragging files,
* documents and folders onto a folder to move them, and dropping files from
* the computer to upload them.
*/
(function () {
'use strict';
// What is being dragged within the page - files and documents, or a folder - while it is
var dragging = null;
/** Open the upload window at url, and hand it the dropped files once it is on screen. */
function uploadInto(url, files) {
var trigger = document.createElement('a');
trigger.className = 'ajax-modal d-none';
trigger.href = '#';
trigger.dataset.modalUrl = url;
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);
}
})();
}
/** Whether a drag carries files from the computer - not something dragged within the page. */
function carriesFiles(e) {
return !dragging && e.dataTransfer && Array.prototype.indexOf.call(e.dataTransfer.types || [], 'Files') !== -1;
}
// ---- Folder tree: subfolders open and close in place, remembered per client ----
var tree = document.querySelector('.itflow-folder-tree');
@@ -178,6 +212,223 @@
});
}
// ---- Drag files, documents and folders onto a folder to move them ----
// A row (or a card in the grid) or a folder in the tree drags onto a folder in the tree or the path above
// the list. Dragging a ticked one takes every ticked one. Holding it over a closed folder opens it. The
// move is Bulk Action's (bulk_move_files) or move_folder, and the page comes back saying what moved.
// Files from the computer dropped on a folder in the tree upload into it.
var mover = document.querySelector('[data-files-move]');
if (mover) {
var lit = null, springRow = null, springTimer = null, leaveTimer = null;
// A link or picture in a row would drag its address instead of the row
mover.querySelectorAll('[data-files-item] a, [data-files-item] img, [data-folder-drag] a').forEach(function (node) {
node.draggable = false;
});
function folderItem(id) {
return id && tree ? tree.querySelector('.itflow-folder[data-folder-id="' + CSS.escape(String(id)) + '"]') : null;
}
/** Whether what is being dragged can go into the folder target stands for. */
function accepts(target, e) {
var to = parseInt(target.dataset.folderDrop, 10) || 0;
if (!dragging) {
// Files from the computer, onto a folder in the tree
return !!mover.dataset.filesUpload && carriesFiles(e) && !!target.closest('.itflow-folder-tree');
}
if (dragging.type === 'items') {
return dragging.items.some(function (item) {
return item.folder !== to;
});
}
// A folder - not where it is already, and never into itself or below itself
var into = folderItem(to);
return to !== dragging.parent && !(into && dragging.li.contains(into));
}
function light(target) {
if (lit === target) {
return;
}
if (lit) {
lit.classList.remove('is-drop-target');
}
lit = target;
if (lit) {
lit.classList.add('is-drop-target');
}
}
// A closed folder held over for a moment opens, to reach the folders in it
function spring(row) {
if (row === springRow) {
return;
}
clearTimeout(springTimer);
springRow = row;
var item = row && row.closest('.itflow-folder');
if (item && item.querySelector('ul') && !item.classList.contains('is-open')) {
springTimer = setTimeout(function () {
item.classList.add('is-open');
}, 700);
}
}
function finish() {
clearTimeout(leaveTimer);
light(null);
spring(null);
if (dragging) {
dragging.sources.forEach(function (source) {
source.classList.remove('is-dragging');
});
}
dragging = null;
}
function post(fields) {
var form = document.createElement('form');
form.method = 'post';
form.action = 'post.php';
form.hidden = true;
fields.unshift(['csrf_token', mover.dataset.filesMove]);
fields.forEach(function (field) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = field[0];
input.value = field[1];
form.appendChild(input);
});
document.body.appendChild(form);
form.submit();
}
mover.addEventListener('dragstart', function (e) {
var item = e.target.closest('[data-files-item]');
var folder = !item && e.target.closest('[data-folder-drag]');
if (!item && !folder) {
return;
}
var label, icon;
if (item) {
var box = item.querySelector('.bulk-select');
var sources = box && box.checked
? Array.prototype.map.call(document.querySelectorAll('#bulkActions .bulk-select:checked'), function (checked) {
return checked.closest('[data-files-item]');
}).filter(Boolean)
: [item];
dragging = {
type: 'items',
sources: sources,
items: sources.map(function (source) {
return { kind: source.dataset.filesItem, id: source.dataset.id, folder: parseInt(source.dataset.folderId, 10) || 0 };
})
};
label = sources.length === 1 ? sources[0].dataset.name : sources.length + ' items';
icon = sources.length > 1 ? 'fa-copy' : (sources[0].dataset.filesItem === 'document' ? 'fa-file-alt' : 'fa-file');
} else {
var li = folder.closest('.itflow-folder');
var parent = li.parentElement.closest('.itflow-folder[data-folder-id]');
dragging = {
type: 'folder',
sources: [folder],
id: parseInt(li.dataset.folderId, 10),
parent: parent ? parseInt(parent.dataset.folderId, 10) : 0,
li: li
};
label = folder.dataset.name;
icon = 'fa-folder';
}
e.dataTransfer.clearData();
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', label);
// What follows the pointer: the name, or how many
var ghost = document.createElement('div');
ghost.className = 'itflow-drag-ghost';
ghost.innerHTML = '<i class="fas fa-fw ' + icon + ' me-2"></i><span class="text-truncate"></span>';
ghost.lastChild.textContent = label;
document.body.appendChild(ghost);
try {
e.dataTransfer.setDragImage(ghost, 16, 16);
} catch (err) {
// The browser's own picture of the row then
}
setTimeout(function () {
ghost.remove();
}, 0);
dragging.sources.forEach(function (source) {
source.classList.add('is-dragging');
});
});
mover.addEventListener('dragover', function (e) {
if (!dragging && !carriesFiles(e)) {
return;
}
clearTimeout(leaveTimer);
var target = e.target.closest('[data-folder-drop]');
var ok = target && accepts(target, e);
light(ok ? target : null);
spring(e.target.closest('.itflow-folder-tree .itflow-folder-row'));
if (ok) {
e.preventDefault();
e.dataTransfer.dropEffect = dragging ? 'move' : 'copy';
}
});
// Left the page's folders and list - unless the next dragover says otherwise, as it does when
// the pointer only crossed into an icon (Chrome often gives no relatedTarget to go by)
mover.addEventListener('dragleave', function (e) {
if (e.relatedTarget && mover.contains(e.relatedTarget)) {
return;
}
clearTimeout(leaveTimer);
leaveTimer = setTimeout(function () {
light(null);
spring(null);
}, 100);
});
mover.addEventListener('drop', function (e) {
var target = e.target.closest('[data-folder-drop]');
if (!target || !accepts(target, e)) {
return;
}
e.preventDefault();
var to = parseInt(target.dataset.folderDrop, 10) || 0;
if (!dragging) {
light(null);
spring(null);
if (e.dataTransfer.files.length) {
uploadInto(mover.dataset.filesUpload + to, e.dataTransfer.files);
}
return;
}
var moving = dragging;
finish();
if (moving.type === 'items') {
var fields = [['bulk_move_files', '1'], ['bulk_folder_id', to]];
moving.items.forEach(function (item) {
if (item.folder !== to) {
fields.push([item.kind + '_ids[]', item.id]);
}
});
moving.sources.forEach(function (source) {
source.classList.add('is-moved');
});
post(fields);
} else {
moving.li.classList.add('is-moved');
post([['move_folder', '1'], ['folder_id', moving.id], ['parent_folder', to]]);
}
});
document.addEventListener('dragend', finish);
}
// ---- Drop files on the list to upload them into the folder on show ----
var main = document.querySelector('[data-files-drop]');
@@ -187,10 +438,6 @@
}
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;
@@ -219,31 +466,8 @@
e.preventDefault();
depth = 0;
overlay.hidden = true;
var files = e.dataTransfer.files;
if (!files.length) {
return;
if (e.dataTransfer.files.length) {
uploadInto(main.dataset.filesDrop, e.dataTransfer.files);
}
// 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);
}
})();
});
})();

View File

@@ -391,15 +391,15 @@ if (isset($_POST['bulk_move_files'])) {
$document_count = 0;
// Each item only moves into a folder of its own client - one of another
// client's folders would take it out of sight of both
// client's folders would take it out of sight of both. One already there is left alone.
foreach (array_map('intval', (array) ($_POST['file_ids'] ?? [])) as $file_id) {
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT file_client_id, file_name FROM files WHERE file_id = $file_id"));
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT file_client_id, file_name, file_folder_id FROM files WHERE file_id = $file_id"));
if (!$row) {
continue;
}
$client_id = intval($row['file_client_id']);
enforceClientAccess();
if ($target_folder && folderOwnedId($target_folder, $client_id) !== $target_folder) {
if (($target_folder && folderOwnedId($target_folder, $client_id) !== $target_folder) || intval($row['file_folder_id']) === $target_folder) {
continue;
}
mysqli_query($mysqli, "UPDATE files SET file_folder_id = $target_folder WHERE file_id = $file_id");
@@ -408,13 +408,13 @@ if (isset($_POST['bulk_move_files'])) {
}
foreach (array_map('intval', (array) ($_POST['document_ids'] ?? [])) as $document_id) {
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_client_id, document_name FROM documents WHERE document_id = $document_id"));
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT document_client_id, document_name, document_folder_id FROM documents WHERE document_id = $document_id"));
if (!$row) {
continue;
}
$client_id = intval($row['document_client_id']);
enforceClientAccess();
if ($target_folder && folderOwnedId($target_folder, $client_id) !== $target_folder) {
if (($target_folder && folderOwnedId($target_folder, $client_id) !== $target_folder) || intval($row['document_folder_id']) === $target_folder) {
continue;
}
mysqli_query($mysqli, "UPDATE documents SET document_folder_id = $target_folder, document_updated_at = document_updated_at WHERE document_id = $document_id");

View File

@@ -84,6 +84,59 @@ if (isset($_POST['rename_folder'])) {
}
/*
* A folder dragged onto another on the Files page (agent/js/files.js), or onto the top level.
* What is in it goes with it.
*/
if (isset($_POST['move_folder'])) {
validateCSRFToken();
enforceUserPermission('module_support', 2);
$folder_id = intval($_POST['folder_id']);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT folder_name, folder_client_id FROM folders WHERE folder_id = $folder_id"));
if (!$row) {
flashAlert('That folder no longer exists.', 'error');
redirect();
}
$folder_name = $row['folder_name'];
$client_id = intval($row['folder_client_id']);
enforceClientAccess();
// Only under one of this client's folders - one that has gone isn't taken as the top level
$wanted = intval($_POST['parent_folder'] ?? 0);
$parent_folder = folderOwnedId($wanted, $client_id);
if ($parent_folder !== $wanted) {
flashAlert('The folder you dropped it on no longer exists.', 'error');
redirect();
}
// Never into itself or below itself
$tree = folderTree($client_id);
if (in_array($parent_folder, folderSubtree($tree, $folder_id), true)) {
flashAlert("A folder can't go inside itself or one of its own folders.", 'error');
redirect();
}
if ($parent_folder === intval($tree[$folder_id]['parent_folder'] ?? -1)) {
redirect();
}
$parent_name = folderDisplayName($parent_folder);
mysqli_query($mysqli, "UPDATE folders SET parent_folder = $parent_folder WHERE folder_id = $folder_id");
logAudit("Folder", "Move", "$session_name moved folder " . escapeSql($folder_name) . " into " . escapeSql($parent_name), $client_id, $folder_id);
flashAlert("Folder <strong>$folder_name</strong> moved into <strong>$parent_name</strong>");
redirect();
}
/*
* Deleting a folder never deletes what is in it: files, documents and
* subfolders move up to the folder it sat in. (Only documents used to be