tripal_feature.views.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * This file contains the basic functions for views integration of
  5. * chado/tripal organism tables. Supplementary functions can be found in
  6. * ./views/
  7. *
  8. * Documentation on views integration can be found at
  9. * http://views2.logrus.com/doc/html/index.html.
  10. */
  11. /*************************************************************************
  12. * Implements hook_views_data()
  13. * Purpose: Describe chado/tripal tables & fields to views
  14. *
  15. * @return: a data array which follows the structure outlined in the
  16. * views2 documentation for this hook. Essentially, it's an array of table
  17. * definitions keyed by chado/tripal table name. Each table definition
  18. * includes basic details about the table, fields in that table and
  19. * relationships between that table and others (joins)
  20. */
  21. require_once('views/feature.views.inc');
  22. function tripal_feature_views_data() {
  23. $data = array();
  24. $data = array_merge($data, retrieve_feature_views_data());
  25. return $data;
  26. }
  27. /*************************************************************************
  28. * Implements hook_views_handlers()
  29. * Purpose: Register all custom handlers with views
  30. * where a handler describes either "the type of field",
  31. * "how a field should be filtered", "how a field should be sorted"
  32. *
  33. * @return: An array of handler definitions
  34. */
  35. function tripal_feature_views_handlers() {
  36. return array(
  37. 'info' => array(
  38. 'path' => drupal_get_path('module', 'tripal_feature') . '/views/handlers',
  39. ),
  40. 'handlers' => array(
  41. 'views_handler_field_feature_nid' => array(
  42. 'parent' => 'views_handler_field_numeric',
  43. ),
  44. 'views_handler_field_readable_date' => array(
  45. 'parent' => 'views_handler_field',
  46. ),
  47. ),
  48. );
  49. }
  50. /**
  51. * Implements hook_views_pre_render
  52. * Purpose: Intercepts the view after the query has been executed
  53. * All the results are stored in $view->result
  54. * Looking up the NID here ensures the query is only executed once
  55. * for all features in the table.
  56. *
  57. * @todo add if !<chado/drupal same db> around NID portion
  58. */
  59. function tripal_feature_views_pre_render (&$view) {
  60. if (preg_match('/feature/', $view->base_table)) {
  61. // retrieve the feature_id for each record in the views current page
  62. $feature_ids = array();
  63. foreach ($view->result as $row_num => $row) {
  64. $feature_ids[$row_num] = $row->feature_id;
  65. }
  66. // Using the list of feature_ids from the view
  67. // lookup the NIDs from drupal
  68. // and add that to the results of the view
  69. $sql = "SELECT nid, feature_id FROM chado_feature WHERE feature_id IN (".implode(',',$feature_ids).")";
  70. $resource = db_query($sql);
  71. while ($r = db_fetch_object($resource)) {
  72. $key = array_search($r->feature_id, $feature_ids);
  73. $view->result[$key]->nid = $r->nid;
  74. }
  75. }
  76. }