1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-27 08:35:15 +02:00

Upgrade ProcessPageList to support showing and use of trash to non-superusers, for pages user is allowed to edit

This commit is contained in:
Ryan Cramer
2018-06-28 12:59:59 -04:00
parent 8e084a1ba0
commit bf62fbb897
3 changed files with 88 additions and 27 deletions

View File

@@ -8,7 +8,7 @@
* For more details about how Process modules work, please see: * For more details about how Process modules work, please see:
* /wire/core/Process.php * /wire/core/Process.php
* *
* ProcessWire 3.x, Copyright 2017 by Ryan Cramer * ProcessWire 3.x, Copyright 2018 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* @property bool $showRootPage Whether root page (like home) should be shown. * @property bool $showRootPage Whether root page (like home) should be shown.
@@ -37,7 +37,7 @@ class ProcessPageList extends Process implements ConfigurableModule {
return array( return array(
'title' => 'Page List', 'title' => 'Page List',
'summary' => 'List pages in a hierarchal tree structure', 'summary' => 'List pages in a hierarchal tree structure',
'version' => 119, 'version' => 120,
'permanent' => true, 'permanent' => true,
'permission' => 'page-edit', 'permission' => 'page-edit',
'icon' => 'sitemap', 'icon' => 'sitemap',

View File

@@ -167,17 +167,20 @@ class ProcessPageListActions extends Wire {
} }
} }
if($this->superuser) { $trashable = $page->trashable();
$trashIcon = "<i class='fa fa-trash-o'></i>&nbsp;"; $trashIcon = "<i class='fa fa-trash-o'></i>&nbsp;";
if($page->trashable()) { if($trashable && !$user->isSuperuser()) {
// do not allow non-superuser ability to trash branches of pages, only individual pages
if($page->numChildren(1) > 0) $trashable = false;
}
if($trashable) {
$extras['trash'] = array( $extras['trash'] = array(
'cn' => 'Trash', 'cn' => 'Trash',
'name' => $trashIcon . $this->actionLabels['trash'], 'name' => $trashIcon . $this->actionLabels['trash'],
'url' => "$adminUrl?action=trash&id=$page->id", 'url' => "$adminUrl?action=trash&id=$page->id",
'ajax' => true 'ajax' => true
); );
} else if($trash) { } else if($trash && $page->restorable()) {
if(preg_match('/^(' . $page->id . ')\.\d+\.\d+_.+$/', $page->name)) {
$extras['restore'] = array( $extras['restore'] = array(
'cn' => 'Restore', 'cn' => 'Restore',
'name' => $trashIcon . $this->actionLabels['restore'], 'name' => $trashIcon . $this->actionLabels['restore'],
@@ -185,8 +188,6 @@ class ProcessPageListActions extends Wire {
'ajax' => true 'ajax' => true
); );
} }
}
}
return $extras; return $extras;
} }

View File

