tripal_analysis.views.inc 2.7 KB

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