]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Bulk action: set visibility 1276/head
authorArthurHoaro <arthur@hoa.ro>
Sat, 9 Feb 2019 16:59:53 +0000 (17:59 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sat, 9 Feb 2019 16:59:53 +0000 (17:59 +0100)
Added 2 buttons when link checkboxes are checked to set them either public or private.

Related to #572 #1160

application/Router.php
assets/default/js/base.js
index.php
tpl/default/page.header.html

index 05877acdb6d2bfd88979b16bae18ade4ca737b6e..d7187487e6ff3a736e797623577af7bc9998ebf3 100644 (file)
@@ -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;
         }
index 99e03370a763c0bdd8214ce43a18a6eccaab394c..d5c29c695295ae574ec27ed5d06cf4b71bdd5f7a 100644 (file)
@@ -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
    */
index 633ab89e6386e90eaa02f76d5a1880cb9366dccb..322c360c650a9ce5ecbcdfebaea71bb553546535 100644 (file)
--- 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']);
index 4f6dd4d8f87afa536db31854f99a3f9083c6080f..4b66502349d7a78e34fcad2c95817813c87eea3f 100644 (file)
   <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>