2008-08-06 05:28:25 +02:00
|
|
|
<?php
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-09 08:40:50 +02:00
|
|
|
* @subpackage search
|
|
|
|
*/
|
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
/**
|
|
|
|
* Matches textual content with a LIKE '%keyword%' construct.
|
|
|
|
*
|
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 PartialMatchFilter 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(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-06 05:28:25 +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);
|
2012-09-20 09:26:05 +02:00
|
|
|
$comparison = $this->comparison(false);
|
2012-09-06 09:54:44 +02:00
|
|
|
$where = sprintf("%s %s '%%%s%%'", $this->getDbName(), $comparison, Convert::raw2sql($this->getValue()));
|
|
|
|
|
|
|
|
return $query->where($where);
|
|
|
|
}
|
2012-02-15 11:42:29 +01:00
|
|
|
|
2012-09-06 09:54:44 +02:00
|
|
|
protected function applyMany(DataQuery $query) {
|
|
|
|
$this->model = $query->applyRelation($this->relation);
|
|
|
|
$where = array();
|
2012-09-20 09:26:05 +02:00
|
|
|
$comparison = $this->comparison(false);
|
2012-09-06 09:54:44 +02:00
|
|
|
foreach($this->getValue() as $value) {
|
|
|
|
$where[]= sprintf("%s %s '%%%s%%'", $this->getDbName(), $comparison, Convert::raw2sql($value));
|
2012-02-15 11:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query->where(implode(' OR ', $where));
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2012-09-06 09:54:44 +02:00
|
|
|
|
|
|
|
protected function excludeOne(DataQuery $query) {
|
|
|
|
$this->model = $query->applyRelation($this->relation);
|
2012-09-20 09:26:05 +02:00
|
|
|
$comparison = $this->comparison(true);
|
|
|
|
$where = sprintf("%s %s '%%%s%%'", $this->getDbName(), $comparison, Convert::raw2sql($this->getValue()));
|
2012-09-06 09:54:44 +02:00
|
|
|
|
|
|
|
return $query->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function excludeMany(DataQuery $query) {
|
|
|
|
$this->model = $query->applyRelation($this->relation);
|
|
|
|
$where = array();
|
2012-09-20 09:26:05 +02:00
|
|
|
$comparison = $this->comparison(true);
|
2012-09-06 09:54:44 +02:00
|
|
|
foreach($this->getValue() as $value) {
|
2012-09-20 09:26:05 +02:00
|
|
|
$where[]= sprintf("%s %s '%%%s%%'", $this->getDbName(), $comparison, Convert::raw2sql($value));
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query->where(implode(' AND ', $where));
|
|
|
|
}
|
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
|
|
|
}
|