tripal_views_integration.views.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. *
  4. * @ingroup tripal_views_integration
  5. */
  6. function tripal_views_integration_views_data(){
  7. $tvi_query = db_query('SELECT * FROM tripal_views_integration');
  8. //tvi = tripal_views_integration
  9. while($tvi_row = db_fetch_object($tvi_query)){
  10. //ids we'll use for queries
  11. $setup_id = $tvi_row->setup_id;
  12. $mview_id = $tvi_row->mview_id;
  13. //let's get the name of the table
  14. $mview_table = db_fetch_object(db_query("SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = '$mview_id';"));
  15. //use name from above and description from $tvi_row
  16. $data[$mview_table->name]['table']['group'] = t('Mview ' . $tvi_row->name);
  17. $data[$mview_table->name]['table']['base'] = array(
  18. 'group' => t($tvi_row->name),
  19. 'title' => t($tvi_row->name),
  20. 'help' => t($tvi_row->description),
  21. );
  22. //let's add fields
  23. //tmj = tripal_mviews_join
  24. $tmj_query = db_query("SELECT * FROM tripal_mviews_join WHERE setup_id = '$setup_id'");
  25. while($tmj_row = db_fetch_object($tmj_query)){
  26. $column_name = $tmj_row->view_column;
  27. $handlers = db_fetch_object(db_query("SELECT handler_filter, handler_field FROM {tripal_views_handlers} WHERE setup_id = '$setup_id' AND column_name = '$column_name';"));
  28. //handlers would be used $handlers->handler_filter, $handlers->handler_field etc, thuogh may need to include new ones in this query or do select *
  29. //let's use handlers we retrieved from above
  30. $data[$mview_table->name][$tmj_row->view_column] = array(
  31. 'title' => t($tmj_row->view_column),
  32. 'help' => t("**"),
  33. 'field' => array(
  34. 'handler' => 'views_handler_field',
  35. 'click sortable' => TRUE,
  36. ),
  37. 'sort' => array(
  38. 'handler' => 'views_handler_sort',
  39. ),
  40. 'filter' => array(
  41. 'handler' => 'views_handler_filter_string',
  42. ),
  43. 'argument' => array(
  44. 'handler' => 'views_handler_argument_string',
  45. ),
  46. );
  47. $chado_join_table = $tmj_row->chado_table_join;
  48. $chado_join_column = $tmj_row->chado_column;
  49. $mview_join_column = $tmj_row->view_column;
  50. // add recipricol join entries
  51. $data["$mview_table->name"]['table']['join']["$chado_join_table"] = array(
  52. 'left_field' => $chado_join_column,
  53. 'field' => $mview_join_column,
  54. );
  55. // check to see if this table is one that correlates with Drupal nodes
  56. // if so, there will be a chado_<table_name> table in the Drupal database
  57. // if there is, then we need to add the linking join information
  58. $sql = "SELECT tablename FROM pg_tables WHERE tablename = 'chado_$chado_join_table'";
  59. if(db_fetch_object(db_query($sql))){
  60. // join the mview to the linker table
  61. $data["$mview_table->name"]['table']['join']["chado_$chado_join_table"] = array(
  62. 'left_field' => $chado_join_column,
  63. 'field' => $mview_join_column,
  64. );
  65. }
  66. }
  67. }
  68. return $data;
  69. }
  70. /** $setup_id = $tvi_row->setup_id;
  71. *
  72. * @ingroup tripal_views_integration
  73. */
  74. function tripal_views_integration_views_data_alter(&$data) {
  75. $tvi_query = db_query('SELECT * FROM tripal_views_integration');
  76. //tvi = tripal_views_integration
  77. while($tvi_row = db_fetch_object($tvi_query)){
  78. //ids we'll use for queries
  79. $mview_id = $tvi_row->mview_id;
  80. $setup_id = $tvi_row->setup_id;
  81. //let's get the name of the table
  82. $mview_table = db_fetch_object(db_query("SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = '$mview_id';"));
  83. // iterate through the columns and alter the existing data array for
  84. // joins to other tables
  85. $tmj_query = db_query("SELECT * FROM tripal_mviews_join WHERE setup_id = '$setup_id'");
  86. while($tmj_row = db_fetch_object($tmj_query)){
  87. $chado_join_table = $tmj_row->chado_table_join;
  88. $chado_join_column = $tmj_row->chado_column;
  89. $mview_join_column = $tmj_row->view_column;
  90. // add the recipricol join entries for each column
  91. $data["$chado_join_table"]['table']['join']["$mview_table->name"] = array(
  92. 'left_field' => $mview_join_column,
  93. 'field' => $chado_join_column,
  94. );
  95. // check to see if this table is one that correlates with Drupal nodes
  96. // if so, there will be a chado_<table_name> table in the Drupal database
  97. // if there is, then we need to add the linking join information. We did
  98. // this step in the hook_views_data function above, but now we need
  99. // to add the reciprical joins
  100. $sql = "SELECT tablename FROM pg_tables WHERE tablename = 'chado_$chado_join_table'";
  101. if(db_fetch_object(db_query($sql))){
  102. // join the linker table to the mview
  103. $data["chado_$chado_join_table"]['table']['join']["$mview_table->name"] = array(
  104. 'left_field' => $mview_join_column,
  105. 'field' => $chado_join_column,
  106. );
  107. // Join the node table to the view by way of the chado linker table
  108. $data['node']['table']['join']["$mview_table->name"] = array(
  109. 'left_table' => "chado_$chado_join_table",
  110. 'left_field' => 'nid',
  111. 'field' => 'nid',
  112. );
  113. }
  114. }
  115. }
  116. return $data;
  117. }