abstract class QueryBase {
protected $entityType;
protected $sort = array();
protected $count = FALSE;
protected $condition;
protected $aggregate = array();
protected $groupBy = array();
protected $conditionAggregate;
protected $sortAggregate = array();
protected $range = array();
protected $alterMetaData;
protected $alterTags;
protected $accessCheck = TRUE;
protected $age = FIELD_LOAD_CURRENT;
protected $pager = array();
public function __construct($entity_type, $conjunction) {
$this->entityType = $entity_type;
$this->conjunction = $conjunction;
$this->condition = $this
->conditionGroupFactory($conjunction);
if ($this instanceof QueryAggregateInterface) {
$this->conditionAggregate = $this
->conditionAggregateGroupFactory($conjunction);
}
}
public function getEntityType() {
return $this->entityType;
}
public function condition($property, $value = NULL, $operator = NULL, $langcode = NULL) {
$this->condition
->condition($property, $value, $operator, $langcode);
return $this;
}
public function exists($property, $langcode = NULL) {
$this->condition
->exists($property, $langcode);
return $this;
}
public function notExists($property, $langcode = NULL) {
$this->condition
->notExists($property, $langcode);
return $this;
}
public function range($start = NULL, $length = NULL) {
$this->range = array(
'start' => $start,
'length' => $length,
);
return $this;
}
public function andConditionGroup() {
return $this
->conditionGroupFactory('and');
}
public function orConditionGroup() {
return $this
->conditionGroupFactory('or');
}
public function sort($field, $direction = 'ASC', $langcode = NULL) {
$this->sort[] = array(
'field' => $field,
'direction' => $direction,
'langcode' => $langcode,
);
return $this;
}
public function count() {
$this->count = TRUE;
return $this;
}
public function accessCheck($access_check = TRUE) {
$this->accessCheck = $access_check;
return $this;
}
public function age($age = FIELD_LOAD_CURRENT) {
$this->age = $age;
return $this;
}
public function pager($limit = 10, $element = NULL) {
if (!isset($element)) {
$element = PagerSelectExtender::$maxElement++;
}
elseif ($element >= PagerSelectExtender::$maxElement) {
PagerSelectExtender::$maxElement = $element + 1;
}
$this->pager = array(
'limit' => $limit,
'element' => $element,
);
return $this;
}
protected function initializePager() {
if ($this->pager && !empty($this->pager['limit']) && !$this->count) {
$page = pager_find_page($this->pager['element']);
$count_query = clone $this;
$this->pager['total'] = $count_query
->count()
->execute();
$this->pager['start'] = $page * $this->pager['limit'];
pager_default_initialize($this->pager['total'], $this->pager['limit'], $this->pager['element']);
$this
->range($this->pager['start'], $this->pager['limit']);
}
}
public function tableSort(&$headers) {
foreach ($headers as $key => $header) {
if (is_array($header) && isset($header['specifier'])) {
$headers[$key]['field'] = '';
}
}
$order = tablesort_get_order($headers);
$direction = tablesort_get_sort($headers);
foreach ($headers as $header) {
if (is_array($header) && $header['data'] == $order['name']) {
$this
->sort($header['specifier'], $direction, isset($header['langcode']) ? $header['langcode'] : NULL);
}
}
return $this;
}
function __clone() {
$this->condition = clone $this->condition;
}
public function addTag($tag) {
$this->alterTags[$tag] = 1;
return $this;
}
public function hasTag($tag) {
return isset($this->alterTags[$tag]);
}
public function hasAllTags() {
return !(bool) array_diff(func_get_args(), array_keys($this->alterTags));
}
public function hasAnyTag() {
return (bool) array_intersect(func_get_args(), array_keys($this->alterTags));
}
public function addMetaData($key, $object) {
$this->alterMetaData[$key] = $object;
return $this;
}
public function getMetaData($key) {
return isset($this->alterMetaData[$key]) ? $this->alterMetaData[$key] : NULL;
}
public function aggregate($field, $function, $langcode = NULL, &$alias = NULL) {
if (!isset($alias)) {
$alias = $this
->getAggregationAlias($field, $function);
}
$this->aggregate[$alias] = array(
'field' => $field,
'function' => $function,
'alias' => $alias,
'langcode' => $langcode,
);
return $this;
}
public function conditionAggregate($field, $function = NULL, $value = NULL, $operator = '=', $langcode = NULL) {
$this
->aggregate($field, $function, $langcode);
$this->conditionAggregate
->condition($field, $function, $value, $operator, $langcode);
return $this;
}
public function sortAggregate($field, $function, $direction = 'ASC', $langcode = NULL) {
$alias = $this
->getAggregationAlias($field, $function);
$this->sortAggregate[$alias] = array(
'field' => $field,
'function' => $function,
'direction' => $direction,
'langcode' => $langcode,
);
$this
->aggregate($field, $function, $langcode, $alias);
return $this;
}
public function groupBy($field, $langcode = NULL) {
$this->groupBy[] = array(
'field' => $field,
'langcode' => $langcode,
);
return $this;
}
protected function getAggregationAlias($field, $function) {
return strtolower($field . '_' . $function);
}
}