From 8d03f705ebbc891e216d509d4de0419842ebd317 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 9 Feb 2019 17:59:53 +0100 Subject: Bulk action: set visibility Added 2 buttons when link checkboxes are checked to set them either public or private. Related to #572 #1160 --- application/Router.php | 6 ++++++ assets/default/js/base.js | 22 ++++++++++++++++++++++ index.php | 45 ++++++++++++++++++++++++++++++++++++++++++++ tpl/default/page.header.html | 13 ++++++++++++- 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/application/Router.php b/application/Router.php index 05877acd..d7187487 100644 --- a/application/Router.php +++ b/application/Router.php @@ -38,6 +38,8 @@ class Router public static $PAGE_DELETELINK = 'delete_link'; + public static $PAGE_CHANGE_VISIBILITY = 'change_visibility'; + public static $PAGE_PINLINK = 'pin'; public static $PAGE_EXPORT = 'export'; @@ -149,6 +151,10 @@ class Router return self::$PAGE_DELETELINK; } + if (isset($get[self::$PAGE_CHANGE_VISIBILITY])) { + return self::$PAGE_CHANGE_VISIBILITY; + } + if (startsWith($query, 'do=' . self::$PAGE_PINLINK)) { return self::$PAGE_PINLINK; } diff --git a/assets/default/js/base.js b/assets/default/js/base.js index 99e03370..d5c29c69 100644 --- a/assets/default/js/base.js +++ b/assets/default/js/base.js @@ -466,6 +466,28 @@ function init(description) { }); } + const changeVisibilityButtons = document.querySelectorAll('.actions-change-visibility'); + if (changeVisibilityButtons != null && token != null) { + [...changeVisibilityButtons].forEach((button) => { + button.addEventListener('click', (event) => { + event.preventDefault(); + const visibility = event.target.getAttribute('data-visibility'); + + const links = []; + const linkCheckedCheckboxes = document.querySelectorAll('.link-checkbox:checked'); + [...linkCheckedCheckboxes].forEach((checkbox) => { + links.push({ + id: checkbox.value, + title: document.querySelector(`.linklist-item[data-id="${checkbox.value}"] .linklist-link`).innerHTML, + }); + }); + + const ids = links.map(item => item.id); + window.location = `?change_visibility&token=${token.value}&newVisibility=${visibility}&ids=${ids.join('+')}`; + }); + }); + } + /** * Select all button */ diff --git a/index.php b/index.php index 633ab89e..322c360c 100644 --- a/index.php +++ b/index.php @@ -1273,6 +1273,51 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, exit; } + // -------- User clicked either "Set public" or "Set private" bulk operation + if ($targetPage == Router::$PAGE_CHANGE_VISIBILITY) { + if (! $sessionManager->checkToken($_GET['token'])) { + die(t('Wrong token.')); + } + + $ids = trim($_GET['ids']); + if (strpos($ids, ' ') !== false) { + // multiple, space-separated ids provided + $ids = array_values(array_filter(preg_split('/\s+/', escape($ids)))); + } else { + // only a single id provided + $ids = [$ids]; + } + + // assert at least one id is given + if (!count($ids)) { + die('no id provided'); + } + // assert that the visibility is valid + if (!isset($_GET['newVisibility']) || !in_array($_GET['newVisibility'], ['public', 'private'])) { + die('invalid visibility'); + } else { + $private = $_GET['newVisibility'] === 'private'; + } + foreach ($ids as $id) { + $id = (int) escape($id); + $link = $LINKSDB[$id]; + $link['private'] = $private; + $pluginManager->executeHooks('save_link', $link); + $LINKSDB[$id] = $link; + } + $LINKSDB->save($conf->get('resource.page_cache')); // save to disk + + $location = '?'; + if (isset($_SERVER['HTTP_REFERER'])) { + $location = generateLocation( + $_SERVER['HTTP_REFERER'], + $_SERVER['HTTP_HOST'] + ); + } + header('Location: ' . $location); // After deleting the link, redirect to appropriate location + exit; + } + // -------- User clicked the "EDIT" button on a link: Display link edit form. if (isset($_GET['edit_link'])) { $id = (int) escape($_GET['edit_link']); diff --git a/tpl/default/page.header.html b/tpl/default/page.header.html index 4f6dd4d8..4b665023 100644 --- a/tpl/default/page.header.html +++ b/tpl/default/page.header.html @@ -118,7 +118,18 @@
-- cgit v1.2.3