tripal_views.api.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. 'help' => '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. );
  50. switch($defn_array['type']) {
  51. case 'chado':
  52. $view_record['table_name'] = $defn_array['table'];
  53. break;
  54. case 'mview':
  55. $mview = db_fetch_object(db_query("SELECT mview_id FROM tripal_mviews WHERE mv_table='%s'",$defn_array['table']));
  56. $view_record['mview_id'] = $mview->mview_id;
  57. $view_record['table_name'] = $defn_array['table'];
  58. break;
  59. }
  60. $status = drupal_write_record('tripal_views',$view_record);
  61. if ($status) {
  62. // Insert Field Definitions
  63. foreach ($defn_array['fields'] as $field) {
  64. $field_record = array(
  65. 'column_name' => $field['name'],
  66. 'title' => $field['title'],
  67. 'help' => $field['help'],
  68. );
  69. //$status = drupal_write_record('tripal_views_fields',$field_record);
  70. if ($status) {
  71. // Insert Handler Definitions
  72. foreach ($field['handlers'] as $handler_type => $handler) {
  73. $handler_record = array(
  74. 'setup_id' => $view_record['setup_id'],
  75. 'column_name' => $field['name'],
  76. 'handler_type' => $handler_type,
  77. 'handler_name' => $handler['name'],
  78. 'arguments' => serialize($handler)
  79. );
  80. $status = drupal_write_record('tripal_views_handlers',$handler_record);
  81. if (!$status) {
  82. drupal_set_message('Unable to integrate '.$handler_type.' handler: '.$handler['name'], 'error');
  83. $no_errors = FALSE;
  84. }
  85. }
  86. // Insert Joins
  87. if (!is_array($field['joins'])) { $field['joins'] = array(); }
  88. foreach($field['joins'] as $join) {
  89. $join_record = array(
  90. 'setup_id' => $view_record['setup_id'],
  91. 'base_table' => $defn_array['table'],
  92. 'base_field' => $field['name'],
  93. 'left_table' => $join['table'],
  94. 'left_field' => $join['field']
  95. );
  96. $status = drupal_write_record('tripal_views_join',$join_record);
  97. if (!$status) {
  98. drupal_set_message('Unable to join '.$join['table'].'.'.$join['field'].' with '.$defn_array['table'].'.'.$field['name'], 'error');
  99. $no_errors = FALSE;
  100. }
  101. }
  102. } else {
  103. drupal_set_message('Unable to integrate field: '.$field['name'],'error');
  104. $no_errors = FALSE;
  105. }
  106. }
  107. } else {
  108. drupal_set_message('Unable to set default views integration','error');
  109. $no_errors = FALSE;
  110. }
  111. return $no_errors;
  112. }
  113. /**
  114. *
  115. */
  116. function tripal_core_views_integration_remove_entry($tablename, $priority) {
  117. $views = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s'",$tablename));
  118. if ($views->setup_id) {
  119. db_query('DELETE FROM {tripal_views} WHERE setup_id=%d',$views->setup_id);
  120. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d',$views->setup_id);
  121. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d',$views->setup_id);
  122. }
  123. }