tripal_stock.install 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // add the new CVs. We can't use the Tripal API because during
  194. // an upgrade from D6 to D7 Tripal is disable. So, we have to manually add these
  195. // new vocabularies.
  196. // add the stock_relationshp CV
  197. try {
  198. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_relationship'")->fetchField();
  199. if (!$cv_id) {
  200. // add the vocabulary
  201. $cv_id = db_insert('chado.cv')
  202. ->fields(array(
  203. 'name' => 'stock_relationship',
  204. 'definition' => 'Contains types of relationships between stocks.'
  205. ))
  206. ->execute();
  207. }
  208. // for backwards compatibility, get the previously set stock relationship CV, otherwise
  209. // use the new stock_relationship CV we just added
  210. $default_stockrel_cv = variable_get('chado_stock_relationship_cv', $cv_id);
  211. db_insert('tripal_cv_defaults')
  212. ->fields(array(
  213. 'table_name' => 'stock_relationship',
  214. 'field_name' => 'type_id',
  215. 'cv_id' => $default_stockrel_cv
  216. ))
  217. ->execute();
  218. }
  219. catch (\PDOException $e) {
  220. $error = $e->getMessage();
  221. throw new DrupalUpdateException('Failed to add stock_relationship vocabulary: '. $error);
  222. }
  223. // add the stock_property CV
  224. try {
  225. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_property'")->fetchField();
  226. if (!$cv_id) {
  227. // add the vocabulary
  228. $cv_id = db_insert('chado.cv')
  229. ->fields(array(
  230. 'name' => 'stock_property',
  231. 'definition' => 'Contains properties for stocks.'
  232. ))
  233. ->execute();
  234. }
  235. // for backwards compatibility, get the previously set stock property CV, otherwise
  236. // use the new stock_property CV we just added
  237. $default_stockprop_cv = variable_get('chado_stock_prop_types_cv', $cv_id);
  238. db_insert('tripal_cv_defaults')
  239. ->fields(array(
  240. 'table_name' => 'stockprop',
  241. 'field_name' => 'type_id',
  242. 'cv_id' => $default_stockprop_cv
  243. ))
  244. ->execute();
  245. }
  246. catch (\PDOException $e) {
  247. $error = $e->getMessage();
  248. throw new DrupalUpdateException('Failed to add stock_property vocabulary: '. $error);
  249. }
  250. // add the stock_type CV
  251. try {
  252. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_type'")->fetchField();
  253. if (!$cv_id) {
  254. // add the vocabulary
  255. $cv_id = db_insert('chado.cv')
  256. ->fields(array(
  257. 'name' => 'stock_type',
  258. 'definition' => 'Contains a list of types for stocks.'
  259. ))
  260. ->execute();
  261. }
  262. // for backwards compatibility, get the previously set stock types CV, otherwise
  263. // use the new stock_type CV we just added
  264. $default_stocktype_cv = variable_get('chado_stock_types_cv', $cv_id);
  265. db_insert('tripal_cv_defaults')
  266. ->fields(array(
  267. 'table_name' => 'stock',
  268. 'field_name' => 'type_id',
  269. 'cv_id' => $default_stocktype_cv
  270. ))
  271. ->execute();
  272. }
  273. catch (\PDOException $e) {
  274. $error = $e->getMessage();
  275. throw new DrupalUpdateException('Failed to add stock_type vocabulary: '. $error);
  276. }
  277. }
  278. /**
  279. * Add materialized views
  280. */
  281. function tripal_stock_update_7201() {
  282. // add the materialized view
  283. tripal_stock_add_organism_count_mview();
  284. }
  285. /**
  286. * Implementation of hook_update_dependencies(). It specifies a list of
  287. * other modules whose updates must be run prior to this one.
  288. */
  289. function tripal_stock_update_dependencies() {
  290. $dependencies = array();
  291. // the tripal_cv update 7200 must run prior to update 7200 of this module
  292. $dependencies['tripal_stock'][7200] = array(
  293. 'tripal_cv' => 7200
  294. );
  295. return $dependencies;
  296. }