PageRenderTime 36ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/profiles/openoutreach/modules/contrib/media/MediaEntityController.php

https://github.com/iaminawe/openoutreach
PHP | 215 lines | 121 code | 22 blank | 72 comment | 28 complexity | d29b56f775736e6a11ecd8a0d5241b04 MD5 | raw file
  1. <?php
  2. /**
  3. * DO NOT EDIT THIS. IT WILL BE REMOVED IN THE NEXT RELEASE.
  4. * This file is only here so that the class autoloader doesn't barf.
  5. *
  6. * It has been moved to includes
  7. */
  8. /**
  9. * Extends DrupalEntityControllerInterface.
  10. *
  11. * @see: http://drupal.org/project/entity_api (perhaps we should be using this)
  12. */
  13. class MediaEntityController extends DrupalDefaultEntityController {
  14. public function load($ids = array(), $conditions = array()) {
  15. $this->ids = $ids;
  16. $this->conditions = $conditions;
  17. $entities = array();
  18. // Revisions are not statically cached, and require a different query to
  19. // other conditions, so separate the revision id into its own variable.
  20. if ($this->revisionKey && isset($this->conditions[$this->revisionKey])) {
  21. $this->revisionId = $this->conditions[$this->revisionKey];
  22. unset($this->conditions[$this->revisionKey]);
  23. }
  24. else {
  25. $this->revisionId = FALSE;
  26. }
  27. // Create a new variable which is either a prepared version of the $ids
  28. // array for later comparison with the entity cache, or FALSE if no $ids
  29. // were passed. The $ids array is reduced as items are loaded from cache,
  30. // and we need to know if it's empty for this reason to avoid querying the
  31. // database when all requested entities are loaded from cache.
  32. $passed_ids = !empty($this->ids) ? array_flip($this->ids) : FALSE;
  33. // Try to load entities from the static cache, if the entity type supports
  34. // static caching.
  35. if ($this->cache) {
  36. $entities += $this->cacheGet($this->ids, $this->conditions);
  37. // If any entities were loaded, remove them from the ids still to load.
  38. if ($passed_ids) {
  39. $this->ids = array_keys(array_diff_key($passed_ids, $entities));
  40. }
  41. }
  42. // Load any remaining entities from the database. This is the case if $ids
  43. // is set to FALSE (so we load all entities), if there are any ids left to
  44. // load, if loading a revision, or if $conditions was passed without $ids.
  45. if ($this->ids === FALSE || $this->ids || $this->revisionId || ($this->conditions && !$passed_ids)) {
  46. // Build the query.
  47. $this->buildQuery();
  48. $queried_entities = $this->query
  49. ->execute()
  50. ->fetchAllAssoc($this->idKey);
  51. }
  52. // Pass all entities loaded from the database through $this->attachLoad(),
  53. // which attaches fields (if supported by the entity type) and calls the
  54. // entity type specific load callback, for example hook_node_load().
  55. if (!empty($queried_entities)) {
  56. $this->attachLoad($queried_entities);
  57. $entities += $queried_entities;
  58. }
  59. if ($this->cache) {
  60. // Add entities to the cache if we are not loading a revision.
  61. if (!empty($queried_entities) && !$this->revisionId) {
  62. $this->cacheSet($queried_entities);
  63. }
  64. }
  65. // Ensure that the returned array is ordered the same as the original
  66. // $ids array if this was passed in and remove any invalid ids.
  67. if ($passed_ids) {
  68. // Remove any invalid ids from the array.
  69. $passed_ids = array_intersect_key($passed_ids, $entities);
  70. foreach ($entities as $entity) {
  71. $passed_ids[$entity->{$this->idKey}] = $entity;
  72. }
  73. $entities = $passed_ids;
  74. }
  75. return $entities;
  76. }
  77. protected function buildQuery() {
  78. $this->query = db_select($this->entityInfo['base table'], 'base');
  79. $this->query->addTag($this->entityType . '_load_multiple');
  80. if ($this->revisionId) {
  81. $this->query->join($this->revisionTable, 'revision', "revision.{$this->idKey} = base.{$this->idKey} AND revision.{$this->revisionKey} = :revisionId", array(':revisionId' => $this->revisionId));
  82. }
  83. elseif ($this->revisionKey) {
  84. $this->query->join($this->revisionTable, 'revision', "revision.{$this->revisionKey} = base.{$this->revisionKey}");
  85. }
  86. // Add fields from the {entity} table.
  87. $entity_fields = drupal_schema_fields_sql($this->entityInfo['base table']);
  88. if ($this->revisionKey) {
  89. // Add all fields from the {entity_revision} table.
  90. $entity_revision_fields = drupal_map_assoc(drupal_schema_fields_sql($this->revisionTable));
  91. // The id field is provided by entity, so remove it.
  92. unset($entity_revision_fields[$this->idKey]);
  93. // Change timestamp to revision_timestamp, and revision uid to
  94. // revision_uid before adding them to the query.
  95. // TODO: This is node specific and has to be moved into NodeController.
  96. unset($entity_revision_fields['timestamp']);
  97. $this->query->addField('revision', 'timestamp', 'revision_timestamp');
  98. unset($entity_revision_fields['uid']);
  99. $this->query->addField('revision', 'uid', 'revision_uid');
  100. // Remove all fields from the base table that are also fields by the same
  101. // name in the revision table.
  102. $entity_field_keys = array_flip($entity_fields);
  103. foreach ($entity_revision_fields as $key => $name) {
  104. if (isset($entity_field_keys[$name])) {
  105. unset($entity_fields[$entity_field_keys[$name]]);
  106. }
  107. }
  108. $this->query->fields('revision', $entity_revision_fields);
  109. }
  110. $this->query->fields('base', $entity_fields);
  111. if ($this->ids) {
  112. $this->query->condition("base.{$this->idKey}", $this->ids, 'IN');
  113. }
  114. if ($this->conditions) {
  115. foreach ($this->conditions as $field => $value) {
  116. $this->query->condition('base.' . $field, $value);
  117. }
  118. }
  119. }
  120. /**
  121. * Attach data to entities upon loading.
  122. *
  123. * This will attach fields, if the entity is fieldable. It calls
  124. * hook_entity_load() for modules which need to add data to all entities.
  125. * It also calls hook_TYPE_load() on the loaded entities. For example
  126. * hook_node_load() or hook_user_load(). If your hook_TYPE_load()
  127. * expects special parameters apart from the queried entities, you can set
  128. * $this->hookLoadArguments prior to calling the method.
  129. * See NodeController::attachLoad() for an example.
  130. */
  131. protected function attachLoad(&$queried_entities) {
  132. // Attach fields.
  133. if ($this->entityInfo['fieldable']) {
  134. if ($this->revisionId) {
  135. field_attach_load_revision($this->entityType, $queried_entities);
  136. }
  137. else {
  138. field_attach_load($this->entityType, $queried_entities);
  139. }
  140. }
  141. // Call hook_entity_load().
  142. foreach (module_implements('entity_load') as $module) {
  143. $function = $module . '_entity_load';
  144. $function($queried_entities, $this->entityType);
  145. }
  146. // Call hook_TYPE_load(). The first argument for hook_TYPE_load() are
  147. // always the queried entities, followed by additional arguments set in
  148. // $this->hookLoadArguments.
  149. $args = array_merge(array($queried_entities), $this->hookLoadArguments);
  150. foreach (module_implements($this->entityInfo['load hook']) as $module) {
  151. call_user_func_array($module . '_' . $this->entityInfo['load hook'], $args);
  152. }
  153. }
  154. /**
  155. * Get entities from the static cache.
  156. *
  157. * @param $ids
  158. * If not empty, return entities that match these IDs.
  159. * @param $conditions
  160. * If set, return entities that match all of these conditions.
  161. */
  162. protected function cacheGet($ids, $conditions = array()) {
  163. $entities = array();
  164. // Load any available entities from the internal cache.
  165. if (!empty($this->entityCache) && !$this->revisionId) {
  166. if ($ids) {
  167. $entities += array_intersect_key($this->entityCache, array_flip($ids));
  168. }
  169. // If loading entities only by conditions, fetch all available entities
  170. // from the cache. Entities which don't match are removed later.
  171. elseif ($conditions) {
  172. $entities = $this->entityCache;
  173. }
  174. }
  175. // Exclude any entities loaded from cache if they don't match $conditions.
  176. // This ensures the same behavior whether loading from memory or database.
  177. if ($conditions) {
  178. foreach ($entities as $entity) {
  179. $entity_values = (array) $entity;
  180. if (array_diff_assoc($conditions, $entity_values)) {
  181. unset($entities[$entity->{$this->idKey}]);
  182. }
  183. }
  184. }
  185. return $entities;
  186. }
  187. /**
  188. * Store entities in the static entity cache.
  189. */
  190. protected function cacheSet($entities) {
  191. $this->entityCache += $entities;
  192. }
  193. }