1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Minor code improvements in ProcessPageAdd module (mostly just code/API usage consistency adjustments)

This commit is contained in:
Ryan Cramer
2021-08-06 14:21:58 -04:00
parent f39d836bf1
commit f0a02bd70a

View File

@@ -8,7 +8,7 @@
* For more details about how Process modules work, please see:
* /wire/core/Process.php
*
* ProcessWire 3.x, Copyright 2017 by Ryan Cramer
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com
*
* @method string executeTemplate()
@@ -103,7 +103,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
'permission' => 'page-edit',
'icon' => 'plus-circle',
'useNavJSON' => true,
);
);
}
/**
@@ -115,7 +115,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
parent::__construct();
$this->set('noAutoPublish', false);
$this->set('shortcutSort', array());
$settings = $this->wire('config')->pageAdd;
$settings = $this->wire()->config->pageAdd;
if(is_array($settings)) $this->settings = array_merge($this->settings, $settings);
}
@@ -137,9 +137,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*
*/
public function set($key, $value) {
if($key == 'parent_id') $this->parent_id = (int) $value;
else if($key == 'template' && $value instanceof Template) $this->template = $value;
else return parent::set($key, $value);
if($key === 'parent_id') {
$this->parent_id = (int) "$value";
} else if($key === 'template') {
if(is_string($value) || is_int($value)) $value = $this->wire()->templates->get($value);
if($value instanceof Template) $this->template = $value;
} else {
return parent::set($key, $value);
}
return $this;
}
@@ -154,15 +159,19 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
public function ___executeNavJSON(array $options = array()) {
$page = $this->wire('page');
$user = $this->wire('user');
/** @var Session $session */
$session = $this->wire('session');
$page = $this->wire()->page;
$user = $this->wire()->user;
$config = $this->wire()->config;
$session = $this->wire()->session;
$modules = $this->wire()->modules;
$templates = $this->wire()->templates;
$sanitizer = $this->wire()->sanitizer;
$data = $session->getFor($this, 'nav');
if(!empty($data)) {
// check that session cache data is still current
foreach($this->wire('templates') as $template) {
foreach($templates as $template) {
if($template->modified > $data['modified']) {
$data = array();
$session->remove($this, 'nav');
@@ -176,7 +185,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(empty($data)) {
$data = array(
'url' => $this->wire('config')->urls->admin . 'page/add/',
'url' => $config->urls->admin . 'page/add/',
'label' => $this->_((string) $page->get('title|name')),
'icon' => 'plus-circle',
'add' => null,
@@ -190,7 +199,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$items = array();
foreach($this->wire('templates') as $template) {
foreach($templates as $template) {
$parent = $template->getParentPage(true);
if(!$parent) continue;
if($parent->id) {
@@ -205,7 +214,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(!$icon) $icon = "plus-circle";
$label = $template->getLabel();
$key = strtolower($label);
$label = $this->wire('sanitizer')->entities1($label);
$label = $sanitizer->entities1($label);
if(isset($items[$key])) $key .= $template->name;
$items[$key] = array(
'url' => $qs,
@@ -217,7 +226,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
}
ksort($items);
$configData = $this->wire('modules')->getModuleConfigData($this); // because admin theme calls with noInit option
$configData = $modules->getModuleConfigData($this); // because admin theme calls with noInit option
$shortcutSort = isset($configData['shortcutSort']) ? $configData['shortcutSort'] : array();
if(!is_array($shortcutSort)) $shortcutSort = array();
if(!empty($shortcutSort)) {
@@ -270,7 +279,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$session->setFor($this, 'numAddable', $listLength + $n);
if(!empty($options['getArray'])) return $data;
if($this->wire('config')->ajax) header("Content-Type: application/json");
if($config->ajax) header("Content-Type: application/json");
return json_encode($data);
}
@@ -284,43 +293,57 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
public function ___executeTemplate() {
$modules = $this->wire()->modules;
$pages = $this->wire()->pages;
$input = $this->wire()->input;
$templateID = (int) $this->input->get('template_id');
if(!$templateID) throw new WireException("No template specified");
$template = $this->templates->get($templateID);
if(!$template) throw new WireException("Unknown template");
$parentTemplates = new TemplatesArray();
foreach($template->parentTemplates as $templateID) {
$t = $this->templates->get((int) $templateID);
if($t) $parentTemplates->add($t);
}
if(!count($parentTemplates)) throw new WireException("No parent templates defined for $template->name");
$parentTemplateIDs = $parentTemplates->implode('|', 'id');
$parents = $this->wire('pages')->find("templates_id=$parentTemplateIDs, include=hidden, limit=500, sort=name");
$parents = $pages->find("templates_id=$parentTemplateIDs, include=hidden, limit=500, sort=name");
if(!count($parents)) throw new WireException("No usable parents match this template");
if(count($parents) == 1) {
$url = "./?parent_id=" . $parents->first()->id;
if($this->wire('input')->get('modal')) $url .= "&modal=1";
$this->wire('session')->redirect($url);
if($input->get('modal')) $url .= "&modal=1";
$this->redirect($url);
}
$templateLabel = $this->getTemplateLabel($template);
$form = $this->wire('modules')->get('InputfieldForm');
/** @var InputfieldForm $form */
$form = $modules->get('InputfieldForm');
$form->description = $this->getTemplateLabel($template);
$form->method = 'get';
$form->action = './';
$form->attr('id', 'select_parent_form');
if($this->wire('input')->get('modal')) {
$f = $this->wire('modules')->get('InputfieldHidden');
if($input->get('modal')) {
/** @var InputfieldHidden $f */
$f = $modules->get('InputfieldHidden');
$f->attr('name', 'modal');
$f->attr('value', 1);
$form->add($f);
}
$f = $this->wire('modules')->get('InputfieldSelect');
/** @var InputfieldSelect $f */
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'parent_id');
$f->attr('id', 'select_parent_id');
$f->label = sprintf($this->_('Where do you want to add the new %s?'), "\"$templateLabel\"");
$f->label = sprintf($this->_('Where do you want to add the new %s?'), "{$templateLabel}");
$f->description = sprintf($this->_('Please select a parent %s page below:'), ''); // Select parent label // you can omit the '%s' (no longer used)
$options = array();
@@ -338,12 +361,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$form->add($f);
$f = $this->wire('modules')->get('InputfieldHidden');
/** @var InputfieldHidden $f */
$f = $modules->get('InputfieldHidden');
$f->attr('name', 'template_id');
$f->attr('value', $template->id);
$form->add($f);
$f = $this->wire('modules')->get('InputfieldSubmit');
/** @var InputfieldSubmit $f */
$f = $modules->get('InputfieldSubmit');
$f->attr('id', 'select_parent_submit');
$form->add($f);
@@ -357,28 +382,32 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*
*/
public function renderChooseTemplate() {
$pages = $this->wire()->pages;
$templates = $this->wire()->templates;
/** @var array $data */
$data = $this->executeNavJSON(array('getArray' => true));
$out = '';
$bookmarkItem = null;
$rightIcon = "<span class='ui-priority-secondary'>" . wireIconMarkup('angle-right', 'fw') . "</span>";
foreach($data['list'] as $item) {
if(strpos($item['url'], '?role=0') !== false) {
$bookmarkItem = $item;
continue;
}
if(!empty($item['parent_id'])) {
$parents = $this->wire('pages')->find("id=$item[parent_id]");
$parents = $pages->find("id=$item[parent_id]");
} else if(!empty($item['template_id'])) {
$template = $this->wire('templates')->get($item['template_id']);
$template = $templates->get($item['template_id']);
$parentTemplates = implode('|', $template->parentTemplates);
if(empty($parentTemplates)) continue;
$parents = $this->wire('pages')->find("template=$parentTemplates, include=unpublished, limit=100, sort=-modified");
$parents = $pages->find("template=$parentTemplates, include=unpublished, limit=100, sort=-modified");
} else {
$parents = array();
}
$out .=
"<dt><a class='label' href='./$item[url]'>" .
"<i class='fa fa-fw fa-$item[icon]'></i> $item[label]</a></dt><dd>";
$icon = wireIconMarkup($item['icon'], 'fw');
$out .= "<dt><a class='label' href='./$item[url]'>$icon $item[label]</a></dt><dd>";
if(count($parents)) {
$out .= "<ul>";
@@ -389,7 +418,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$parentParents = $parent->parents()->and($parent);
foreach($parentParents as $p) {
if($p->id == 1 && $parentParents->count() > 1) continue;
$out .= "$p->title<i class='ui-priority-secondary fa fa-fw fa-angle-right'></i>";
$out .= $p->title . $rightIcon;
}
$out .= "</a></li>";
}
@@ -406,13 +435,15 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
"</h2>";
}
if($bookmarkItem) {
$button = $this->wire('modules')->get('InputfieldButton');
/** @var InputfieldButton $button */
$button = $this->wire()->modules->get('InputfieldButton');
$button->href = $bookmarkItem['url'];
$button->value = $bookmarkItem['label'];
$button->showInHeader();
$button->icon = $bookmarkItem['icon'];
$out .= $button->render();
}
return $out;
}
@@ -426,16 +457,17 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
public function executeExists() {
/** @var Pages $pages */
$pages = $this->wire('pages');
$pages = $this->wire()->pages;
$input = $this->wire()->input;
$sanitizer = $this->wire()->sanitizer;
$parentID = (int) $this->wire('input')->get('parent_id');
$parentID = (int) $input->get('parent_id');
if(!$parentID) return '';
$parent = $this->wire('pages')->get($parentID);
$parent = $pages->get($parentID);
if(!$parent->addable()) return '';
$name = $this->wire('sanitizer')->pageNameUTF8($this->wire('input')->get('name'));
$name = $sanitizer->pageNameUTF8($input->get('name'));
if(!strlen($name)) return '';
$parentID = count($this->predefinedParents) ? $this->predefinedParents : $parentID;
@@ -450,6 +482,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
} else {
$out = "<span class='ui-priority-secondary'>" . wireIconMarkup('check-square-o') . ' ' . $this->_('Ok') . "</span>";
}
return $out;
}
@@ -465,7 +498,6 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$input = $this->wire()->input;
$config = $this->wire()->config;
$session = $this->wire()->session;
$this->headline($this->_('Add New')); // Headline
@@ -491,7 +523,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(!$this->parent_id) return $this->renderChooseTemplate();
$this->parent = $this->pages->get((int) $this->parent_id);
$this->parent = $this->wire()->pages->get((int) $this->parent_id);
if(!$this->parent->id) {
throw new Wire404Exception("Unable to load parent page $this->parent_id", Wire404Exception::codeSecondary);
}
@@ -503,7 +535,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if($this->parent->template->name === 'admin' && $this->wire()->page->id != $this->parent->id) {
$process = $this->parent->process;
if($process && wireInstanceOf($process, 'ProcessPageType')) {
$session->location($this->parent->url . 'add/');
$this->redirect($this->parent->url . 'add/');
}
}
@@ -513,13 +545,13 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(in_array($this->parent_id, $config->usersPageIDs) && $this->wire()->process != 'ProcessUser') {
$url = $config->urls->admin . "access/users/add/?parent_id=$this->parent_id";
if($template_id && in_array($template_id, $config->userTemplateIDs)) $url .= "&template_id=$template_id";
$session->location($url);
$this->redirect($url);
}
if(count($this->parent->template->childTemplates) == 1) {
// only one type of template is allowed for the parent
$childTemplates = $this->parent->template->childTemplates;
$template = $this->templates->get(reset($childTemplates));
$template = $this->wire()->templates->get(reset($childTemplates));
if($this->template && $template->id != $this->template->id) {
throw new WireException("Template $template is required for parent {$this->parent->path}");
}
@@ -567,16 +599,18 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
protected function ___getAllowedTemplates($parent = null) {
$config = $this->wire()->config;
if(is_null($parent)) $parent = $this->parent;
if(!$parent) return array();
if(is_array($this->allowedTemplates)) return $this->allowedTemplates;
$user = $this->wire('user');
$user = $this->wire()->user;
$templates = array();
$allTemplates = count($this->predefinedTemplates) ? $this->predefinedTemplates : $this->wire('templates');
$allTemplates = count($this->predefinedTemplates) ? $this->predefinedTemplates : $this->wire()->templates;
$allParents = $this->getAllowedParents();
$usersPageIDs = $this->wire('config')->usersPageIDs;
$userTemplateIDs = $this->wire('config')->userTemplateIDs;
$usersPageIDs = $config->usersPageIDs;
$userTemplateIDs = $config->userTemplateIDs;
if($parent->hasStatus(Page::statusUnpublished)) {
$parentEditable = $parent->editable();
@@ -632,11 +666,11 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
}
if(!$allow) continue;
} else if($t->name == 'role' && $parent->id != $this->config->rolesPageID) {
} else if($t->name == 'role' && $parent->id != $config->rolesPageID) {
// only allow role templates below rolesPageID
continue;
} else if($t->name == 'permission' && $parent->id != $this->config->permissionsPageID) {
} else if($t->name == 'permission' && $parent->id != $config->permissionsPageID) {
// only allow permission templates below permissionsPageID
continue;
}
@@ -671,7 +705,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*
*/
protected function isAllowedTemplate($template, Page $parent = null) {
if(!is_object($template)) $template = $this->wire('templates')->get($template);
if(!is_object($template)) $template = $this->wire()->templates->get($template);
if(!$template) throw new WireException('Unknown template');
$templates = $this->getAllowedTemplates($parent);
$allowed = isset($templates[$template->id]);
@@ -755,7 +789,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(count($this->predefinedParents)) {
$parents = $this->predefinedParents;
} else {
$parents = $this->wire('pages')->newPageArray();
$parents = $this->wire()->pages->newPageArray();
if($this->parent) $parents->add($this->parent);
}
foreach($parents as $parent) {
@@ -789,19 +823,25 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
protected function ___buildForm() {
/** @var InputfieldForm $form */
$form = $this->modules->get('InputfieldForm');
$modules = $this->wire()->modules;
$input = $this->wire()->input;
$config = $this->wire()->config;
$pages = $this->wire()->pages;
$fields = $this->wire()->fields;
$user = $this->wire()->user;
/** @var InputfieldForm $form */
$form = $modules->get('InputfieldForm');
$form->attr('id', 'ProcessPageAdd');
$form->addClass('InputfieldFormFocusFirst');
$form->attr('action', './' . ($this->input->get('modal') ? "?modal=1" : ''));
$form->attr('data-ajax-url', $this->wire('config')->urls->admin . 'page/add/');
$form->attr('action', './' . ($input->get('modal') ? "?modal=1" : ''));
$form->attr('data-ajax-url', $config->urls->admin . 'page/add/');
$form->attr('data-dup-note', $this->_('The name entered is already in use. If you do not modify it, the name will be made unique automatically after you save.'));
$form->attr('method', 'post');
$page = $this->wire('pages')->newNullPage(); // for getInputfield
$page = $pages->newNullPage(); // for getInputfield
if(is_null($this->template) || !$this->template->noGlobal) {
foreach($this->wire('fields') as $field) {
foreach($fields as $field) {
if($field->flags & Field::flagGlobal && ($field->type instanceof FieldtypePageTitle || $field->type instanceof FieldtypePageTitleLanguage)) {
if($this->template) {
$_field = $this->template->fieldgroup->getField($field->id, true); // get in context of fieldgroup
@@ -832,7 +872,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
}
/** @var InputfieldPageName $field */
$field = $this->modules->get('InputfieldPageName');
$field = $modules->get('InputfieldPageName');
$field->parentPage = $this->parent;
$field->attr('name', '_pw_page_name');
$field->required = true;
@@ -843,14 +883,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if($label) $field->label = $label;
$languages = $this->template->getLanguages();
} else {
$languages = $this->wire('languages');
$languages = $this->wire()->languages;
}
/** @var Languages $languages */
if($languages && $this->parent && $this->parent_id > 0) {
if($this->template) {
// dummy edit page for examination by InputfieldPageName
$editPage = $this->wire('pages')->newPage(array(
$editPage = $pages->newPage(array(
'template' => $this->template,
'parent' => $this->parent,
));
@@ -859,7 +899,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
}
$form->append($field);
$defaultTemplateId = (int) $this->wire('input')->get('template_id');
$defaultTemplateId = (int) $input->get('template_id');
if(!$defaultTemplateId && $this->parent->numChildren > 0) {
$sibling = $this->parent->child('sort=-created, include=hidden');
if($sibling && $sibling->id) $defaultTemplateId = $sibling->template->id;
@@ -875,7 +915,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
// only 1 template can be used here, so store it in a hidden field (no need for selection)
$template = $this->template ? $this->template : reset($allowedTemplates);
$field = $this->modules->get('InputfieldHidden');
/** @var InputfieldHidden $field */
$field = $modules->get('InputfieldHidden');
$field->attr('id+name', 'template');
$field->attr('value', $template->id);
if(count($template->fieldgroup) == 1 && $template->fieldgroup->hasField('title')) $numPublishable++;
@@ -885,7 +926,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
// multiple templates are possible so give them a select
/** @var InputfieldSelect $field */
$field = $this->modules->get('InputfieldSelect');
$field = $modules->get('InputfieldSelect');
$noSuggest = $this->settings['noSuggestTemplates'];
if(empty($noSuggest)) {
@@ -928,7 +969,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$field instanceof InputfieldHidden ? $form->append($field) : $form->prepend($field);
if(count($this->predefinedParents) > 1) {
$field = $this->modules->get('InputfieldSelect');
/** @var InputfieldSelect $field */
$field = $modules->get('InputfieldSelect');
$field->attr('name', 'parent_id');
$field->label = $this->_('Parent');
$field->required = true;
@@ -942,7 +984,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$form->prepend($field);
} else {
$field = $this->modules->get('InputfieldHidden');
/** @var InputfieldHidden $field */
$field = $modules->get('InputfieldHidden');
$field->attr('name', 'parent_id');
$value = count($this->predefinedParents) == 1 ? $this->predefinedParents->first()->id : $this->parent_id;
$field->attr('value', $value);
@@ -950,7 +993,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
}
/** @var InputfieldSubmit $field */
$field = $this->modules->get('InputfieldSubmit');
$field = $modules->get('InputfieldSubmit');
$field->attr('name', 'submit_save');
$field->attr('value', $this->_('Save'));
$field->showInHeader();
@@ -958,20 +1001,21 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if($numPublishable && !$this->noAutoPublish) {
$allowPublish = true;
if(!$this->wire('user')->isSuperuser()) {
$publishPermission = $this->wire('permissions')->get('page-publish');
if($publishPermission->id && !$this->wire('user')->hasPermission('page-publish')) $allowPublish = false;
if(!$user->isSuperuser()) {
$publishPermission = $this->wire()->permissions->get('page-publish');
if($publishPermission->id && !$user->hasPermission('page-publish')) $allowPublish = false;
}
if($allowPublish) {
/** @var InputfieldSubmit $field */
$field = $this->modules->get('InputfieldSubmit');
$field = $modules->get('InputfieldSubmit');
$field->attr('id+name', 'submit_publish');
$field->attr('value', $this->_('Save + Publish'));
$field->setSecondary();
$form->append($field);
if(!$this->wire('input')->get('modal')) {
$field = $this->modules->get('InputfieldSubmit');
if(!$input->get('modal')) {
/** @var InputfieldSubmit $field */
$field = $modules->get('InputfieldSubmit');
$field->attr('id+name', 'submit_publish_add');
$field->attr('value', $this->_('Save + Publish + Add Another'));
$field->setSecondary();
@@ -997,8 +1041,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
protected function getTemplateLabel(Template $template) {
$label = '';
$user = $this->wire('user');
$language = $this->wire('languages') && $user->language->id && !$user->language->isDefault ? $user->language : null;
$user = $this->wire()->user;
$language = $this->wire()->languages && $user->language->id && !$user->language->isDefault() ? $user->language : null;
if($language) $label = $template->get('label' . $language->id);
if(!$label) $label = $template->label ? $template->label : $template->name;
return $label;
@@ -1012,7 +1056,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$old = time() - 86400;
$selector = "include=all, modified<$old, limit=10, status&" . Page::statusTemp . ", status<" . Page::statusTrash;
$items = $this->wire('pages')->find($selector);
$items = $this->wire()->pages->find($selector);
foreach($items as $item) {
$this->message("Checking temporary item: $item->path", Notice::debug);
@@ -1023,16 +1067,15 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$msg = "Automatically trashed unused page: $item->path";
$this->message($msg, Notice::debug);
$this->wire('log')->message($msg);
$this->wire()->log->message($msg);
try {
if(!$item->title) $item->title = $this->_('Unused temp page') . ' - ' . $item->name;
$this->wire('pages')->trash($item);
$this->wire()->pages->trash($item);
} catch(\Exception $e) {
$this->error($e->getMessage());
}
}
}
/**
@@ -1045,11 +1088,15 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
protected function ___processQuickAdd(Page $parent, Template $template) {
$pages = $this->wire()->pages;
$input = $this->wire()->input;
$sanitizer = $this->wire()->sanitizer;
$this->deleteOldTempPages();
// allow for nameFormat to come from a name_format GET variable
$nameFormat = $this->wire('input')->get('name_format');
$nameFormat = $input->get('name_format');
if(strlen($nameFormat)) {
$nameFormat = $this->sanitizer->chars($this->sanitizer->text($nameFormat), '-_:./| [alpha][digit]', '-');
$nameFormat = $sanitizer->chars($sanitizer->text($nameFormat), '-_:./| [alpha][digit]', '-');
} else {
if(count($parent->template->childTemplates) > 1) return false;
$nameFormat = '';
@@ -1062,11 +1109,11 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
// if not specified in get variable, next check parent template for setting
$nameFormat = $nameFormatTemplate;
}
$page = $this->wire('pages')->newPage(array(
$page = $pages->newPage(array(
'template' => $template,
'parent' => $parent,
));
$this->wire('pages')->setupNew($page);
$pages->setupNew($page);
if(!strlen($page->name)) return false;
if(!$this->isAllowedTemplate($template)) return false;
$page->addStatus(Page::statusUnpublished);
@@ -1074,14 +1121,16 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
// if languages are in use, make the new page inherit the parent's language status (active vs. inactive)
$languages = $template->getLanguages();
if($languages) foreach($languages as $language) {
if($language->isDefault()) continue;
$languageStatus = $parent->get("status$language");
if($languageStatus) $page->set("status$language", $languageStatus);
if($languages) {
foreach($languages as $language) {
if($language->isDefault()) continue;
$languageStatus = $parent->get("status$language");
if($languageStatus) $page->set("status$language", $languageStatus);
}
}
try {
$this->wire('pages')->save($page);
$pages->save($page);
$this->createdPageMessage($page);
} catch(\Exception $e) {
@@ -1097,7 +1146,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
// allow for classes descending Page to redirect to alternate editor if $this->editor is not the right kind
$page->setEditor($this->editor);
// redirect to edit the page
$this->session->redirect("../edit/?id=$page->id&new=1" . ($this->wire('input')->get('modal') ? '&modal=1' : ''));
$this->redirect("../edit/?id=$page->id&new=1" . ($input->get('modal') ? '&modal=1' : ''));
return true;
} else {
return false;
@@ -1111,7 +1160,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*
*/
protected function createdPageMessage(Page $page) {
$this->session->message(
$this->wire()->session->message(
sprintf(
$this->_('Created page %1$s using template: %2$s'),
$page->parent->url . $page->name, $page->template->getLabel()
@@ -1144,9 +1193,12 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
protected function ___processInput(InputfieldForm $form) {
$pages = $this->wire()->pages;
$input = $this->wire()->input;
$template = $this->template;
$this->page = $this->wire('pages')->newPage($template ? $template : array()); // must exist before processInput for language hooks
$form->processInput($this->input->post);
$this->page = $pages->newPage($template ? $template : array()); // must exist before processInput for language hooks
$form->processInput($input->post);
/** @var InputfieldPageName $nameField */
$nameField = $form->getChildByName('_pw_page_name');
@@ -1184,8 +1236,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$this->page->name = $name;
$this->page->sort = $this->parent->numChildren;
$publishAdd = $this->wire('input')->post('submit_publish_add');
$publishNow = $this->page->publishable() && ($this->wire('input')->post('submit_publish') || $publishAdd);
$publishAdd = $input->post('submit_publish_add');
$publishNow = $this->page->publishable() && ($input->post('submit_publish') || $publishAdd);
$languages = $template->getLanguages();
foreach($this->page->fieldgroup as $field) {
@@ -1224,31 +1276,44 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$this->createdPageMessage($this->page);
if($this->wire('pages')->names()->hasAdjustedName($this->page)) {
if($pages->names()->hasAdjustedName($this->page)) {
$warning = $this->nameChangedWarning($this->page, $pageName);
if($warning) $this->warning($warning);
}
if($publishNow && $publishAdd) {
$this->session->redirect("./?parent_id={$this->page->parent_id}&template_id={$this->page->template->id}");
$this->redirect("./?parent_id={$this->page->parent_id}&template_id={$this->page->template->id}");
} else {
$this->session->redirect("../edit/?id={$this->page->id}&new=1" . ($this->input->get('modal') ? "&modal=1" : ''));
$this->redirect("../edit/?id={$this->page->id}&new=1" . ($input->get('modal') ? "&modal=1" : ''));
}
return true;
}
/**
* Redirect
*
* @param string $url
* @param bool $permanent
*
*/
protected function redirect($url, $permanent = false) {
if($permanent) $this->wire()->session->redirect($url);
$this->wire()->session->location($url);
}
/**
* Setup the UI breadcrumb trail
*
*/
public function setupBreadcrumbs() {
if($this->wire('page')->process != $this->className()) return;
$config = $this->wire()->config;
if($this->wire()->page->process != $this->className()) return;
$breadcrumbs = $this->wire(new Breadcrumbs());
$breadcrumbs->add(new Breadcrumb($this->config->urls->admin . 'page/list/', "Pages"));
$breadcrumbs->add(new Breadcrumb($config->urls->admin . 'page/list/', "Pages"));
foreach($this->parent->parents()->append($this->parent) as $p) {
/** @var Page $p */
$breadcrumbs->add(new Breadcrumb($this->config->urls->admin . "page/list/?open=" . $p->id, $p->get("title|name")));
$breadcrumbs->add(new Breadcrumb($config->urls->admin . "page/list/?open=" . $p->id, $p->get("title|name")));
}
$this->wire('breadcrumbs', $breadcrumbs);
}
@@ -1260,7 +1325,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*
*/
public function getPage() {
return $this->page ? $this->page : $this->wire('pages')->newNullPage();
return $this->page ? $this->page : $this->wire()->pages->newNullPage();
}
/**
@@ -1300,7 +1365,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*
*/
protected function getPageBookmarks() {
require_once($this->wire('config')->paths->ProcessPageEdit . 'PageBookmarks.php');
require_once($this->wire()->config->paths->ProcessPageEdit . 'PageBookmarks.php');
return $this->wire(new PageBookmarks($this));
}
@@ -1314,18 +1379,22 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
public function ___executeBookmarks() {
if(is_array($this->wire('input')->post('shortcutSort')) && $this->wire('user')->isSuperuser()) {
$data = $this->wire('modules')->getModuleConfigData($this);
$data['shortcutSort'] = $this->wire('input')->post->intArray('shortcutSort');
$this->wire('modules')->saveModuleConfigData($this, $data);
$input = $this->wire()->input;
$user = $this->wire()->user;
$modules = $this->wire()->modules;
if(is_array($input->post('shortcutSort')) && $user->isSuperuser()) {
$data = $modules->getModuleConfigData($this);
$data['shortcutSort'] = $input->post->intArray('shortcutSort');
$modules->saveModuleConfigData($this, $data);
}
$bookmarks = $this->getPageBookmarks();
$form = $bookmarks->editBookmarksForm();
$roleID = $this->wire('input')->get('role'); // no integer sanitization is intentional
$roleID = $input->get('role'); // no integer sanitization is intentional
if(!is_null($roleID) && $roleID == 0 && $this->wire('user')->isSuperuser()) {
if(!is_null($roleID) && $roleID == 0 && $user->isSuperuser()) {
$f = $this->getShortcutSortField();
$form->insertBefore($f, $form->getChildByName('submit_save_bookmarks'));
}
@@ -1334,7 +1403,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if($f->notes) $f->notes .= "\n\n";
$f->notes .= $this->_('The pages you select above represent bookmarks to the parent pages where you want children added. Note that if a user does not have permission to add a page to a given parent page (whether due to access control or template family settings), the bookmark will not appear.'); // Notes for bookmarks
$this->wire('session')->remove($this, 'numAddable');
$this->wire()->session->remove($this, 'numAddable');
return $form->render();
}
@@ -1347,14 +1416,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
public function getShortcutSortField() {
$this->wire('session')->remove($this, 'nav');
$this->wire()->session->remove($this, 'nav');
/** @var array $data */
$data = $this->executeNavJSON(array('getArray' => true));
$name = 'shortcutSort';
/** @var InputfieldAsmSelect $f */
$f = $this->wire('modules')->get('InputfieldAsmSelect');
$f = $this->wire()->modules->get('InputfieldAsmSelect');
$f->label = $this->_('Template shortcut sort order');
$f->description = $this->_('To change the order of the "Add New" page-template shortcuts, drag and drop the options to the order you want them in.');
$f->notes = $this->_('To add or remove templates from these shortcuts, see the Template editor Family tab.');
@@ -1366,14 +1435,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
foreach($data['list'] as $item) {
if(empty($item['template_id'])) continue;
$template = $this->wire('templates')->get($item['template_id']);
$template = $this->wire()->templates->get($item['template_id']);
if(!$template) continue;
$f->addOption($template->id, $template->getLabel());
$value[] = $template->id;
}
if(!count($f->getOptions())) {
$f = $this->wire('modules')->get('InputfieldHidden');
$f = $this->wire()->modules->get('InputfieldHidden');
$f->attr('name', $name);
return $f;
}
@@ -1393,10 +1462,12 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/
public function getModuleConfigInputfields(array $data) {
/** @var InputfieldWrapper $form */
$form = $this->wire(new InputfieldWrapper());
$form->add($this->getShortcutSortField());
$f = $this->wire('modules')->get('InputfieldCheckbox');
/** @var InputfieldCheckbox $f */
$f = $this->wire()->modules->get('InputfieldCheckbox');
$f->label = $this->_('Disable automatic publishing');
$f->description = $this->_('By default, pages with nothing but global fields (most commonly "title") will be published automatically when added, bypassing the usual unpublished state. Usually this is a desirable time saver. But you may cancel this behavior by checking the box below.'); // Description of automatic publishing
$f->attr('name', 'noAutoPublish');