1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-08 07:47:00 +02:00

Refactor of FieldtypeToggle module getMatchQuery() method to add more potential match possibilities but also to fix processwire/processwire-issues#1887

This commit is contained in:
Ryan Cramer
2024-02-29 12:01:53 -05:00
parent 76ad3ab984
commit 128538fcd8

View File

@@ -143,6 +143,7 @@ class FieldtypeToggle extends Fieldtype {
*/ */
public function isEmptyValue(Field $field, $value) { public function isEmptyValue(Field $field, $value) {
// 0 is allowed because it represents "no/off" selection // 0 is allowed because it represents "no/off" selection
if($value instanceof Selector) return true;
if($value === 0 || $value === "0") return false; if($value === 0 || $value === "0") return false;
if($value === 'unknown' || "$value" === "-1") return true; if($value === 'unknown' || "$value" === "-1") return true;
$value = trim($value, '"\''); $value = trim($value, '"\'');
@@ -251,23 +252,50 @@ class FieldtypeToggle extends Fieldtype {
* *
*/ */
public function getMatchQuery($query, $table, $subfield, $operator, $value) { public function getMatchQuery($query, $table, $subfield, $operator, $value) {
$originalValue = strtolower("$value");
$value = $this->_sanitizeValue($value); // 0, 1, 2 or '' $value = $this->_sanitizeValue($value); // 0, 1, 2 or ''
$matchNull = false; $matchNull = false;
$matchTemplates = false;
$defaultOption = $query->field->get('defaultOption');
$defaultNo = $defaultOption === 'no';
if($value === '' && $operator === '=') { if($value === '') {
// match only no-selection // blank/null value specified
$matchNull = true; if($operator === '=') {
// match no-selection or "no"
} else if($value === '' && $operator === '>') { $matchNull = true;
// match all except no-selection if($originalValue === 'unknown') {
$value = 0; // if literal string 'unknown' specified then match null but only for pages having field
$operator = '>='; $matchTemplates = true;
} else if($defaultNo) {
} else if($value !== '' && $operator === '!=') { // experimental: if default is "no" then allow both "no" and null values to match
// match value as well as no-selection // $value = 0;
$matchNull = true; }
} if($originalValue === '' && $this->wire()->config->installed < 1709225722) {
// maintain original behavior for existing installations
// which will match both null and "no" when value is blank string
$value = 0;
}
} else if(strpos($operator, '>') === 0 || $operator === '!=') {
// match all except no-selection
$value = 0;
$operator = '>=';
}
} else {
// value -1, 0, 1, or 2
if($operator === '<' && ((int) $value) > 0) {
// match less than as well as no-selection
$matchNull = true;
} else if($operator === '!=' && $value > -1) {
// match value as well as no-selection
$matchNull = true;
} else if($operator === '=' && ($value === 0 && $defaultNo)) {
// experimental: match 0 or null for templates having field when default is "no"
// $matchNull = true;
// $matchTemplates = true;
}
}
if($value === '' && ($operator[0] === '<' || $operator[0] === '>')) { if($value === '' && ($operator[0] === '<' || $operator[0] === '>')) {
throw new WireException("Operator $operator not supported here for non-value"); throw new WireException("Operator $operator not supported here for non-value");
@@ -280,7 +308,12 @@ class FieldtypeToggle extends Fieldtype {
$query->leftjoin("$table AS $_table ON $_table.pages_id=pages.id"); $query->leftjoin("$table AS $_table ON $_table.pages_id=pages.id");
$where = "$_table.pages_id IS NULL "; $where = "$_table.pages_id IS NULL ";
if($value !== '') $where .= "OR $_table.data$operator" . ((int) $value); if($value !== '') $where .= "OR $_table.data$operator" . ((int) $value);
$query->where(trim($where)); if($matchTemplates) {
// limit match to templates having the field
$templateIds = $query->field->getTemplates()->implode(',', 'id');
$where = "pages.templates_id IN($templateIds) AND ($where)";
}
$query->parentQuery->where("($where)");
} else { } else {
$query = parent::getMatchQuery($query, $table, $subfield, $operator, $value); $query = parent::getMatchQuery($query, $table, $subfield, $operator, $value);
} }