tripal_feature.install 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /************************************************************************
  3. * Implementation of hook_install();
  4. */
  5. function tripal_feature_install(){
  6. // create the module's data directory
  7. tripal_create_moddir('tripal_feature');
  8. // create the tables that correlate drupal nodes with chado
  9. // features, organisms, etc....
  10. drupal_install_schema('tripal_feature');
  11. // add the materialized view
  12. tripal_feature_add_organism_count_mview();
  13. }
  14. /*******************************************************************************
  15. * Update for Drupal 6.x, Tripal 0.2b, Feature Module 0.2
  16. * This update adjusts the materialized view by adding a 'cvterm_id' column
  17. */
  18. function tripal_feature_update_6000(){
  19. // recreate the materialized view
  20. tripal_feature_add_organism_count_mview();
  21. $ret = array(
  22. '#finished' => 1,
  23. );
  24. return $ret;
  25. }
  26. /*******************************************************************************
  27. * Implementation of hook_schema().
  28. */
  29. function tripal_feature_add_organism_count_mview(){
  30. $view_name = 'organism_feature_count';
  31. // Drop the MView table if it exists
  32. $mview_id = tripal_mviews_get_mview_id($view_name);
  33. if($mview_id){
  34. tripal_mviews_action("delete",$mview_id);
  35. }
  36. // Create the MView
  37. tripal_add_mview(
  38. // view name
  39. $view_name,
  40. // tripal module name
  41. 'tripal_feature',
  42. // table name
  43. $view_name,
  44. // table schema definition
  45. 'organism_id integer, genus character varying(255), '.
  46. ' species character varying(255), '.
  47. ' common_name character varying(255), '.
  48. ' num_features integer, cvterm_id integer, '.
  49. ' feature_type character varying(255)',
  50. // columns for indexing
  51. 'organism_id,cvterm_id,feature_type',
  52. // SQL statement to populate the view
  53. 'SELECT O.organism_id, O.genus, O.species, O.common_name,
  54. count(F.feature_id) as num_features,
  55. CVT.cvterm_id, CVT.name as feature_type
  56. FROM {Organism} O
  57. INNER JOIN Feature F ON O.Organism_id = F.organism_id
  58. INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id
  59. GROUP BY O.Organism_id, O.genus, O.species, O.common_name,
  60. CVT.cvterm_id, CVT.name',
  61. // special index
  62. ''
  63. );
  64. // add a job to the job queue so this view gets updated automatically next
  65. // time the job facility is run
  66. $mview_id = tripal_mviews_get_mview_id($view_name);
  67. if($mview_id){
  68. tripal_mviews_action('update',$mview_id);
  69. }
  70. }
  71. /************************************************************************
  72. * Implementation of hook_schema().
  73. */
  74. function tripal_feature_schema() {
  75. $schema = tripal_feature_get_schemas();
  76. return $schema;
  77. }
  78. /************************************************************************
  79. * Implementation of hook_uninstall()
  80. */
  81. function tripal_feature_uninstall(){
  82. // Drop the MView table if it exists
  83. $mview_id = tripal_mviews_get_mview_id('organism_feature_count');
  84. if($mview_id){
  85. tripal_mviews_action("delete",$mview_id);
  86. }
  87. drupal_uninstall_schema('tripal_feature');
  88. // Get the list of nodes to remove
  89. $sql_feature_id = "SELECT nid, vid ".
  90. "FROM {node} ".
  91. "WHERE type='chado_feature'";
  92. $result = db_query($sql_feature_id);
  93. while ($node = db_fetch_object($result)) {
  94. node_delete($node->nid);
  95. }
  96. }
  97. /************************************************************************
  98. * This function simply defines all tables needed for the module to work
  99. * correctly. By putting the table definitions in a separate function we
  100. * can easily provide the entire list for hook_install or individual
  101. * tables for an update.
  102. */
  103. function tripal_feature_get_schemas (){
  104. $schema = array();
  105. $schema['chado_feature'] = array(
  106. 'fields' => array(
  107. 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  108. 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  109. 'feature_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  110. 'sync_date' => array ('type' => 'int', 'not null' => FALSE, 'description' => 'UNIX integer sync date/time'),
  111. ),
  112. 'indexes' => array(
  113. 'feature_id' => array('feature_id')
  114. ),
  115. 'unique keys' => array(
  116. 'nid_vid' => array('nid','vid'),
  117. 'vid' => array('vid')
  118. ),
  119. 'primary key' => array('nid'),
  120. );
  121. return $schema;
  122. }
  123. /*******************************************************************************
  124. * Implementation of hook_requirements(). Make sure 'Tripal Core' is enabled
  125. * before installation
  126. */
  127. function tripal_feature_requirements($phase) {
  128. $requirements = array();
  129. if ($phase == 'install') {
  130. if (!function_exists('tripal_create_moddir')) {
  131. $requirements ['tripal_feature'] = array(
  132. 'title' => "tripal_feature",
  133. 'value' => "error. Some required modules are just being installed. Please try again.",
  134. 'severity' => REQUIREMENT_ERROR,
  135. );
  136. }
  137. }
  138. return $requirements;
  139. }