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 {
|
|
|
|
|
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;
|
2009-09-15 23:18:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-08-09 06:06:52 +02:00
|
|
|
protected $name;
|
2009-09-15 23:18:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $fullName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2008-08-09 06:38:44 +02:00
|
|
|
protected $value;
|
2009-09-15 23:18:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Name of a has-one, has-many or many-many relation (not the classname).
|
|
|
|
* Set in the constructor as part of the name in dot-notation, and used in
|
|
|
|
* {@link applyRelation()}.
|
|
|
|
*/
|
2008-08-09 06:53:34 +02:00
|
|
|
protected $relation;
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
|
|
|
* @param string $fullName Determines the name of the field, as well as the searched database
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
function __construct($fullName, $value = false) {
|
|
|
|
$this->fullName = $fullName;
|
|
|
|
// sets $this->name and $this->relation
|
|
|
|
$this->addRelation($fullName);
|
2008-08-09 06:38:44 +02:00
|
|
|
$this->value = $value;
|
2008-08-09 06:06:52 +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
|
|
|
}
|
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
|
|
|
* Set the root model class to be selected by this
|
|
|
|
* search query.
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
*/
|
2008-08-09 06:53:34 +02:00
|
|
|
public function setModel($className) {
|
|
|
|
$this->model = $className;
|
|
|
|
}
|
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
|
|
|
* Set the current value to be filtered on.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*/
|
|
|
|
public function setValue($value) {
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accessor for the current value to be filtered on.
|
2008-10-05 21:20:35 +02:00
|
|
|
* Caution: Data is not escaped.
|
2008-08-09 08:40:50 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getValue() {
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2008-08-11 01:03:35 +02:00
|
|
|
|
2009-09-15 23:18:21 +02:00
|
|
|
/**
|
|
|
|
* The full name passed to the constructor,
|
|
|
|
* including any (optional) relations in dot notation.
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2009-09-15 23:18:21 +02:00
|
|
|
|
2008-08-11 01:03:35 +02:00
|
|
|
/**
|
|
|
|
* Normalizes the field name to table mapping.
|
|
|
|
*
|
|
|
|
* @return string
|
2008-08-09 06:53:34 +02:00
|
|
|
*/
|
2008-08-19 12:09:28 +02:00
|
|
|
function getDbName() {
|
2008-12-04 23:38:32 +01:00
|
|
|
// Special handler for "NULL" relations
|
|
|
|
if($this->name == "NULL") return $this->name;
|
|
|
|
|
2008-08-09 06:53:34 +02: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;
|
|
|
|
while($candidateClass != 'DataObject') {
|
2012-06-04 17:27:58 +02:00
|
|
|
if(DataObject::has_own_table($candidateClass) && singleton($candidateClass)->hasOwnTableDatabaseField($this->name)) break;
|
2008-08-09 06:53:34 +02:00
|
|
|
$candidateClass = get_parent_class($candidateClass);
|
|
|
|
}
|
|
|
|
if($candidateClass == 'DataObject') user_error("Couldn't find field $this->name in any of $this->model's tables.", E_USER_ERROR);
|
|
|
|
|
2009-03-29 23:03:17 +02:00
|
|
|
return "\"$candidateClass\".\"$this->name\"";
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
|
|
|
|
2008-11-13 00:39:07 +01:00
|
|
|
/**
|
|
|
|
* Return the value of the field as processed by the DBField class
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getDbFormattedValue() {
|
|
|
|
// 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
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
|
|
|
* Apply filter criteria to a SQL query.
|
|
|
|
*
|
|
|
|
* @param SQLQuery $query
|
2008-08-09 08:40:50 +02:00
|
|
|
* @return SQLQuery
|
2008-08-09 06:38:44 +02:00
|
|
|
*/
|
2011-03-21 09:37:55 +01:00
|
|
|
abstract public function apply(DataQuery $query);
|
2008-08-09 06:06:52 +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()}
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isEmpty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|