diff --git a/wire/modules/Process/ProcessPageSearch/ProcessPageSearch.module b/wire/modules/Process/ProcessPageSearch/ProcessPageSearch.module index bc04dcaa..8bb29ca4 100644 --- a/wire/modules/Process/ProcessPageSearch/ProcessPageSearch.module +++ b/wire/modules/Process/ProcessPageSearch/ProcessPageSearch.module @@ -8,8 +8,10 @@ * For more details about how Process modules work, please see: * /wire/core/Process.php * - * ProcessWire 3.x, Copyright 2016 by Ryan Cramer + * ProcessWire 3.x, Copyright 2018 by Ryan Cramer * https://processwire.com + * + * @method string findReady($selector) * */ @@ -25,8 +27,18 @@ class ProcessPageSearch extends Process implements ConfigurableModule { ); } + /** + * Default operator for text searches + * + */ const defaultOperator = '%='; + /** + * Native/system sortable properties + * + * @var array + * + */ protected $nativeSorts = array( 'relevance', 'name', @@ -46,11 +58,36 @@ class ProcessPageSearch extends Process implements ConfigurableModule { 'sortfield', ); + /** + * Names of all Field objects in PW + * + * @var array + * + */ protected $fieldOptions = array(); - protected $customSorts = array(); + + /** + * All operators where key is operator and value is description + * + * @var array + * + */ protected $operators = array(); + + /** + * Items per pagination + * + * @var int + * + */ protected $resultLimit = 25; - protected $maxLimit = 250; + + /** + * Lister instance, when applicable + * + * @var null|ProcessPageLister + * + */ protected $lister = null; /** @@ -61,7 +98,11 @@ class ProcessPageSearch extends Process implements ConfigurableModule { * */ protected $adminSearchMode = false; - + + /** + * Initialize module + * + */ public function init() { foreach($this->fields as $field) { @@ -74,6 +115,12 @@ class ProcessPageSearch extends Process implements ConfigurableModule { parent::init(); } + /** + * Get operators used for searches, where key is operator and value is description + * + * @return array + * + */ static public function getOperators() { $f = __FILE__; return array( @@ -105,6 +152,20 @@ class ProcessPageSearch extends Process implements ConfigurableModule { $this->operators = self::getOperators(); } + /** + * Hookable function to optionally modify selector before it is sent to $pages->find() + * + * Not applicable when Lister is handling the search/render. + * + * #pw-hooker + * + * @param string $selector Selector that will be used to find pages + * @return string Must return the selector (optionally modified) + * + */ + protected function ___findReady($selector) { + return $selector; + } /** * Perform an interactive search and provide a search form (default) @@ -140,6 +201,7 @@ class ProcessPageSearch extends Process implements ConfigurableModule { $lister->columns = $this->getDisplayFields(); return $lister->execute(); } else { + $selector = $this->findReady($selector); $matches = $this->pages->find($selector); return $this->render($matches, $displaySelector); } @@ -157,7 +219,6 @@ class ProcessPageSearch extends Process implements ConfigurableModule { $this->fullSetup(); $selector = ''; - $displaySelector = ''; $limit = $this->resultLimit; $start = 0; $status = 0; @@ -314,6 +375,7 @@ class ProcessPageSearch extends Process implements ConfigurableModule { } } + $selector = $this->findReady($selector); $items = $this->pages->find($selector); if(!$superuser && $checkEditAccess) { @@ -395,12 +457,25 @@ class ProcessPageSearch extends Process implements ConfigurableModule { /** * Render the search results + * + * @param PageArray $matches + * @param string $displaySelector + * @return string * */ protected function render(PageArray $matches, $displaySelector) { $out = ''; - if($displaySelector) $this->message(sprintf($this->_n('Found %1$d page using selector: %2$s', 'Found %1$d pages using selector: %2$s', $matches->getTotal()), $matches->getTotal(), $displaySelector)); + + if($displaySelector) { + $this->message( + sprintf( + $this->_n('Found %1$d page using selector: %2$s', 'Found %1$d pages using selector: %2$s', $matches->getTotal()), + $matches->getTotal(), + $displaySelector + ) + ); + } // determine what fields will be displayed $display = array(); @@ -423,7 +498,10 @@ class ProcessPageSearch extends Process implements ConfigurableModule { $class = 'show_options'; } - $out .= "\n
"; - $out .= "\n\t
" . + $out .= + "\n\t
" . "\n\t" . "\n\t" . "\n\t
"; - $out .= "\n\t" . + $out .= + "\n\t
" . "\n\t" . "\n\t" . + $out .= + "\n\t" . "\n\t
"; - $out .= "\n\t" . + $out .= + "\n\t" . "\n\t" . "\n\t" . "\n\t"; @@ -646,7 +746,8 @@ class ProcessPageSearch extends Process implements ConfigurableModule { $advCollapsed = true; - $out2 = "\n\t" . + $out2 = + "\n\t
" . "\n\t" . "\n\t" . + $out2 .= + "\n\t" . "\n\t
"; - $out2.= "\n\t" . + $out2.= + "\n\t
" . "\n\t" . "\n\t" . + $out2 .= + "\n\t" . "\n\t
"; if($sort != 'relevance') { $reverse = $this->input->whitelist('reverse'); - $out2 .= "\n\t" . + $out2 .= + "\n\t
" . "\n\t" . "\n\t
"; if($reverse) $advCollapsed = false; } $display = $this->input->whitelist('display'); - $out2.= "\n\t" . + $out2 .= + "\n\t
" . "\n\t" . "\n\t" . "\n\t
"; if($display && $display != 'title,path') $advCollapsed = false; + /** @var InputfieldSubmit $submit */ $submit = $this->modules->get("InputfieldSubmit"); $submit->attr('name', 'submit'); $submit->attr('value', $this->_x('Search', 'submit')); // Search submit button for advanced search $out .= "" . $submit->render() . "
"; + /** @var InputfieldForm $form */ $form = $this->modules->get("InputfieldForm"); $form->attr('id', 'ProcessPageSearchOptionsForm'); $form->method = 'get'; $form->action = './'; + /** @var InputfieldMarkup $field */ $field = $this->modules->get("InputfieldMarkup"); $field->label = $this->_("Search Options"); $field->value = $out; @@ -719,7 +828,7 @@ class ProcessPageSearch extends Process implements ConfigurableModule { $form->add($field); - /* Remove temporarily + /* no longer in use $field = $this->modules->get("InputfieldMarkup"); $field->id = 'ProcessPageSearchShortcuts'; $field->collapsed = true; @@ -733,46 +842,22 @@ class ProcessPageSearch extends Process implements ConfigurableModule { } - protected function renderShortcuts() { - $out = ''; - $links = array( - 'Quick Links', - "All by creation date" => '?q=&submit=Search&display=title+path+created&sort=created&reverse=1' , - "All by latest edit date" => '?q=&submit=Search&display=title+path+created&sort=modified&reverse=1', - "Users by creation date" => '?q=&template=user&submit=Search&operator=~%3D&display=name+email+created&sort=created&reverse=1', - 'New pages by template', - ); - - foreach($this->templates as $template) { - // Quick links only for content with more than one page - // if($template->getNumPages() < 2) continue; - - // Users get own quick link earlier, others are rather irrelevant - if($template->flags & Template::flagSystem) continue; - - $links[$template->name] = "?q=&template={$template->name}&submit=Search&operator=~%3D&display=title+path+created&sort=created&reverse=1"; - } - - foreach($links as $label => $value) { - if(is_int($label)) { - $out .= "