2008-08-06 05:28:25 +02:00
|
|
|
<?php
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
2009-09-15 23:18:21 +02:00
|
|
|
* Base class for filtering implementations,
|
|
|
|
* which work together with {@link SearchContext}
|
|
|
|
* to create or amend a query for {@link DataObject} instances.
|
|
|
|
* See {@link SearchContext} for more information.
|
2008-08-06 05:28:25 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-06 05:28:25 +02:00
|
|
|
* @subpackage search
|
|
|
|
*/
|
|
|
|
abstract class SearchFilter extends Object {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
|
|
|
* @var string Classname of the inspected {@link DataObject}
|
|
|
|
*/
|
2008-08-09 06:53:34 +02:00
|
|
|
protected $model;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-08-09 06:06:52 +02:00
|
|
|
protected $name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @var string
|
2009-09-15 23:18:21 +02:00
|
|
|
*/
|
|
|
|
protected $fullName;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2008-08-09 06:38:44 +02:00
|
|
|
protected $value;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-20 09:26:05 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $modifiers;
|
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
|
|
|
* @var string Name of a has-one, has-many or many-many relation (not the classname).
|
2014-08-15 08:53:05 +02:00
|
|
|
* Set in the constructor as part of the name in dot-notation, and used in
|
2009-09-15 23:18:21 +02:00
|
|
|
* {@link applyRelation()}.
|
|
|
|
*/
|
2008-08-09 06:53:34 +02:00
|
|
|
protected $relation;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $fullName Determines the name of the field, as well as the searched database
|
2009-09-15 23:18:21 +02:00
|
|
|
* column. Can contain a relation name in dot notation, which will automatically join
|
|
|
|
* the necessary tables (e.g. "Comments.Name" to join the "Comments" has-many relationship and
|
|
|
|
* search the "Name" column when applying this filter to a SiteTree class).
|
|
|
|
* @param mixed $value
|
2012-09-20 09:26:05 +02:00
|
|
|
* @param array $modifiers
|
2009-09-15 23:18:21 +02:00
|
|
|
*/
|
2012-09-20 09:26:05 +02:00
|
|
|
public function __construct($fullName, $value = false, array $modifiers = array()) {
|
2009-09-15 23:18:21 +02:00
|
|
|
$this->fullName = $fullName;
|
2013-06-08 10:28:15 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
// sets $this->name and $this->relation
|
|
|
|
$this->addRelation($fullName);
|
2008-08-09 06:38:44 +02:00
|
|
|
$this->value = $value;
|
2012-12-10 23:35:04 +01:00
|
|
|
$this->setModifiers($modifiers);
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
|
|
|
* Called by constructor to convert a string pathname into
|
|
|
|
* a well defined relationship sequence.
|
|
|
|
*
|
2009-09-15 23:18:21 +02:00
|
|
|
* @param string $name
|
2008-08-09 08:40:50 +02:00
|
|
|
*/
|
|
|
|
protected function addRelation($name) {
|
|
|
|
if (strstr($name, '.')) {
|
|
|
|
$parts = explode('.', $name);
|
|
|
|
$this->name = array_pop($parts);
|
|
|
|
$this->relation = $parts;
|
|
|
|
} else {
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
|
|
|
* Set the root model class to be selected by this
|
|
|
|
* search query.
|
|
|
|
*
|
|
|
|
* @param string $className
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2008-08-09 06:53:34 +02:00
|
|
|
public function setModel($className) {
|
|
|
|
$this->model = $className;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Set the current value(s) to be filtered on.
|
2008-08-09 08:40:50 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array $value
|
2008-08-09 08:40:50 +02:00
|
|
|
*/
|
|
|
|
public function setValue($value) {
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
|
|
|
* Accessor for the current value to be filtered on.
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return string|array
|
2008-08-09 08:40:50 +02:00
|
|
|
*/
|
|
|
|
public function getValue() {
|
|
|
|
return $this->value;
|
|
|
|
}
|
2012-09-20 09:26:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current modifiers to apply to the filter
|
|
|
|
*
|
|
|
|
* @param array $modifiers
|
|
|
|
*/
|
|
|
|
public function setModifiers(array $modifiers) {
|
|
|
|
$this->modifiers = array_map('strtolower', $modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accessor for the current modifiers to apply to the filter.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getModifiers() {
|
|
|
|
return $this->modifiers;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
2008-08-11 01:03:35 +02:00
|
|
|
* The original name of the field.
|
2008-08-09 06:53:34 +02:00
|
|
|
*
|
2008-08-11 01:03:35 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return $this->name;
|
|
|
|
}
|
2012-03-02 13:41:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
|
|
|
public function setName($name) {
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
|
|
|
* The full name passed to the constructor,
|
|
|
|
* including any (optional) relations in dot notation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-09-15 23:18:21 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFullName() {
|
|
|
|
return $this->fullName;
|
|
|
|
}
|
2012-03-02 13:41:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
|
|
|
public function setFullName($name) {
|
|
|
|
$this->fullName = $name;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 01:03:35 +02:00
|
|
|
/**
|
|
|
|
* Normalizes the field name to table mapping.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-11 01:03:35 +02:00
|
|
|
* @return string
|
2008-08-09 06:53:34 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDbName() {
|
2008-12-04 23:38:32 +01:00
|
|
|
// Special handler for "NULL" relations
|
2013-06-08 10:28:15 +02:00
|
|
|
if($this->name == "NULL") {
|
|
|
|
return $this->name;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
// This code finds the table where the field named $this->name lives
|
|
|
|
// Todo: move to somewhere more appropriate, such as DataMapper, the
|
|
|
|
// magical class-to-be?
|
2008-08-09 06:53:34 +02:00
|
|
|
$candidateClass = $this->model;
|
2013-06-08 10:28:15 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
while($candidateClass != 'DataObject') {
|
2014-08-15 08:53:05 +02:00
|
|
|
if( DataObject::has_own_table($candidateClass)
|
2013-06-21 00:32:08 +02:00
|
|
|
&& DataObject::has_own_table_database_field($candidateClass, $this->name)
|
|
|
|
) {
|
2012-09-26 23:34:00 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-06-08 10:28:15 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
$candidateClass = get_parent_class($candidateClass);
|
|
|
|
}
|
2013-06-08 10:28:15 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if($candidateClass == 'DataObject') {
|
2013-06-08 10:29:36 +02:00
|
|
|
// fallback to the provided name in the event of a joined column
|
|
|
|
// name (as the candidate class doesn't check joined records)
|
2013-07-05 00:41:18 +02:00
|
|
|
$parts = explode('.', $this->fullName);
|
|
|
|
return '"' . implode('"."', $parts) . '"';
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-29 23:03:17 +02:00
|
|
|
return "\"$candidateClass\".\"$this->name\"";
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-13 00:39:07 +01:00
|
|
|
/**
|
|
|
|
* Return the value of the field as processed by the DBField class
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDbFormattedValue() {
|
2008-11-13 00:39:07 +01:00
|
|
|
// SRM: This code finds the table where the field named $this->name lives
|
|
|
|
// Todo: move to somewhere more appropriate, such as DataMapper, the magical class-to-be?
|
|
|
|
$candidateClass = $this->model;
|
|
|
|
$dbField = singleton($this->model)->dbObject($this->name);
|
|
|
|
$dbField->setValue($this->value);
|
|
|
|
return $dbField->RAW();
|
|
|
|
}
|
2011-03-21 09:37:55 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
|
|
|
* Apply filter criteria to a SQL query.
|
|
|
|
*
|
2012-09-06 09:54:44 +02:00
|
|
|
* @param DataQuery $query
|
|
|
|
* @return DataQuery
|
2008-08-09 06:38:44 +02:00
|
|
|
*/
|
2012-09-06 09:54:44 +02:00
|
|
|
public function apply(DataQuery $query) {
|
2012-09-20 09:26:05 +02:00
|
|
|
if(($key = array_search('not', $this->modifiers)) !== false) {
|
|
|
|
unset($this->modifiers[$key]);
|
|
|
|
return $this->exclude($query);
|
|
|
|
}
|
2012-09-06 09:54:44 +02:00
|
|
|
if(is_array($this->value)) {
|
|
|
|
return $this->applyMany($query);
|
|
|
|
} else {
|
|
|
|
return $this->applyOne($query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply filter criteria to a SQL query with a single value.
|
|
|
|
*
|
|
|
|
* @param DataQuery $query
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
abstract protected function applyOne(DataQuery $query);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply filter criteria to a SQL query with an array of values.
|
|
|
|
*
|
|
|
|
* @param DataQuery $query
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
protected function applyMany(DataQuery $query) {
|
|
|
|
throw new InvalidArgumentException(get_class($this) . "can't be used to filter by a list of items.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exclude filter criteria from a SQL query.
|
|
|
|
*
|
|
|
|
* @param DataQuery $query
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
public function exclude(DataQuery $query) {
|
2012-09-20 09:26:05 +02:00
|
|
|
if(($key = array_search('not', $this->modifiers)) !== false) {
|
|
|
|
unset($this->modifiers[$key]);
|
|
|
|
return $this->apply($query);
|
|
|
|
}
|
2012-09-06 09:54:44 +02:00
|
|
|
if(is_array($this->value)) {
|
|
|
|
return $this->excludeMany($query);
|
|
|
|
} else {
|
|
|
|
return $this->excludeOne($query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exclude filter criteria from a SQL query with a single value.
|
|
|
|
*
|
|
|
|
* @param DataQuery $query
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
abstract protected function excludeOne(DataQuery $query);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exclude filter criteria from a SQL query with an array of values.
|
|
|
|
*
|
|
|
|
* @param DataQuery $query
|
|
|
|
* @return DataQuery
|
|
|
|
*/
|
|
|
|
protected function excludeMany(DataQuery $query) {
|
|
|
|
throw new InvalidArgumentException(get_class($this) . "can't be used to filter by a list of items.");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 02:03:57 +02:00
|
|
|
/**
|
|
|
|
* Determines if a field has a value,
|
|
|
|
* and that the filter should be applied.
|
|
|
|
* Relies on the field being populated with
|
|
|
|
* {@link setValue()}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-11 02:03:57 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isEmpty() {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-10 23:35:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines case sensitivity based on {@link getModifiers()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-12-10 23:35:04 +01:00
|
|
|
* @return Mixed TRUE or FALSE to enforce sensitivity, NULL to use field collation.
|
|
|
|
*/
|
|
|
|
protected function getCaseSensitive() {
|
|
|
|
$modifiers = $this->getModifiers();
|
|
|
|
if(in_array('case', $modifiers)) return true;
|
|
|
|
else if(in_array('nocase', $modifiers)) return false;
|
|
|
|
else return null;
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:09:05 +02:00
|
|
|
}
|