tripal_organism.install 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // Make sure we have the full API loaded this will help during a
  119. // site upgrade when the tripal_core module is disabled.
  120. module_load_include('module', 'tripal_core', 'tripal_core');
  121. tripal_core_import_api();
  122. // Add the new organism_property vocabulary
  123. // We cannot use the Tripal API calls in the 7000 update
  124. // because during upgrade the tripal_core should also be disabled
  125. try {
  126. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'organism_property'")->fetchField();
  127. if (!$cv_id) {
  128. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  129. 'organism_property',
  130. 'Contains properties for organisms.')
  131. ";
  132. db_query($sql);
  133. }
  134. // If a record already exists then don't change it.
  135. $cdi = db_select('tripal_cv_defaults', 't')
  136. ->fields('t', array('cv_default_id'))
  137. ->condition('table_name', 'organismprop')
  138. ->condition('field_name', 'type_id')
  139. ->execute()
  140. ->fetchField();
  141. // Add in the default vocabulary.
  142. if (!$cdi) {
  143. db_insert('tripal_cv_defaults')
  144. ->fields(array(
  145. 'table_name' => 'organismprop',
  146. 'field_name' => 'type_id',
  147. 'cv_id' => $cv_id
  148. ))
  149. ->execute();
  150. }
  151. }
  152. catch (\PDOException $e) {
  153. $error = $e->getMessage();
  154. throw new DrupalUpdateException('Failed to add organism_property vocabulary: '. $error);
  155. }
  156. // During the upgrade from D6 to D7 the vocabulary terms assigned to organisms were
  157. // copied to the field_data_taxonomyextra table rather than to the correct
  158. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  159. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Organism'")->fetchField();
  160. if ($vid) {
  161. try {
  162. if (db_table_exists('field_data_taxonomyextra')) {
  163. // first move from the field_data_taxonomyextra table
  164. $sql = "
  165. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  166. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  167. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  168. FROM field_data_taxonomyextra
  169. WHERE bundle = 'chado_feature')
  170. ";
  171. db_query($sql);
  172. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_organism'";
  173. db_query($sql);
  174. // next move from the field_revision_taxonomyextra table
  175. $sql = "
  176. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  177. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  178. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  179. FROM field_revision_taxonomyextra
  180. WHERE bundle = 'chado_feature')
  181. ";
  182. db_query($sql);
  183. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_organism'";
  184. db_query($sql);
  185. }
  186. }
  187. catch (\PDOException $e) {
  188. $error = $e->getMessage();
  189. throw new DrupalUpdateException('Could not move organism taxonomy terms: '. $error);
  190. }
  191. }
  192. }
  193. /**
  194. * Implementation of hook_update_dependencies().
  195. *
  196. * It specifies a list of other modules whose updates must be run prior to
  197. * this one. It also ensures the the Tripal API is in scope for site
  198. * upgrades when tripal_core is disabled.
  199. */
  200. function tripal_organism_update_dependencies() {
  201. $dependencies = array();
  202. // the tripal_cv update 7200 must run prior to update 7200 of this module
  203. $dependencies['tripal_organism'][7200] = array(
  204. 'tripal_cv' => 7200
  205. );
  206. return $dependencies;
  207. }