1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 17:54:44 +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: * 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 2021 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* @method string executeTemplate() * @method string executeTemplate()
@@ -103,7 +103,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
'permission' => 'page-edit', 'permission' => 'page-edit',
'icon' => 'plus-circle', 'icon' => 'plus-circle',
'useNavJSON' => true, 'useNavJSON' => true,
); );
} }
/** /**
@@ -115,7 +115,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
parent::__construct(); parent::__construct();
$this->set('noAutoPublish', false); $this->set('noAutoPublish', false);
$this->set('shortcutSort', array()); $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); 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) { public function set($key, $value) {
if($key == 'parent_id') $this->parent_id = (int) $value; if($key === 'parent_id') {
else if($key == 'template' && $value instanceof Template) $this->template = $value; $this->parent_id = (int) "$value";
else return parent::set($key, $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; return $this;
} }
@@ -154,15 +159,19 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
public function ___executeNavJSON(array $options = array()) { public function ___executeNavJSON(array $options = array()) {
$page = $this->wire('page'); $page = $this->wire()->page;
$user = $this->wire('user'); $user = $this->wire()->user;
/** @var Session $session */ $config = $this->wire()->config;
$session = $this->wire('session'); $session = $this->wire()->session;
$modules = $this->wire()->modules;
$templates = $this->wire()->templates;
$sanitizer = $this->wire()->sanitizer;
$data = $session->getFor($this, 'nav'); $data = $session->getFor($this, 'nav');
if(!empty($data)) { if(!empty($data)) {
// check that session cache data is still current // check that session cache data is still current
foreach($this->wire('templates') as $template) { foreach($templates as $template) {
if($template->modified > $data['modified']) { if($template->modified > $data['modified']) {
$data = array(); $data = array();
$session->remove($this, 'nav'); $session->remove($this, 'nav');
@@ -176,7 +185,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(empty($data)) { if(empty($data)) {
$data = array( $data = array(
'url' => $this->wire('config')->urls->admin . 'page/add/', 'url' => $config->urls->admin . 'page/add/',
'label' => $this->_((string) $page->get('title|name')), 'label' => $this->_((string) $page->get('title|name')),
'icon' => 'plus-circle', 'icon' => 'plus-circle',
'add' => null, 'add' => null,
@@ -190,7 +199,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$items = array(); $items = array();
foreach($this->wire('templates') as $template) { foreach($templates as $template) {
$parent = $template->getParentPage(true); $parent = $template->getParentPage(true);
if(!$parent) continue; if(!$parent) continue;
if($parent->id) { if($parent->id) {
@@ -205,7 +214,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(!$icon) $icon = "plus-circle"; if(!$icon) $icon = "plus-circle";
$label = $template->getLabel(); $label = $template->getLabel();
$key = strtolower($label); $key = strtolower($label);
$label = $this->wire('sanitizer')->entities1($label); $label = $sanitizer->entities1($label);
if(isset($items[$key])) $key .= $template->name; if(isset($items[$key])) $key .= $template->name;
$items[$key] = array( $items[$key] = array(
'url' => $qs, 'url' => $qs,
@@ -217,7 +226,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
} }
ksort($items); 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(); $shortcutSort = isset($configData['shortcutSort']) ? $configData['shortcutSort'] : array();
if(!is_array($shortcutSort)) $shortcutSort = array(); if(!is_array($shortcutSort)) $shortcutSort = array();
if(!empty($shortcutSort)) { if(!empty($shortcutSort)) {
@@ -270,7 +279,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$session->setFor($this, 'numAddable', $listLength + $n); $session->setFor($this, 'numAddable', $listLength + $n);
if(!empty($options['getArray'])) return $data; 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); return json_encode($data);
} }
@@ -284,43 +293,57 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
public function ___executeTemplate() { public function ___executeTemplate() {
$modules = $this->wire()->modules;
$pages = $this->wire()->pages;
$input = $this->wire()->input;
$templateID = (int) $this->input->get('template_id'); $templateID = (int) $this->input->get('template_id');
if(!$templateID) throw new WireException("No template specified"); if(!$templateID) throw new WireException("No template specified");
$template = $this->templates->get($templateID); $template = $this->templates->get($templateID);
if(!$template) throw new WireException("Unknown template"); if(!$template) throw new WireException("Unknown template");
$parentTemplates = new TemplatesArray(); $parentTemplates = new TemplatesArray();
foreach($template->parentTemplates as $templateID) { foreach($template->parentTemplates as $templateID) {
$t = $this->templates->get((int) $templateID); $t = $this->templates->get((int) $templateID);
if($t) $parentTemplates->add($t); if($t) $parentTemplates->add($t);
} }
if(!count($parentTemplates)) throw new WireException("No parent templates defined for $template->name"); if(!count($parentTemplates)) throw new WireException("No parent templates defined for $template->name");
$parentTemplateIDs = $parentTemplates->implode('|', 'id'); $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)) throw new WireException("No usable parents match this template");
if(count($parents) == 1) { if(count($parents) == 1) {
$url = "./?parent_id=" . $parents->first()->id; $url = "./?parent_id=" . $parents->first()->id;
if($this->wire('input')->get('modal')) $url .= "&modal=1"; if($input->get('modal')) $url .= "&modal=1";
$this->wire('session')->redirect($url); $this->redirect($url);
} }
$templateLabel = $this->getTemplateLabel($template); $templateLabel = $this->getTemplateLabel($template);
$form = $this->wire('modules')->get('InputfieldForm');
/** @var InputfieldForm $form */
$form = $modules->get('InputfieldForm');
$form->description = $this->getTemplateLabel($template); $form->description = $this->getTemplateLabel($template);
$form->method = 'get'; $form->method = 'get';
$form->action = './'; $form->action = './';
$form->attr('id', 'select_parent_form'); $form->attr('id', 'select_parent_form');
if($this->wire('input')->get('modal')) { if($input->get('modal')) {
$f = $this->wire('modules')->get('InputfieldHidden'); /** @var InputfieldHidden $f */
$f = $modules->get('InputfieldHidden');
$f->attr('name', 'modal'); $f->attr('name', 'modal');
$f->attr('value', 1); $f->attr('value', 1);
$form->add($f); $form->add($f);
} }
$f = $this->wire('modules')->get('InputfieldSelect'); /** @var InputfieldSelect $f */
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'parent_id'); $f->attr('name', 'parent_id');
$f->attr('id', 'select_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) $f->description = sprintf($this->_('Please select a parent %s page below:'), ''); // Select parent label // you can omit the '%s' (no longer used)
$options = array(); $options = array();
@@ -338,12 +361,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$form->add($f); $form->add($f);
$f = $this->wire('modules')->get('InputfieldHidden'); /** @var InputfieldHidden $f */
$f = $modules->get('InputfieldHidden');
$f->attr('name', 'template_id'); $f->attr('name', 'template_id');
$f->attr('value', $template->id); $f->attr('value', $template->id);
$form->add($f); $form->add($f);
$f = $this->wire('modules')->get('InputfieldSubmit'); /** @var InputfieldSubmit $f */
$f = $modules->get('InputfieldSubmit');
$f->attr('id', 'select_parent_submit'); $f->attr('id', 'select_parent_submit');
$form->add($f); $form->add($f);
@@ -357,28 +382,32 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
* *
*/ */
public function renderChooseTemplate() { public function renderChooseTemplate() {
$pages = $this->wire()->pages;
$templates = $this->wire()->templates;
/** @var array $data */ /** @var array $data */
$data = $this->executeNavJSON(array('getArray' => true)); $data = $this->executeNavJSON(array('getArray' => true));
$out = ''; $out = '';
$bookmarkItem = null; $bookmarkItem = null;
$rightIcon = "<span class='ui-priority-secondary'>" . wireIconMarkup('angle-right', 'fw') . "</span>";
foreach($data['list'] as $item) { foreach($data['list'] as $item) {
if(strpos($item['url'], '?role=0') !== false) { if(strpos($item['url'], '?role=0') !== false) {
$bookmarkItem = $item; $bookmarkItem = $item;
continue; continue;
} }
if(!empty($item['parent_id'])) { 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'])) { } else if(!empty($item['template_id'])) {
$template = $this->wire('templates')->get($item['template_id']); $template = $templates->get($item['template_id']);
$parentTemplates = implode('|', $template->parentTemplates); $parentTemplates = implode('|', $template->parentTemplates);
if(empty($parentTemplates)) continue; 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 { } else {
$parents = array(); $parents = array();
} }
$out .= $icon = wireIconMarkup($item['icon'], 'fw');
"<dt><a class='label' href='./$item[url]'>" . $out .= "<dt><a class='label' href='./$item[url]'>$icon $item[label]</a></dt><dd>";
"<i class='fa fa-fw fa-$item[icon]'></i> $item[label]</a></dt><dd>";
if(count($parents)) { if(count($parents)) {
$out .= "<ul>"; $out .= "<ul>";
@@ -389,7 +418,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$parentParents = $parent->parents()->and($parent); $parentParents = $parent->parents()->and($parent);
foreach($parentParents as $p) { foreach($parentParents as $p) {
if($p->id == 1 && $parentParents->count() > 1) continue; 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>"; $out .= "</a></li>";
} }
@@ -406,13 +435,15 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
"</h2>"; "</h2>";
} }
if($bookmarkItem) { if($bookmarkItem) {
$button = $this->wire('modules')->get('InputfieldButton'); /** @var InputfieldButton $button */
$button = $this->wire()->modules->get('InputfieldButton');
$button->href = $bookmarkItem['url']; $button->href = $bookmarkItem['url'];
$button->value = $bookmarkItem['label']; $button->value = $bookmarkItem['label'];
$button->showInHeader(); $button->showInHeader();
$button->icon = $bookmarkItem['icon']; $button->icon = $bookmarkItem['icon'];
$out .= $button->render(); $out .= $button->render();
} }
return $out; return $out;
} }
@@ -426,16 +457,17 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
public function executeExists() { 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 ''; if(!$parentID) return '';
$parent = $this->wire('pages')->get($parentID); $parent = $pages->get($parentID);
if(!$parent->addable()) return ''; if(!$parent->addable()) return '';
$name = $this->wire('sanitizer')->pageNameUTF8($this->wire('input')->get('name')); $name = $sanitizer->pageNameUTF8($input->get('name'));
if(!strlen($name)) return ''; if(!strlen($name)) return '';
$parentID = count($this->predefinedParents) ? $this->predefinedParents : $parentID; $parentID = count($this->predefinedParents) ? $this->predefinedParents : $parentID;
@@ -450,6 +482,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
} else { } else {
$out = "<span class='ui-priority-secondary'>" . wireIconMarkup('check-square-o') . ' ' . $this->_('Ok') . "</span>"; $out = "<span class='ui-priority-secondary'>" . wireIconMarkup('check-square-o') . ' ' . $this->_('Ok') . "</span>";
} }
return $out; return $out;
} }
@@ -465,7 +498,6 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$input = $this->wire()->input; $input = $this->wire()->input;
$config = $this->wire()->config; $config = $this->wire()->config;
$session = $this->wire()->session;
$this->headline($this->_('Add New')); // Headline $this->headline($this->_('Add New')); // Headline
@@ -491,7 +523,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(!$this->parent_id) return $this->renderChooseTemplate(); 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) { if(!$this->parent->id) {
throw new Wire404Exception("Unable to load parent page $this->parent_id", Wire404Exception::codeSecondary); 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) { if($this->parent->template->name === 'admin' && $this->wire()->page->id != $this->parent->id) {
$process = $this->parent->process; $process = $this->parent->process;
if($process && wireInstanceOf($process, 'ProcessPageType')) { 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') { if(in_array($this->parent_id, $config->usersPageIDs) && $this->wire()->process != 'ProcessUser') {
$url = $config->urls->admin . "access/users/add/?parent_id=$this->parent_id"; $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"; 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) { if(count($this->parent->template->childTemplates) == 1) {
// only one type of template is allowed for the parent // only one type of template is allowed for the parent
$childTemplates = $this->parent->template->childTemplates; $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) { if($this->template && $template->id != $this->template->id) {
throw new WireException("Template $template is required for parent {$this->parent->path}"); 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) { protected function ___getAllowedTemplates($parent = null) {
$config = $this->wire()->config;
if(is_null($parent)) $parent = $this->parent; if(is_null($parent)) $parent = $this->parent;
if(!$parent) return array(); if(!$parent) return array();
if(is_array($this->allowedTemplates)) return $this->allowedTemplates; if(is_array($this->allowedTemplates)) return $this->allowedTemplates;
$user = $this->wire('user'); $user = $this->wire()->user;
$templates = array(); $templates = array();
$allTemplates = count($this->predefinedTemplates) ? $this->predefinedTemplates : $this->wire('templates'); $allTemplates = count($this->predefinedTemplates) ? $this->predefinedTemplates : $this->wire()->templates;
$allParents = $this->getAllowedParents(); $allParents = $this->getAllowedParents();
$usersPageIDs = $this->wire('config')->usersPageIDs; $usersPageIDs = $config->usersPageIDs;
$userTemplateIDs = $this->wire('config')->userTemplateIDs; $userTemplateIDs = $config->userTemplateIDs;
if($parent->hasStatus(Page::statusUnpublished)) { if($parent->hasStatus(Page::statusUnpublished)) {
$parentEditable = $parent->editable(); $parentEditable = $parent->editable();
@@ -632,11 +666,11 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
} }
if(!$allow) continue; 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 // only allow role templates below rolesPageID
continue; 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 // only allow permission templates below permissionsPageID
continue; continue;
} }
@@ -671,7 +705,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
* *
*/ */
protected function isAllowedTemplate($template, Page $parent = null) { 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'); if(!$template) throw new WireException('Unknown template');
$templates = $this->getAllowedTemplates($parent); $templates = $this->getAllowedTemplates($parent);
$allowed = isset($templates[$template->id]); $allowed = isset($templates[$template->id]);
@@ -755,7 +789,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if(count($this->predefinedParents)) { if(count($this->predefinedParents)) {
$parents = $this->predefinedParents; $parents = $this->predefinedParents;
} else { } else {
$parents = $this->wire('pages')->newPageArray(); $parents = $this->wire()->pages->newPageArray();
if($this->parent) $parents->add($this->parent); if($this->parent) $parents->add($this->parent);
} }
foreach($parents as $parent) { foreach($parents as $parent) {
@@ -789,19 +823,25 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
protected function ___buildForm() { protected function ___buildForm() {
/** @var InputfieldForm $form */ $modules = $this->wire()->modules;
$form = $this->modules->get('InputfieldForm'); $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->attr('id', 'ProcessPageAdd');
$form->addClass('InputfieldFormFocusFirst'); $form->addClass('InputfieldFormFocusFirst');
$form->attr('action', './' . ($this->input->get('modal') ? "?modal=1" : '')); $form->attr('action', './' . ($input->get('modal') ? "?modal=1" : ''));
$form->attr('data-ajax-url', $this->wire('config')->urls->admin . 'page/add/'); $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('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'); $form->attr('method', 'post');
$page = $this->wire('pages')->newNullPage(); // for getInputfield $page = $pages->newNullPage(); // for getInputfield
if(is_null($this->template) || !$this->template->noGlobal) { 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($field->flags & Field::flagGlobal && ($field->type instanceof FieldtypePageTitle || $field->type instanceof FieldtypePageTitleLanguage)) {
if($this->template) { if($this->template) {
$_field = $this->template->fieldgroup->getField($field->id, true); // get in context of fieldgroup $_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 */ /** @var InputfieldPageName $field */
$field = $this->modules->get('InputfieldPageName'); $field = $modules->get('InputfieldPageName');
$field->parentPage = $this->parent; $field->parentPage = $this->parent;
$field->attr('name', '_pw_page_name'); $field->attr('name', '_pw_page_name');
$field->required = true; $field->required = true;
@@ -843,14 +883,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if($label) $field->label = $label; if($label) $field->label = $label;
$languages = $this->template->getLanguages(); $languages = $this->template->getLanguages();
} else { } else {
$languages = $this->wire('languages'); $languages = $this->wire()->languages;
} }
/** @var Languages $languages */ /** @var Languages $languages */
if($languages && $this->parent && $this->parent_id > 0) { if($languages && $this->parent && $this->parent_id > 0) {
if($this->template) { if($this->template) {
// dummy edit page for examination by InputfieldPageName // dummy edit page for examination by InputfieldPageName
$editPage = $this->wire('pages')->newPage(array( $editPage = $pages->newPage(array(
'template' => $this->template, 'template' => $this->template,
'parent' => $this->parent, 'parent' => $this->parent,
)); ));
@@ -859,7 +899,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
} }
$form->append($field); $form->append($field);
$defaultTemplateId = (int) $this->wire('input')->get('template_id'); $defaultTemplateId = (int) $input->get('template_id');
if(!$defaultTemplateId && $this->parent->numChildren > 0) { if(!$defaultTemplateId && $this->parent->numChildren > 0) {
$sibling = $this->parent->child('sort=-created, include=hidden'); $sibling = $this->parent->child('sort=-created, include=hidden');
if($sibling && $sibling->id) $defaultTemplateId = $sibling->template->id; 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) // 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); $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('id+name', 'template');
$field->attr('value', $template->id); $field->attr('value', $template->id);
if(count($template->fieldgroup) == 1 && $template->fieldgroup->hasField('title')) $numPublishable++; 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 // multiple templates are possible so give them a select
/** @var InputfieldSelect $field */ /** @var InputfieldSelect $field */
$field = $this->modules->get('InputfieldSelect'); $field = $modules->get('InputfieldSelect');
$noSuggest = $this->settings['noSuggestTemplates']; $noSuggest = $this->settings['noSuggestTemplates'];
if(empty($noSuggest)) { if(empty($noSuggest)) {
@@ -928,7 +969,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$field instanceof InputfieldHidden ? $form->append($field) : $form->prepend($field); $field instanceof InputfieldHidden ? $form->append($field) : $form->prepend($field);
if(count($this->predefinedParents) > 1) { if(count($this->predefinedParents) > 1) {
$field = $this->modules->get('InputfieldSelect'); /** @var InputfieldSelect $field */
$field = $modules->get('InputfieldSelect');
$field->attr('name', 'parent_id'); $field->attr('name', 'parent_id');
$field->label = $this->_('Parent'); $field->label = $this->_('Parent');
$field->required = true; $field->required = true;
@@ -942,7 +984,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$form->prepend($field); $form->prepend($field);
} else { } else {
$field = $this->modules->get('InputfieldHidden'); /** @var InputfieldHidden $field */
$field = $modules->get('InputfieldHidden');
$field->attr('name', 'parent_id'); $field->attr('name', 'parent_id');
$value = count($this->predefinedParents) == 1 ? $this->predefinedParents->first()->id : $this->parent_id; $value = count($this->predefinedParents) == 1 ? $this->predefinedParents->first()->id : $this->parent_id;
$field->attr('value', $value); $field->attr('value', $value);
@@ -950,7 +993,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
} }
/** @var InputfieldSubmit $field */ /** @var InputfieldSubmit $field */
$field = $this->modules->get('InputfieldSubmit'); $field = $modules->get('InputfieldSubmit');
$field->attr('name', 'submit_save'); $field->attr('name', 'submit_save');
$field->attr('value', $this->_('Save')); $field->attr('value', $this->_('Save'));
$field->showInHeader(); $field->showInHeader();
@@ -958,20 +1001,21 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
if($numPublishable && !$this->noAutoPublish) { if($numPublishable && !$this->noAutoPublish) {
$allowPublish = true; $allowPublish = true;
if(!$this->wire('user')->isSuperuser()) { if(!$user->isSuperuser()) {
$publishPermission = $this->wire('permissions')->get('page-publish'); $publishPermission = $this->wire()->permissions->get('page-publish');
if($publishPermission->id && !$this->wire('user')->hasPermission('page-publish')) $allowPublish = false; if($publishPermission->id && !$user->hasPermission('page-publish')) $allowPublish = false;
} }
if($allowPublish) { if($allowPublish) {
/** @var InputfieldSubmit $field */ /** @var InputfieldSubmit $field */
$field = $this->modules->get('InputfieldSubmit'); $field = $modules->get('InputfieldSubmit');
$field->attr('id+name', 'submit_publish'); $field->attr('id+name', 'submit_publish');
$field->attr('value', $this->_('Save + Publish')); $field->attr('value', $this->_('Save + Publish'));
$field->setSecondary(); $field->setSecondary();
$form->append($field); $form->append($field);
if(!$this->wire('input')->get('modal')) { if(!$input->get('modal')) {
$field = $this->modules->get('InputfieldSubmit'); /** @var InputfieldSubmit $field */
$field = $modules->get('InputfieldSubmit');
$field->attr('id+name', 'submit_publish_add'); $field->attr('id+name', 'submit_publish_add');
$field->attr('value', $this->_('Save + Publish + Add Another')); $field->attr('value', $this->_('Save + Publish + Add Another'));
$field->setSecondary(); $field->setSecondary();
@@ -997,8 +1041,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
protected function getTemplateLabel(Template $template) { protected function getTemplateLabel(Template $template) {
$label = ''; $label = '';
$user = $this->wire('user'); $user = $this->wire()->user;
$language = $this->wire('languages') && $user->language->id && !$user->language->isDefault ? $user->language : null; $language = $this->wire()->languages && $user->language->id && !$user->language->isDefault() ? $user->language : null;
if($language) $label = $template->get('label' . $language->id); if($language) $label = $template->get('label' . $language->id);
if(!$label) $label = $template->label ? $template->label : $template->name; if(!$label) $label = $template->label ? $template->label : $template->name;
return $label; return $label;
@@ -1012,7 +1056,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$old = time() - 86400; $old = time() - 86400;
$selector = "include=all, modified<$old, limit=10, status&" . Page::statusTemp . ", status<" . Page::statusTrash; $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) { foreach($items as $item) {
$this->message("Checking temporary item: $item->path", Notice::debug); $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"; $msg = "Automatically trashed unused page: $item->path";
$this->message($msg, Notice::debug); $this->message($msg, Notice::debug);
$this->wire('log')->message($msg); $this->wire()->log->message($msg);
try { try {
if(!$item->title) $item->title = $this->_('Unused temp page') . ' - ' . $item->name; if(!$item->title) $item->title = $this->_('Unused temp page') . ' - ' . $item->name;
$this->wire('pages')->trash($item); $this->wire()->pages->trash($item);
} catch(\Exception $e) { } catch(\Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
} }
} }
/** /**
@@ -1045,11 +1088,15 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
protected function ___processQuickAdd(Page $parent, Template $template) { protected function ___processQuickAdd(Page $parent, Template $template) {
$pages = $this->wire()->pages;
$input = $this->wire()->input;
$sanitizer = $this->wire()->sanitizer;
$this->deleteOldTempPages(); $this->deleteOldTempPages();
// allow for nameFormat to come from a name_format GET variable // 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)) { if(strlen($nameFormat)) {
$nameFormat = $this->sanitizer->chars($this->sanitizer->text($nameFormat), '-_:./| [alpha][digit]', '-'); $nameFormat = $sanitizer->chars($sanitizer->text($nameFormat), '-_:./| [alpha][digit]', '-');
} else { } else {
if(count($parent->template->childTemplates) > 1) return false; if(count($parent->template->childTemplates) > 1) return false;
$nameFormat = ''; $nameFormat = '';
@@ -1062,11 +1109,11 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
// if not specified in get variable, next check parent template for setting // if not specified in get variable, next check parent template for setting
$nameFormat = $nameFormatTemplate; $nameFormat = $nameFormatTemplate;
} }
$page = $this->wire('pages')->newPage(array( $page = $pages->newPage(array(
'template' => $template, 'template' => $template,
'parent' => $parent, 'parent' => $parent,
)); ));
$this->wire('pages')->setupNew($page); $pages->setupNew($page);
if(!strlen($page->name)) return false; if(!strlen($page->name)) return false;
if(!$this->isAllowedTemplate($template)) return false; if(!$this->isAllowedTemplate($template)) return false;
$page->addStatus(Page::statusUnpublished); $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) // if languages are in use, make the new page inherit the parent's language status (active vs. inactive)
$languages = $template->getLanguages(); $languages = $template->getLanguages();
if($languages) foreach($languages as $language) { if($languages) {
if($language->isDefault()) continue; foreach($languages as $language) {
$languageStatus = $parent->get("status$language"); if($language->isDefault()) continue;
if($languageStatus) $page->set("status$language", $languageStatus); $languageStatus = $parent->get("status$language");
if($languageStatus) $page->set("status$language", $languageStatus);
}
} }
try { try {
$this->wire('pages')->save($page); $pages->save($page);
$this->createdPageMessage($page); $this->createdPageMessage($page);
} catch(\Exception $e) { } 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 // allow for classes descending Page to redirect to alternate editor if $this->editor is not the right kind
$page->setEditor($this->editor); $page->setEditor($this->editor);
// redirect to edit the page // 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; return true;
} else { } else {
return false; return false;
@@ -1111,7 +1160,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
* *
*/ */
protected function createdPageMessage(Page $page) { protected function createdPageMessage(Page $page) {
$this->session->message( $this->wire()->session->message(
sprintf( sprintf(
$this->_('Created page %1$s using template: %2$s'), $this->_('Created page %1$s using template: %2$s'),
$page->parent->url . $page->name, $page->template->getLabel() $page->parent->url . $page->name, $page->template->getLabel()
@@ -1144,9 +1193,12 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
protected function ___processInput(InputfieldForm $form) { protected function ___processInput(InputfieldForm $form) {
$pages = $this->wire()->pages;
$input = $this->wire()->input;
$template = $this->template; $template = $this->template;
$this->page = $this->wire('pages')->newPage($template ? $template : array()); // must exist before processInput for language hooks $this->page = $pages->newPage($template ? $template : array()); // must exist before processInput for language hooks
$form->processInput($this->input->post); $form->processInput($input->post);
/** @var InputfieldPageName $nameField */ /** @var InputfieldPageName $nameField */
$nameField = $form->getChildByName('_pw_page_name'); $nameField = $form->getChildByName('_pw_page_name');
@@ -1184,8 +1236,8 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$this->page->name = $name; $this->page->name = $name;
$this->page->sort = $this->parent->numChildren; $this->page->sort = $this->parent->numChildren;
$publishAdd = $this->wire('input')->post('submit_publish_add'); $publishAdd = $input->post('submit_publish_add');
$publishNow = $this->page->publishable() && ($this->wire('input')->post('submit_publish') || $publishAdd); $publishNow = $this->page->publishable() && ($input->post('submit_publish') || $publishAdd);
$languages = $template->getLanguages(); $languages = $template->getLanguages();
foreach($this->page->fieldgroup as $field) { foreach($this->page->fieldgroup as $field) {
@@ -1224,31 +1276,44 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
$this->createdPageMessage($this->page); $this->createdPageMessage($this->page);
if($this->wire('pages')->names()->hasAdjustedName($this->page)) { if($pages->names()->hasAdjustedName($this->page)) {
$warning = $this->nameChangedWarning($this->page, $pageName); $warning = $this->nameChangedWarning($this->page, $pageName);
if($warning) $this->warning($warning); if($warning) $this->warning($warning);
} }
if($publishNow && $publishAdd) { 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 { } 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; 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 * Setup the UI breadcrumb trail
* *
*/ */
public function setupBreadcrumbs() { 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 = $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) { foreach($this->parent->parents()->append($this->parent) as $p) {
/** @var Page $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); $this->wire('breadcrumbs', $breadcrumbs);
} }
@@ -1260,7 +1325,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
* *
*/ */
public function getPage() { 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() { 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)); return $this->wire(new PageBookmarks($this));
} }
@@ -1314,18 +1379,22 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
public function ___executeBookmarks() { public function ___executeBookmarks() {
if(is_array($this->wire('input')->post('shortcutSort')) && $this->wire('user')->isSuperuser()) { $input = $this->wire()->input;
$data = $this->wire('modules')->getModuleConfigData($this); $user = $this->wire()->user;
$data['shortcutSort'] = $this->wire('input')->post->intArray('shortcutSort'); $modules = $this->wire()->modules;
$this->wire('modules')->saveModuleConfigData($this, $data);
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(); $bookmarks = $this->getPageBookmarks();
$form = $bookmarks->editBookmarksForm(); $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(); $f = $this->getShortcutSortField();
$form->insertBefore($f, $form->getChildByName('submit_save_bookmarks')); $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"; 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 $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(); return $form->render();
} }
@@ -1347,14 +1416,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
public function getShortcutSortField() { public function getShortcutSortField() {
$this->wire('session')->remove($this, 'nav'); $this->wire()->session->remove($this, 'nav');
/** @var array $data */ /** @var array $data */
$data = $this->executeNavJSON(array('getArray' => true)); $data = $this->executeNavJSON(array('getArray' => true));
$name = 'shortcutSort'; $name = 'shortcutSort';
/** @var InputfieldAsmSelect $f */ /** @var InputfieldAsmSelect $f */
$f = $this->wire('modules')->get('InputfieldAsmSelect'); $f = $this->wire()->modules->get('InputfieldAsmSelect');
$f->label = $this->_('Template shortcut sort order'); $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->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.'); $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) { foreach($data['list'] as $item) {
if(empty($item['template_id'])) continue; 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; if(!$template) continue;
$f->addOption($template->id, $template->getLabel()); $f->addOption($template->id, $template->getLabel());
$value[] = $template->id; $value[] = $template->id;
} }
if(!count($f->getOptions())) { if(!count($f->getOptions())) {
$f = $this->wire('modules')->get('InputfieldHidden'); $f = $this->wire()->modules->get('InputfieldHidden');
$f->attr('name', $name); $f->attr('name', $name);
return $f; return $f;
} }
@@ -1393,10 +1462,12 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit
*/ */
public function getModuleConfigInputfields(array $data) { public function getModuleConfigInputfields(array $data) {
/** @var InputfieldWrapper $form */
$form = $this->wire(new InputfieldWrapper()); $form = $this->wire(new InputfieldWrapper());
$form->add($this->getShortcutSortField()); $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->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->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'); $f->attr('name', 'noAutoPublish');