PageFinder::preProcessSelector PHP Method

preProcessSelector() protected method

This is primarily used to handle sub-selections, i.e. "bar=foo, id=[this=that, foo=bar]"
protected preProcessSelector ( Selector $selector ) : boolean
$selector Selector
return boolean Returns false if selector should be skipped over by getQuery()
    protected function preProcessSelector(Selector $selector)
    {
        if ($selector->quote && !is_array($selector->value) && Selectors::stringHasSelector($selector->value)) {
            // selector contains an embedded quoted selector
            if ($selector->quote == '[') {
                // i.e. field=[id>0, name=something, this=that]
                // selector contains another embedded selector that we need to convert to page IDs
                $selectors = new Selectors($selector->value);
                $hasTemplate = false;
                $hasParent = false;
                foreach ($selectors as $s) {
                    if (is_array($s->field)) {
                        continue;
                    }
                    if ($s->field == 'template') {
                        $hasTemplate = true;
                    }
                    if ($s->field == 'parent' || $s->field == 'parent_id') {
                        $hasParent = true;
                    }
                }
                // special handling for page references, detect if parent or template is defined,
                // and add it to the selector if available. This makes it faster.
                if (!$hasTemplate || !$hasParent) {
                    $fields = is_array($selector->field) ? $selector->field : array($selector->field);
                    $templates = array();
                    $parents = array();
                    $findSelector = '';
                    foreach ($fields as $fieldName) {
                        if (strpos($fieldName, '.') !== false) {
                            list($unused, $fieldName) = explode('.', $fieldName);
                        }
                        $field = $this->wire('fields')->get($fieldName);
                        if (!$field) {
                            continue;
                        }
                        if (!$hasTemplate && $field->template_id) {
                            if (is_array($field->template_id)) {
                                $templates = array_merge($templates, $field->template_id);
                            } else {
                                $templates[] = (int) $field->template_id;
                            }
                        }
                        if (!$hasParent && $field->parent_id) {
                            $parents[] = (int) $field->parent_id;
                        }
                        if ($field->findPagesSelector && count($fields) == 1) {
                            $findSelector = $field->findPagesSelector;
                        }
                    }
                    if (count($templates)) {
                        $selectors->prepend(new SelectorEqual('template', $templates));
                    }
                    if (count($parents)) {
                        $selectors->prepend(new SelectorEqual('parent_id', $parents));
                    }
                    if ($findSelector) {
                        foreach (new Selectors($findSelector) as $s) {
                            $selectors->append($s);
                        }
                    }
                }
                $pageFinder = new PageFinder();
                $ids = $pageFinder->findIDs($selectors);
                // populate selector value with array of page IDs
                if (count($ids) == 0) {
                    // subselector resulted in 0 matches
                    // force non-match for this subselector by populating 'id' subfield to field name(s)
                    $fieldNames = array();
                    foreach ($selector->fields as $key => $fieldName) {
                        if (strpos($fieldName, '.') !== false) {
                            // reduce fieldName to just field name without subfield name
                            list($fieldName, $subname) = explode('.', $fieldName);
                            // subname intentionally unused
                        }
                        $fieldName .= '.id';
                        $fieldNames[$key] = $fieldName;
                    }
                    $selector->fields = $fieldNames;
                    $selector->value = 0;
                } else {
                    $selector->value = count($ids) > 1 ? $ids : reset($ids);
                }
                $selector->quote = '';
                /*
                } else {
                	$fieldName = $selector->field ? $selector->field : 'none';
                	if(!isset($this->extraSubSelectors[$fieldName])) $this->extraSubSelectors[$fieldName] = array();
                	$this->extraSubSelectors[$fieldName][] = new Selectors($selector->value);
                	return false;
                }
                */
            } else {
                if ($selector->quote == '(') {
                    // selector contains an quoted selector. At least one () quoted selector must match for each field specified in front of it
                    $fieldName = $selector->field ? $selector->field : 'none';
                    if (!isset($this->extraOrSelectors[$fieldName])) {
                        $this->extraOrSelectors[$fieldName] = array();
                    }
                    $this->extraOrSelectors[$fieldName][] = new Selectors($selector->value);
                    return false;
                }
            }
        }
        return true;
    }