TripalFieldQuery.inc 4.1 KB

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