tripal_organism.views.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*******************************************************************************
  3. * This function provides data for the VIEWS module. The function generates
  4. * a new field 'Tripal organism: Common name' when adding a 'Node' view type.
  5. */
  6. function tripal_organism_views_data() {
  7. //Delete the custom table that provides data for the cutom view if exists
  8. if (db_table_exists('tripal_organism_views_common_name')) {
  9. $sql = "DROP TABLE {tripal_organism_views_common_name}";
  10. db_query ($sql);
  11. }
  12. // Create the custom table. After created, it will be deleted only when the
  13. // tripal_organism module is uninstalled.
  14. drupal_install_schema('tripal_organism_views_common_name');
  15. // Populate the custom table
  16. // Get organism_id from chado_organism table
  17. $sql = 'SELECT nid, organism_id FROM {chado_organism}';
  18. $result = db_query($sql);
  19. //Retrieve common_names from chado database using the organism_id
  20. $sql = 'SELECT common_name FROM {organism} WHERE organism_id=%d';
  21. $previous_db = db_set_active ('chado');
  22. $common_names = array ();
  23. while ($org = db_fetch_object($result)) {
  24. $common_name = db_result(db_query ($sql, $org->organism_id));
  25. $common_names [$org->nid] = $common_name;
  26. }
  27. db_set_active ($previous_db);
  28. // Insert common_name and nid to the custom table
  29. $sql = "INSERT INTO {tripal_organism_views_common_name} (nid, common_name) VALUES (%d, '%s')";
  30. foreach ($common_names as $nid => $common_name) {
  31. db_query($sql, $nid, $common_name);
  32. }
  33. // The following describes our custom table to the Views module
  34. $data['tripal_organism_views_common_name'] = array(
  35. // This table section is needed for each custom view created
  36. 'table' => array(
  37. 'group' => 'Tripal organism',
  38. 'join' => array(
  39. 'node' => array(
  40. 'left_field' => 'nid',
  41. 'field' => 'nid',
  42. ),
  43. ),
  44. ),
  45. // Describe the column 'common_name' to show as a field in the Views
  46. 'common_name' => array(
  47. 'title' => t('Common name'),
  48. // The help that appears on the UI,
  49. 'help' => t('The common name of an organism.'),
  50. // Information for displaying the nid
  51. 'field' => array(
  52. 'handler' => 'views_handler_field_node',
  53. 'click sortable' => TRUE,
  54. ),
  55. // Information for accepting a common_name as a filter
  56. 'filter' => array(
  57. 'handler' => 'views_handler_filter_numeric',
  58. ),
  59. // Information for sorting on a common_name.
  60. 'sort' => array(
  61. 'handler' => 'views_handler_sort',
  62. ),
  63. ),
  64. );
  65. return $data;
  66. }
  67. /*******************************************************************************
  68. * The table schema generated to provide data for the Views module. The table
  69. * will be created when the Views module calls to hook_view_data(). It will
  70. * be deleted only when the tripal_organism module is uninstalled.
  71. */
  72. function tripal_organism_views_common_name_schema() {
  73. $schema['tripal_organism_views_common_name'] = array(
  74. 'fields' => array(
  75. 'nid' => array(
  76. 'type' => 'int',
  77. 'unsigned' => TRUE,
  78. 'not null' => TRUE,
  79. 'default' => 0
  80. ),
  81. 'common_name' => array(
  82. 'type' => 'varchar',
  83. 'length' => 255,
  84. 'not null' => TRUE,
  85. 'default' => 'NA',
  86. 'description' => t('To provide common name for the Views module')
  87. ),
  88. ),
  89. );
  90. return $schema;
  91. }
  92. // Experimental codes that don't create extra tables in Drupal database
  93. /*
  94. function tripal_organism_views_data() {
  95. // Make Chado 'Organism' as a base table, so it can show as a Views type
  96. $data['organism'] = array(
  97. // This table section is needed for each custom view created
  98. 'table' => array(
  99. 'group' => 'Tripal',
  100. 'base' => array(
  101. 'field' => 'organism_id',
  102. 'title' => t('Tripal Organism'),
  103. 'help' => t('Tripal Organism'),
  104. 'database' => 'chado',
  105. ),
  106. ),
  107. // Describe the columns of organism table to the Views so it knows how to make the query
  108. 'common_name' => array(
  109. 'title' => t('Common Name'),
  110. 'help' => t('Showing the common name of an organism.'), // The help that appears on the UI,
  111. // Information for displaying the nid
  112. 'field' => array(
  113. 'handler' => 'views_handler_field',
  114. 'click sortable' => TRUE,
  115. ),
  116. // Information for accepting a common_name as a filter
  117. 'filter' => array(
  118. 'handler' => 'views_handler_filter_string',
  119. ),
  120. // Information for sorting on a common_name.
  121. 'sort' => array(
  122. 'handler' => 'views_handler_sort',
  123. ),
  124. ),
  125. );
  126. return $data;
  127. }
  128. */