tripal_organism.install 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * @file
  4. * Functions pertaining to the install/uninstall of this module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_organism
  11. */
  12. function tripal_organism_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_organism.views_default.inc");
  15. $views = tripal_organism_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  18. }
  19. }
  20. /**
  21. * Implementation of hook_install().
  22. *
  23. * @ingroup tripal_organism
  24. */
  25. function tripal_organism_install() {
  26. // cvs & cvterms
  27. tripal_organism_add_cvs();
  28. tripal_organism_add_cvterms();
  29. // set the default vocabularies
  30. tripal_set_default_cv('organismprop', 'type_id', 'organism_property');
  31. }
  32. /**
  33. * Implementation of hook_schema().
  34. *
  35. * @ingroup tripal_organism
  36. */
  37. function tripal_organism_schema() {
  38. $schema['chado_organism'] = array(
  39. 'fields' => array(
  40. 'vid' => array(
  41. 'type' => 'int',
  42. 'unsigned' => TRUE,
  43. 'not null' => TRUE,
  44. 'default' => 0
  45. ),
  46. 'nid' => array(
  47. 'type' => 'int',
  48. 'unsigned' => TRUE,
  49. 'not null' => TRUE,
  50. 'default' => 0
  51. ),
  52. 'organism_id' => array(
  53. 'type' => 'int',
  54. 'not null' => TRUE,
  55. 'default' => 0
  56. )
  57. ),
  58. 'indexes' => array(
  59. 'organism_id' => array('organism_id')
  60. ),
  61. 'unique keys' => array(
  62. 'nid_vid' => array('nid', 'vid'),
  63. 'vid' => array('vid')
  64. ),
  65. 'primary key' => array('nid'),
  66. );
  67. return $schema;
  68. }
  69. /**
  70. * Implementation of hook_uninstall().
  71. *
  72. * @ingroup tripal_organism
  73. */
  74. function tripal_organism_uninstall() {
  75. }
  76. /**
  77. * Implementation of hook_requirements().
  78. *
  79. * @ingroup tripal_organism
  80. */
  81. function tripal_organism_requirements($phase) {
  82. $requirements = array();
  83. if ($phase == 'install') {
  84. // make sure chado is installed
  85. if (!$GLOBALS["chado_is_installed"]) {
  86. $requirements ['tripal_organism'] = array(
  87. 'title' => "tripal_organism",
  88. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  89. 'severity' => REQUIREMENT_ERROR,
  90. );
  91. }
  92. }
  93. return $requirements;
  94. }
  95. /**
  96. * Add cvterms related to organisms
  97. *
  98. * @ingroup tripal_organism
  99. */
  100. function tripal_organism_add_cvs() {
  101. tripal_insert_cv(
  102. 'organism_property',
  103. 'Contains properties for organisms'
  104. );
  105. }
  106. /**
  107. * Add cvterms pertaining to organisms
  108. *
  109. * @ingroup tripal_organism
  110. */
  111. function tripal_organism_add_cvterms() {
  112. }
  113. /**
  114. * This is the required update for tripal_organism when upgrading from Drupal core API 6.x.
  115. *
  116. */
  117. function tripal_organism_update_7200() {
  118. // Add the new organism_property vocabulary
  119. // We cannot use the Tripal API calls in the 7000 update
  120. // because during upgrade the tripal_core should also be disabled
  121. try {
  122. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'organism_property'")->fetchField();
  123. if (!$cv_id) {
  124. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  125. 'organism_property',
  126. 'Contains properties for organisms.')
  127. ";
  128. db_query($sql);
  129. }
  130. // If a record already exists then don't change it.
  131. $cdi = db_select('tripal_cv_defaults', 't')
  132. ->fields('t', array('cv_default_id'))
  133. ->condition('table_name', 'organismprop')
  134. ->condition('field_name', 'type_id')
  135. ->execute()
  136. ->fetchField();
  137. // Add in the default vocabulary.
  138. if (!$cdi) {
  139. db_insert('tripal_cv_defaults')
  140. ->fields(array(
  141. 'table_name' => 'organismprop',
  142. 'field_name' => 'type_id',
  143. 'cv_id' => $cv_id
  144. ))
  145. ->execute();
  146. }
  147. }
  148. catch (\PDOException $e) {
  149. $error = $e->getMessage();
  150. throw new DrupalUpdateException('Failed to add organism_property vocabulary: '. $error);
  151. }
  152. // During the upgrade from D6 to D7 the vocabulary terms assigned to organisms were
  153. // copied to the field_data_taxonomyextra table rather than to the correct
  154. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  155. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Organism'")->fetchField();
  156. if ($vid) {
  157. try {
  158. if (db_table_exists('field_data_taxonomyextra')) {
  159. // first move from the field_data_taxonomyextra table
  160. $sql = "
  161. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  162. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  163. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  164. FROM field_data_taxonomyextra
  165. WHERE bundle = 'chado_feature')
  166. ";
  167. db_query($sql);
  168. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_organism'";
  169. db_query($sql);
  170. // next move from the field_revision_taxonomyextra table
  171. $sql = "
  172. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  173. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  174. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  175. FROM field_revision_taxonomyextra
  176. WHERE bundle = 'chado_feature')
  177. ";
  178. db_query($sql);
  179. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_organism'";
  180. db_query($sql);
  181. }
  182. }
  183. catch (\PDOException $e) {
  184. $error = $e->getMessage();
  185. throw new DrupalUpdateException('Could not move organism taxonomy terms: '. $error);
  186. }
  187. }
  188. }
  189. /**
  190. * Implementation of hook_update_dependencies(). It specifies a list of
  191. * other modules whose updates must be run prior to this one.
  192. */
  193. function tripal_organism_update_dependencies() {
  194. $dependencies = array();
  195. // the tripal_cv update 7200 must run prior to update 7200 of this module
  196. $dependencies['tripal_organism'][7200] = array(
  197. 'tripal_cv' => 7200
  198. );
  199. return $dependencies;
  200. }