tripal_stock.install 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. }