TripalFieldQuery.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 = array();
  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 = array('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 if (!$this->fields){
  103. $callback = $this->queryCallback();
  104. $st_results = call_user_func($callback, $this);
  105. $results = $this->_intersectResults($results, $st_results);
  106. }
  107. return $results;
  108. }
  109. /**
  110. * Generates an intersection of results from different storage back-ends.
  111. */
  112. protected function _intersectResults($current_results, $new_results) {
  113. // If we currently don't have any results then just allow all through.
  114. // This is the first result set.
  115. if (array_key_exists('first_results', $current_results)) {
  116. return $new_results;
  117. }
  118. // set defaults to prevent warnings
  119. if (empty($new_results)){
  120. $new_results['TripalEntity'] = [];
  121. }
  122. if (empty($current_results)){
  123. $current_results['TripalEntity'] = [];
  124. }
  125. // Iterate through all of the new results and only include those that
  126. // exist in both the current and new.
  127. $intersection = [];
  128. foreach ($new_results['TripalEntity'] as $entity_id => $stub) {
  129. if (array_key_exists($entity_id, $current_results['TripalEntity'])) {
  130. $intersection[$entity_id] = $stub;
  131. }
  132. }
  133. if (count($intersection) > 0) {
  134. return ['TripalEntity' => $intersection];
  135. }
  136. else {
  137. return [];
  138. }
  139. }
  140. /**
  141. * Overides the EntityFieldQuery::queryCallback function.
  142. */
  143. public function queryCallback() {
  144. // Use the override from $this->executeCallback. It can be set either
  145. // while building the query, or using hook_entity_query_alter().
  146. if (function_exists($this->executeCallback)) {
  147. return $this->executeCallback;
  148. }
  149. // If there are no field conditions and sorts, and no execute callback
  150. // then we default to querying entity tables in SQL.
  151. if (empty($this->fields)) {
  152. return array(
  153. $this,
  154. 'propertyQuery',
  155. );
  156. }
  157. // If no override, find the storage engine to be used.
  158. foreach ($this->fields as $field) {
  159. if (!isset($storage)) {
  160. $storage = $field['storage']['module'];
  161. }
  162. elseif ($storage != $field['storage']['module']) {
  163. throw new EntityFieldQueryException(t("Can't handle more than one field storage engine"));
  164. }
  165. }
  166. if ($storage) {
  167. // Use hook_field_storage_query() from the field storage.
  168. return $storage . '_field_storage_query';
  169. }
  170. else {
  171. throw new EntityFieldQueryException(t("Field storage engine not found."));
  172. }
  173. }
  174. /**
  175. * Determines the query callback to use for this entity query.
  176. *
  177. * This function is a replacement for queryCallback() from the
  178. * parent EntityFieldQuery class because that class only allows a single
  179. * storage type per query.
  180. *
  181. * @param $storage
  182. * The storage module
  183. *
  184. * @throws EntityFieldQueryException
  185. * @return
  186. * A callback that can be used with call_user_func().
  187. *
  188. */
  189. protected function queryStorageCallback($storage) {
  190. // Use the override from $this->executeCallback. It can be set either
  191. // while building the query, or using hook_entity_query_alter().
  192. if (function_exists($this->executeCallback)) {
  193. return $this->executeCallback;
  194. }
  195. // If there are no field conditions and sorts, and no execute callback
  196. // then we default to querying entity tables in SQL.
  197. if (empty($this->fields)) {
  198. return array($this, 'propertyQuery');
  199. }
  200. if ($storage) {
  201. // Use hook_field_storage_query() from the field storage.
  202. return $storage . '_field_storage_query';
  203. }
  204. else {
  205. throw new EntityFieldQueryException(t("Field storage engine not found."));
  206. }
  207. }
  208. }