12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412 |
- <?php
- interface DrupalEntityControllerInterface {
-
- public function resetCache(array $ids = NULL);
-
- public function load($ids = array(), $conditions = array());
- }
- class DrupalDefaultEntityController implements DrupalEntityControllerInterface {
-
- protected $entityCache;
-
- protected $entityType;
-
- protected $entityInfo;
-
- protected $hookLoadArguments;
-
- protected $idKey;
-
- protected $revisionKey;
-
- protected $revisionTable;
-
- protected $cache;
-
- public function __construct($entityType) {
- $this->entityType = $entityType;
- $this->entityInfo = entity_get_info($entityType);
- $this->entityCache = array();
- $this->hookLoadArguments = array();
- $this->idKey = $this->entityInfo['entity keys']['id'];
-
- if (!empty($this->entityInfo['entity keys']['revision'])) {
- $this->revisionKey = $this->entityInfo['entity keys']['revision'];
- $this->revisionTable = $this->entityInfo['revision table'];
- }
- else {
- $this->revisionKey = FALSE;
- }
-
- $this->cache = !empty($this->entityInfo['static cache']);
- }
-
- public function resetCache(array $ids = NULL) {
- if (isset($ids)) {
- foreach ($ids as $id) {
- unset($this->entityCache[$id]);
- }
- }
- else {
- $this->entityCache = array();
- }
- }
-
- public function load($ids = array(), $conditions = array()) {
- $entities = array();
-
-
- if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
- $revision_id = $conditions[$this->revisionKey];
- unset($conditions[$this->revisionKey]);
- }
- else {
- $revision_id = FALSE;
- }
-
-
-
-
-
- $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
-
-
- if ($this->cache && !$revision_id) {
- $entities += $this->cacheGet($ids, $conditions);
-
- if ($passed_ids) {
- $ids = array_keys(array_diff_key($passed_ids, $entities));
- }
- }
-
- if (!empty($ids)) {
- $this->cleanIds($ids);
- }
-
-
-
- if ($ids === FALSE || $ids || $revision_id || ($conditions && !$passed_ids)) {
-
- $query = $this->buildQuery($ids, $conditions, $revision_id);
- $queried_entities = $query
- ->execute()
- ->fetchAllAssoc($this->idKey);
- }
-
-
-
- if (!empty($queried_entities)) {
- $this->attachLoad($queried_entities, $revision_id);
- $entities += $queried_entities;
- }
- if ($this->cache) {
-
- if (!empty($queried_entities) && !$revision_id) {
- $this->cacheSet($queried_entities);
- }
- }
-
-
- if ($passed_ids) {
-
- $passed_ids = array_intersect_key($passed_ids, $entities);
- foreach ($entities as $entity) {
- $passed_ids[$entity->{$this->idKey}] = $entity;
- }
- $entities = $passed_ids;
- }
- return $entities;
- }
-
- protected function cleanIds(&$ids) {
- $entity_info = entity_get_info($this->entityType);
- if (isset($entity_info['base table field types'])) {
- $id_type = $entity_info['base table field types'][$this->idKey];
- if ($id_type == 'serial' || $id_type == 'int') {
- $ids = array_filter($ids, array($this, 'filterId'));
- $ids = array_map('intval', $ids);
- }
- }
- }
-
- protected function filterId($id) {
- return is_numeric($id) && $id == (int) $id;
- }
-
- protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
- $query = db_select($this->entityInfo['base table'], 'base');
- $query->addTag($this->entityType . '_load_multiple');
- if ($revision_id) {
- $query->join($this->revisionTable, 'revision', "revision.{$this->idKey} = base.{$this->idKey} AND revision.{$this->revisionKey} = :revisionId", array(':revisionId' => $revision_id));
- }
- elseif ($this->revisionKey) {
- $query->join($this->revisionTable, 'revision', "revision.{$this->revisionKey} = base.{$this->revisionKey}");
- }
-
- $entity_fields = $this->entityInfo['schema_fields_sql']['base table'];
- if ($this->revisionKey) {
-
- $entity_revision_fields = drupal_map_assoc($this->entityInfo['schema_fields_sql']['revision table']);
-
- unset($entity_revision_fields[$this->idKey]);
-
-
- $entity_field_keys = array_flip($entity_fields);
- foreach ($entity_revision_fields as $key => $name) {
- if (isset($entity_field_keys[$name])) {
- unset($entity_fields[$entity_field_keys[$name]]);
- }
- }
- $query->fields('revision', $entity_revision_fields);
- }
- $query->fields('base', $entity_fields);
- if ($ids) {
- $query->condition("base.{$this->idKey}", $ids, 'IN');
- }
- if ($conditions) {
- foreach ($conditions as $field => $value) {
- $query->condition('base.' . $field, $value);
- }
- }
- return $query;
- }
-
- protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
-
- if ($this->entityInfo['fieldable']) {
- if ($revision_id) {
- field_attach_load_revision($this->entityType, $queried_entities);
- }
- else {
- field_attach_load($this->entityType, $queried_entities);
- }
- }
-
- foreach (module_implements('entity_load') as $module) {
- $function = $module . '_entity_load';
- $function($queried_entities, $this->entityType);
- }
-
-
-
- $args = array_merge(array($queried_entities), $this->hookLoadArguments);
- foreach (module_implements($this->entityInfo['load hook']) as $module) {
- call_user_func_array($module . '_' . $this->entityInfo['load hook'], $args);
- }
- }
-
- protected function cacheGet($ids, $conditions = array()) {
- $entities = array();
-
- if (!empty($this->entityCache)) {
- if ($ids) {
- $entities += array_intersect_key($this->entityCache, array_flip($ids));
- }
-
-
- elseif ($conditions) {
- $entities = $this->entityCache;
- }
- }
-
-
- if ($conditions) {
- foreach ($entities as $entity) {
-
-
-
- foreach ($conditions as $property_name => $condition) {
- if (is_array($condition)) {
-
-
-
- if (!in_array($entity->{$property_name}, $condition)) {
- unset($entities[$entity->{$this->idKey}]);
- continue 2;
- }
- }
- elseif ($condition != $entity->{$property_name}) {
- unset($entities[$entity->{$this->idKey}]);
- continue 2;
- }
- }
- }
- }
- return $entities;
- }
-
- protected function cacheSet($entities) {
- $this->entityCache += $entities;
- }
- }
- class EntityFieldQueryException extends Exception {}
- class EntityFieldQuery {
-
- const RETURN_ALL = NULL;
-
- public $altered = FALSE;
-
- public $entityConditions = array();
-
- public $fieldConditions = array();
-
- public $fieldMetaConditions = array();
-
- public $propertyConditions = array();
-
- public $order = array();
-
- public $range = array();
-
- public $pager = array();
-
- public $deleted = FALSE;
-
- public $fields = array();
-
- public $count = FALSE;
-
- public $age = FIELD_LOAD_CURRENT;
-
- public $tags = array();
-
- public $metaData = array();
-
- public $orderedResults = array();
-
- public $executeCallback = '';
-
- public function entityCondition($name, $value, $operator = NULL) {
-
-
- if ($operator == '!=') {
- $operator = '<>';
- }
- $this->entityConditions[$name] = array(
- 'value' => $value,
- 'operator' => $operator,
- );
- return $this;
- }
-
- public function fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
- return $this->addFieldCondition($this->fieldConditions, $field, $column, $value, $operator, $delta_group, $language_group);
- }
-
- public function fieldLanguageCondition($field, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
- return $this->addFieldCondition($this->fieldMetaConditions, $field, 'language', $value, $operator, $delta_group, $language_group);
- }
-
- public function fieldDeltaCondition($field, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
- return $this->addFieldCondition($this->fieldMetaConditions, $field, 'delta', $value, $operator, $delta_group, $language_group);
- }
-
- protected function addFieldCondition(&$conditions, $field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
-
-
- if ($operator == '!=') {
- $operator = '<>';
- }
- if (is_scalar($field)) {
- $field_definition = field_info_field($field);
- if (empty($field_definition)) {
- throw new EntityFieldQueryException(t('Unknown field: @field_name', array('@field_name' => $field)));
- }
- $field = $field_definition;
- }
-
- $index = count($this->fields);
- $this->fields[$index] = $field;
- if (isset($column)) {
- $conditions[$index] = array(
- 'field' => $field,
- 'column' => $column,
- 'value' => $value,
- 'operator' => $operator,
- 'delta_group' => $delta_group,
- 'language_group' => $language_group,
- );
- }
- return $this;
- }
-
- public function propertyCondition($column, $value, $operator = NULL) {
-
-
- if ($operator == '!=') {
- $operator = '<>';
- }
- $this->propertyConditions[] = array(
- 'column' => $column,
- 'value' => $value,
- 'operator' => $operator,
- );
- return $this;
- }
-
- public function entityOrderBy($name, $direction = 'ASC') {
- $this->order[] = array(
- 'type' => 'entity',
- 'specifier' => $name,
- 'direction' => $direction,
- );
- return $this;
- }
-
- public function fieldOrderBy($field, $column, $direction = 'ASC') {
- if (is_scalar($field)) {
- $field_definition = field_info_field($field);
- if (empty($field_definition)) {
- throw new EntityFieldQueryException(t('Unknown field: @field_name', array('@field_name' => $field)));
- }
- $field = $field_definition;
- }
-
- $index = count($this->fields);
- $this->fields[$index] = $field;
- $this->order[] = array(
- 'type' => 'field',
- 'specifier' => array(
- 'field' => $field,
- 'index' => $index,
- 'column' => $column,
- ),
- 'direction' => $direction,
- );
- return $this;
- }
-
- public function propertyOrderBy($column, $direction = 'ASC') {
- $this->order[] = array(
- 'type' => 'property',
- 'specifier' => $column,
- 'direction' => $direction,
- );
- return $this;
- }
-
- public function count() {
- $this->count = TRUE;
- return $this;
- }
-
- public function range($start = NULL, $length = NULL) {
- $this->range = array(
- 'start' => $start,
- 'length' => $length,
- );
- return $this;
- }
-
- public function pager($limit = 10, $element = NULL) {
- if (!isset($element)) {
- $element = PagerDefault::$maxElement++;
- }
- elseif ($element >= PagerDefault::$maxElement) {
- PagerDefault::$maxElement = $element + 1;
- }
- $this->pager = array(
- 'limit' => $limit,
- 'element' => $element,
- );
- return $this;
- }
-
- 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'])) {
- if ($header['type'] == 'field') {
- $this->fieldOrderBy($header['specifier']['field'], $header['specifier']['column'], $direction);
- }
- else {
- $header['direction'] = $direction;
- $this->order[] = $header;
- }
- }
- }
- return $this;
- }
-
- public function deleted($deleted = TRUE) {
- $this->deleted = $deleted;
- return $this;
- }
-
- public function age($age) {
- $this->age = $age;
- return $this;
- }
-
- public function addTag($tag) {
- $this->tags[$tag] = $tag;
- return $this;
- }
-
- public function addMetaData($key, $object) {
- $this->metaData[$key] = $object;
- return $this;
- }
-
- public function execute() {
-
- drupal_alter('entity_query', $this);
- $this->altered = TRUE;
-
- $this->initializePager();
-
- $result = call_user_func($this->queryCallback(), $this);
- return $result;
- }
-
- public function queryCallback() {
-
-
- if (function_exists($this->executeCallback)) {
- return $this->executeCallback;
- }
-
-
- if (empty($this->fields)) {
- return array($this, 'propertyQuery');
- }
-
- foreach ($this->fields as $field) {
- if (!isset($storage)) {
- $storage = $field['storage']['module'];
- }
- elseif ($storage != $field['storage']['module']) {
- throw new EntityFieldQueryException(t("Can't handle more than one field storage engine"));
- }
- }
- if ($storage) {
-
- return $storage . '_field_storage_query';
- }
- else {
- throw new EntityFieldQueryException(t("Field storage engine not found."));
- }
- }
-
- protected function propertyQuery() {
- if (empty($this->entityConditions['entity_type'])) {
- throw new EntityFieldQueryException(t('For this query an entity type must be specified.'));
- }
- $entity_type = $this->entityConditions['entity_type']['value'];
- $entity_info = entity_get_info($entity_type);
- if (empty($entity_info['base table'])) {
- throw new EntityFieldQueryException(t('Entity %entity has no base table.', array('%entity' => $entity_type)));
- }
- $base_table = $entity_info['base table'];
- $base_table_schema = drupal_get_schema($base_table);
- $select_query = db_select($base_table);
- $select_query->addExpression(':entity_type', 'entity_type', array(':entity_type' => $entity_type));
-
- foreach ($this->propertyConditions as $property_condition) {
- $this->addCondition($select_query, $base_table . '.' . $property_condition['column'], $property_condition);
- }
-
-
- $sql_field = $entity_info['entity keys']['id'];
- $id_map['entity_id'] = $sql_field;
- $select_query->addField($base_table, $sql_field, 'entity_id');
- if (isset($this->entityConditions['entity_id'])) {
- $this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['entity_id']);
- }
-
- if (!empty($entity_info['entity keys']['revision'])) {
- $sql_field = $entity_info['entity keys']['revision'];
- $select_query->addField($base_table, $sql_field, 'revision_id');
- if (isset($this->entityConditions['revision_id'])) {
- $this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['revision_id']);
- }
- }
- else {
- $sql_field = 'revision_id';
- $select_query->addExpression('NULL', 'revision_id');
- }
- $id_map['revision_id'] = $sql_field;
-
- if (!empty($entity_info['entity keys']['bundle'])) {
- $sql_field = $entity_info['entity keys']['bundle'];
- $having = FALSE;
- if (!empty($base_table_schema['fields'][$sql_field])) {
- $select_query->addField($base_table, $sql_field, 'bundle');
- }
- }
- else {
- $sql_field = 'bundle';
- $select_query->addExpression(':bundle', 'bundle', array(':bundle' => $entity_type));
- $having = TRUE;
- }
- $id_map['bundle'] = $sql_field;
- if (isset($this->entityConditions['bundle'])) {
- if (!empty($entity_info['entity keys']['bundle'])) {
- $this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['bundle'], $having);
- }
- else {
-
- $select_query->where('1 = 0');
- }
- }
-
- foreach ($this->order as $order) {
- if ($order['type'] == 'entity') {
- $key = $order['specifier'];
- if (!isset($id_map[$key])) {
- throw new EntityFieldQueryException(t('Do not know how to order on @key for @entity_type', array('@key' => $key, '@entity_type' => $entity_type)));
- }
- $select_query->orderBy($id_map[$key], $order['direction']);
- }
- elseif ($order['type'] == 'property') {
- $select_query->orderBy($base_table . '.' . $order['specifier'], $order['direction']);
- }
- }
- return $this->finishQuery($select_query);
- }
-
- 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']);
- }
- }
-
- function finishQuery($select_query, $id_key = 'entity_id') {
- foreach ($this->tags as $tag) {
- $select_query->addTag($tag);
- }
- foreach ($this->metaData as $key => $object) {
- $select_query->addMetaData($key, $object);
- }
- $select_query->addMetaData('entity_field_query', $this);
- if ($this->range) {
- $select_query->range($this->range['start'], $this->range['length']);
- }
- if ($this->count) {
- return $select_query->countQuery()->execute()->fetchField();
- }
- $return = array();
- foreach ($select_query->execute() as $partial_entity) {
- $bundle = isset($partial_entity->bundle) ? $partial_entity->bundle : NULL;
- $entity = entity_create_stub_entity($partial_entity->entity_type, array($partial_entity->entity_id, $partial_entity->revision_id, $bundle));
- $return[$partial_entity->entity_type][$partial_entity->$id_key] = $entity;
- $this->ordered_results[] = $partial_entity;
- }
- return $return;
- }
-
- public function addCondition(SelectQuery $select_query, $sql_field, $condition, $having = FALSE) {
- $method = $having ? 'havingCondition' : 'condition';
- $like_prefix = '';
- switch ($condition['operator']) {
- case 'CONTAINS':
- $like_prefix = '%';
- case 'STARTS_WITH':
- $select_query->$method($sql_field, $like_prefix . db_like($condition['value']) . '%', 'LIKE');
- break;
- default:
- $select_query->$method($sql_field, $condition['value'], $condition['operator']);
- }
- }
- }
- class EntityMalformedException extends Exception { }
|