silverstripe-framework/ORM/Search/SearchContext.php

300 lines
8.4 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\ORM\Search;
2016-09-09 08:43:05 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Object;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\Filters\SearchFilter;
use InvalidArgumentException;
use Exception;
/**
* Manages searching of properties on one or more {@link DataObject}
* types, based on a given set of input parameters.
* SearchContext is intentionally decoupled from any controller-logic,
* it just receives a set of search parameters and an object class it acts on.
2014-08-15 08:53:05 +02:00
*
* The default output of a SearchContext is either a {@link SQLSelect} object
* for further refinement, or a {@link SS_List} that can be used to display
* search results, e.g. in a {@link TableListField} instance.
2014-08-15 08:53:05 +02:00
*
* In case you need multiple contexts, consider namespacing your request parameters
* by using {@link FieldList->namespace()} on the $fields constructor parameter.
2014-08-15 08:53:05 +02:00
*
* Each DataObject subclass can have multiple search contexts for different cases,
* e.g. for a limited frontend search and a fully featured backend search.
* By default, you can use {@link DataObject->getDefaultSearchContext()} which is automatically
* scaffolded. It uses {@link DataObject::$searchable_fields} to determine which fields
* to include.
2014-08-15 08:53:05 +02:00
*
* @see http://doc.silverstripe.com/doku.php?id=searchcontext
*/
class SearchContext extends Object {
2014-08-15 08:53:05 +02:00
/**
* DataObject subclass to which search parameters relate to.
* Also determines as which object each result is provided.
*
* @var string
*/
protected $modelClass;
2014-08-15 08:53:05 +02:00
/**
* FormFields mapping to {@link DataObject::$db} properties
* which are supposed to be searchable.
*
* @var FieldList
*/
protected $fields;
2014-08-15 08:53:05 +02:00
/**
* Array of {@link SearchFilter} subclasses.
*
* @var SearchFilter[]
*/
protected $filters;
2014-08-15 08:53:05 +02:00
/**
* The logical connective used to join WHERE clauses. Defaults to AND.
* @var string
*/
public $connective = 'AND';
2014-08-15 08:53:05 +02:00
/**
* A key value pair of values that should be searched for.
* The keys should match the field names specified in {@link self::$fields}.
* Usually these values come from a submitted searchform
* in the form of a $_REQUEST object.
* CAUTION: All values should be treated as insecure client input.
2014-08-15 08:53:05 +02:00
*
* @param string $modelClass The base {@link DataObject} class that search properties related to.
* Also used to generate a set of result objects based on this class.
* @param FieldList $fields Optional. FormFields mapping to {@link DataObject::$db} properties
2014-08-15 08:53:05 +02:00
* which are to be searched. Derived from modelclass using
* {@link DataObject::scaffoldSearchFields()} if left blank.
* @param array $filters Optional. Derived from modelclass if left blank
2014-08-15 08:53:05 +02:00
*/
public function __construct($modelClass, $fields = null, $filters = null) {
$this->modelClass = $modelClass;
$this->fields = ($fields) ? $fields : new FieldList();
$this->filters = ($filters) ? $filters : array();
2014-08-15 08:53:05 +02:00
parent::__construct();
}
2014-08-15 08:53:05 +02:00
/**
* Returns scaffolded search fields for UI.
*
* @return FieldList
*/
public function getSearchFields() {
return ($this->fields) ? $this->fields : singleton($this->modelClass)->scaffoldSearchFields();
// $this->fields is causing weirdness, so we ignore for now, using the default scaffolding
//return singleton($this->modelClass)->scaffoldSearchFields();
}
2014-08-15 08:53:05 +02:00
/**
* @todo move to SQLSelect
* @todo fix hack
*/
protected function applyBaseTableFields() {
$classes = ClassInfo::dataClassesFor($this->modelClass);
$baseTable = DataObject::getSchema()->baseDataTable($this->modelClass);
$fields = array("\"{$baseTable}\".*");
if($this->modelClass != $classes[0]) {
$fields[] = '"'.$classes[0].'".*';
}
//$fields = array_keys($model->db());
$fields[] = '"'.$classes[0].'".\"ClassName\" AS "RecordClassName"';
return $fields;
}
2014-08-15 08:53:05 +02:00
/**
* Returns a SQL object representing the search context for the given
* list of query parameters.
*
* @param array $searchParams Map of search criteria, mostly taked from $_REQUEST.
* If a filter is applied to a relationship in dot notation,
* the parameter name should have the dots replaced with double underscores,
* for example "Comments__Name" instead of the filter name "Comments.Name".
* @param array|bool|string $sort Database column to sort on.
* Falls back to {@link DataObject::$default_sort} if not provided.
* @param array|bool|string $limit
2012-03-08 07:23:32 +01:00
* @param DataList $existingQuery
* @return DataList
* @throws Exception
*/
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null) {
/** DataList $query */
if($existingQuery) {
if(!($existingQuery instanceof DataList)) {
throw new InvalidArgumentException("existingQuery must be DataList");
}
if($existingQuery->dataClass() != $this->modelClass) {
2014-08-15 08:53:05 +02:00
throw new InvalidArgumentException("existingQuery's dataClass is " . $existingQuery->dataClass()
. ", $this->modelClass expected.");
}
$query = $existingQuery;
} else {
$query = DataList::create($this->modelClass);
}
if(is_array($limit)) {
$query = $query->limit(
2014-08-15 08:53:05 +02:00
isset($limit['limit']) ? $limit['limit'] : null,
isset($limit['start']) ? $limit['start'] : null
);
} else {
$query = $query->limit($limit);
}
$query = $query->sort($sort);
2014-08-15 08:53:05 +02:00
// hack to work with $searchParems when it's an Object
2016-09-09 08:43:05 +02:00
if ($searchParams instanceof HTTPRequest) {
$searchParamArray = $searchParams->getVars();
} else {
$searchParamArray = $searchParams;
}
foreach($searchParamArray as $key => $value) {
$key = str_replace('__', '.', $key);
if($filter = $this->getFilter($key)) {
$filter->setModel($this->modelClass);
$filter->setValue($value);
if(! $filter->isEmpty()) {
$query = $query->alterDataQuery(array($filter, 'apply'));
}
}
}
2014-08-15 08:53:05 +02:00
if($this->connective != "AND") {
throw new Exception("SearchContext connective '$this->connective' not supported after ORM-rewrite.");
}
2014-08-15 08:53:05 +02:00
return $query;
}
/**
* Returns a result set from the given search parameters.
*
* @todo rearrange start and limit params to reflect DataObject
2014-08-15 08:53:05 +02:00
*
* @param array $searchParams
* @param array|bool|string $sort
* @param array|bool|string $limit
* @return DataList
* @throws Exception
*/
public function getResults($searchParams, $sort = false, $limit = false) {
$searchParams = array_filter((array)$searchParams, array($this,'clearEmptySearchFields'));
// getQuery actually returns a DataList
return $this->getQuery($searchParams, $sort, $limit);
}
/**
* Callback map function to filter fields with empty values from
* being included in the search expression.
*
* @param mixed $value
* @return boolean
*/
public function clearEmptySearchFields($value) {
return ($value != '');
}
2014-08-15 08:53:05 +02:00
/**
* Accessor for the filter attached to a named field.
*
* @param string $name
* @return SearchFilter
*/
public function getFilter($name) {
if (isset($this->filters[$name])) {
return $this->filters[$name];
} else {
return null;
}
}
2014-08-15 08:53:05 +02:00
/**
* Get the map of filters in the current search context.
*
* @return SearchFilter[]
*/
public function getFilters() {
return $this->filters;
}
2014-08-15 08:53:05 +02:00
/**
* Overwrite the current search context filter map.
*
* @param array $filters
*/
public function setFilters($filters) {
$this->filters = $filters;
2014-08-15 08:53:05 +02:00
}
/**
* Adds a instance of {@link SearchFilter}.
*
* @param SearchFilter $filter
*/
public function addFilter($filter) {
$this->filters[$filter->getFullName()] = $filter;
}
2014-08-15 08:53:05 +02:00
/**
* Removes a filter by name.
*
* @param string $name
*/
public function removeFilterByName($name) {
unset($this->filters[$name]);
}
2014-08-15 08:53:05 +02:00
/**
* Get the list of searchable fields in the current search context.
*
* @return FieldList
*/
public function getFields() {
2014-08-15 08:53:05 +02:00
return $this->fields;
}
2014-08-15 08:53:05 +02:00
/**
* Apply a list of searchable fields to the current search context.
*
* @param FieldList $fields
*/
public function setFields($fields) {
$this->fields = $fields;
}
2014-08-15 08:53:05 +02:00
/**
* Adds a new {@link FormField} instance.
*
* @param FormField $field
*/
public function addField($field) {
$this->fields->push($field);
}
2014-08-15 08:53:05 +02:00
/**
* Removes an existing formfield instance by its name.
*
* @param string $fieldName
*/
public function removeFieldByName($fieldName) {
$this->fields->removeByName($fieldName);
}
2014-08-15 08:53:05 +02:00
}