TripalFieldQuery.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Extends the EntityFieldQuery to support queries from multiple storage types.
  4. */
  5. class TripalFieldQuery extends EntityFieldQuery {
  6. /**
  7. * Holds a list of fields that should be included in the results
  8. */
  9. protected $includes = [];
  10. /**
  11. * These member variables keep track of join relationships with the
  12. * tripal_entity table. This is useful for non Tripal Storage API fields that
  13. * want to filter based on on other Drupal tables. The most important
  14. * example for this would be Drupal Views. These variables are only meant
  15. * to be used by the tripal_field_storage_query() function as that is the
  16. * only storage system that should be doing quries on the tripal_entity
  17. * table itself.
  18. */
  19. public $relationships = [];
  20. public $relationshipConditions = [];
  21. /**
  22. * Adds a relationsihp via a table join to the tripal_entity table
  23. *
  24. * This is specific for Drupal schema tables and is useful for Views
  25. * integration when non Tripal Storage API fields are attached to an entity.
  26. *
  27. * @param $table
  28. * The name of the table to join.
  29. * @param $alias
  30. * The alias for the table.
  31. * @param $field
  32. * The field to join on.
  33. */
  34. public function addRelationship($table, $alias, $field) {
  35. $this->relationships[$alias] = [
  36. 'table' => $table,
  37. 'field' => $field,
  38. ];
  39. }
  40. /**
  41. * Adds a where statement to a relationship.
  42. *
  43. * The relationship is added by the $table
  44. */
  45. public function relationshipCondition($table, $field, $value, $op) {
  46. $table_alias = '';
  47. // Get the alias for this table.
  48. foreach ($this->relationships as $alias => $details) {
  49. if ($details['table'] == $table) {
  50. $table_alias = $alias;
  51. }
  52. }
  53. if ($table_alias) {
  54. $this->relationshipConditions[$table_alias] = [
  55. 'field' => $field,
  56. 'value' => $value,
  57. 'op' => $op,
  58. ];
  59. }
  60. }
  61. /**
  62. * Overrides the EntityFieldQuery::execute() function.
  63. */
  64. public function execute() {
  65. // Initialize the results array.
  66. $results = ['first_results' => TRUE];
  67. // Give a chance for other modules to alter the query.
  68. drupal_alter('entity_query', $this);
  69. $this->altered = TRUE;
  70. // Initialize the pager.
  71. $this->initializePager();
  72. // If there are fields then we need to support multiple backends, call
  73. // the function for each one and merge the results.
  74. if ($this->fields) {
  75. // Build the list of all of the different field storage types used
  76. // for this query.
  77. foreach ($this->fields as $field) {
  78. $this->field_storage[$field['storage']['type']] = $field['storage']['module'];
  79. }
  80. // Iterate through the field storage types and call each one.
  81. foreach ($this->field_storage as $storage_type => $storage_module) {
  82. $callback = $this->queryStorageCallback($storage_module);
  83. $st_results = call_user_func($callback, $this);
  84. $results = $this->_intersectResults($results, $st_results);
  85. }
  86. }
  87. // If we have relationships, then handle those.
  88. if (!empty($this->relationshipConditions)) {
  89. $st_results = tripal_field_storage_query($this);
  90. $results = $this->_intersectResults($results, $st_results);
  91. // Are there any other callbacks that need running other than
  92. // propertyQuery as we would have run those with the relationships.
  93. $callback = $this->queryCallback();
  94. if ($callback and $callback[1] != 'propertyQuery' and
  95. $callback[1] != 'tripal_field_storage_query') {
  96. $st_results = call_user_func($callback, $this);
  97. $results = $this->_intersectResults($results, $st_results);
  98. }
  99. }
  100. // There are no fields or relationships so just use the default
  101. // callback for the query.
  102. else {
  103. if (!$this->fields) {
  104. $callback = $this->queryCallback();
  105. $st_results = call_user_func($callback, $this);
  106. $results = $this->_intersectResults($results, $st_results);
  107. }
  108. }
  109. if ($this->count == TRUE) {
  110. if (is_numeric($results)) {
  111. return $results;
  112. }
  113. if (array_key_exists('TripalEntity', $results)) {
  114. return count($results['TripalEntity']);
  115. }
  116. return 0;
  117. }
  118. return $results;
  119. }
  120. /**
  121. * Generates an intersection of results from different storage back-ends.
  122. */
  123. protected function _intersectResults($current_results, $new_results) {
  124. // If we currently don't have any results then just allow all through.
  125. // This is the first result set.
  126. if (array_key_exists('first_results', $current_results)) {
  127. return $new_results;
  128. }
  129. // set defaults to prevent warnings
  130. if (empty($new_results)) {
  131. $new_results['TripalEntity'] = [];
  132. }
  133. if (empty($current_results)) {
  134. $current_results['TripalEntity'] = [];
  135. }
  136. // Iterate through all of the new results and only include those that
  137. // exist in both the current and new.
  138. $intersection = [];
  139. foreach ($new_results['TripalEntity'] as $entity_id => $stub) {
  140. if (array_key_exists($entity_id, $current_results['TripalEntity'])) {
  141. $intersection[$entity_id] = $stub;
  142. }
  143. }
  144. if (count($intersection) > 0) {
  145. return ['TripalEntity' => $intersection];
  146. }
  147. else {
  148. return [];
  149. }
  150. }
  151. /**
  152. * Overides the EntityFieldQuery::queryCallback function.
  153. */
  154. public function queryCallback() {
  155. // Use the override from $this->executeCallback. It can be set either
  156. // while building the query, or using hook_entity_query_alter().
  157. if (function_exists($this->executeCallback)) {
  158. return $this->executeCallback;
  159. }
  160. // If there are no field conditions and sorts, and no execute callback
  161. // then we default to querying entity tables in SQL.
  162. if (empty($this->fields)) {
  163. return [
  164. $this,
  165. 'propertyQuery',
  166. ];
  167. }
  168. // If no override, find the storage engine to be used.
  169. foreach ($this->fields as $field) {
  170. if (!isset($storage)) {
  171. $storage = $field['storage']['module'];
  172. }
  173. elseif ($storage != $field['storage']['module']) {
  174. throw new EntityFieldQueryException(t("Can't handle more than one field storage engine"));
  175. }
  176. }
  177. if ($storage) {
  178. // Use hook_field_storage_query() from the field storage.
  179. return $storage . '_field_storage_query';
  180. }
  181. else {
  182. throw new EntityFieldQueryException(t("Field storage engine not found."));
  183. }
  184. }
  185. /**
  186. * Determines the query callback to use for this entity query.
  187. *
  188. * This function is a replacement for queryCallback() from the
  189. * parent EntityFieldQuery class because that class only allows a single
  190. * storage type per query.
  191. *
  192. * @param $storage
  193. * The storage module
  194. *
  195. * @throws EntityFieldQueryException
  196. * @return
  197. * A callback that can be used with call_user_func().
  198. *
  199. */
  200. protected function queryStorageCallback($storage) {
  201. // Use the override from $this->executeCallback. It can be set either
  202. // while building the query, or using hook_entity_query_alter().
  203. if (function_exists($this->executeCallback)) {
  204. return $this->executeCallback;
  205. }
  206. // If there are no field conditions and sorts, and no execute callback
  207. // then we default to querying entity tables in SQL.
  208. if (empty($this->fields)) {
  209. return [$this, 'propertyQuery'];
  210. }
  211. if ($storage) {
  212. // Use hook_field_storage_query() from the field storage.
  213. return $storage . '_field_storage_query';
  214. }
  215. else {
  216. throw new EntityFieldQueryException(t("Field storage engine not found."));
  217. }
  218. }
  219. /**
  220. * Allows query object to be dumped as a string.
  221. */
  222. public function __toString() {
  223. return print_r($this, TRUE);
  224. }
  225. }