mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-18 23:46:21 +02:00
e6b877df27
# Conflicts: # control/Director.php # control/HTTP.php # core/startup/ParameterConfirmationToken.php # docs/en/00_Getting_Started/01_Installation/05_Common_Problems.md # docs/en/00_Getting_Started/04_Directory_Structure.md # docs/en/00_Getting_Started/05_Coding_Conventions.md # docs/en/01_Tutorials/01_Building_A_Basic_Site.md # docs/en/01_Tutorials/02_Extending_A_Basic_Site.md # docs/en/01_Tutorials/03_Forms.md # docs/en/01_Tutorials/04_Site_Search.md # docs/en/01_Tutorials/05_Dataobject_Relationship_Management.md # docs/en/02_Developer_Guides/12_Search/01_Searchcontext.md # docs/en/02_Developer_Guides/13_i18n/index.md # docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/06_Javascript_Development.md # docs/en/03_Upgrading/index.md # docs/en/changelogs/index.md # docs/en/howto/customize-cms-menu.md # docs/en/howto/navigation-menu.md # docs/en/index.md # docs/en/installation/index.md # docs/en/installation/windows-manual-iis-6.md # docs/en/misc/contributing/code.md # docs/en/misc/contributing/issues.md # docs/en/misc/module-release-process.md # docs/en/reference/dataobject.md # docs/en/reference/execution-pipeline.md # docs/en/reference/grid-field.md # docs/en/reference/modeladmin.md # docs/en/reference/rssfeed.md # docs/en/reference/templates.md # docs/en/topics/commandline.md # docs/en/topics/debugging.md # docs/en/topics/email.md # docs/en/topics/forms.md # docs/en/topics/index.md # docs/en/topics/module-development.md # docs/en/topics/modules.md # docs/en/topics/page-type-templates.md # docs/en/topics/page-types.md # docs/en/topics/search.md # docs/en/topics/testing/index.md # docs/en/topics/testing/testing-guide-troubleshooting.md # docs/en/topics/theme-development.md # docs/en/tutorials/1-building-a-basic-site.md # docs/en/tutorials/2-extending-a-basic-site.md # docs/en/tutorials/3-forms.md # docs/en/tutorials/4-site-search.md # docs/en/tutorials/5-dataobject-relationship-management.md # docs/en/tutorials/building-a-basic-site.md # docs/en/tutorials/dataobject-relationship-management.md # docs/en/tutorials/extending-a-basic-site.md # docs/en/tutorials/forms.md # docs/en/tutorials/index.md # docs/en/tutorials/site-search.md # main.php # model/SQLQuery.php # security/ChangePasswordForm.php # security/MemberLoginForm.php # tests/control/ControllerTest.php # tests/core/startup/ParameterConfirmationTokenTest.php # tests/model/SQLQueryTest.php # tests/security/SecurityTest.php # tests/view/SSViewerTest.php # view/SSTemplateParser.php # view/SSTemplateParser.php.inc # view/SSViewer.php
193 lines
5.3 KiB
PHP
193 lines
5.3 KiB
PHP
<?php
|
|
/**
|
|
* @package framework
|
|
* @subpackage search
|
|
*/
|
|
|
|
/**
|
|
* Selects textual content with an exact match between columnname and keyword.
|
|
*
|
|
* @todo case sensitivity switch
|
|
* @todo documentation
|
|
*
|
|
* @package framework
|
|
* @subpackage search
|
|
*/
|
|
class ExactMatchFilter extends SearchFilter {
|
|
|
|
public function setModifiers(array $modifiers) {
|
|
if(($extras = array_diff($modifiers, array('not', 'nocase', 'case'))) != array()) {
|
|
throw new InvalidArgumentException(
|
|
get_class($this) . ' does not accept ' . implode(', ', $extras) . ' as modifiers');
|
|
}
|
|
|
|
parent::setModifiers($modifiers);
|
|
}
|
|
|
|
/**
|
|
* Applies an exact match (equals) on a field value.
|
|
*
|
|
* @return DataQuery
|
|
*/
|
|
protected function applyOne(DataQuery $query) {
|
|
return $this->oneFilter($query, true);
|
|
}
|
|
|
|
/**
|
|
* Excludes an exact match (equals) on a field value.
|
|
*
|
|
* @return DataQuery
|
|
*/
|
|
protected function excludeOne(DataQuery $query) {
|
|
return $this->oneFilter($query, false);
|
|
}
|
|
|
|
/**
|
|
* Applies a single match, either as inclusive or exclusive
|
|
*
|
|
* @param DataQuery $query
|
|
* @param bool $inclusive True if this is inclusive, or false if exclusive
|
|
* @return DataQuery
|
|
*/
|
|
protected function oneFilter(DataQuery $query, $inclusive) {
|
|
$this->model = $query->applyRelation($this->relation);
|
|
$field = $this->getDbName();
|
|
$value = $this->getValue();
|
|
|
|
// Null comparison check
|
|
if($value === null) {
|
|
$where = DB::get_conn()->nullCheckClause($field, $inclusive);
|
|
return $query->where($where);
|
|
}
|
|
|
|
// Value comparison check
|
|
$where = DB::get_conn()->comparisonClause(
|
|
$field,
|
|
null,
|
|
true, // exact?
|
|
!$inclusive, // negate?
|
|
$this->getCaseSensitive(),
|
|
true
|
|
);
|
|
// for != clauses include IS NULL values, since they would otherwise be excluded
|
|
if(!$inclusive) {
|
|
$nullClause = DB::get_conn()->nullCheckClause($field, true);
|
|
$where .= " OR {$nullClause}";
|
|
}
|
|
return $query->where(array($where => $value));
|
|
}
|
|
|
|
/**
|
|
* Applies an exact match (equals) on a field value against multiple
|
|
* possible values.
|
|
*
|
|
* @return DataQuery
|
|
*/
|
|
protected function applyMany(DataQuery $query) {
|
|
return $this->manyFilter($query, true);
|
|
}
|
|
|
|
/**
|
|
* Excludes an exact match (equals) on a field value against multiple
|
|
* possible values.
|
|
*
|
|
* @return DataQuery
|
|
*/
|
|
protected function excludeMany(DataQuery $query) {
|
|
return $this->manyFilter($query, false);
|
|
}
|
|
|
|
/**
|
|
* Applies matches for several values, either as inclusive or exclusive
|
|
*
|
|
* @param DataQuery $query
|
|
* @param bool $inclusive True if this is inclusive, or false if exclusive
|
|
* @return DataQuery
|
|
*/
|
|
protected function manyFilter(DataQuery $query, $inclusive) {
|
|
$this->model = $query->applyRelation($this->relation);
|
|
$caseSensitive = $this->getCaseSensitive();
|
|
|
|
// Check values for null
|
|
$field = $this->getDbName();
|
|
$values = $this->getValue();
|
|
if(empty($values)) {
|
|
throw new \InvalidArgumentException("Cannot filter {$field} against an empty set");
|
|
}
|
|
$hasNull = in_array(null, $values, true);
|
|
if($hasNull) {
|
|
$values = array_filter($values, function($value) {
|
|
return $value !== null;
|
|
});
|
|
}
|
|
|
|
$connective = '';
|
|
if(empty($values)) {
|
|
$predicate = '';
|
|
} elseif($caseSensitive === null) {
|
|
// For queries using the default collation (no explicit case) we can use the WHERE .. NOT IN .. syntax,
|
|
// providing simpler SQL than many WHERE .. AND .. fragments.
|
|
$column = $this->getDbName();
|
|
$placeholders = DB::placeholders($values);
|
|
if($inclusive) {
|
|
$predicate = "$column IN ($placeholders)";
|
|
} else {
|
|
$predicate = "$column NOT IN ($placeholders)";
|
|
}
|
|
} else {
|
|
// Generate reusable comparison clause
|
|
$comparisonClause = DB::get_conn()->comparisonClause(
|
|
$this->getDbName(),
|
|
null,
|
|
true, // exact?
|
|
!$inclusive, // negate?
|
|
$this->getCaseSensitive(),
|
|
true
|
|
);
|
|
$count = count($values);
|
|
if($count > 1) {
|
|
$connective = $inclusive ? ' OR ' : ' AND ';
|
|
$conditions = array_fill(0, $count, $comparisonClause);
|
|
$predicate = implode($connective, $conditions);
|
|
} else {
|
|
$predicate = $comparisonClause;
|
|
}
|
|
}
|
|
|
|
// Always check for null when doing exclusive checks (either AND IS NOT NULL / OR IS NULL)
|
|
// or when including the null value explicitly (OR IS NULL)
|
|
if($hasNull || !$inclusive) {
|
|
// If excluding values which don't include null, or including
|
|
// values which include null, we should do an `OR IS NULL`.
|
|
// Otherwise we are excluding values that do include null, so `AND IS NOT NULL`.
|
|
// Simplified from (!$inclusive && !$hasNull) || ($inclusive && $hasNull);
|
|
$isNull = !$hasNull || $inclusive;
|
|
$nullCondition = DB::get_conn()->nullCheckClause($field, $isNull);
|
|
|
|
// Determine merge strategy
|
|
if(empty($predicate)) {
|
|
$predicate = $nullCondition;
|
|
} else {
|
|
// Merge null condition with predicate
|
|
if($isNull) {
|
|
$nullCondition = " OR {$nullCondition}";
|
|
} else {
|
|
$nullCondition = " AND {$nullCondition}";
|
|
}
|
|
// If current predicate connective doesn't match the same as the null connective
|
|
// make sure to group the prior condition
|
|
if($connective && (($connective === ' OR ') !== $isNull)) {
|
|
$predicate = "({$predicate})";
|
|
}
|
|
$predicate .= $nullCondition;
|
|
}
|
|
}
|
|
|
|
return $query->where(array($predicate => $values));
|
|
}
|
|
|
|
public function isEmpty() {
|
|
return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
|
|
}
|
|
}
|