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-12-10 23:35:04 +01:00
|
|
|
|
|
|
|
public function setModifiers(array $modifiers) {
|
2012-09-20 09:26:05 +02:00
|
|
|
if(($extras = array_diff($modifiers, array('not', 'nocase', 'case'))) != array()) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
get_class($this) . ' does not accept ' . implode(', ', $extras) . ' as modifiers');
|
|
|
|
}
|
2012-12-10 23:35:04 +01:00
|
|
|
|
|
|
|
parent::setModifiers($modifiers);
|
2012-09-20 09:26:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Apply the match filter to the given variable value
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $value The raw value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getMatchPattern($value) {
|
|
|
|
return "%$value%";
|
|
|
|
}
|
2014-08-15 08:53:05 +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);
|
2013-06-21 00:32:08 +02:00
|
|
|
$comparisonClause = DB::get_conn()->comparisonClause(
|
2012-12-10 23:35:04 +01:00
|
|
|
$this->getDbName(),
|
2013-06-21 00:32:08 +02:00
|
|
|
null,
|
2012-12-10 23:35:04 +01:00
|
|
|
false, // exact?
|
|
|
|
false, // negate?
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->getCaseSensitive(),
|
|
|
|
true
|
2012-12-10 23:35:04 +01:00
|
|
|
);
|
2013-06-21 00:32:08 +02:00
|
|
|
return $query->where(array(
|
|
|
|
$comparisonClause => $this->getMatchPattern($this->getValue())
|
|
|
|
));
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
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);
|
2013-06-21 00:32:08 +02:00
|
|
|
$whereClause = array();
|
|
|
|
$comparisonClause = DB::get_conn()->comparisonClause(
|
|
|
|
$this->getDbName(),
|
|
|
|
null,
|
|
|
|
false, // exact?
|
|
|
|
false, // negate?
|
|
|
|
$this->getCaseSensitive(),
|
|
|
|
true
|
|
|
|
);
|
2012-09-06 09:54:44 +02:00
|
|
|
foreach($this->getValue() as $value) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$whereClause[] = array($comparisonClause => $this->getMatchPattern($value));
|
2012-02-15 11:42:29 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
return $query->whereAny($whereClause);
|
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);
|
2013-06-21 00:32:08 +02:00
|
|
|
$comparisonClause = DB::get_conn()->comparisonClause(
|
2012-12-10 23:35:04 +01:00
|
|
|
$this->getDbName(),
|
2013-06-21 00:32:08 +02:00
|
|
|
null,
|
2012-12-10 23:35:04 +01:00
|
|
|
false, // exact?
|
|
|
|
true, // negate?
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->getCaseSensitive(),
|
|
|
|
true
|
2012-12-10 23:35:04 +01:00
|
|
|
);
|
2013-06-21 00:32:08 +02:00
|
|
|
return $query->where(array(
|
|
|
|
$comparisonClause => $this->getMatchPattern($this->getValue())
|
|
|
|
));
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function excludeMany(DataQuery $query) {
|
|
|
|
$this->model = $query->applyRelation($this->relation);
|
2013-06-21 00:32:08 +02:00
|
|
|
$values = $this->getValue();
|
|
|
|
$comparisonClause = DB::get_conn()->comparisonClause(
|
|
|
|
$this->getDbName(),
|
|
|
|
null,
|
|
|
|
false, // exact?
|
|
|
|
true, // negate?
|
|
|
|
$this->getCaseSensitive(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$parameters = array();
|
|
|
|
foreach($values as $value) {
|
|
|
|
$parameters[] = $this->getMatchPattern($value);
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
// Since query connective is ambiguous, use AND explicitly here
|
|
|
|
$count = count($values);
|
|
|
|
$predicate = implode(' AND ', array_fill(0, $count, $comparisonClause));
|
|
|
|
return $query->where(array($predicate => $parameters));
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +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
|
|
|
}
|