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:
@@ -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',
|
||||||
|
@@ -167,25 +167,26 @@ class ProcessPageListActions extends Wire {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->superuser) {
|
$trashable = $page->trashable();
|
||||||
$trashIcon = "<i class='fa fa-trash-o'></i> ";
|
$trashIcon = "<i class='fa fa-trash-o'></i> ";
|
||||||
if($page->trashable()) {
|
if($trashable && !$user->isSuperuser()) {
|
||||||
$extras['trash'] = array(
|
// do not allow non-superuser ability to trash branches of pages, only individual pages
|
||||||
'cn' => 'Trash',
|
if($page->numChildren(1) > 0) $trashable = false;
|
||||||
'name' => $trashIcon . $this->actionLabels['trash'],
|
}
|
||||||
'url' => "$adminUrl?action=trash&id=$page->id",
|
if($trashable) {
|
||||||
'ajax' => true
|
$extras['trash'] = array(
|
||||||
);
|
'cn' => 'Trash',
|
||||||
} else if($trash) {
|
'name' => $trashIcon . $this->actionLabels['trash'],
|
||||||
if(preg_match('/^(' . $page->id . ')\.\d+\.\d+_.+$/', $page->name)) {
|
'url' => "$adminUrl?action=trash&id=$page->id",
|
||||||
$extras['restore'] = array(
|
'ajax' => true
|
||||||
'cn' => 'Restore',
|
);
|
||||||
'name' => $trashIcon . $this->actionLabels['restore'],
|
} else if($trash && $page->restorable()) {
|
||||||
'url' => "$adminUrl?action=restore&id=$page->id",
|
$extras['restore'] = array(
|
||||||
'ajax' => true
|
'cn' => 'Restore',
|
||||||
);
|
'name' => $trashIcon . $this->actionLabels['restore'],
|
||||||
}
|
'url' => "$adminUrl?action=restore&id=$page->id",
|
||||||
}
|
'ajax' => true
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $extras;
|
return $extras;
|
||||||
|
@@ -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 = "< " . $this->_("Trash open: drag pages below here to trash them"); // Message that appears next to the Trash page when open
|
$note = '';
|
||||||
|
if($this->superuser) {
|
||||||
|
$note = "< " . $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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user