mirror of
https://github.com/processwire/processwire.git
synced 2025-08-08 07:47:00 +02:00
Various minor code updates primarily in Process modules.
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
* #pw-body
|
||||
* #pw-use-constructor
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
|
||||
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
* @method Page add($name)
|
||||
@@ -333,8 +333,7 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
if(!isset($options['loadOptions'])) $options['loadOptions'] = array();
|
||||
$options['loadOptions'] = $this->getLoadOptions($options['loadOptions']);
|
||||
if(empty($options['caller'])) $options['caller'] = $this->className() . ".find($selectorString)";
|
||||
$pages = $this->wire('pages')->find($this->selectorString($selectorString), $options);
|
||||
/** @var PageArray $pages */
|
||||
$pages = $this->wire()->pages->find($this->selectorString($selectorString), $options);
|
||||
foreach($pages as $page) {
|
||||
if(!$this->isValid($page)) {
|
||||
$pages->remove($page);
|
||||
@@ -358,7 +357,7 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
public function findIDs($selectorString, $options = array()) {
|
||||
if(!isset($options['findAll'])) $options['findAll'] = true;
|
||||
if(empty($options['caller'])) $options['caller'] = $this->className() . ".findIDs($selectorString)";
|
||||
$ids = $this->wire('pages')->findIDs($this->selectorString($selectorString), $options);
|
||||
$ids = $this->wire()->pages->findIDs($this->selectorString($selectorString), $options);
|
||||
return $ids;
|
||||
}
|
||||
|
||||
@@ -435,6 +434,11 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
* - This is the same as calling $page->save()
|
||||
* - If the page is new, it will be inserted. If existing, it will be updated.
|
||||
* - If you want to just save a particular field in a Page, use `$page->save($fieldName)` instead.
|
||||
*
|
||||
* Hook note:
|
||||
* If you want to hook this method, please hook the `saveReady`, `saved`, or one of
|
||||
* the `Pages::save*` methods instead, as hooking this method will not hook relevant pages
|
||||
* saved directly through $pages->save().
|
||||
*
|
||||
* @param Page $page
|
||||
* @return bool True on success
|
||||
@@ -453,6 +457,10 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
*
|
||||
* If you attempt to delete a page with children, and don’t specifically set the `$recursive` argument to `true`, then
|
||||
* this method will throw an exception. If a recursive delete fails for any reason, an exception will be thrown.
|
||||
*
|
||||
* Hook note:
|
||||
* If you want to hook this method, please hook the `deleteReady`, `deleted`, or `Pages::delete` method
|
||||
* instead, as hooking this method will not hook relevant pages deleted directly through $pages->delete().
|
||||
*
|
||||
* @param Page $page
|
||||
* @param bool $recursive If set to true, then this will attempt to delete all children too.
|
||||
@@ -471,6 +479,10 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
* - If the page has any other fields, they will not be populated, only the name will.
|
||||
* - Returns a `NullPage` on error, such as when a page of this type already exists with the same name/parent.
|
||||
*
|
||||
* Hook note:
|
||||
* If you want to hook this method, please hook the `addReady`, `Pages::add`, or `Pages::addReady` method
|
||||
* instead, as hooking this method will not hook relevant pages added directly through $pages->add().
|
||||
*
|
||||
* @param string $name Name to use for the new page
|
||||
* @return Page|NullPage
|
||||
*
|
||||
@@ -479,7 +491,7 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
|
||||
$parent = $this->getParent();
|
||||
|
||||
$page = $this->wire('pages')->newPage(array(
|
||||
$page = $this->wire()->pages->newPage(array(
|
||||
'pageClass' => $this->getPageClass(),
|
||||
'template' => $this->template
|
||||
));
|
||||
@@ -492,7 +504,7 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
|
||||
} catch(\Exception $e) {
|
||||
$this->trackException($e, false);
|
||||
$page = $this->wire('pages')->newNullPage();
|
||||
$page = $this->wire()->pages->newNullPage();
|
||||
}
|
||||
|
||||
return $page;
|
||||
@@ -570,7 +582,7 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
*
|
||||
*/
|
||||
public function getParent() {
|
||||
return $this->wire('pages')->get($this->parent_id);
|
||||
return $this->wire()->pages->get($this->parent_id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -583,10 +595,10 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
*/
|
||||
public function getParents() {
|
||||
if(count($this->parents)) {
|
||||
return $this->wire('pages')->getById($this->parents);
|
||||
return $this->wire()->pages->getById($this->parents);
|
||||
} else {
|
||||
$parent = $this->getParent();
|
||||
$parents = $this->wire('pages')->newPageArray();
|
||||
$parents = $this->wire()->pages->newPageArray();
|
||||
$parents->add($parent);
|
||||
return $parents;
|
||||
}
|
||||
@@ -642,7 +654,7 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
$selectorString = $this->selectorString($selectorString);
|
||||
$defaults = array('findAll' => true);
|
||||
$options = array_merge($defaults, $options);
|
||||
return $this->wire('pages')->count($selectorString, $options);
|
||||
return $this->wire()->pages->count($selectorString, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -671,7 +683,6 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
*
|
||||
*/
|
||||
public function ___saveReady(Page $page) {
|
||||
if($page) {}
|
||||
return array();
|
||||
}
|
||||
|
||||
|
@@ -8,7 +8,7 @@
|
||||
* For more details about how Process modules work, please see:
|
||||
* /wire/core/Process.php
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer
|
||||
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
* @property array $showFields Names of fields to show in the main list table (default=['name'])
|
||||
@@ -29,7 +29,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
'permanent' => true,
|
||||
'useNavJSON' => true,
|
||||
'addFlag' => Modules::flagsNoUserConfig
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,7 +49,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
/**
|
||||
* ProcessPageEdit or ProcessPageAdd
|
||||
*
|
||||
* @var WirePageEditor|ProcessPageEdit|WirePageEditor|null
|
||||
* @var WirePageEditor|ProcessPageEdit|null
|
||||
*
|
||||
*/
|
||||
protected $editor = null;
|
||||
@@ -86,15 +86,16 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
*
|
||||
*/
|
||||
public function init() {
|
||||
|
||||
$config = $this->wire()->config;
|
||||
|
||||
$this->config->scripts->add($this->config->urls('ProcessPageType') . 'ProcessPageType.js');
|
||||
$this->config->styles->add($this->config->urls('ProcessPageType') . 'ProcessPageType.css');
|
||||
$config->scripts->add($config->urls('ProcessPageType') . 'ProcessPageType.js');
|
||||
$config->styles->add($config->urls('ProcessPageType') . 'ProcessPageType.css');
|
||||
|
||||
$this->pages = $this->wire($this->page->name);
|
||||
|
||||
if(is_null($this->pages) || !$this->pages instanceof PagesType) {
|
||||
// $this->error("Unable to find API variable named '{$this->page->name}' (of type: PagesType)", Notice::debug);
|
||||
$this->pages = $this->wire('pages');
|
||||
if(!$this->pages instanceof PagesType) {
|
||||
$this->pages = $this->wire()->pages;
|
||||
}
|
||||
|
||||
if($this->pages instanceof PagesType) {
|
||||
@@ -127,8 +128,9 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
|
||||
// init lister, but only if executing an action that will use it
|
||||
$modules = $this->wire()->modules;
|
||||
$segment = $this->wire('input')->urlSegment1;
|
||||
$user = $this->wire('user');
|
||||
$segment = $this->wire()->input->urlSegment1;
|
||||
$user = $this->wire()->user;
|
||||
|
||||
$listerSegments = array(
|
||||
'list',
|
||||
'config',
|
||||
@@ -187,7 +189,6 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
*
|
||||
*/
|
||||
public function ___executeUnknown() {
|
||||
$lister = null;
|
||||
if($this->useLister()) {
|
||||
$this->initLister();
|
||||
$lister = $this->getLister();
|
||||
@@ -213,8 +214,8 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
*
|
||||
*/
|
||||
public function ___executeList() {
|
||||
$templateID = (int) $this->wire('input')->get('templates_id');
|
||||
if(!$templateID) $templateID = (int) $this->wire('session')->get($this->className() . 'TemplatesID');
|
||||
$templateID = (int) $this->wire()->input->get('templates_id');
|
||||
if(!$templateID) $templateID = (int) $this->wire()->session->get($this->className() . 'TemplatesID');
|
||||
$selector = $templateID ? "templates_id=$templateID, " : "";
|
||||
return $this->renderList($selector . "limit=100, status<" . Page::statusMax);
|
||||
}
|
||||
@@ -229,9 +230,10 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
public function ___executeNavJSON(array $options = array()) {
|
||||
|
||||
if(!isset($options['items'])) {
|
||||
$limit = (int) $this->wire('input')->get('limit');
|
||||
$input = $this->wire()->input;
|
||||
$limit = (int) $input->get('limit');
|
||||
if(!$limit || $limit > 100) $limit = 100;
|
||||
$start = (int) $this->wire('input')->get('start');
|
||||
$start = (int) $input->get('start');
|
||||
$pages = $this->pages->find("start=$start, limit=$limit");
|
||||
foreach($pages as $page) {
|
||||
if(!$page->editable()) $pages->remove($page);
|
||||
@@ -241,7 +243,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
if($icon) $page->setQuietly('_labelIcon', $icon);
|
||||
}
|
||||
$options['items'] = $pages;
|
||||
$parent = $this->pages->getParent();
|
||||
$parent = $this->pages instanceof PagesType ? $this->pages->getParent() : null;
|
||||
if($parent && $parent->id && $parent->addable()) {
|
||||
$options['add'] = 'add/';
|
||||
} else {
|
||||
@@ -249,6 +251,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
}
|
||||
$options['edit'] = "edit/?id={id}";
|
||||
}
|
||||
|
||||
if(!isset($options['itemLabel'])) $options['itemLabel'] = $this->jsonListLabel;
|
||||
if(!isset($options['iconKey'])) $options['iconKey'] = '_labelIcon';
|
||||
|
||||
@@ -392,7 +395,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
$templates = $this->pages->getTemplates();
|
||||
$parents = $this->pages->getParentIDs();
|
||||
$this->editor->setPredefinedTemplates($templates);
|
||||
$this->editor->setPredefinedParents($this->wire('pages')->getById($parents));
|
||||
$this->editor->setPredefinedParents($this->wire()->pages->getById($parents));
|
||||
}
|
||||
}
|
||||
return $this->editor;
|
||||
@@ -409,7 +412,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
$pageTitle = $this->page->get('title|name');
|
||||
$this->breadcrumb('../', $pageTitle);
|
||||
$editor = $this->getEditor('ProcessPageEdit');
|
||||
$urlSegment = ucfirst($this->wire('input')->urlSegment2);
|
||||
$urlSegment = ucfirst($this->wire()->input->urlSegment2);
|
||||
$editPage = $this->getPage();
|
||||
$this->browserTitle("$pageTitle > " . $editPage->name);
|
||||
|
||||
@@ -511,7 +514,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
}
|
||||
|
||||
/** @var MarkupAdminDataTable $table */
|
||||
$table = $this->modules->get("MarkupAdminDataTable");
|
||||
$table = $this->wire()->modules->get("MarkupAdminDataTable");
|
||||
$table->setEncodeEntities(false);
|
||||
$fieldNames = $this->showFields;
|
||||
$fieldLabels = $fieldNames;
|
||||
@@ -521,7 +524,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
$fieldLabels[$key] = $this->_('Name'); // Label for 'name' field
|
||||
continue;
|
||||
}
|
||||
$field = $this->wire('fields')->get($name);
|
||||
$field = $this->wire()->fields->get($name);
|
||||
if($field) {
|
||||
$label = $field->getLabel();
|
||||
$fieldLabels[$key] = htmlentities($label, ENT_QUOTES, "UTF-8");
|
||||
@@ -553,7 +556,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
$numRows++;
|
||||
}
|
||||
|
||||
if($this->wire('page')->addable()) $table->action(array($this->addLabel => 'add/'));
|
||||
if($this->wire()->page->addable()) $table->action(array($this->addLabel => 'add/'));
|
||||
|
||||
if($pages->getTotal() > count($pages)) {
|
||||
/** @var MarkupPagerNav $pager */
|
||||
@@ -576,7 +579,6 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
*
|
||||
*/
|
||||
protected function renderEmptyList(PageArray $pages) {
|
||||
if($pages) {} // ignore
|
||||
$out = "<p>" . $this->_('No items to display yet.') . "</p>";
|
||||
return $out;
|
||||
}
|
||||
@@ -653,12 +655,13 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
}
|
||||
|
||||
$filterName = $this->className . 'TemplatesID';
|
||||
if(isset($_GET['templates_id'])) {
|
||||
$this->session->set($filterName, (int) $this->input->get('templates_id'));
|
||||
$templatesId = (int) $this->wire()->input->get('templates_id');
|
||||
if($templatesId) {
|
||||
$this->wire()->session->set($filterName, (int) $templatesId);
|
||||
}
|
||||
|
||||
$filterValue = (int) $this->session->$filterName;
|
||||
if($filterValue) $this->template = $this->templates->get($filterValue);
|
||||
$filterValue = (int) $this->wire()->session->get($filterName);
|
||||
if($filterValue) $this->template = $this->wire()->templates->get($filterValue);
|
||||
|
||||
$field->attr('value', $filterValue);
|
||||
$form->append($field);
|
||||
@@ -677,12 +680,14 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
|
||||
$showFields = isset($data['showFields']) ? $data['showFields'] : array();
|
||||
$fields = array('name');
|
||||
foreach($this->wire('fields') as $field) $fields[] = $field->name;
|
||||
foreach($this->wire()->fields as $field) {
|
||||
$fields[] = $field->name;
|
||||
}
|
||||
|
||||
/** @var InputfieldWrapper $inputfields */
|
||||
$inputfields = $this->wire(new InputfieldWrapper());
|
||||
/** @var InputfieldAsmSelect $f */
|
||||
$f = $this->wire('modules')->get('InputfieldAsmSelect');
|
||||
$f = $this->wire()->modules->get('InputfieldAsmSelect');
|
||||
$f->label = $this->_("What fields should be displayed in the page listing?");
|
||||
$f->attr('id+name', 'showFields');
|
||||
foreach($fields as $name) $f->addOption($name);
|
||||
@@ -700,7 +705,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
*/
|
||||
public function getPage() {
|
||||
if($this->editor) return $this->editor->getPage();
|
||||
return $this->wire('pages')->newNullPage();
|
||||
return $this->wire()->pages->newNullPage();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -743,7 +748,7 @@ class ProcessPageType extends Process implements ConfigurableModule, WirePageEdi
|
||||
return $result;
|
||||
}
|
||||
|
||||
$text = $this->wire('sanitizer')->selectorValue($text);
|
||||
$text = $this->wire()->sanitizer->selectorValue($text);
|
||||
$property = empty($options['property']) ? 'name' : $options['property'];
|
||||
$operator = isset($options['operator']) ? $options['operator'] : '%=';
|
||||
$selector = "$property$operator$text, ";
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* For more details about how Process modules work, please see:
|
||||
* /wire/core/Process.php
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
|
||||
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
*/
|
||||
@@ -22,7 +22,7 @@ class ProcessPermission extends ProcessPageType {
|
||||
'permission' => 'permission-admin', // add this permission if you want this Process available for roles other than Superuser
|
||||
'icon' => 'gear',
|
||||
'useNavJSON' => true,
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,29 +33,34 @@ class ProcessPermission extends ProcessPageType {
|
||||
*/
|
||||
protected function getOptionalPermissionsForm() {
|
||||
|
||||
$form = $this->wire('modules')->get('InputfieldForm');
|
||||
$modules = $this->wire()->modules;
|
||||
$permissions = $this->wire()->permissions;
|
||||
|
||||
/** @var InputfieldForm $form */
|
||||
$form = $modules->get('InputfieldForm');
|
||||
$form->action = '../install-permissions/';
|
||||
$form->method = 'post';
|
||||
$form->attr('id', 'ProcessPermissionAddSystem');
|
||||
|
||||
$fieldset = $this->wire('modules')->get('InputfieldFieldset');
|
||||
|
||||
/** @var InputfieldFieldset $fieldset */
|
||||
$fieldset = $modules->get('InputfieldFieldset');
|
||||
$fieldset->label = $this->_('Install predefined system permissions');
|
||||
$fieldset->collapsed = Inputfield::collapsedYes;
|
||||
$fieldset->icon = 'gear';
|
||||
|
||||
$form->add($fieldset);
|
||||
|
||||
$optionalPermissions = $this->wire('permissions')->getOptionalPermissions();
|
||||
$optionalPermissions = $permissions->getOptionalPermissions();
|
||||
|
||||
if(count($optionalPermissions)) {
|
||||
$reducerPermissions = $this->wire('permissions')->getReducerPermissions();
|
||||
|
||||
$f = $this->wire('modules')->get('InputfieldCheckboxes');
|
||||
$reducerPermissions = $permissions->getReducerPermissions();
|
||||
|
||||
/** @var InputfieldCheckboxes $f */
|
||||
$f = $modules->get('InputfieldCheckboxes');
|
||||
$f->name = 'install_permissions';
|
||||
$f->label = $this->_('Check the box next to each optional permission you would like to install.');
|
||||
$f->table = true;
|
||||
|
||||
|
||||
foreach($optionalPermissions as $name => $label) {
|
||||
$displayName = $name;
|
||||
if(isset($reducerPermissions[$name])) $displayName .= '*';
|
||||
@@ -69,7 +74,8 @@ class ProcessPermission extends ProcessPageType {
|
||||
|
||||
$fieldset->add($f);
|
||||
|
||||
$button = $this->wire('modules')->get('InputfieldSubmit');
|
||||
/** @var InputfieldSubmit $button */
|
||||
$button = $modules->get('InputfieldSubmit');
|
||||
$button->name = 'submit_install_permissions';
|
||||
$button->value = $this->_('Install');
|
||||
|
||||
@@ -100,7 +106,7 @@ class ProcessPermission extends ProcessPageType {
|
||||
public function ___executeAdd() {
|
||||
|
||||
// hide the title field, since it is counterproductive when adding a new permission
|
||||
$template = $this->wire('templates')->get('permission');
|
||||
$template = $this->wire()->templates->get('permission');
|
||||
$titleField = $template->fieldgroup->getField('title');
|
||||
$titleCollapsed = $titleField->collapsed;
|
||||
$titleField->collapsed = Inputfield::collapsedYesLocked;
|
||||
@@ -111,8 +117,8 @@ class ProcessPermission extends ProcessPageType {
|
||||
$form = $this->getOptionalPermissionsForm();
|
||||
|
||||
if($form) {
|
||||
$fieldset = $this->wire(new InputfieldWrapper());
|
||||
$f = $this->wire('modules')->get('InputfieldMarkup');
|
||||
$fieldset = $this->wire(new InputfieldWrapper()); /** @var InputfieldWrapper $fieldset */
|
||||
$f = $this->wire()->modules->get('InputfieldMarkup'); /** @var InputfieldMarkup $f */
|
||||
$f->attr('id', 'ProcessPermissionAddCustom');
|
||||
$f->value = $out;
|
||||
$f->label = $this->_('Add a new custom permission');
|
||||
@@ -127,28 +133,30 @@ class ProcessPermission extends ProcessPageType {
|
||||
|
||||
protected function ___executeInstallPermissions() {
|
||||
|
||||
$user = $this->wire('user');
|
||||
$languages = $this->wire('languages');
|
||||
$user = $this->wire()->user;
|
||||
$permissions = $this->wire()->permissions;
|
||||
$languages = $this->wire()->languages;
|
||||
$userLanguage = null;
|
||||
|
||||
if($languages) {
|
||||
$userLanguage = $user->language;
|
||||
$user->language = $languages->getDefault();
|
||||
}
|
||||
|
||||
$installPermissions = $this->wire('input')->post->array('install_permissions', 'pageName');
|
||||
$optionalPermissions = $this->wire('permissions')->getOptionalPermissions();
|
||||
$installPermissions = $this->wire()->input->post->array('install_permissions', 'pageName');
|
||||
$optionalPermissions = $permissions->getOptionalPermissions();
|
||||
|
||||
foreach($installPermissions as $name) {
|
||||
if(!isset($optionalPermissions[$name])) continue;
|
||||
if($this->wire('permissions')->has($name)) continue; // already installed
|
||||
$permission = $this->wire('permissions')->add($name);
|
||||
if($permissions->has($name)) continue; // already installed
|
||||
$permission = $permissions->add($name);
|
||||
if(!$permission->id) continue;
|
||||
$permission->title = $optionalPermissions[$name];
|
||||
if($languages && $permission->title instanceof LanguagesValueInterface) {
|
||||
// if the permission titles have been translated, ensure that the translation goes in for each language
|
||||
foreach($languages as $language) {
|
||||
if($language->isDefault()) continue;
|
||||
$a = $this->wire('permissions')->getOptionalPermissions();
|
||||
$a = $permissions->getOptionalPermissions();
|
||||
if($a[$name] == $optionalPermissions[$name]) continue;
|
||||
$permission->title->setLanguageValue($language, $a[$name]);
|
||||
}
|
||||
@@ -159,8 +167,7 @@ class ProcessPermission extends ProcessPageType {
|
||||
|
||||
if($userLanguage) $user->language = $userLanguage;
|
||||
|
||||
$this->wire('session')->redirect('../');
|
||||
$this->wire()->session->location('../');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* For more details about how Process modules work, please see:
|
||||
* /wire/core/Process.php
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2018 by Ryan Cramer
|
||||
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
*/
|
||||
@@ -22,7 +22,7 @@ class ProcessRole extends ProcessPageType {
|
||||
'permission' => 'role-admin', // add this permission if you want this Process available for roles other than Superuser
|
||||
'icon' => 'gears',
|
||||
'useNavJSON' => true,
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,9 +64,12 @@ class ProcessRole extends ProcessPageType {
|
||||
public function init() {
|
||||
|
||||
parent::init();
|
||||
|
||||
$this->wire('modules')->get('JqueryUI')->use('vex');
|
||||
$this->guestRole = $this->wire('roles')->get($this->wire('config')->guestUserRolePageID);
|
||||
|
||||
/** @var JqueryUI $jQueryUI */
|
||||
$jQueryUI = $this->wire()->modules->get('JqueryUI');
|
||||
$jQueryUI->use('vex');
|
||||
|
||||
$this->guestRole = $this->wire()->roles->get($this->wire()->config->guestUserRolePageID);
|
||||
$this->addHookBefore('InputfieldForm::render', $this, 'hookFormRender');
|
||||
$this->addHookBefore('ProcessPageEdit::processInput', $this, 'hookProcessInput');
|
||||
|
||||
@@ -109,7 +112,7 @@ class ProcessRole extends ProcessPageType {
|
||||
*/
|
||||
public function hookProcessInput(HookEvent $event) {
|
||||
static $n = 0;
|
||||
if(!$n && $event->wire('input')->post('_pw_page_name')) {
|
||||
if(!$n && $event->wire()->input->post('_pw_page_name')) {
|
||||
$this->savePermissionOptions();
|
||||
$n++;
|
||||
}
|
||||
@@ -129,7 +132,7 @@ class ProcessRole extends ProcessPageType {
|
||||
$f = $form->getChildByName('permissions');
|
||||
if(!$f) return;
|
||||
|
||||
if($this->getPage()->id == $this->wire('config')->superUserRolePageID) {
|
||||
if($this->getPage()->id == $this->wire()->config->superUserRolePageID) {
|
||||
$f->wrapAttr('style', 'display:none');
|
||||
$fn = $form->getChildByName('_pw_page_name');
|
||||
if($fn) $fn->notes = $this->_('Note: superuser role always has all permissions, so permissions field is not shown.');
|
||||
@@ -153,10 +156,12 @@ class ProcessRole extends ProcessPageType {
|
||||
$options = $f->getOptions();
|
||||
$pageViewID = 0;
|
||||
|
||||
foreach($options as $name => $label) $f->removeOption($name);
|
||||
foreach($options as $name => $label) {
|
||||
$f->removeOption($name);
|
||||
}
|
||||
|
||||
// establish root permission containers
|
||||
foreach($this->wire('permissions') as $permission) {
|
||||
foreach($this->wire()->permissions as $permission) {
|
||||
if($permission->getParentPermission()->id) continue;
|
||||
$permissions[$permission->name] = array();
|
||||
if($permission->name == 'page-view') $pageViewID = $permission->id;
|
||||
@@ -171,7 +176,7 @@ class ProcessRole extends ProcessPageType {
|
||||
$pageEdit = $permissions['page-edit'];
|
||||
$permissions = array_merge(array('page-view' => $pageView, 'page-edit' => $pageEdit), $permissions);
|
||||
|
||||
foreach($this->wire('permissions') as $permission) {
|
||||
foreach($this->wire()->permissions as $permission) {
|
||||
/** @var Permission $permission */
|
||||
/** @var Permission $parent */
|
||||
$parent = $permission->getParentPermission();
|
||||
@@ -227,7 +232,7 @@ class ProcessRole extends ProcessPageType {
|
||||
$pageEditTemplates = array();
|
||||
|
||||
if($name == 'page-add' || $name == 'page-create') {
|
||||
$parent = $this->wire('permissions')->get('page-edit');
|
||||
$parent = $this->wire()->permissions->get('page-edit');
|
||||
$rootParent = $parent;
|
||||
$permission = new Permission();
|
||||
$permission->set('name', $name);
|
||||
@@ -238,9 +243,9 @@ class ProcessRole extends ProcessPageType {
|
||||
}
|
||||
$alert = $this->_('This permission can only be assigned by template.');
|
||||
} else {
|
||||
$permission = $this->wire('permissions')->get($name);
|
||||
$permission = $this->wire()->permissions->get($name);
|
||||
if(!$permission->id) continue;
|
||||
$title = str_replace('|', ' ', $this->wire('sanitizer')->entities($permission->getUnformatted('title')));
|
||||
$title = str_replace('|', ' ', $this->wire()->sanitizer->entities($permission->getUnformatted('title')));
|
||||
$parent = $permission->getParentPermission();
|
||||
$rootParent = $permission->getRootParentPermission();
|
||||
$checked = in_array($permission->id, $inputfieldValue);
|
||||
@@ -262,8 +267,7 @@ class ProcessRole extends ProcessPageType {
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->wire('templates') as $template) {
|
||||
/** @var Template $template */
|
||||
foreach($this->wire()->templates as $template) {
|
||||
|
||||
if(!$template->useRoles) continue;
|
||||
$rolesPermissions = $template->rolesPermissions;
|
||||
@@ -548,7 +552,7 @@ class ProcessRole extends ProcessPageType {
|
||||
$attr['href'] = $href;
|
||||
$out = "<a ";
|
||||
foreach($attr as $key => $value) {
|
||||
$out .= " $key='" . $this->wire('sanitizer')->entities($value) . "'";
|
||||
$out .= " $key='" . $this->wire()->sanitizer->entities($value) . "'";
|
||||
}
|
||||
$out .= ">$text</a>";
|
||||
return $out;
|
||||
@@ -592,13 +596,13 @@ class ProcessRole extends ProcessPageType {
|
||||
if(!$role->id) return;
|
||||
$isGuestRole = $role->id == $this->guestRole->id;
|
||||
|
||||
/** @var WireInput $input */
|
||||
$input = $this->wire('input');
|
||||
$input = $this->wire()->input;
|
||||
$permissions = $this->wire()->permissions;
|
||||
|
||||
$viewPermission = $this->wire('permissions')->get('page-view');
|
||||
$editPermission = $this->wire('permissions')->get('page-edit');
|
||||
$viewPermission = $permissions->get('page-view');
|
||||
$editPermission = $permissions->get('page-edit');
|
||||
|
||||
foreach($this->wire('templates') as $template) {
|
||||
foreach($this->wire()->templates as $template) {
|
||||
|
||||
/** @var Template $template */
|
||||
if(!$template->useRoles) continue;
|
||||
@@ -694,7 +698,7 @@ class ProcessRole extends ProcessPageType {
|
||||
|
||||
if(!$edit) {
|
||||
/** @var Permission $permission */
|
||||
$permission = $this->wire('permissions')->get((int) $permissionID);
|
||||
$permission = $permissions->get((int) $permissionID);
|
||||
if(!$permission->id) continue;
|
||||
$parentPermission = $permission->getParentPermission();
|
||||
// if permission requires page-edit, and user doesn't have page-edit, don't allow it to be added
|
||||
@@ -717,18 +721,18 @@ class ProcessRole extends ProcessPageType {
|
||||
sort($rolePermissionsNew);
|
||||
sort($rolePermissions);
|
||||
if($rolePermissionsNew != $rolePermissions) {
|
||||
if($this->wire('config')->debug) {
|
||||
if($this->wire()->config->debug) {
|
||||
$removedPermissions = array_diff($rolePermissions, $rolePermissionsNew);
|
||||
$addedPermissions = array_diff($rolePermissionsNew, $rolePermissions);
|
||||
foreach($removedPermissions as $permissionID) {
|
||||
$permissionID = (int) $permissionID;
|
||||
$permission = $this->wire('permissions')->get(abs($permissionID));
|
||||
$permission = $permissions->get(abs($permissionID));
|
||||
$updates[] = ($permissionID < 0 ? "Removed revoke" : "Removed add") . " " .
|
||||
"$permission->name for template $template->name" ;
|
||||
}
|
||||
foreach($addedPermissions as $permissionID) {
|
||||
$permissionID = (int) $permissionID;
|
||||
$permission = $this->wire('permissions')->get(abs($permissionID));
|
||||
$permission = $permissions->get(abs($permissionID));
|
||||
$updates[] = ($permissionID < 0 ? "Added revoke" : "Added add") . " " .
|
||||
"$permission->name for template $template->name" ;
|
||||
}
|
||||
@@ -745,7 +749,7 @@ class ProcessRole extends ProcessPageType {
|
||||
if($addRoles != $template->addRoles) $template->addRoles = $addRoles;
|
||||
if($createRoles != $template->createRoles) $template->createRoles = $createRoles;
|
||||
|
||||
if($this->wire('config')->debug) {
|
||||
if($this->wire()->config->debug) {
|
||||
foreach($updates as $update) $this->message($update);
|
||||
}
|
||||
|
||||
@@ -754,4 +758,3 @@ class ProcessRole extends ProcessPageType {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* For more details about how Process modules work, please see:
|
||||
* /wire/core/Process.php
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer
|
||||
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
* @property int $maxAjaxQty
|
||||
@@ -44,15 +44,16 @@ class ProcessUser extends ProcessPageType {
|
||||
*
|
||||
*/
|
||||
public function init() {
|
||||
$this->wire('pages')->addHookBefore('save', $this, 'hookPageSave');
|
||||
$this->wire()->pages->addHookBefore('save', $this, 'hookPageSave');
|
||||
parent::init();
|
||||
|
||||
if($this->lister) {
|
||||
$this->lister->addHookBefore('execute', $this, 'hookListerExecute');
|
||||
$this->lister->addHookAfter('getSelector', $this, 'hookListerGetSelector');
|
||||
}
|
||||
|
||||
// make this field translatable
|
||||
$roles = $this->wire('fields')->get('roles');
|
||||
$roles = $this->wire()->fields->get('roles');
|
||||
$roles->label = $this->_('Roles');
|
||||
$roles->description = $this->_("User will inherit the permissions assigned to each role. You may assign multiple roles to a user. When accessing a page, the user will only inherit permissions from the roles that are also assigned to the page's template."); // Roles description
|
||||
}
|
||||
@@ -64,7 +65,7 @@ class ProcessUser extends ProcessPageType {
|
||||
*
|
||||
*/
|
||||
protected function useLister() {
|
||||
return $this->wire('user')->hasPermission('page-lister');
|
||||
return $this->wire()->user->hasPermission('page-lister');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,12 +80,12 @@ class ProcessUser extends ProcessPageType {
|
||||
$max = $this->maxAjaxQty > 0 && $this->maxAjaxQty <= 100 ? (int) $this->maxAjaxQty : 50;
|
||||
|
||||
if(empty($options) && $this->pages->count("id>0") > $max) {
|
||||
$user = $this->wire('user');
|
||||
$userAdminAll = $user->isSuperuser() ? $this->wire('pages')->newNullPage() : $this->wire('permissions')->get('user-admin-all');
|
||||
$user = $this->wire()->user;
|
||||
$userAdminAll = $user->isSuperuser() ? $this->wire()->pages->newNullPage() : $this->wire()->permissions->get('user-admin-all');
|
||||
/** @var PagePermissions $pagePermissions */
|
||||
$pagePermissions = $this->wire('modules')->get('PagePermissions');
|
||||
$pagePermissions = $this->wire()->modules->get('PagePermissions');
|
||||
$items = array();
|
||||
foreach($this->wire('roles') as $role) {
|
||||
foreach($this->wire()->roles as $role) {
|
||||
if($userAdminAll->id && !$pagePermissions->userCanAssignRole($role)) continue;
|
||||
$cnt = $this->pages->count("roles=$role");
|
||||
$item = array(
|
||||
@@ -229,7 +230,8 @@ class ProcessUser extends ProcessPageType {
|
||||
$filterRole = $filterRoleId ? $this->wire()->roles->get($filterRoleId) : null;
|
||||
$user = $this->wire()->user;
|
||||
$session = $this->wire()->session;
|
||||
$ajax = $this->wire('config')->ajax;
|
||||
$config = $this->wire()->config;
|
||||
$ajax = $config->ajax;
|
||||
|
||||
if($filterRole && $filterRole->id) {
|
||||
$lister->resetLister();
|
||||
@@ -252,7 +254,7 @@ class ProcessUser extends ProcessPageType {
|
||||
// system has user-admin-all permission, and user doesn't have it
|
||||
// so limit them only to the permission user-admin-[role] roles that they have assigned
|
||||
$roles = array();
|
||||
$rolesSelector .= ", roles!=" . $this->wire()->config->superUserRolePageID;
|
||||
$rolesSelector .= ", roles!=" . $config->superUserRolePageID;
|
||||
foreach($user->getPermissions() as $permission) {
|
||||
if(strpos($permission->name, 'user-admin-') !== 0) continue;
|
||||
$roleName = str_replace('user-admin-', '', $permission->name);
|
||||
@@ -260,7 +262,6 @@ class ProcessUser extends ProcessPageType {
|
||||
if($rolePage->id) $roles[$rolePage->id] = $rolePage->id;
|
||||
}
|
||||
// allow them to view users that only have guest role
|
||||
$config = $this->wire()->config;
|
||||
$guestRoleID = $config->guestUserRolePageID;
|
||||
$guestUserID = $config->guestUserPageID;
|
||||
$rolesSelector .= ", id!=$guestUserID, roles=(roles.count=1, roles=$guestRoleID)";
|
||||
@@ -296,7 +297,7 @@ class ProcessUser extends ProcessPageType {
|
||||
}
|
||||
}
|
||||
/** @var User $page */
|
||||
if($page->id && !$page->get('_rolesPrevious') && $this->wire('input')->post('roles') !== null) {
|
||||
if($page->id && !$page->get('_rolesPrevious') && $this->wire()->input->post('roles') !== null) {
|
||||
$page->setQuietly('_rolesPrevious', clone $page->roles);
|
||||
}
|
||||
return $page;
|
||||
@@ -310,13 +311,13 @@ class ProcessUser extends ProcessPageType {
|
||||
*
|
||||
*/
|
||||
protected function getEditableRoles(User $page) {
|
||||
/** @var User $user */
|
||||
$user = $this->wire('user');
|
||||
|
||||
$user = $this->wire()->user;
|
||||
$superuser = $user->isSuperuser();
|
||||
$editableRoles = array();
|
||||
$userAdminAll = $this->wire('permissions')->get('user-admin-all');
|
||||
$userAdminAll = $this->wire()->permissions->get('user-admin-all');
|
||||
|
||||
foreach($this->wire('roles') as $role) {
|
||||
foreach($this->wire()->roles as $role) {
|
||||
if($role->name == 'guest') continue;
|
||||
// if non-superuser editing a user, don't allow them to assign new roles with user-admin permission,
|
||||
// unless the user already has the role checked, OR the non-superuser has user-admin-all permission
|
||||
@@ -362,13 +363,12 @@ class ProcessUser extends ProcessPageType {
|
||||
/**
|
||||
* Edit user
|
||||
*
|
||||
* @return array|string
|
||||
* @return string
|
||||
*
|
||||
*/
|
||||
public function ___executeEdit() {
|
||||
|
||||
/** @var User $user */
|
||||
$user = $this->wire('user');
|
||||
$user = $this->wire()->user;
|
||||
|
||||
if(!$user->isSuperuser()) {
|
||||
// prevent showing superuser role at all
|
||||
@@ -382,7 +382,7 @@ class ProcessUser extends ProcessPageType {
|
||||
/** @var User $page Available only after executeEdit() */
|
||||
$page = $this->getPage();
|
||||
|
||||
$this->wire('config')->js('ProcessUser', array(
|
||||
$this->wire()->config->js('ProcessUser', array(
|
||||
'editableRoles' => array_keys($this->getEditableRoles($page)),
|
||||
'notEditableAlert' => $this->_('You may not change this role'),
|
||||
));
|
||||
@@ -418,7 +418,7 @@ class ProcessUser extends ProcessPageType {
|
||||
$theme = $form->getChildByName('admin_theme');
|
||||
if(!$theme) return;
|
||||
if(!$theme->attr('value')) {
|
||||
$theme->attr('value', $this->wire('config')->defaultAdminTheme);
|
||||
$theme->attr('value', $this->wire()->config->defaultAdminTheme);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -434,22 +434,24 @@ class ProcessUser extends ProcessPageType {
|
||||
*/
|
||||
public function hookPageSave(HookEvent $event) {
|
||||
|
||||
$config = $this->wire()->config;
|
||||
$arguments = $event->arguments;
|
||||
$page = $arguments[0];
|
||||
$page = $arguments[0]; /** @var Page|User $page */
|
||||
|
||||
if(!$page instanceof User && !in_array($page->template->id, $this->wire('config')->userTemplateIDs)) {
|
||||
if(!$page instanceof User && !in_array($page->template->id, $config->userTemplateIDs)) {
|
||||
// don't handle anything other than User page saves
|
||||
return;
|
||||
}
|
||||
|
||||
$pages = $this->wire('pages');
|
||||
$user = $this->wire('user');
|
||||
$pages = $this->wire()->pages;
|
||||
$user = $this->wire()->user;
|
||||
$roles = $this->wire()->roles;
|
||||
$superuser = $user->isSuperuser();
|
||||
$suRole = $this->wire('roles')->get($this->wire('config')->superUserRolePageID);
|
||||
$suRole = $roles->get($config->superUserRolePageID);
|
||||
|
||||
// don't allow removal of the guest role
|
||||
if(!$page->roles->has("name=guest")) {
|
||||
$page->roles->add($this->wire('roles')->get('guest'));
|
||||
$page->roles->add($roles->get('guest'));
|
||||
}
|
||||
|
||||
// check if user is editing themself
|
||||
@@ -533,21 +535,21 @@ class ProcessUser extends ProcessPageType {
|
||||
|
||||
if($user->isSuperuser()) return;
|
||||
|
||||
$userAdminAll = $this->wire('permissions')->get('user-admin-all');
|
||||
$userAdminAll = $this->wire()->permissions->get('user-admin-all');
|
||||
if(!$userAdminAll->id || $user->hasPermission($userAdminAll)) return;
|
||||
|
||||
// user-admin-all permission is installed and user doesn't have it
|
||||
// check that the role assignments are valid
|
||||
|
||||
/** @var Pages $pages */
|
||||
$pages = $this->wire('pages');
|
||||
$pages = $this->wire()->pages;
|
||||
$changedUser = $page;
|
||||
$pages->uncache($page, array('shallow' => true));
|
||||
$originalUser = $this->wire('users')->get($page->id); // get a fresh, unmodified copy
|
||||
$originalUser = $this->wire()->users->get($page->id); // get a fresh, unmodified copy
|
||||
|
||||
if(!$originalUser->id) return;
|
||||
|
||||
/** @var PagePermissions $pagePermissions */
|
||||
$pagePermissions = $this->wire('modules')->get('PagePermissions');
|
||||
$pagePermissions = $this->wire()->modules->get('PagePermissions');
|
||||
$removedRoles = array();
|
||||
|
||||
foreach($originalUser->roles as $role) {
|
||||
@@ -579,4 +581,3 @@ class ProcessUser extends ProcessPageType {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user