TripalFieldQuery.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * A simple class for querying entities based on field values for fields.
  4. *
  5. * This class supports the use of multiple field storage. This class is loosely
  6. * modeled after the EntityFieldQuery class.
  7. *
  8. */
  9. class TripalFieldQuery {
  10. // A list of the field storage instances for the fields in the filter.
  11. protected $field_storage = array();
  12. // The order in which the field storage execute() function should be
  13. // called.
  14. protected $fs_order = array();
  15. // An associative array of the filters to apply.
  16. protected $conditions = array();
  17. /**
  18. *
  19. * @param $field_name
  20. * The name of the field.
  21. * @param $value
  22. * The value to use for filtering.
  23. * @param $operator
  24. * The operation to apply: '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH',
  25. * 'CONTAINS': These operators expect $value to be a literal of the same
  26. * type as the column. 'IN', 'NOT IN': These operators expect $value to
  27. * be an array of literals of the same type as the column.
  28. */
  29. public function fieldCondition($field_name, $value, $operator = '=') {
  30. $field = field_info_field($field_name);
  31. if ($field) {
  32. $field_storage_type = $field['storage']['type'];
  33. $this->conditions[$field_storage_type][] = array(
  34. 'field' => $field,
  35. 'value' => $value,
  36. 'operator' => $operator,
  37. );
  38. if (!array_key_exists($field_storage_type, $this->field_storage)) {
  39. $this->field_storage[$field_storage_type] = $field['storage'];
  40. $this->fs_order[] = $field_storage_type;
  41. }
  42. }
  43. return $this;
  44. }
  45. /**
  46. * Executes the query and returns results.
  47. *
  48. * This function does not actually perform any queries itself but passes
  49. * on the task to field storage backends for all of the fields in the
  50. * filter. Each backend should return a list of entity IDs that match
  51. * the filters provided. The intersection of this list is returned.
  52. *
  53. * @return
  54. * An array of associative arrays of stub entities. The result can be
  55. * used in the same way that results from the EntityFieldQuery->execute()
  56. * function are used.
  57. */
  58. public function execute() {
  59. // Are there any conditions? If so, then let the field storage
  60. // systems handle the query. If there are no fields then just pull out
  61. // the list of entities.
  62. dpm($this->conditions);
  63. $entity_ids = array();
  64. if (count($this->conditions) > 0) {
  65. // Iterate through each of the field storage types and run their
  66. // tquery() function.
  67. foreach ($this->fs_order as $field_storage_type) {
  68. $storage = $this->field_storage[$field_storage_type];
  69. $module = $storage['module'];
  70. $function_name = $module . '_field_storage_tquery';
  71. $filter_ids = array();
  72. if (function_exists($function_name)) {
  73. $filter_ids = $function_name($this->conditions[$field_storage_type]);
  74. }
  75. // Take the intersection of IDs in this filter with those in the
  76. // final $entity_ids;
  77. if (count($entity_ids) == 0) {
  78. $entity_ids = $filter_ids;
  79. }
  80. else {
  81. $entity_ids = array_intersect($entity_ids, $filter_ids);
  82. }
  83. }
  84. }
  85. else {
  86. $query = db_select('tripal_entity', 'td');
  87. $query->fields('td', array('id'));
  88. $query->orderBy('created', 'DESC');
  89. $query->range(0,25);
  90. $results = $query->execute();
  91. while ($entity_id = $results->fetchField()) {
  92. $entity_ids[] = $entity_id;
  93. }
  94. }
  95. // Generate the entities for the keys.
  96. $return = array();
  97. foreach ($entity_ids as $entity_id) {
  98. $entity = entity_create_stub_entity('TripalEntity', array($entity_id, NULL, NULL));
  99. $return['TripalEntity'][$entity_id] = $entity;
  100. }
  101. return $return;
  102. }
  103. }