PageFinder::whereEmptyValuePossible PHP Méthode

whereEmptyValuePossible() protected méthode

This can include equals/not-equals with blank or 0, as well as greater/less-than searches that can potentially match blank or 0.
protected whereEmptyValuePossible ( Field $field, $selector, $query, string $value, string &$where ) : boolean
$field Field
$selector
$query
$value string The value presumed to be blank (passed the empty() test)
$where string SQL where string that will be modified/appended
Résultat boolean Whether or not the query was handled and modified
    protected function whereEmptyValuePossible(Field $field, $selector, $query, $value, &$where)
    {
        // look in table that has no pages_id relation back to pages, using the LEFT JOIN / IS NULL trick
        // OR check for blank value as defined by the fieldtype
        $operator = $selector->operator;
        $database = $this->wire('database');
        static $tableCnt = 0;
        $table = $database->escapeTable($field->table);
        $tableAlias = $table . "__blank" . ++$tableCnt;
        $blankValue = $field->type->getBlankValue(new NullPage(), $field, $value);
        $blankIsObject = is_object($blankValue);
        if ($blankIsObject) {
            $blankValue = '';
        }
        $blankValue = $database->escapeStr($blankValue);
        $whereType = 'OR';
        $operators = array('=' => '!=', '!=' => '=', '<' => '>=', '<=' => '>', '>' => '<=', '>=' => '<');
        if (!isset($operators[$operator])) {
            return false;
        }
        if ($selector->not) {
            $operator = $operators[$operator];
        }
        // reverse
        if ($operator == '=') {
            // equals
            // non-presence of row is equal to value being blank
            if ($field->type->isEmptyValue($field, $value)) {
                $sql = "{$tableAlias}.pages_id IS NULL OR ({$tableAlias}.data='{$blankValue}'";
            } else {
                $sql = "({$tableAlias}.data='{$blankValue}'";
            }
            if ($value !== "0" && $blankValue !== "0" && !$field->type->isEmptyValue($field, "0")) {
                // if zero is not considered an empty value, exclude it from matching
                // if the search isn't specifically for a "0"
                $sql .= " AND {$tableAlias}.data!='0'";
            }
            $sql .= ")";
        } else {
            if ($operator == '!=' || $operator == '<>') {
                // not equals
                // $whereType = 'AND';
                if ($value === "0" && !$field->type->isEmptyValue($field, "0")) {
                    // may match rows with no value present
                    $sql = "{$tableAlias}.pages_id IS NULL OR ({$tableAlias}.data!='0'";
                } else {
                    if ($blankIsObject) {
                        $sql = "{$tableAlias}.pages_id IS NOT NULL AND ({$tableAlias}.data IS NOT NULL";
                    } else {
                        $sql = "{$tableAlias}.pages_id IS NOT NULL AND ({$tableAlias}.data!='{$blankValue}'";
                        if ($blankValue !== "0" && !$field->type->isEmptyValue($field, "0")) {
                            $sql .= " OR {$tableAlias}.data='0'";
                        }
                    }
                }
                $sql .= ")";
            } else {
                if ($operator == '<' || $operator == '<=') {
                    // less than
                    if ($value > 0 && $field->type->isEmptyValue($field, "0")) {
                        // non-rows can be included as counting for 0
                        $value = $database->escapeStr($value);
                        $sql = "{$tableAlias}.pages_id IS NULL OR {$tableAlias}.data{$operator}'{$value}'";
                    } else {
                        // we won't handle it here
                        return false;
                    }
                } else {
                    if ($operator == '>' || $operator == '>=') {
                        if ($value < 0 && $field->type->isEmptyValue($field, "0")) {
                            // non-rows can be included as counting for 0
                            $value = $database->escapeStr($value);
                            $sql = "{$tableAlias}.pages_id IS NULL OR {$tableAlias}.data{$operator}'{$value}'";
                        } else {
                            // we won't handle it here
                            return false;
                        }
                    }
                }
            }
        }
        $query->leftjoin("{$table} AS {$tableAlias} ON {$tableAlias}.pages_id=pages.id");
        $where .= strlen($where) ? " {$whereType} ({$sql})" : "({$sql})";
        return true;
    }