From 5daa38729a67bdcfd704dcd7a4513972ffdeec75 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Tue, 3 Dec 2019 08:41:37 -0500 Subject: [PATCH] A couple of small improvements to ProcessPageLister, including improved editable() check of mock Page per template that also now includes parent in the check, and improved debug output of selector that now includes modifications to selector made by PageFinder --- .../ProcessPageLister.module | 80 ++++++++++++++++--- 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/wire/modules/Process/ProcessPageLister/ProcessPageLister.module b/wire/modules/Process/ProcessPageLister/ProcessPageLister.module index 378e98e7..762d712c 100644 --- a/wire/modules/Process/ProcessPageLister/ProcessPageLister.module +++ b/wire/modules/Process/ProcessPageLister/ProcessPageLister.module @@ -138,6 +138,14 @@ class ProcessPageLister extends Process implements ConfigurableModule { */ protected $finalSelector = ''; + /** + * Final selector string after being fully parsed by PageFinder (used only in debug mode) + * + * @var string + * + */ + protected $finalSelectorParsed = ''; + /** * IDs of pages that should appear open automatically * @@ -994,8 +1002,14 @@ class ProcessPageLister extends Process implements ConfigurableModule { * */ protected function validateSelector($selector) { - + + /** @var User $user */ $user = $this->wire('user'); + /** @var Sanitizer $sanitizer */ + $sanitizer = $this->wire('sanitizer'); + /** @var Pages $pages */ + $pages = $this->wire('pages'); + $showIncludeWarnings = $this->showIncludeWarnings; // whether to show warning message about removed include modes if($user->isSuperuser()) { @@ -1015,6 +1029,7 @@ class ProcessPageLister extends Process implements ConfigurableModule { /** @var Selectors $selectors */ $selectors = $this->wire(new Selectors($selector)); $templates = array(); + $parents = array(); $changed = false; $templateSelector = null; $includeSelector = null; @@ -1028,6 +1043,8 @@ class ProcessPageLister extends Process implements ConfigurableModule { $fields[$key] = strtolower($name); } + $firstField = reset($fields); + if(in_array('check_access', $fields) || in_array('checkaccess', $fields)) { if(!$this->allowIncludeAll) { // don't allow non-superusers to specify a check_access property @@ -1041,7 +1058,7 @@ class ProcessPageLister extends Process implements ConfigurableModule { foreach($values as $key => $value) { - $value = $this->wire('sanitizer')->templateName($value); + $value = $sanitizer->templateName($value); if(ctype_digit("$value")) $value = (int) $value; $template = $this->wire('templates')->get($value); @@ -1064,8 +1081,20 @@ class ProcessPageLister extends Process implements ConfigurableModule { } } $templateSelector = $s; + } + if(($firstField === 'parent' || $firstField === 'parent.id') && count($fields) == 1) { + foreach($values as $value) { + if(ctype_digit("$value")) { + $parent = $pages->get((int) $value); + } else { + $parent = $pages->get($sanitizer->selectorValue($value)); + } + if($parent->id) $parents[] = $parent; + } + } + if(in_array('include', $fields)) { if(count($values) > 1) throw new WireException("The 'include=' selector may not have more than 1 value."); @@ -1098,11 +1127,24 @@ class ProcessPageLister extends Process implements ConfigurableModule { // user specified 1 or more templates $numEditable = 0; // determine how many templates are editable - $test = $this->wire('pages')->newPage(); - foreach($templates as $template) { - $test->template = $template; - $test->id = 999; // required (any ID number works) - if($test->editable()) $numEditable++; + $test = $pages->newPage(); + $test->id = 999; // required (any ID number works) + if(count($parents)) { + foreach($templates as $template) { + $test->template = $template; + foreach($parents as $parent) { + $test->parent = $parent; + if($test->editable()) { + $numEditable++; + break; + } + } + } + } else { + foreach($templates as $template) { + $test->template = $template; + if($test->editable()) $numEditable++; + } } // if all specified templates are editable, include=unpublished is allowed if($numEditable == count($templates)) { @@ -1802,7 +1844,13 @@ class ProcessPageLister extends Process implements ConfigurableModule { try { $options = array('allowCustom' => true); - $results = $selector ? $this->wire('pages')->find($selector, $options) : $this->wire('pages')->newPageArray(); + /** @var Pages $pages */ + $pages = $this->wire('pages'); + $results = $selector ? $pages->find($selector, $options) : $pages->newPageArray(); + $this->finalSelector = $results->getSelectors(true); + if($this->wire('config')->debug && $this->wire('config')->advanced) { + $this->finalSelectorParsed = (string) $pages->loader()->getLastPageFinder()->getSelectors(); + } } catch(\Exception $e) { $this->error($e->getMessage()); @@ -1833,6 +1881,9 @@ class ProcessPageLister extends Process implements ConfigurableModule { * */ protected function ___renderResults($selector = null) { + + /** @var Sanitizer $sanitizer */ + $sanitizer = $this->wire('sanitizer'); if(is_null($selector)) $selector = $this->getSelector(); if(!count($this->columns)) $this->columns = $this->defaultColumns; @@ -1883,12 +1934,21 @@ class ProcessPageLister extends Process implements ConfigurableModule { if(count($this->resultNotes)) { $notes = array(); foreach($this->resultNotes as $note) { - $notes[] = wireIconMarkup('warning') . ' ' . $this->wire('sanitizer')->entities1($note); + $notes[] = wireIconMarkup('warning') . ' ' . $sanitizer->entities1($note); } $out .= "

" . implode('
', $notes) . "

"; } if($this->wire('config')->debug) { - $out .= "

" . $this->wire('sanitizer')->entities($this->finalSelector) . "

"; + $out .= "

"; + if($this->finalSelectorParsed && $this->finalSelector != $this->finalSelectorParsed) { + // selector was modified after being parsed by PageFinder + $out .= + "1: " . $sanitizer->entities($this->finalSelector) . "
" . + "2: " . $sanitizer->entities($this->finalSelectorParsed); + } else { + $out .= $sanitizer->entities($this->finalSelector); + } + $out .= "

"; } if(!$this->editOption) {