2008-08-06 05:28:25 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-09 08:40:50 +02:00
|
|
|
* @subpackage search
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects textual content with an exact match between columnname and keyword.
|
2008-08-06 05:28:25 +02:00
|
|
|
*
|
|
|
|
* @todo case sensitivity switch
|
|
|
|
* @todo documentation
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-06 05:28:25 +02:00
|
|
|
* @subpackage search
|
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
class ExactMatchFilter extends SearchFilter {
|
2012-09-20 09:26:05 +02:00
|
|
|
protected function comparison($exclude = false) {
|
|
|
|
$modifiers = $this->getModifiers();
|
|
|
|
if(($extras = array_diff($modifiers, array('not', 'nocase', 'case'))) != array()) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
get_class($this) . ' does not accept ' . implode(', ', $extras) . ' as modifiers');
|
|
|
|
}
|
|
|
|
if(!in_array('case', $modifiers) && !in_array('nocase', $modifiers)) {
|
|
|
|
if($exclude) {
|
|
|
|
return '!=';
|
|
|
|
} else {
|
|
|
|
return '=';
|
|
|
|
}
|
|
|
|
} elseif(DB::getConn() instanceof PostgreSQLDatabase) {
|
|
|
|
if(in_array('case', $modifiers)) {
|
|
|
|
$comparison = 'LIKE';
|
|
|
|
} else {
|
|
|
|
$comparison = 'ILIKE';
|
|
|
|
}
|
|
|
|
} elseif(in_array('case', $modifiers)) {
|
|
|
|
$comparison = 'LIKE BINARY';
|
|
|
|
} else {
|
|
|
|
$comparison = 'LIKE';
|
|
|
|
}
|
|
|
|
if($exclude) {
|
|
|
|
$comparison = 'NOT ' . $comparison;
|
|
|
|
}
|
|
|
|
return $comparison;
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* Applies an exact match (equals) on a field value.
|
|
|
|
*
|
2012-09-06 09:54:44 +02:00
|
|
|
* @return DataQuery
|
2008-08-09 06:06:52 +02:00
|
|
|
*/
|
2012-09-06 09:54:44 +02:00
|
|
|
protected function applyOne(DataQuery $query) {
|
2011-03-21 09:37:55 +01:00
|
|
|
$this->model = $query->applyRelation($this->relation);
|
2011-04-05 13:01:57 +02:00
|
|
|
return $query->where(sprintf(
|
2012-09-20 09:26:05 +02:00
|
|
|
"%s %s '%s'",
|
2008-10-05 21:20:35 +02:00
|
|
|
$this->getDbName(),
|
2012-09-20 09:26:05 +02:00
|
|
|
$this->comparison(false),
|
2008-10-05 21:20:35 +02:00
|
|
|
Convert::raw2sql($this->getValue())
|
|
|
|
));
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2012-09-06 09:54:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies an exact match (equals) on a field value against multiple
|
|
|
|
* possible values.
|
|
|
|
*
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
protected function applyMany(DataQuery $query) {
|
|
|
|
$this->model = $query->applyRelation($this->relation);
|
|
|
|
$values = array();
|
|
|
|
foreach($this->getValue() as $value) {
|
|
|
|
$values[] = Convert::raw2sql($value);
|
|
|
|
}
|
2012-09-20 09:26:05 +02:00
|
|
|
if($this->comparison(false) == '=') {
|
|
|
|
// Neither :case nor :nocase
|
|
|
|
$valueStr = "'" . implode("', '", $values) . "'";
|
|
|
|
return $query->where(sprintf(
|
|
|
|
'%s IN (%s)',
|
|
|
|
$this->getDbName(),
|
|
|
|
$valueStr
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
foreach($values as &$v) {
|
|
|
|
$v = sprintf(
|
|
|
|
"%s %s '%s'",
|
|
|
|
$this->getDbName(),
|
|
|
|
$this->comparison(false),
|
|
|
|
$v
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$where = implode(' OR ', $values);
|
|
|
|
return $query->where($where);
|
|
|
|
}
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Excludes an exact match (equals) on a field value.
|
|
|
|
*
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
protected function excludeOne(DataQuery $query) {
|
|
|
|
$this->model = $query->applyRelation($this->relation);
|
|
|
|
return $query->where(sprintf(
|
2012-09-20 09:26:05 +02:00
|
|
|
"%s %s '%s'",
|
2012-09-06 09:54:44 +02:00
|
|
|
$this->getDbName(),
|
2012-09-20 09:26:05 +02:00
|
|
|
$this->comparison(true),
|
2012-09-06 09:54:44 +02:00
|
|
|
Convert::raw2sql($this->getValue())
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Excludes an exact match (equals) on a field value against multiple
|
|
|
|
* possible values.
|
|
|
|
*
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
protected function excludeMany(DataQuery $query) {
|
|
|
|
$this->model = $query->applyRelation($this->relation);
|
|
|
|
$values = array();
|
|
|
|
foreach($this->getValue() as $value) {
|
|
|
|
$values[] = Convert::raw2sql($value);
|
|
|
|
}
|
2012-09-20 09:26:05 +02:00
|
|
|
if($this->comparison(false) == '=') {
|
|
|
|
// Neither :case nor :nocase
|
|
|
|
$valueStr = "'" . implode("', '", $values) . "'";
|
|
|
|
return $query->where(sprintf(
|
|
|
|
'%s NOT IN (%s)',
|
|
|
|
$this->getDbName(),
|
|
|
|
$valueStr
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
foreach($values as &$v) {
|
|
|
|
$v = sprintf(
|
|
|
|
"%s %s '%s'",
|
|
|
|
$this->getDbName(),
|
|
|
|
$this->comparison(true),
|
|
|
|
$v
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$where = implode(' OR ', $values);
|
|
|
|
return $query->where($where);
|
|
|
|
}
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2008-08-11 02:03:57 +02:00
|
|
|
public function isEmpty() {
|
2012-09-06 09:54:44 +02:00
|
|
|
return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
|
2008-08-11 02:03:57 +02:00
|
|
|
}
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|