tripal_stock.install

  1. 2.x tripal_stock/tripal_stock.install
  2. 3.x legacy/tripal_stock/tripal_stock.install
  3. 1.x tripal_stock/tripal_stock.install

Install the tripal stock module including it's content type

File

tripal_stock/tripal_stock.install
View source
  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_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. // add some controlled vocabularies
  46. tripal_stock_add_cvs();
  47. tripal_stock_add_cvterms();
  48. // set the default vocabularies
  49. tripal_set_default_cv('stock', 'type_id', 'stock_type');
  50. tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  51. tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  52. // add the materialized view
  53. tripal_stock_add_organism_count_mview();
  54. }
  55. /**
  56. * Implementation of hook_uninstall().
  57. *
  58. * @ingroup tripal_stock
  59. */
  60. function tripal_stock_uninstall() {
  61. }
  62. /**
  63. * Implementation of hook_schema().
  64. *
  65. * @ingroup tripal_stock
  66. */
  67. function tripal_stock_schema() {
  68. $schema['chado_stock'] = array(
  69. 'fields' => array(
  70. 'vid' => array(
  71. 'type' => 'int',
  72. 'unsigned' => TRUE,
  73. 'not null' => TRUE,
  74. ),
  75. 'nid' => array(
  76. 'type' => 'int',
  77. 'unsigned' => TRUE,
  78. 'not null' => TRUE,
  79. ),
  80. 'stock_id' => array(
  81. 'type' => 'int',
  82. 'unsigned' => TRUE,
  83. 'not null' => TRUE,
  84. ),
  85. ),
  86. 'indexes' => array(
  87. 'stock_id' => array('stock_id'),
  88. 'nid' => array('nid'),
  89. ),
  90. 'unique' => array(
  91. 'stock_id' => array('stock_id'),
  92. ),
  93. 'primary key' => array('vid'),
  94. );
  95. return $schema;
  96. }
  97. /**
  98. * Creates a materialized view that stores the type & number of stocks per organism
  99. *
  100. * @ingroup tripal_stock
  101. */
  102. function tripal_stock_add_organism_count_mview() {
  103. $view_name = 'organism_stock_count';
  104. $comment = 'Stores the type and number of stocks per organism';
  105. $schema = array(
  106. 'description' => $comment,
  107. 'table' => $view_name,
  108. 'fields' => array(
  109. 'organism_id' => array(
  110. 'size' => 'big',
  111. 'type' => 'int',
  112. 'not null' => TRUE,
  113. ),
  114. 'genus' => array(
  115. 'type' => 'varchar',
  116. 'length' => '255',
  117. 'not null' => TRUE,
  118. ),
  119. 'species' => array(
  120. 'type' => 'varchar',
  121. 'length' => '255',
  122. 'not null' => TRUE,
  123. ),
  124. 'common_name' => array(
  125. 'type' => 'varchar',
  126. 'length' => '255',
  127. 'not null' => FALSE,
  128. ),
  129. 'num_stocks' => array(
  130. 'type' => 'int',
  131. 'not null' => TRUE,
  132. ),
  133. 'cvterm_id' => array(
  134. 'size' => 'big',
  135. 'type' => 'int',
  136. 'not null' => TRUE,
  137. ),
  138. 'stock_type' => array(
  139. 'type' => 'varchar',
  140. 'length' => '255',
  141. 'not null' => TRUE,
  142. ),
  143. ),
  144. 'indexes' => array(
  145. 'organism_stock_count_idx1' => array('organism_id'),
  146. 'organism_stock_count_idx2' => array('cvterm_id'),
  147. 'organism_stock_count_idx3' => array('stock_type'),
  148. ),
  149. );
  150. $sql = "
  151. SELECT
  152. O.organism_id, O.genus, O.species, O.common_name,
  153. count(S.stock_id) as num_stocks,
  154. CVT.cvterm_id, CVT.name as stock_type
  155. FROM organism O
  156. INNER JOIN stock S ON O.Organism_id = S.organism_id
  157. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  158. GROUP BY
  159. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  160. ";
  161. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  162. }
  163. /**
  164. * Add cvs related to publications
  165. *
  166. * @ingroup tripal_pub
  167. */
  168. function tripal_stock_add_cvs() {
  169. // Add cv for relationship types
  170. tripal_insert_cv(
  171. 'stock_relationship',
  172. 'Contains types of relationships between stocks.'
  173. );
  174. tripal_insert_cv(
  175. 'stock_property',
  176. 'Contains properties for stocks.'
  177. );
  178. tripal_insert_cv(
  179. 'stock_type',
  180. 'Contains a list of types for stocks.'
  181. );
  182. }
  183. /**
  184. * Add cvterms related to publications
  185. *
  186. * @ingroup tripal_pub
  187. */
  188. function tripal_stock_add_cvterms() {
  189. }
  190. /**
  191. * This is the required update for tripal_stock when upgrading from Drupal core API 6.x.
  192. *
  193. */
  194. function tripal_stock_update_7200() {
  195. // Make sure we have the full API loaded this will help during a
  196. // site upgrade when the tripal_core module is disabled.
  197. module_load_include('module', 'tripal_core', 'tripal_core');
  198. tripal_core_import_api();
  199. module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');
  200. // add the stock_relationshp CV
  201. try {
  202. // First we add the cv.
  203. // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
  204. $cv = tripal_insert_cv(
  205. 'stock_relationship',
  206. 'Contains types of relationships between stocks.'
  207. );
  208. if ($cv) {
  209. $cv_id = $cv->cv_id;
  210. // for backwards compatibility, get the previously set stock relationship CV, otherwise
  211. // use the new stock_relationship CV we just added
  212. $default_stockrel_cv = variable_get('chado_stock_relationship_cv', $cv_id);
  213. // Set as Default CV for stock relationship types.
  214. $is_set = tripal_get_default_cv('stock_relationship', 'type_id');
  215. if (!$is_set) {
  216. tripal_set_default_cv('stock_relationship','type_id', 'stock_relationship', $default_stockrel_cv);
  217. }
  218. }
  219. }
  220. catch (\PDOException $e) {
  221. $error = $e->getMessage();
  222. throw new DrupalUpdateException('Failed to add stock_relationship vocabulary: '. $error);
  223. }
  224. // add the stock_property CV
  225. try {
  226. // First we add the cv.
  227. // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
  228. $cv = tripal_insert_cv(
  229. 'stock_property',
  230. 'Contains properties for stocks.'
  231. );
  232. if ($cv) {
  233. $cv_id = $cv->cv_id;
  234. // for backwards compatibility, get the previously set stock property CV, otherwise
  235. // use the new stock_property CV we just added
  236. $default_stockprop_cv = variable_get('chado_stock_prop_types_cv', $cv_id);
  237. // Set as Default CV for stock properties.
  238. $is_set = tripal_get_default_cv('stockprop', 'type_id');
  239. if (!$is_set) {
  240. tripal_set_default_cv('stockprop','type_id', 'stock_property', $default_stockprop_cv);
  241. }
  242. }
  243. }
  244. catch (\PDOException $e) {
  245. $error = $e->getMessage();
  246. throw new DrupalUpdateException('Failed to add stock_property vocabulary: '. $error);
  247. }
  248. // add the stock_type CV
  249. try {
  250. // First we add the cv.
  251. // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
  252. $cv = tripal_insert_cv(
  253. 'stock_type',
  254. 'Contains a list of types for stocks.'
  255. );
  256. if ($cv) {
  257. $cv_id = $cv->cv_id;
  258. // for backwards compatibility, get the previously set stock types CV, otherwise
  259. // use the new stock_type CV we just added
  260. $default_stocktype_cv = variable_get('chado_stock_types_cv', $cv_id);
  261. // Set as Default CV for stock types.
  262. $is_set = tripal_get_default_cv('stock', 'type_id');
  263. if (!$is_set) {
  264. tripal_set_default_cv('stock','type_id', 'stock_type', $default_stocktype_cv);
  265. }
  266. }
  267. }
  268. catch (\PDOException $e) {
  269. $error = $e->getMessage();
  270. throw new DrupalUpdateException('Failed to add stock_type vocabulary: '. $error);
  271. }
  272. }
  273. /**
  274. * Add materialized views
  275. */
  276. function tripal_stock_update_7201() {
  277. // Make sure we have the full API loaded this will help during a
  278. // site upgrade when the tripal_core module is disabled.
  279. module_load_include('module', 'tripal_core', 'tripal_core');
  280. tripal_core_import_api();
  281. // add the materialized view
  282. tripal_stock_add_organism_count_mview();
  283. }
  284. /**
  285. * Implementation of hook_update_dependencies().
  286. *
  287. * It specifies a list of other modules whose updates must be run prior to
  288. * this one. It also ensures the the Tripal API is in scope for site
  289. * upgrades when tripal_core is disabled.
  290. */
  291. function tripal_stock_update_dependencies() {
  292. $dependencies = array();
  293. // the tripal_cv update 7200 must run prior to update 7200 of this module
  294. $dependencies['tripal_stock'][7200] = array(
  295. 'tripal_cv' => 7200
  296. );
  297. return $dependencies;
  298. }