tripal_views.api.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Add views integration records into the tripal_views* tables
  4. *
  5. * @param $defn_array
  6. * An array describing the structure and fields of the table
  7. *
  8. * @return
  9. * True/False if completed successfully/not
  10. *
  11. * Example usage (in hook_install()):
  12. * @code
  13. $defn_array = array(
  14. 'table' => 'feature', //tablename or materialized view name
  15. 'name' => 'Sequence Features', // Human readable name
  16. 'type' => 'chado', //either chado or mview depending on tablename
  17. 'description' => 'Create a listing of features.', //description seen when creating a view of this type
  18. 'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
  19. 'fields' => array(
  20. 'feature_id' => array(
  21. 'name' => 'feature_id', //field name in database
  22. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  23. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  24. 'type' => 'int', // the type of field
  25. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  26. 'field' => array(
  27. 'name' => 'chado_views_handler_numeric' //name of handler
  28. ),
  29. 'filter' => array( ... ),
  30. ...
  31. ),
  32. 'join' => array( //describe a table that joins to this one via this field
  33. 'table' => 'featureprop', //table to join to
  34. 'field' => 'feature_id', //field in above table (featureprop)
  35. 'handler' => 'views_handler_join_chado_aggregator', //handler to use
  36. ),
  37. )
  38. ),
  39. );
  40. tripal_core_views_integration_add_entry($defn_array);
  41. * @endcode
  42. */
  43. function tripal_core_views_integration_add_entry($defn_array) {
  44. $no_errors = TRUE;
  45. // First insert into tripal_views
  46. $view_record = array(
  47. 'name' => $defn_array['name'],
  48. 'comment' => $defn_array['description'],
  49. 'priority' => $defn_array['priority'],
  50. );
  51. switch($defn_array['type']) {
  52. case 'chado':
  53. $view_record['table_name'] = $defn_array['table'];
  54. break;
  55. case 'mview':
  56. $mview = db_fetch_object(db_query("SELECT mview_id FROM tripal_mviews WHERE mv_table='%s'",$defn_array['table']));
  57. $view_record['mview_id'] = $mview->mview_id;
  58. $view_record['table_name'] = $defn_array['table'];
  59. break;
  60. }
  61. $status = drupal_write_record('tripal_views',$view_record);
  62. if ($status) {
  63. // Insert Field Definitions
  64. foreach ($defn_array['fields'] as $field) {
  65. $field_record = array(
  66. 'setup_id' => $view_record['setup_id'],
  67. 'column_name' => $field['name'],
  68. 'name' => $field['title'],
  69. 'description' => $field['description'],
  70. 'type' => $field['type'],
  71. );
  72. $status = drupal_write_record('tripal_views_field',$field_record);
  73. if ($status) {
  74. // Insert Handler Definitions
  75. foreach ($field['handlers'] as $handler_type => $handler) {
  76. $handler_record = array(
  77. 'setup_id' => $view_record['setup_id'],
  78. 'column_name' => $field['name'],
  79. 'handler_type' => $handler_type,
  80. 'handler_name' => $handler['name'],
  81. 'arguments' => serialize($handler)
  82. );
  83. $status = drupal_write_record('tripal_views_handlers',$handler_record);
  84. if (!$status) {
  85. drupal_set_message('Unable to integrate '.$handler_type.' handler: '.$handler['name'], 'error');
  86. $no_errors = FALSE;
  87. }
  88. }
  89. // Insert Joins
  90. if (!is_array($field['joins'])) { $field['joins'] = array(); }
  91. foreach($field['joins'] as $join) {
  92. $join_record = array(
  93. 'setup_id' => $view_record['setup_id'],
  94. 'base_table' => $defn_array['table'],
  95. 'base_field' => $field['name'],
  96. 'left_table' => $join['table'],
  97. 'left_field' => $join['field'],
  98. );
  99. if (!empty($join['handler'])) {
  100. $join_record['handler'] = $join['handler'];
  101. } else {
  102. $join_record['handler'] = 'views_join';
  103. }
  104. $status = drupal_write_record('tripal_views_join',$join_record);
  105. if (!$status) {
  106. drupal_set_message('Unable to join '.$join['table'].'.'.$join['field'].' with '.$defn_array['table'].'.'.$field['name'], 'error');
  107. $no_errors = FALSE;
  108. }
  109. }
  110. } else {
  111. drupal_set_message('Unable to integrate field: '.$field['name'],'error');
  112. $no_errors = FALSE;
  113. }
  114. }
  115. } else {
  116. drupal_set_message('Unable to set default views integration','error');
  117. $no_errors = FALSE;
  118. }
  119. return $no_errors;
  120. }
  121. /**
  122. *
  123. */
  124. function tripal_core_views_integration_remove_entry($tablename, $priority) {
  125. $views = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s'",$tablename));
  126. if ($views->setup_id) {
  127. db_query('DELETE FROM {tripal_views} WHERE setup_id=%d',$views->setup_id);
  128. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d',$views->setup_id);
  129. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d',$views->setup_id);
  130. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d',$views->setup_id);
  131. }
  132. }