tripal_stock.install 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. // create the module's data directory
  46. tripal_create_files_dir('tripal_stock');
  47. // add some controlled vocabularies
  48. tripal_stock_add_cvs();
  49. tripal_stock_add_cvterms();
  50. // set the default vocabularies
  51. tripal_set_default_cv('stock', 'type_id', 'stock_type');
  52. tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  53. tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  54. // add the materialized view
  55. tripal_stock_add_organism_count_mview();
  56. }
  57. /**
  58. * Implementation of hook_uninstall().
  59. *
  60. * @ingroup tripal_stock
  61. */
  62. function tripal_stock_uninstall() {
  63. }
  64. /**
  65. * Implementation of hook_schema().
  66. *
  67. * @ingroup tripal_stock
  68. */
  69. function tripal_stock_schema() {
  70. $schema['chado_stock'] = array(
  71. 'fields' => array(
  72. 'vid' => array(
  73. 'type' => 'int',
  74. 'unsigned' => TRUE,
  75. 'not null' => TRUE,
  76. ),
  77. 'nid' => array(
  78. 'type' => 'int',
  79. 'unsigned' => TRUE,
  80. 'not null' => TRUE,
  81. ),
  82. 'stock_id' => array(
  83. 'type' => 'int',
  84. 'unsigned' => TRUE,
  85. 'not null' => TRUE,
  86. ),
  87. ),
  88. 'indexes' => array(
  89. 'stock_id' => array('stock_id'),
  90. 'nid' => array('nid'),
  91. ),
  92. 'unique' => array(
  93. 'stock_id' => array('stock_id'),
  94. ),
  95. 'primary key' => array('vid'),
  96. );
  97. return $schema;
  98. }
  99. /**
  100. * Creates a materialized view that stores the type & number of stocks per organism
  101. *
  102. * @ingroup tripal_stock
  103. */
  104. function tripal_stock_add_organism_count_mview() {
  105. $view_name = 'organism_stock_count';
  106. $comment = 'Stores the type and number of stocks per organism';
  107. $schema = array(
  108. 'description' => $comment,
  109. 'table' => $view_name,
  110. 'fields' => array(
  111. 'organism_id' => array(
  112. 'type' => 'int',
  113. 'not null' => TRUE,
  114. ),
  115. 'genus' => array(
  116. 'type' => 'varchar',
  117. 'length' => '255',
  118. 'not null' => TRUE,
  119. ),
  120. 'species' => array(
  121. 'type' => 'varchar',
  122. 'length' => '255',
  123. 'not null' => TRUE,
  124. ),
  125. 'common_name' => array(
  126. 'type' => 'varchar',
  127. 'length' => '255',
  128. 'not null' => FALSE,
  129. ),
  130. 'num_stocks' => array(
  131. 'type' => 'int',
  132. 'not null' => TRUE,
  133. ),
  134. 'cvterm_id' => array(
  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. // add the new CVs. We can't use the Tripal API because during
  196. // an upgrade from D6 to D7 Tripal is disable. So, we have to manually add these
  197. // new vocabularies.
  198. // add the stock_relationshp CV
  199. try {
  200. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_relationship'")->fetchField();
  201. if (!$cv_id) {
  202. // add the vocabulary
  203. $cv_id = db_insert('chado.cv')
  204. ->fields(array(
  205. 'name' => 'stock_relationship',
  206. 'definition' => 'Contains types of relationships between stocks.'
  207. ))
  208. ->execute();
  209. }
  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. db_insert('tripal_cv_defaults')
  214. ->fields(array(
  215. 'table_name' => 'stock_relationship',
  216. 'field_name' => 'type_id',
  217. 'cv_id' => $default_stockrel_cv
  218. ))
  219. ->execute();
  220. }
  221. catch (\PDOException $e) {
  222. $error = $e->getMessage();
  223. throw new DrupalUpdateException('Failed to add stock_relationship vocabulary: '. $error);
  224. }
  225. // add the stock_property CV
  226. try {
  227. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_property'")->fetchField();
  228. if (!$cv_id) {
  229. // add the vocabulary
  230. $cv_id = db_insert('chado.cv')
  231. ->fields(array(
  232. 'name' => 'stock_property',
  233. 'definition' => 'Contains properties for stocks.'
  234. ))
  235. ->execute();
  236. }
  237. // for backwards compatibility, get the previously set stock property CV, otherwise
  238. // use the new stock_property CV we just added
  239. $default_stockprop_cv = variable_get('chado_stock_prop_types_cv', $cv_id);
  240. db_insert('tripal_cv_defaults')
  241. ->fields(array(
  242. 'table_name' => 'stockprop',
  243. 'field_name' => 'type_id',
  244. 'cv_id' => $default_stockprop_cv
  245. ))
  246. ->execute();
  247. }
  248. catch (\PDOException $e) {
  249. $error = $e->getMessage();
  250. throw new DrupalUpdateException('Failed to add stock_property vocabulary: '. $error);
  251. }
  252. // add the stock_type CV
  253. try {
  254. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_type'")->fetchField();
  255. if (!$cv_id) {
  256. // add the vocabulary
  257. $cv_id = db_insert('chado.cv')
  258. ->fields(array(
  259. 'name' => 'stock_type',
  260. 'definition' => 'Contains a list of types for stocks.'
  261. ))
  262. ->execute();
  263. }
  264. // for backwards compatibility, get the previously set stock types CV, otherwise
  265. // use the new stock_type CV we just added
  266. $default_stocktype_cv = variable_get('chado_stock_types_cv', $cv_id);
  267. db_insert('tripal_cv_defaults')
  268. ->fields(array(
  269. 'table_name' => 'stock',
  270. 'field_name' => 'type_id',
  271. 'cv_id' => $default_stocktype_cv
  272. ))
  273. ->execute();
  274. }
  275. catch (\PDOException $e) {
  276. $error = $e->getMessage();
  277. throw new DrupalUpdateException('Failed to add stock_type vocabulary: '. $error);
  278. }
  279. }
  280. /**
  281. * Add materialized views
  282. */
  283. function tripal_stock_update_7201() {
  284. // add the materialized view
  285. tripal_stock_add_organism_count_mview();
  286. }
  287. /**
  288. * Implementation of hook_update_dependencies(). It specifies a list of
  289. * other modules whose updates must be run prior to this one.
  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. }