tripal_stock.install 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Install the tripal stock module including it's content type
  4. * @file
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_stock
  11. */
  12. function tripal_stock_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_stock.views_default.inc");
  15. $views = tripal_stock_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_views_admin_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  18. }
  19. }
  20. /**
  21. * Implementation of hook_requirements().
  22. *
  23. * @ingroup tripal_stock
  24. */
  25. function tripal_stock_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_stock'] = array(
  31. 'title' => "tripal_stock",
  32. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  33. 'severity' => REQUIREMENT_ERROR,
  34. );
  35. }
  36. }
  37. return $requirements;
  38. }
  39. /**
  40. * Implementation of hook_install().
  41. *
  42. * @ingroup tripal_stock
  43. */
  44. function tripal_stock_install() {
  45. // create the module's data directory
  46. tripal_create_files_dir('tripal_stock');
  47. // set the default vocabularies
  48. tripal_set_default_cv('stock', 'type_id', 'stock_type');
  49. tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  50. tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  51. }
  52. /**
  53. * Implementation of hook_uninstall().
  54. *
  55. * @ingroup tripal_stock
  56. */
  57. function tripal_stock_uninstall() {
  58. }
  59. /**
  60. * Implementation of hook_schema().
  61. *
  62. * @ingroup tripal_stock
  63. */
  64. function tripal_stock_schema() {
  65. $schema['chado_stock'] = array(
  66. 'fields' => array(
  67. 'vid' => array(
  68. 'type' => 'int',
  69. 'unsigned' => TRUE,
  70. 'not null' => TRUE,
  71. ),
  72. 'nid' => array(
  73. 'type' => 'int',
  74. 'unsigned' => TRUE,
  75. 'not null' => TRUE,
  76. ),
  77. 'stock_id' => array(
  78. 'type' => 'int',
  79. 'unsigned' => TRUE,
  80. 'not null' => TRUE,
  81. ),
  82. ),
  83. 'indexes' => array(
  84. 'stock_id' => array('stock_id'),
  85. 'nid' => array('nid'),
  86. ),
  87. 'unique' => array(
  88. 'stock_id' => array('stock_id'),
  89. ),
  90. 'primary key' => array('vid'),
  91. );
  92. return $schema;
  93. }
  94. /**
  95. * Add cvs related to publications
  96. *
  97. * @ingroup tripal_pub
  98. */
  99. function tripal_stock_add_cvs() {
  100. // Add cv for relationship types
  101. tripal_cv_add_cv(
  102. 'stock_relationship',
  103. 'Contains types of relationships between stocks.'
  104. );
  105. tripal_cv_add_cv(
  106. 'stock_property',
  107. 'Contains properties for stocks.'
  108. );
  109. tripal_cv_add_cv(
  110. 'stock_type',
  111. 'Contains a list of types for stocks.'
  112. );
  113. }
  114. /**
  115. * Add cvterms related to publications
  116. *
  117. * @ingroup tripal_pub
  118. */
  119. function tripal_stock_add_cvterms() {
  120. }
  121. /**
  122. * This is the required update for tripal_stock when upgrading from Drupal core API 6.x.
  123. *
  124. */
  125. function tripal_stock_update_7200() {
  126. // add the new CVs. We can't use the Tripal API because during
  127. // an upgrade from D6 to D7 Tripal is disable. So, we have to manually add these
  128. // new vocabularies.
  129. // add the stock_relationshp CV
  130. try {
  131. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_relationship'")->fetchField();
  132. if (!$cv_id) {
  133. // add the vocabulary
  134. $cv_id = db_insert('chado.cv')
  135. ->fields(array(
  136. 'name' => 'stock_relationship',
  137. 'definition' => 'Contains types of relationships between stocks.'
  138. ))
  139. ->execute();
  140. }
  141. // for backwards compatibility, get the previously set stock relationship CV, otherwise
  142. // use the new stock_relationship CV we just added
  143. $default_stockrel_cv = variable_get('chado_stock_relationship_cv', $cv_id);
  144. db_insert('tripal_cv_defaults')
  145. ->fields(array(
  146. 'table_name' => 'stock_relationship',
  147. 'field_name' => 'type_id',
  148. 'cv_id' => $default_stockrel_cv
  149. ))
  150. ->execute();
  151. }
  152. catch (\PDOException $e) {
  153. $error = $e->getMessage();
  154. throw new DrupalUpdateException('Failed to add stock_relationship vocabulary: '. $error);
  155. }
  156. // add the stock_property CV
  157. try {
  158. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_property'")->fetchField();
  159. if (!$cv_id) {
  160. // add the vocabulary
  161. $cv_id = db_insert('chado.cv')
  162. ->fields(array(
  163. 'name' => 'stock_property',
  164. 'definition' => 'Contains properties for stocks.'
  165. ))
  166. ->execute();
  167. }
  168. // for backwards compatibility, get the previously set stock property CV, otherwise
  169. // use the new stock_property CV we just added
  170. $default_stockprop_cv = variable_get('chado_stock_prop_types_cv', $cv_id);
  171. db_insert('tripal_cv_defaults')
  172. ->fields(array(
  173. 'table_name' => 'stockprop',
  174. 'field_name' => 'type_id',
  175. 'cv_id' => $default_stockprop_cv
  176. ))
  177. ->execute();
  178. }
  179. catch (\PDOException $e) {
  180. $error = $e->getMessage();
  181. throw new DrupalUpdateException('Failed to add stock_property vocabulary: '. $error);
  182. }
  183. // add the stock_type CV
  184. try {
  185. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_type'")->fetchField();
  186. if (!$cv_id) {
  187. // add the vocabulary
  188. $cv_id = db_insert('chado.cv')
  189. ->fields(array(
  190. 'name' => 'stock_type',
  191. 'definition' => 'Contains a list of types for stocks.'
  192. ))
  193. ->execute();
  194. }
  195. // for backwards compatibility, get the previously set stock types CV, otherwise
  196. // use the new stock_type CV we just added
  197. $default_stocktype_cv = variable_get('chado_stock_types_cv', $cv_id);
  198. db_insert('tripal_cv_defaults')
  199. ->fields(array(
  200. 'table_name' => 'stock',
  201. 'field_name' => 'type_id',
  202. 'cv_id' => $default_stocktype_cv
  203. ))
  204. ->execute();
  205. }
  206. catch (\PDOException $e) {
  207. $error = $e->getMessage();
  208. throw new DrupalUpdateException('Failed to add stock_type vocabulary: '. $error);
  209. }
  210. }