From f0a02bd70ab99d9e08daa18e3b47f4819cca6ef1 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 6 Aug 2021 14:21:58 -0400 Subject: [PATCH] Minor code improvements in ProcessPageAdd module (mostly just code/API usage consistency adjustments) --- .../ProcessPageAdd/ProcessPageAdd.module | 307 +++++++++++------- 1 file changed, 189 insertions(+), 118 deletions(-) diff --git a/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module b/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module index 3ff23378..45be817f 100644 --- a/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module +++ b/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module @@ -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); } @@ -283,44 +292,58 @@ 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(); @@ -337,13 +360,15 @@ 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 = "" . wireIconMarkup('angle-right', 'fw') . ""; + 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 .= - "
" . - " $item[label]
"; + $icon = wireIconMarkup($item['icon'], 'fw'); + $out .= "
$icon $item[label]
"; if(count($parents)) { $out .= "