tripal_views_query.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. class tripal_views_query extends views_plugin_query {
  3. // The EntityFieldQuery object.
  4. var $query;
  5. var $fields;
  6. var $filters;
  7. /**
  8. * Ensure a table exists in the queue.
  9. *
  10. * This function overrides the views_plugin_query version of the function
  11. * but does nothing other than return the "table" (or bundle) name as
  12. * we won't be using aliases for bundles.
  13. *
  14. * @param $table
  15. * The unaliased name of the table to ensure.
  16. * @param $relationship
  17. * The relationship to ensure the table links to. Each relationship will
  18. * get a unique instance of the table being added. If not specified, will
  19. * be the primary table.
  20. * @param $join
  21. * A views_join object (or derived object) to join the alias in.
  22. *
  23. * @return
  24. * The alias used to refer to this specific table, or NULL if the table
  25. * cannot be ensured.
  26. */
  27. public function ensure_table($table, $relationship = NULL, $join = NULL) {
  28. // Because we are not querying a table, we're querying a TripalFieldQuery
  29. // object we don't need to ensure the table.
  30. return $table;
  31. }
  32. /**
  33. *
  34. */
  35. public function init($base_table = 'tripal_entity', $base_field = 'id', $options) {
  36. parent::init($base_table, $base_field, $options);
  37. $this->fields = array();
  38. $this->where = array();
  39. // Creqte the TripalFieldQuery object.
  40. $this->query = new TripalFieldQuery();
  41. // Make suer we only query on the entities for this bundle type.
  42. $this->query->entityCondition('entity_type', 'TripalEntity');
  43. $this->query->entityCondition('bundle', $base_table);
  44. }
  45. /**
  46. *
  47. */
  48. public function add_field($table_alias, $field_name, $alias = '', $params = array()) {
  49. $this->fields[] = array(
  50. 'table_alias' => $table_alias,
  51. 'field_name' => $field_name,
  52. 'alias' => $alias,
  53. 'params' => $params
  54. );
  55. }
  56. /**
  57. * Add a simple WHERE clause to the query.
  58. *
  59. * @param $group
  60. * The WHERE group to add these to; groups are used to create AND/OR
  61. * sections. Groups cannot be nested. Use 0 as the default group. If the
  62. * group does not yet exist it will be created as an AND group.
  63. * @param $field
  64. * The name of the field to check.
  65. * @param $value
  66. * The value to test the field against. In most cases, this is a scalar.
  67. * For more complex options, it is an array. The meaning of each element
  68. * in the array is dependent on the $operator.
  69. * @param $operator
  70. * The comparison operator, such as =, <, or >=. It also accepts more
  71. * complex options such as IN, LIKE, or BETWEEN. Defaults to IN if $value
  72. * is an array = otherwise. If $field is a string you have to use 'formula'
  73. * here.
  74. */
  75. public function add_where($group, $field_name, $value = NULL, $operator = NULL) {
  76. // Remove the preceeding period from the $field_name
  77. $field_name = preg_replace('/^\./', '', $field_name);
  78. $this->filters[] = array(
  79. 'group' => $group,
  80. 'field_name' => $field_name,
  81. 'value' => $value,
  82. 'op' => $operator
  83. );
  84. if ($value) {
  85. $this->query->fieldCondition($field_name, $value, $value, $op);
  86. }
  87. }
  88. /**
  89. *
  90. * @param $view
  91. */
  92. function execute(&$view) {
  93. $query = $this->query;
  94. $start = microtime(TRUE);
  95. // Execute the count query
  96. $cquery = clone $query;
  97. $cquery->count();
  98. $num_records = $cquery->execute();
  99. $views->total_rows = count($num_records['TripalEntity']);
  100. $results = $query->execute();
  101. // Iterate through the entities that were returned and get the field
  102. // values that are requested. Those go in the view->result array.
  103. $i = 0;
  104. $view->result = array();
  105. foreach ($results['TripalEntity'] as $entity_id => $stub) {
  106. $entities = array($entity_id => $stub);
  107. $view->result[$i] = new stdClass();
  108. $view->result[$i]->entity = $stub;
  109. foreach ($this->fields as $details) {
  110. $field_name = $details['field_name'];
  111. $field = field_info_field($field_name);
  112. module_invoke($field['storage']['module'], 'field_storage_load', 'TripalEntity',
  113. $entities, FIELD_LOAD_CURRENT, array($field['id'] => array($entity_id)));
  114. $view->result[$i]->$field_name = $entities[$entity_id]->$field_name['und'][0]['value'];
  115. }
  116. $view->result[$i]->entity_id = $stub->id;
  117. $i++;
  118. }
  119. $view->execute_time = microtime(TRUE) - $start;
  120. $view->current_page = 0;
  121. //dpm($view->query);
  122. //dpm($view->result);
  123. }
  124. }