Browse Source

Merge pull request #1276 from ArthurHoaro/feature/bulk-visibility

Bulk action: set visibility
ArthurHoaro 4 months ago
parent
commit
786f35f270
4 changed files with 85 additions and 1 deletions
  1. 6 0
      application/Router.php
  2. 22 0
      assets/default/js/base.js
  3. 45 0
      index.php
  4. 12 1
      tpl/default/page.header.html

+ 6 - 0
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;
         }

+ 22 - 0
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
    */

+ 45 - 0
index.php

@@ -1266,6 +1266,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']);

+ 12 - 1
tpl/default/page.header.html

@@ -118,7 +118,18 @@
   <div id="actions" class="subheader-form">
     <div class="pure-g">
       <div class="pure-u-1">
-        <a href="" id="actions-delete" class="button">{'Delete'|t}</a>
+        <a href="" id="actions-delete" class="button">
+          <i class="fa fa-trash"></i>
+          {'Delete'|t}
+        </a>&nbsp;
+        <a href="" class="actions-change-visibility button" data-visibility="public">
+          <i class="fa fa-globe"></i>
+          {'Set public'|t}
+        </a>&nbsp;
+        <a href="" class="actions-change-visibility button" data-visibility="private">
+          <i class="fa fa-user-secret"></i>
+          {'Set private'|t}
+        </a>
       </div>
     </div>
   </div>