2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implements a "lazy loading" DataObjectSet.
|
|
|
|
* Uses {@link DataQuery} to do the actual query generation.
|
|
|
|
*/
|
2011-05-02 10:14:55 +02:00
|
|
|
class DataList extends ViewableData implements SS_List {
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* The DataObject class name that this data list is querying
|
|
|
|
*/
|
|
|
|
protected $dataClass;
|
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* The {@link DataQuery} object responsible for getting this DataList's records
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
protected $dataQuery;
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* The DataModel from which this DataList comes.
|
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Synonym of the constructor. Can be chained with literate methods.
|
|
|
|
* DataList::create("SiteTree")->sort("Title") is legal, but
|
|
|
|
* new DataList("SiteTree")->sort("Title") is not.
|
|
|
|
*/
|
|
|
|
static function create($dataClass) {
|
|
|
|
return new DataList($dataClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new DataList.
|
|
|
|
* No querying is done on construction, but the initial query schema is set up.
|
|
|
|
* @param $dataClass The DataObject class to query.
|
|
|
|
*/
|
|
|
|
public function __construct($dataClass) {
|
|
|
|
$this->dataClass = $dataClass;
|
|
|
|
$this->dataQuery = new DataQuery($this->dataClass);
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
public function setModel(DataModel $model) {
|
|
|
|
$this->model = $model;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
public function dataClass() {
|
|
|
|
return $this->dataClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone this object
|
|
|
|
*/
|
|
|
|
function __clone() {
|
|
|
|
$this->dataQuery = clone $this->dataQuery;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the internal {@link DataQuery} object for direct manipulation
|
|
|
|
*/
|
|
|
|
public function dataQuery() {
|
|
|
|
return $this->dataQuery;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the SQL query that will be used to get this DataList's records. Good for debugging. :-)
|
|
|
|
*/
|
|
|
|
public function sql() {
|
|
|
|
return $this->dataQuery->query()->sql();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-05 13:01:57 +02:00
|
|
|
* Add a WHERE clause to the query.
|
|
|
|
*
|
|
|
|
* @param string $filter
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-04-05 13:01:57 +02:00
|
|
|
public function where($filter) {
|
|
|
|
$this->dataQuery->where($filter);
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the sort order of this data list
|
|
|
|
*/
|
|
|
|
public function sort($sort, $direction = "ASC") {
|
|
|
|
if($direction && strtoupper($direction) != 'ASC') $sort = "$sort $direction";
|
|
|
|
$this->dataQuery->sort($sort);
|
|
|
|
return $this;
|
|
|
|
}
|
2011-03-30 03:19:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this DataList can be sorted by the given field.
|
|
|
|
*/
|
|
|
|
public function canSortBy($field) {
|
|
|
|
return $this->dataQuery()->query()->canSortBy($field);
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an join clause to this data list's query.
|
|
|
|
*/
|
|
|
|
public function join($join) {
|
2011-10-29 06:11:27 +02:00
|
|
|
Deprecation::notice('3.0', 'Use innerJoin() or leftJoin() instead.');
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->dataQuery->join($join);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restrict the records returned in this query by a limit clause
|
|
|
|
*/
|
|
|
|
public function limit($limit) {
|
|
|
|
$this->dataQuery->limit($limit);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an inner join clause to this data list's query.
|
|
|
|
*/
|
|
|
|
public function innerJoin($table, $onClause, $alias = null) {
|
|
|
|
$this->dataQuery->innerJoin($table, $onClause, $alias);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an left join clause to this data list's query.
|
|
|
|
*/
|
|
|
|
public function leftJoin($table, $onClause, $alias = null) {
|
|
|
|
$this->dataQuery->leftJoin($table, $onClause, $alias);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array of the actual items that this DataList contains at this stage.
|
|
|
|
* This is when the query is actually executed.
|
2011-05-02 10:14:55 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-05-02 10:14:55 +02:00
|
|
|
public function toArray() {
|
2009-11-22 06:16:38 +01:00
|
|
|
$query = $this->dataQuery->query();
|
|
|
|
$rows = $query->execute();
|
|
|
|
$results = array();
|
|
|
|
foreach($rows as $row) {
|
|
|
|
$results[] = $this->createDataObject($row);
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
2011-05-02 10:14:55 +02:00
|
|
|
|
|
|
|
public function toNestedArray() {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this as $item) {
|
|
|
|
$result[] = $item->toMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-10-29 03:04:17 +02:00
|
|
|
public function map($keyField = 'ID', $titleField = 'Title') {
|
|
|
|
return new SS_Map($this, $keyField, $titleField);
|
2011-05-02 10:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Create a data object from the given SQL row
|
|
|
|
*/
|
|
|
|
protected function createDataObject($row) {
|
|
|
|
$defaultClass = $this->dataClass;
|
|
|
|
|
|
|
|
// Failover from RecordClassName to ClassName
|
|
|
|
if(empty($row['RecordClassName'])) $row['RecordClassName'] = $row['ClassName'];
|
|
|
|
|
|
|
|
// Instantiate the class mentioned in RecordClassName only if it exists, otherwise default to $this->dataClass
|
2011-05-01 07:33:02 +02:00
|
|
|
if(class_exists($row['RecordClassName'])) $item = new $row['RecordClassName']($row, false, $this->model);
|
|
|
|
else $item = new $defaultClass($row, false, $this->model);
|
|
|
|
|
|
|
|
return $item;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Returns an Iterator for this DataList.
|
|
|
|
* This function allows you to use DataLists in foreach loops
|
|
|
|
* @return ArrayIterator
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function getIterator() {
|
2011-05-02 10:14:55 +02:00
|
|
|
return new ArrayIterator($this->toArray());
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of items in this DataList
|
|
|
|
*/
|
2011-05-02 10:14:55 +02:00
|
|
|
function count() {
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->dataQuery->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the maximum value of the given field in this DataList
|
|
|
|
*/
|
|
|
|
function Max($field) {
|
|
|
|
return $this->dataQuery->max($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the minimum value of the given field in this DataList
|
|
|
|
*/
|
|
|
|
function Min($field) {
|
|
|
|
return $this->dataQuery->min($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the average value of the given field in this DataList
|
|
|
|
*/
|
|
|
|
function Avg($field) {
|
|
|
|
return $this->dataQuery->avg($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the sum of the values of the given field in this DataList
|
|
|
|
*/
|
|
|
|
function Sum($field) {
|
|
|
|
return $this->dataQuery->sum($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first item in this DataList
|
|
|
|
*/
|
|
|
|
function First() {
|
|
|
|
foreach($this->dataQuery->firstRow()->execute() as $row) {
|
|
|
|
return $this->createDataObject($row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the last item in this DataList
|
|
|
|
*/
|
|
|
|
function Last() {
|
|
|
|
foreach($this->dataQuery->lastRow()->execute() as $row) {
|
|
|
|
return $this->createDataObject($row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this DataList has items
|
|
|
|
*/
|
|
|
|
function exists() {
|
|
|
|
return $this->count() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a sub-range of this dataobjectset as an array
|
|
|
|
*/
|
|
|
|
public function getRange($offset, $length) {
|
|
|
|
return $this->limit(array('start' => $offset, 'limit' => $length));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find an element of this DataList where the given key = value
|
|
|
|
*/
|
|
|
|
public function find($key, $value) {
|
2011-10-29 06:12:02 +02:00
|
|
|
$clone = clone $this;
|
|
|
|
|
|
|
|
if($key == 'ID') {
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
|
|
|
$SQL_col = "\"$baseClass\".\"$key\"";
|
|
|
|
} else {
|
|
|
|
$SQL_col = "\"$key\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $clone->where("$SQL_col = '" . Convert::raw2sql($value) . "'")->First();
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter this list to only contain the given IDs
|
|
|
|
*/
|
|
|
|
public function byIDs(array $ids) {
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
2011-04-05 13:01:57 +02:00
|
|
|
$this->where("\"$baseClass\".\"ID\" IN (" . implode(',', $ids) .")");
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2011-03-30 03:19:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the item of the given ID
|
|
|
|
*/
|
|
|
|
public function byID($id) {
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
2011-04-05 13:01:57 +02:00
|
|
|
return $this->where("\"$baseClass\".\"ID\" = " . (int)$id)->First();
|
2011-03-30 03:19:27 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a single column from this DataList.
|
|
|
|
* @param $colNum The DataObject field to return.
|
|
|
|
*/
|
|
|
|
function column($colName = "ID") {
|
|
|
|
return $this->dataQuery->column($colName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Member altering methods
|
|
|
|
/**
|
|
|
|
* Sets the ComponentSet to be the given ID list.
|
|
|
|
* Records will be added and deleted as appropriate.
|
|
|
|
* @param array $idList List of IDs.
|
|
|
|
*/
|
|
|
|
function setByIDList($idList) {
|
|
|
|
$has = array();
|
2011-10-29 06:27:21 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Index current data
|
|
|
|
foreach($this->column() as $id) {
|
|
|
|
$has[$id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of items to delete
|
|
|
|
$itemsToDelete = $has;
|
|
|
|
|
|
|
|
// add items in the list
|
|
|
|
// $id is the database ID of the record
|
|
|
|
if($idList) foreach($idList as $id) {
|
|
|
|
unset($itemsToDelete[$id]);
|
2011-10-29 06:27:21 +02:00
|
|
|
if($id && !isset($has[$id])) {
|
|
|
|
$this->add($id);
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any items that haven't been mentioned
|
|
|
|
$this->removeMany(array_keys($itemsToDelete));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array with both the keys and values set to the IDs of the records in this list.
|
|
|
|
*/
|
|
|
|
function getIDList() {
|
|
|
|
$ids = $this->column("ID");
|
|
|
|
return $ids ? array_combine($ids, $ids) : array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a HasManyList or ManyMany list representing the querying of a relation across all
|
|
|
|
* objects in this data list. For it to work, the relation must be defined on the data class
|
|
|
|
* that you used to create this DataList.
|
|
|
|
*
|
|
|
|
* Example: Get members from all Groups:
|
|
|
|
*
|
|
|
|
* DataObject::get("Group")->relation("Members")
|
|
|
|
*/
|
|
|
|
|
|
|
|
function relation($relationName) {
|
|
|
|
$ids = $this->column('ID');
|
|
|
|
return singleton($this->dataClass)->$relationName()->forForeignID($ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a number of items to the component set.
|
|
|
|
* @param array $items Items to add, as either DataObjects or IDs.
|
|
|
|
*/
|
|
|
|
function addMany($items) {
|
|
|
|
foreach($items as $item) {
|
|
|
|
$this->add($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the items from this list with the given IDs
|
|
|
|
*/
|
|
|
|
function removeMany($idList) {
|
|
|
|
foreach($idList as $id) {
|
2011-03-30 03:19:27 +02:00
|
|
|
$this->removeByID($id);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove every element in this DataList matching the given $filter.
|
|
|
|
*/
|
|
|
|
function removeByFilter($filter) {
|
2011-04-05 13:01:57 +02:00
|
|
|
foreach($this->where($filter) as $item) {
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->remove($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove every element in this DataList.
|
|
|
|
*/
|
|
|
|
function removeAll() {
|
|
|
|
foreach($this as $item) {
|
|
|
|
$this->remove($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These methods are overloaded by HasManyList and ManyMany list to perform
|
|
|
|
// more sophisticated list manipulation
|
|
|
|
|
|
|
|
function add($item) {
|
|
|
|
// Nothing needs to happen by default
|
|
|
|
// TO DO: If a filter is given to this data list then
|
|
|
|
}
|
2011-05-01 07:33:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new item to add to this DataList.
|
|
|
|
* @todo This doesn't factor in filters.
|
|
|
|
*/
|
|
|
|
function newObject($initialFields = null) {
|
|
|
|
$class = $this->dataClass;
|
|
|
|
return new $class($initialFields, false, $this->model);
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
function remove($item) {
|
|
|
|
// TO DO: Allow for amendment of this behaviour - for exmaple, we can remove an item from
|
|
|
|
// an "ActiveItems" DataList by chaning the status to inactive.
|
|
|
|
|
|
|
|
// By default, we remove an item from a DataList by deleting it.
|
|
|
|
if($item instanceof $this->dataClass) $item->delete();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-30 03:19:27 +02:00
|
|
|
/**
|
|
|
|
* Remove an item from this DataList by ID
|
|
|
|
*/
|
|
|
|
function removeByID($itemID) {
|
|
|
|
$item = $this->byID($itemID);
|
|
|
|
if($item) return $item->delete();
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Methods that won't function on DataLists
|
|
|
|
|
|
|
|
function push($item) {
|
|
|
|
user_error("Can't call DataList::push() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
function insertFirst($item) {
|
|
|
|
user_error("Can't call DataList::insertFirst() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
function shift() {
|
|
|
|
user_error("Can't call DataList::shift() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
function replace() {
|
|
|
|
user_error("Can't call DataList::replace() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
function merge() {
|
|
|
|
user_error("Can't call DataList::merge() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
function removeDuplicates() {
|
|
|
|
user_error("Can't call DataList::removeDuplicates() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
2011-03-30 05:51:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Necessary for interface ArrayAccess. Returns whether an item with $key exists
|
|
|
|
* @param mixed $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function offsetExists($key) {
|
|
|
|
return ($this->getRange($key, 1)->First() != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Necessary for interface ArrayAccess. Returns item stored in array with index $key
|
|
|
|
* @param mixed $key
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
public function offsetGet($key) {
|
|
|
|
return $this->getRange($key, 1)->First();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Necessary for interface ArrayAccess. Set an item with the key in $key
|
|
|
|
* @param mixed $key
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function offsetSet($key, $value) {
|
|
|
|
throw new Exception("Can't alter items in a DataList using array-access");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Necessary for interface ArrayAccess. Unset an item with the key in $key
|
|
|
|
* @param mixed $key
|
|
|
|
*/
|
|
|
|
public function offsetUnset($key) {
|
|
|
|
throw new Exception("Can't alter items in a DataList using array-access");
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
}
|