tripal_stock.install 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. 'type' => 'int',
  111. 'not null' => TRUE,
  112. ),
  113. 'genus' => array(
  114. 'type' => 'varchar',
  115. 'length' => '255',
  116. 'not null' => TRUE,
  117. ),
  118. 'species' => array(
  119. 'type' => 'varchar',
  120. 'length' => '255',
  121. 'not null' => TRUE,
  122. ),
  123. 'common_name' => array(
  124. 'type' => 'varchar',
  125. 'length' => '255',
  126. 'not null' => FALSE,
  127. ),
  128. 'num_stocks' => array(
  129. 'type' => 'int',
  130. 'not null' => TRUE,
  131. ),
  132. 'cvterm_id' => array(
  133. 'type' => 'int',
  134. 'not null' => TRUE,
  135. ),
  136. 'stock_type' => array(
  137. 'type' => 'varchar',
  138. 'length' => '255',
  139. 'not null' => TRUE,
  140. ),
  141. ),
  142. 'indexes' => array(
  143. 'organism_stock_count_idx1' => array('organism_id'),
  144. 'organism_stock_count_idx2' => array('cvterm_id'),
  145. 'organism_stock_count_idx3' => array('stock_type'),
  146. ),
  147. );
  148. $sql = "
  149. SELECT
  150. O.organism_id, O.genus, O.species, O.common_name,
  151. count(S.stock_id) as num_stocks,
  152. CVT.cvterm_id, CVT.name as stock_type
  153. FROM organism O
  154. INNER JOIN stock S ON O.Organism_id = S.organism_id
  155. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  156. GROUP BY
  157. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  158. ";
  159. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  160. }
  161. /**
  162. * Add cvs related to publications
  163. *
  164. * @ingroup tripal_pub
  165. */
  166. function tripal_stock_add_cvs() {
  167. // Add cv for relationship types
  168. tripal_insert_cv(
  169. 'stock_relationship',
  170. 'Contains types of relationships between stocks.'
  171. );
  172. tripal_insert_cv(
  173. 'stock_property',
  174. 'Contains properties for stocks.'
  175. );
  176. tripal_insert_cv(
  177. 'stock_type',
  178. 'Contains a list of types for stocks.'
  179. );
  180. }
  181. /**
  182. * Add cvterms related to publications
  183. *
  184. * @ingroup tripal_pub
  185. */
  186. function tripal_stock_add_cvterms() {
  187. }
  188. /**
  189. * This is the required update for tripal_stock when upgrading from Drupal core API 6.x.
  190. *
  191. */
  192. function tripal_stock_update_7200() {
  193. // Make sure we have the full API loaded this will help during a
  194. // site upgrade when the tripal_core module is disabled.
  195. module_load_include('module', 'tripal_core', 'tripal_core');
  196. tripal_core_import_api();
  197. // add the stock_relationshp CV
  198. try {
  199. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_relationship'")->fetchField();
  200. if (!$cv_id) {
  201. // add the vocabulary
  202. $cv_id = db_insert('chado.cv')
  203. ->fields(array(
  204. 'name' => 'stock_relationship',
  205. 'definition' => 'Contains types of relationships between stocks.'
  206. ))
  207. ->execute();
  208. }
  209. // for backwards compatibility, get the previously set stock relationship CV, otherwise
  210. // use the new stock_relationship CV we just added
  211. $default_stockrel_cv = variable_get('chado_stock_relationship_cv', $cv_id);
  212. $cdi = db_select('tripal_cv_defaults', 't')
  213. ->fields('t', array('cv_default_id'))
  214. ->condition('table_name', 'stock_relationship')
  215. ->condition('field_name', 'type_id')
  216. ->execute()
  217. ->fetchField();
  218. if (!$cdi) {
  219. db_insert('tripal_cv_defaults')
  220. ->fields(array(
  221. 'table_name' => 'stock_relationship',
  222. 'field_name' => 'type_id',
  223. 'cv_id' => $default_stockrel_cv
  224. ))
  225. ->execute();
  226. }
  227. }
  228. catch (\PDOException $e) {
  229. $error = $e->getMessage();
  230. throw new DrupalUpdateException('Failed to add stock_relationship vocabulary: '. $error);
  231. }
  232. // add the stock_property CV
  233. try {
  234. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_property'")->fetchField();
  235. if (!$cv_id) {
  236. // add the vocabulary
  237. $cv_id = db_insert('chado.cv')
  238. ->fields(array(
  239. 'name' => 'stock_property',
  240. 'definition' => 'Contains properties for stocks.'
  241. ))
  242. ->execute();
  243. }
  244. // for backwards compatibility, get the previously set stock property CV, otherwise
  245. // use the new stock_property CV we just added
  246. $default_stockprop_cv = variable_get('chado_stock_prop_types_cv', $cv_id);
  247. $cdi = db_select('tripal_cv_defaults', 't')
  248. ->fields('t', array('cv_default_id'))
  249. ->condition('table_name', 'stockprop')
  250. ->condition('field_name', 'type_id')
  251. ->execute()
  252. ->fetchField();
  253. if (!$cdi) {
  254. db_insert('tripal_cv_defaults')
  255. ->fields(array(
  256. 'table_name' => 'stockprop',
  257. 'field_name' => 'type_id',
  258. 'cv_id' => $default_stockprop_cv
  259. ))
  260. ->execute();
  261. }
  262. }
  263. catch (\PDOException $e) {
  264. $error = $e->getMessage();
  265. throw new DrupalUpdateException('Failed to add stock_property vocabulary: '. $error);
  266. }
  267. // add the stock_type CV
  268. try {
  269. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_type'")->fetchField();
  270. if (!$cv_id) {
  271. // add the vocabulary
  272. $cv_id = db_insert('chado.cv')
  273. ->fields(array(
  274. 'name' => 'stock_type',
  275. 'definition' => 'Contains a list of types for stocks.'
  276. ))
  277. ->execute();
  278. }
  279. // for backwards compatibility, get the previously set stock types CV, otherwise
  280. // use the new stock_type CV we just added
  281. $default_stocktype_cv = variable_get('chado_stock_types_cv', $cv_id);
  282. $cdi = db_select('tripal_cv_defaults', 't')
  283. ->fields('t', array('cv_default_id'))
  284. ->condition('table_name', 'stock')
  285. ->condition('field_name', 'type_id')
  286. ->execute()
  287. ->fetchField();
  288. if (!$cdi) {
  289. db_insert('tripal_cv_defaults')
  290. ->fields(array(
  291. 'table_name' => 'stock',
  292. 'field_name' => 'type_id',
  293. 'cv_id' => $default_stocktype_cv
  294. ))
  295. ->execute();
  296. }
  297. }
  298. catch (\PDOException $e) {
  299. $error = $e->getMessage();
  300. throw new DrupalUpdateException('Failed to add stock_type vocabulary: '. $error);
  301. }
  302. }
  303. /**
  304. * Add materialized views
  305. */
  306. function tripal_stock_update_7201() {
  307. // Make sure we have the full API loaded this will help during a
  308. // site upgrade when the tripal_core module is disabled.
  309. module_load_include('module', 'tripal_core', 'tripal_core');
  310. tripal_core_import_api();
  311. // add the materialized view
  312. tripal_stock_add_organism_count_mview();
  313. }
  314. /**
  315. * Implementation of hook_update_dependencies().
  316. *
  317. * It specifies a list of other modules whose updates must be run prior to
  318. * this one. It also ensures the the Tripal API is in scope for site
  319. * upgrades when tripal_core is disabled.
  320. */
  321. function tripal_stock_update_dependencies() {
  322. $dependencies = array();
  323. // the tripal_cv update 7200 must run prior to update 7200 of this module
  324. $dependencies['tripal_stock'][7200] = array(
  325. 'tripal_cv' => 7200
  326. );
  327. return $dependencies;
  328. }