tripal_views_query.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. class tripal_views_query extends views_plugin_query_default {
  3. private $attach_fields = array();
  4. /**
  5. *
  6. */
  7. public function init($base_table = 'tripal_entity', $base_field = 'id', $options) {
  8. parent::init($base_table, $base_field, $options);
  9. // Always add the TripaEntity id and the bundle name so we can keep
  10. // track of which rows are in the SQL results
  11. $this->add_field('tripal_entity', 'id', 'tripal_entity_id');
  12. $this->add_field('tripal_bundle', 'name', 'tripal_bundle_name');
  13. }
  14. /**
  15. *
  16. */
  17. public function add_field($table_alias, $field, $alias = '', $params = array()) {
  18. // If this is not a TripalField then allow the add_field to work as usual.
  19. $f = field_info_field($field);
  20. if (!$f) {
  21. return parent::add_field($table_alias, $field, $alias, $params);
  22. }
  23. else {
  24. $this->attach_fields[] = $f;
  25. }
  26. }
  27. /**
  28. *
  29. * @param $view
  30. */
  31. function execute(&$view) {
  32. // First execute any SQL that needs to run.
  33. parent::execute($view);
  34. // Add in the entity object to the results and any additional fields
  35. // that have been requested.
  36. for ($i = 0; $i < count($view->result); $i++) {
  37. // Build a simple entity stub (it's faster than trying to load the
  38. // whole things!).
  39. $entity_id = $view->result[$i]->tripal_entity_id;
  40. $bundle_name = $view->result[$i]->tripal_bundle_name;
  41. $ids = array($entity_id, 0, $bundle_name);
  42. $entity = entity_create_stub_entity('TripalEntity', $ids);
  43. // Iterate through the field and attach only those that have been
  44. // requested.
  45. foreach ($this->attach_fields as $field) {
  46. // Don't load this field if it doesn't belong to the selected bundle
  47. if (in_array($bundle_name, $field['bundles']['TripalEntity'])) {
  48. $entities = array($entity_id => $entity);
  49. module_invoke($field['storage']['module'], 'field_storage_load', 'TripalEntity',
  50. $entities, FIELD_LOAD_CURRENT, array($field['id'] => array($entity_id)));
  51. }
  52. }
  53. $view->result[$i]->entity = $entity;
  54. }
  55. return;
  56. // The base table indicates our content type.
  57. $base_table = $view->base_table;
  58. // Get the bundle that the view base table represents.
  59. // $bundle = tripal_load_bundle_entity(array('name' => $view->base_table));
  60. // The base table for the view is a bundle therefore the first condition
  61. // must be with the content_type field.
  62. $query = new TripalFieldQuery();
  63. // $query->entityCondition('entity_type', 'TripalEntity');
  64. $query->entityCondition('bundle', $bundle->name);
  65. // Apply filters
  66. foreach ($view->filter as $field_name => $handler) {
  67. if (trim($handler->value)) {
  68. $query->fieldCondition($field_name, $field_name, $handler->value, $handler->operator);
  69. }
  70. }
  71. // Apply sorting
  72. foreach ($view->sort as $field_name => $sort) {
  73. $options = $sort->options;
  74. $query->fieldOrderBy($field_name, $options['order']);
  75. }
  76. $results = $query->execute();
  77. if (count($results) > 0) {
  78. foreach ($results['TripalEntity'] as $entity_id => $stub) {
  79. // Begin a new row for Views output.
  80. $row = new stdClass;
  81. // Get the entity object.
  82. $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
  83. $entity = reset($entity);
  84. // Iterate through the fields that are added to the view and attach those
  85. // to the entity. After attaching we can get the value and include
  86. // it in the output results.
  87. foreach($view->field as $field_name => $handler) {
  88. $field = field_info_field($field_name);
  89. field_attach_load($entity->type, array($entity->id => $entity), FIELD_LOAD_CURRENT,
  90. array('field_id' => $field['id']));
  91. $items = field_get_items('TripalEntity', $entity, $field_name);
  92. $value = $items[0]['value'];
  93. $row->entity = $entity;
  94. $row->$field_name = $value;
  95. }
  96. // Add the row to the results list.
  97. $view->result[] = $row;
  98. }
  99. }
  100. $view->total_rows = count($view->result);
  101. $view->pager['current_page'] = 0;
  102. }
  103. }