tripal_stock.install 8.3 KB

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