@@ -10,6 +10,8 @@ class ProcessPageListRenderJSON extends ProcessPageListRender {
protected $systemIDs = array(); protected $systemIDs = array();
protected $allowTrash = null;
public function __construct(Page $page, PageArray $children) { public function __construct(Page $page, PageArray $children) {
parent::__construct($page, $children); parent::__construct($page, $children);
@@ -22,6 +24,44 @@ class ProcessPageListRenderJSON extends ProcessPageListRender {
); );
} }
/**
* Are we allowed to display the Trash page?
*
* @return bool
*
*/
protected function allowTrash() {
if($this->allowTrash !== null) return $this->allowTrash;
/** @var User $user */
$user = $this->wire('user');
$petc = 'page-edit-trash-created';
if(!$this->wire('permissions')->has($petc)) $petc = false;
if($user->isSuperuser()) {
// superuser
$this->allowTrash = true;
} else if($user->hasPermission('page-delete')) {
// has page-delete globally
$this->allowTrash = true;
} else if($petc && $user->hasPermission($petc)) {
// has page-edit-trash-created globally
$this->allowTrash = true;
} else if($user->hasPermission('page-delete', true)) {
// has page-delete added specifically at a template
$this->allowTrash = true;
} else if($petc && $user->hasPermission($petc, true)) {
// has page-edit-trash-created added specifically at a template
$this->allowTrash = true;
} else {
$this->allowTrash = false;
}
return $this->allowTrash;
}
public function renderChild(Page $page) { public function renderChild(Page $page) {
$outputFormatting = $page->outputFormatting; $outputFormatting = $page->outputFormatting;
@@ -59,12 +99,24 @@ class ProcessPageListRenderJSON extends ProcessPageListRender {
} }
if($page->id == $this->config->trashPageID) { if($page->id == $this->config->trashPageID) {
$note = '';
if($this->superuser) {
$note = "&lt; " . $this->_("Trash open: drag pages below here to trash them"); // Message that appears next to the Trash page when open $note = "&lt; " . $this->_("Trash open: drag pages below here to trash them"); // Message that appears next to the Trash page when open
}
$icons = array('trash-o'); // override any other icons $icons = array('trash-o'); // override any other icons
$numChildren = $page->numChildren(false);
if($numChildren > 0 && !$this->superuser) {
// manually count quantity that are listable in the trash
$numChildren = 0;
foreach($page->children("include=all") as $child) {
if($child->listable()) $numChildren++;
}
}
} else { } else {
if($page->hasStatus(Page::statusTemp)) $icons[] = 'bolt'; if($page->hasStatus(Page::statusTemp)) $icons[] = 'bolt';
if($page->hasStatus(Page::statusLocked)) $icons[] = 'lock'; if($page->hasStatus(Page::statusLocked)) $icons[] = 'lock';
if($page->hasStatus(Page::statusDraft)) $icons[] = 'paperclip'; if($page->hasStatus(Page::statusDraft)) $icons[] = 'paperclip';
$numChildren = $page->numChildren(1);
} }
if(!$label) $label = $this->getPageLabel($page); if(!$label) $label = $this->getPageLabel($page);
@@ -77,7 +129,7 @@ class ProcessPageListRenderJSON extends ProcessPageListRender {
'id' => $page->id, 'id' => $page->id,
'label' => $label, 'label' => $label,
'status' => $page->status, 'status' => $page->status,
'numChildren' => $page->numChildren(1), 'numChildren' => $numChildren,
'path' => $page->template->slashUrls || $page->id == 1 ? $page->path() : rtrim($page->path(), '/'), 'path' => $page->template->slashUrls || $page->id == 1 ? $page->path() : rtrim($page->path(), '/'),
'template' => $page->template->name, 'template' => $page->template->name,
//'rm' => $this->superuser && $page->trashable(), //'rm' => $this->superuser && $page->trashable(),
@@ -97,18 +149,21 @@ class ProcessPageListRenderJSON extends ProcessPageListRender {
public function render() { public function render() {
$children = array(); $children = array();
$extraPages = array(); // pages forced to bottom of list $extraPages = array(); // pages forced to bottom of list
$id404 = $this->wire('config')->http404PageID; $config = $this->wire('config');
$idTrash = $config->trashPageID;
$id404 = $config->http404PageID;
$page404 = null;
foreach($this->children as $page) { foreach($this->children as $page) {
if(!$this->superuser && !$page->listable()) continue; if(!$this->superuser && !$page->listable()) continue;
if($page->id == $id404 && !$this->superuser) { if($page->id == $id404 && !$this->superuser) {
// allow showing 404 page, only if it's editable // allow showing 404 page, only if it's editable
$page404 = $page;
if(!$page->editable()) continue; if(!$page->editable()) continue;
} else if(in_array($page->id, $this->systemIDs)) { } else if(in_array($page->id, $this->systemIDs)) {
$extraPages[] = $page; if($this->superuser) $extraPages[$page->id] = $page;
continue; continue;
} }
@@ -116,7 +171,12 @@ class ProcessPageListRenderJSON extends ProcessPageListRender {
$children[] = $child; $children[] = $child;
} }
if($this->superuser) foreach($extraPages as $page) { if(!$this->superuser && $page404 && $page404->parent_id == 1 && !isset($extraPages[$idTrash])) {
$pageTrash = $this->wire('pages')->get($idTrash);
if($pageTrash->id && $pageTrash->listable()) $extraPages[$pageTrash->id] = $pageTrash;
}
foreach($extraPages as $page) {
$children[] = $this->renderChild($page); $children[] = $this->renderChild($page);
} }