tripal_views_query.inc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @file
  4. * Views query plugin for Apache Solr Views.
  5. * Gets its data not from the database, but from a Solr server.
  6. */
  7. class tripal_views_query extends views_plugin_query {
  8. public function add_field($table_alias, $field, $alias = '', $params = array()) {
  9. // Make sure an alias is assigned.
  10. $alias = $alias ? $alias : $field;
  11. return $alias;
  12. }
  13. /**
  14. * We don't support construction of SQL queries.
  15. * @param $get_count
  16. */
  17. function query($get_count = FALSE) {
  18. }
  19. /**
  20. *
  21. * @param $view
  22. */
  23. function execute(&$view) {
  24. // The base table indicates our content type.
  25. $base_table = $view->base_table;
  26. // Get the bundle that the view base table represents.
  27. $bundle = tripal_load_bundle_entity(array('name' => $view->base_table));
  28. // The base table for the view is a bundle therefore the first condition
  29. // must be with the content_type field.
  30. $query = new TripalFieldQuery();
  31. $query->entityCondition('entity_type', 'TripalEntity');
  32. $query->entityCondition('bundle', $bundle->name);
  33. // Apply filters
  34. foreach ($view->filter as $field_name => $handler) {
  35. if (trim($handler->value)) {
  36. $query->fieldCondition($field_name, $field_name, $handler->value, $handler->operator);
  37. }
  38. }
  39. // Apply sorting
  40. foreach ($view->sort as $field_name => $sort) {
  41. $options = $sort->options;
  42. $query->fieldOrderBy($field_name, $options['order']);
  43. }
  44. $results = $query->execute();
  45. if (count($results) > 0) {
  46. foreach ($results['TripalEntity'] as $entity_id => $stub) {
  47. // Begin a new row for Views output.
  48. $row = new stdClass;
  49. // Get the entity object.
  50. $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
  51. $entity = reset($entity);
  52. // Iterate through the fields that are added to the view and attach those
  53. // to the entity. After attaching we can get the value and include
  54. // it in the output results.
  55. foreach($view->field as $field_name => $handler) {
  56. $field = field_info_field($field_name);
  57. field_attach_load($entity->type, array($entity->id => $entity), FIELD_LOAD_CURRENT,
  58. array('field_id' => $field['id']));
  59. $items = field_get_items('TripalEntity', $entity, $field_name);
  60. $value = $items[0]['value'];
  61. $row->entity = $entity;
  62. $row->$field_name = $value;
  63. }
  64. // Add the row to the results list.
  65. $view->result[] = $row;
  66. }
  67. }
  68. $view->total_rows = count($view->result);
  69. $view->pager['current_page'] = 0;
  70. }
  71. }