1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 17:24:46 +02:00
This commit is contained in:
Ryan Cramer
2021-08-06 09:49:01 -04:00
parent 29c58f836b
commit 67df29ba87
2 changed files with 95 additions and 7 deletions

View File

@@ -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

View File

@@ -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