diff options
author | ArthurHoaro <arthur@hoa.ro> | 2019-04-22 12:31:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-22 12:31:09 +0200 |
commit | 786f35f2700d83687ef494b98e3dace37d268e1c (patch) | |
tree | f18c3fbae17445c29b031a5405a2779bba314b32 | |
parent | e7ffbb7ed1c9e771b40df2d8911daedd6e7c5198 (diff) | |
parent | 8d03f705ebbc891e216d509d4de0419842ebd317 (diff) | |
download | Shaarli-786f35f2700d83687ef494b98e3dace37d268e1c.tar.gz Shaarli-786f35f2700d83687ef494b98e3dace37d268e1c.tar.zst Shaarli-786f35f2700d83687ef494b98e3dace37d268e1c.zip |
Merge pull request #1276 from ArthurHoaro/feature/bulk-visibility
Bulk action: set visibility
-rw-r--r-- | application/Router.php | 6 | ||||
-rw-r--r-- | assets/default/js/base.js | 22 | ||||
-rw-r--r-- | index.php | 45 | ||||
-rw-r--r-- | tpl/default/page.header.html | 13 |
4 files changed, 85 insertions, 1 deletions
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 | |||
38 | 38 | ||
39 | public static $PAGE_DELETELINK = 'delete_link'; | 39 | public static $PAGE_DELETELINK = 'delete_link'; |
40 | 40 | ||
41 | public static $PAGE_CHANGE_VISIBILITY = 'change_visibility'; | ||
42 | |||
41 | public static $PAGE_PINLINK = 'pin'; | 43 | public static $PAGE_PINLINK = 'pin'; |
42 | 44 | ||
43 | public static $PAGE_EXPORT = 'export'; | 45 | public static $PAGE_EXPORT = 'export'; |
@@ -149,6 +151,10 @@ class Router | |||
149 | return self::$PAGE_DELETELINK; | 151 | return self::$PAGE_DELETELINK; |
150 | } | 152 | } |
151 | 153 | ||
154 | if (isset($get[self::$PAGE_CHANGE_VISIBILITY])) { | ||
155 | return self::$PAGE_CHANGE_VISIBILITY; | ||
156 | } | ||
157 | |||
152 | if (startsWith($query, 'do=' . self::$PAGE_PINLINK)) { | 158 | if (startsWith($query, 'do=' . self::$PAGE_PINLINK)) { |
153 | return self::$PAGE_PINLINK; | 159 | return self::$PAGE_PINLINK; |
154 | } | 160 | } |
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) { | |||
466 | }); | 466 | }); |
467 | } | 467 | } |
468 | 468 | ||
469 | const changeVisibilityButtons = document.querySelectorAll('.actions-change-visibility'); | ||
470 | if (changeVisibilityButtons != null && token != null) { | ||
471 | [...changeVisibilityButtons].forEach((button) => { | ||
472 | button.addEventListener('click', (event) => { | ||
473 | event.preventDefault(); | ||
474 | const visibility = event.target.getAttribute('data-visibility'); | ||
475 | |||
476 | const links = []; | ||
477 | const linkCheckedCheckboxes = document.querySelectorAll('.link-checkbox:checked'); | ||
478 | [...linkCheckedCheckboxes].forEach((checkbox) => { | ||
479 | links.push({ | ||
480 | id: checkbox.value, | ||
481 | title: document.querySelector(`.linklist-item[data-id="${checkbox.value}"] .linklist-link`).innerHTML, | ||
482 | }); | ||
483 | }); | ||
484 | |||
485 | const ids = links.map(item => item.id); | ||
486 | window.location = `?change_visibility&token=${token.value}&newVisibility=${visibility}&ids=${ids.join('+')}`; | ||
487 | }); | ||
488 | }); | ||
489 | } | ||
490 | |||
469 | /** | 491 | /** |
470 | * Select all button | 492 | * Select all button |
471 | */ | 493 | */ |
@@ -1266,6 +1266,51 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, | |||
1266 | exit; | 1266 | exit; |
1267 | } | 1267 | } |
1268 | 1268 | ||
1269 | // -------- User clicked either "Set public" or "Set private" bulk operation | ||
1270 | if ($targetPage == Router::$PAGE_CHANGE_VISIBILITY) { | ||
1271 | if (! $sessionManager->checkToken($_GET['token'])) { | ||
1272 | die(t('Wrong token.')); | ||
1273 | } | ||
1274 | |||
1275 | $ids = trim($_GET['ids']); | ||
1276 | if (strpos($ids, ' ') !== false) { | ||
1277 | // multiple, space-separated ids provided | ||
1278 | $ids = array_values(array_filter(preg_split('/\s+/', escape($ids)))); | ||
1279 | } else { | ||
1280 | // only a single id provided | ||
1281 | $ids = [$ids]; | ||
1282 | } | ||
1283 | |||
1284 | // assert at least one id is given | ||
1285 | if (!count($ids)) { | ||
1286 | die('no id provided'); | ||
1287 | } | ||
1288 | // assert that the visibility is valid | ||
1289 | if (!isset($_GET['newVisibility']) || !in_array($_GET['newVisibility'], ['public', 'private'])) { | ||
1290 | die('invalid visibility'); | ||
1291 | } else { | ||
1292 | $private = $_GET['newVisibility'] === 'private'; | ||
1293 | } | ||
1294 | foreach ($ids as $id) { | ||
1295 | $id = (int) escape($id); | ||
1296 | $link = $LINKSDB[$id]; | ||
1297 | $link['private'] = $private; | ||
1298 | $pluginManager->executeHooks('save_link', $link); | ||
1299 | $LINKSDB[$id] = $link; | ||
1300 | } | ||
1301 | $LINKSDB->save($conf->get('resource.page_cache')); // save to disk | ||
1302 | |||
1303 | $location = '?'; | ||
1304 | if (isset($_SERVER['HTTP_REFERER'])) { | ||
1305 | $location = generateLocation( | ||
1306 | $_SERVER['HTTP_REFERER'], | ||
1307 | $_SERVER['HTTP_HOST'] | ||
1308 | ); | ||
1309 | } | ||
1310 | header('Location: ' . $location); // After deleting the link, redirect to appropriate location | ||
1311 | exit; | ||
1312 | } | ||
1313 | |||
1269 | // -------- User clicked the "EDIT" button on a link: Display link edit form. | 1314 | // -------- User clicked the "EDIT" button on a link: Display link edit form. |
1270 | if (isset($_GET['edit_link'])) { | 1315 | if (isset($_GET['edit_link'])) { |
1271 | $id = (int) escape($_GET['edit_link']); | 1316 | $id = (int) escape($_GET['edit_link']); |
diff --git a/tpl/default/page.header.html b/tpl/default/page.header.html index 5f1e4d66..2832ebbb 100644 --- a/tpl/default/page.header.html +++ b/tpl/default/page.header.html | |||
@@ -118,7 +118,18 @@ | |||
118 | <div id="actions" class="subheader-form"> | 118 | <div id="actions" class="subheader-form"> |
119 | <div class="pure-g"> | 119 | <div class="pure-g"> |
120 | <div class="pure-u-1"> | 120 | <div class="pure-u-1"> |
121 | <a href="" id="actions-delete" class="button">{'Delete'|t}</a> | 121 | <a href="" id="actions-delete" class="button"> |
122 | <i class="fa fa-trash"></i> | ||
123 | {'Delete'|t} | ||
124 | </a> | ||
125 | <a href="" class="actions-change-visibility button" data-visibility="public"> | ||
126 | <i class="fa fa-globe"></i> | ||
127 | {'Set public'|t} | ||
128 | </a> | ||
129 | <a href="" class="actions-change-visibility button" data-visibility="private"> | ||
130 | <i class="fa fa-user-secret"></i> | ||
131 | {'Set private'|t} | ||
132 | </a> | ||
122 | </div> | 133 | </div> |
123 | </div> | 134 | </div> |
124 | </div> | 135 | </div> |