diff --git a/wire/core/PagesNames.php b/wire/core/PagesNames.php index 421c5d45..7a83eeb1 100644 --- a/wire/core/PagesNames.php +++ b/wire/core/PagesNames.php @@ -765,15 +765,18 @@ class PagesNames extends Wire { */ public function pageNameHasConflict(Page $page) { + $config = $this->wire()->config; + $usersPageIDs = $config->usersPageIDs; + $checkUser = in_array($page->parent_id, $usersPageIDs); $reason = ''; $name = $page->name; - if($this->wire('config')->pageNameCharset == 'UTF8') { - $name = $this->wire('sanitizer')->pageName($name, Sanitizer::toAscii); + if($config->pageNameCharset == 'UTF8') { + $name = $this->wire()->sanitizer->pageName($name, Sanitizer::toAscii); } $sql = "SELECT id, status, parent_id FROM pages WHERE name=:name AND id!=:id"; - $query = $this->wire('database')->prepare($sql); + $query = $this->wire()->database->prepare($sql); $query->bindValue(':name', $name); $query->bindValue(':id', $page->id, \PDO::PARAM_INT); $query->execute(); @@ -784,15 +787,27 @@ class PagesNames extends Wire { } while($row = $query->fetch(\PDO::FETCH_ASSOC)) { - if($row['status'] & Page::statusUnique) { + + $parentID = (int) $row['parent_id']; + $status = (int) $row['status']; + + if($status & Page::statusUnique) { // name is already required to be unique globally $reason = sprintf($this->_("Another page is using name “%s” and requires it to be globally unique"), $page->name); + break; } - if((int) $row['parent_id'] === $page->parent_id) { + + if($checkUser && in_array($parentID, $usersPageIDs)) { + // username collision + $reason = sprintf($this->_('Another user is already using the name “%s”'), $page->name); + break; + } + + if($parentID === $page->parent_id) { // name already consumed by another page with same parent $reason = sprintf($this->_('Another page with same parent is already using name “%s”'), $page->name); - } - if($reason) break; + break; + } } // page requires that it be the only one with this name, so if others have it, then disallow diff --git a/wire/modules/Process/ProcessUser/ProcessUser.module b/wire/modules/Process/ProcessUser/ProcessUser.module index 1861990c..b5b88c67 100644 --- a/wire/modules/Process/ProcessUser/ProcessUser.module +++ b/wire/modules/Process/ProcessUser/ProcessUser.module @@ -98,6 +98,79 @@ class ProcessUser extends ProcessPageType { return parent::___executeNavJSON($options); } + + /** + * Add item of this page type + * + * @return string + * + */ + public function ___executeAdd() { + + // use parent method if there are no custom user parents or templates + $config = $this->wire()->config; + if(count($config->usersPageIDs) < 2 && count($config->userTemplateIDs) < 2) return parent::___executeAdd(); + + $input = $this->wire()->input; + $pages = $this->wire()->pages; + $parentId = (int) $input->get('parent_id'); + $parent = $parentId ? $pages->get($parentId) : null; + $userTemplates = $this->pages->getTemplates(); + $userParentIds = $this->pages->getParentIDs(); + + // if requested parent not one allowed by config.usersPageIDs then disregard it + if($parent && !in_array($parent->id, $userParentIds, true)) $parent = null; + + // delegate to ProcessPageAdd + $editor = $this->wire()->modules->getModule('ProcessPageAdd'); /** @var ProcessPageAdd $editor */ + $this->editor = $editor; + $editor->setEditor($this); // set us as the parent editor + + // identify parent(s) allowed + if(count($userParentIds) > 1 && $parent) { + // more than one parent allowed but only one requested + $parents = $pages->newPageArray(); + $parents->add($parent); + $editor->parent_id = $parent->id; + $editor->setPredefinedParents($parents); + } else { + // one or more parents allowed + $editor->setPredefinedParents($pages->getById($userParentIds)); + } + + // identify template(s) allowed + if(count($userTemplates) < 2) { + // only one user template allowed + $editor->template = $this->template; + } else if($parent) { + // parent specified, reduce to only allowed templates for parent + $childTemplates = $parent->template->childTemplates; + if(count($childTemplates)) { + foreach($userTemplates as $key => $template) { + /** @var Template $template */ + if(!in_array($template->id, $childTemplates)) unset($userTemplates[$key]); + } + } + if(!count($userTemplates)) $userTemplates = array($this->template); + } + + $editor->setPredefinedTemplates($userTemplates); + + try { + $out = $editor->execute(); + } catch(\Exception $e) { + $out = ''; + $this->error($e->getMessage()); + if($input->is('POST')) { + $this->wire()->session->location("./" . ($parentId ? "?parent_id=$parentId" : "")); + } + } + + $this->browserTitle($this->page->get('title|name') . " > $this->addLabel"); + + return $out; + } + /** * Hook to ProcessPageLister::execute method to adjust selector to show specific roles