tripal_project.install 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * @file
  4. * Install the tripal project
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_project
  11. */
  12. function tripal_project_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_project.views_default.inc");
  15. $views = tripal_project_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_project
  24. */
  25. function tripal_project_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_project'] = array(
  31. 'title' => "tripal_project",
  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_project
  43. */
  44. function tripal_project_install() {
  45. // create the module's data directory
  46. tripal_create_files_dir('tripal_project');
  47. tripal_project_add_cvs();
  48. tripal_project_add_cvterms();
  49. // set the default vocabularies
  50. tripal_set_default_cv('projectprop', 'type_id', 'project_property');
  51. tripal_set_default_cv('project_relationship', 'type_id', 'project_relationship');
  52. }
  53. /**
  54. * Implementation of hook_uninstall().
  55. *
  56. * @ingroup tripal_project
  57. */
  58. function tripal_project_uninstall() {
  59. }
  60. /**
  61. * Implementation of hook_schema().
  62. *
  63. * @ingroup tripal_project
  64. */
  65. function tripal_project_schema() {
  66. $schema['chado_project'] = array(
  67. 'fields' => array(
  68. 'nid' => array(
  69. 'type' => 'int',
  70. 'unsigned' => TRUE,
  71. 'not null' => TRUE,
  72. ),
  73. 'vid' => array(
  74. 'type' => 'int',
  75. 'not null' => TRUE,
  76. ),
  77. 'project_id' => array(
  78. 'type' => 'int',
  79. 'unsigned' => TRUE,
  80. 'not null' => TRUE,
  81. ),
  82. ),
  83. 'primary key' => array('nid', 'vid', 'project_id'),
  84. );
  85. return $schema;
  86. }
  87. /**
  88. * Add cvs pertaining to projects
  89. *
  90. * @ingroup tripal_project
  91. */
  92. function tripal_project_add_cvs() {
  93. // Add the cv for project properties
  94. tripal_insert_cv(
  95. 'project_property',
  96. 'Contains properties for projects'
  97. );
  98. // Add cv for relationship types
  99. tripal_insert_cv(
  100. 'project_relationship',
  101. 'Contains Types of relationships between projects.'
  102. );
  103. }
  104. /**
  105. * Add cvterms pertaining to projects
  106. *
  107. * @ingroup tripal_project
  108. */
  109. function tripal_project_add_cvterms() {
  110. // Insert cvterm 'Project Description' into cvterm table of chado
  111. // database. This CV term is used to keep track of the project
  112. // description in the projectprop table.
  113. tripal_insert_cvterm(
  114. array(
  115. 'name' => 'Project Description',
  116. 'definition' => 'Description of a project',
  117. 'cv_name' => 'project_property',
  118. 'is_relationship' => 0,
  119. 'db_name' => 'tripal'
  120. ),
  121. array('update_existing' => TRUE)
  122. );
  123. }
  124. /**
  125. * This is the required update for tripal_project when upgrading from Drupal core API 6.x.
  126. *
  127. */
  128. function tripal_project_update_7200() {
  129. // add the project_property CV
  130. try {
  131. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_property'")->fetchField();
  132. if (!$cv_id) {
  133. // add the vocabulary
  134. $cv_id = db_insert('chado.cv')
  135. ->fields(array(
  136. 'name' => 'project_property',
  137. 'definition' => 'Contains properties for projects.'
  138. ))
  139. ->execute();
  140. }
  141. // use the new project_property CV we just added
  142. db_insert('tripal_cv_defaults')
  143. ->fields(array(
  144. 'table_name' => 'projectprop',
  145. 'field_name' => 'type_id',
  146. 'cv_id' => $cv_id
  147. ))
  148. ->execute();
  149. }
  150. catch (\PDOException $e) {
  151. $error = $e->getMessage();
  152. throw new DrupalUpdateException('Failed to add project_property vocabulary: '. $error);
  153. }
  154. // add the project_relationship CV
  155. try {
  156. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_relationship'")->fetchField();
  157. if (!$cv_id) {
  158. // add the vocabulary
  159. $cv_id = db_insert('chado.cv')
  160. ->fields(array(
  161. 'name' => 'project_relationship',
  162. 'definition' => 'Contains types of relationships between projects.'
  163. ))
  164. ->execute();
  165. }
  166. // use the new project_property CV we just added
  167. db_insert('tripal_cv_defaults')
  168. ->fields(array(
  169. 'table_name' => 'project_relationship',
  170. 'field_name' => 'type_id',
  171. 'cv_id' => $cv_id
  172. ))
  173. ->execute();
  174. }
  175. catch (\PDOException $e) {
  176. $error = $e->getMessage();
  177. throw new DrupalUpdateException('Failed to add project_relationship vocabulary: '. $error);
  178. }
  179. // For Tripal in Drupal 6 the project_description cvterm was stored in the
  180. // 'tripal' CV. It should be stored in the new project_property CV that
  181. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  182. // reset the CV ID for that term and rename the term to 'Project Description'
  183. // We cannot use the Tripal API calls'because during upgrade the tripal_core
  184. // should also be disabled
  185. $sql = "
  186. UPDATE chado.cvterm CVT
  187. SET
  188. name = 'Project Description',
  189. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'project_property')
  190. WHERE
  191. name = 'project_description' AND
  192. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
  193. ";
  194. try {
  195. db_query($sql);
  196. }
  197. catch (\PDOException $e) {
  198. $error = $e->getMessage();
  199. throw new DrupalUpdateException('Failed to change project_description property type to the project_property CV and update the name: '. $error);
  200. }
  201. }
  202. /**
  203. * Implementation of hook_update_dependencies(). It specifies a list of
  204. * other modules whose updates must be run prior to this one.
  205. */
  206. function tripal_project_update_dependencies() {
  207. $dependencies = array();
  208. // the tripal_cv update 7200 must run prior to update 7200 of this module
  209. $dependencies['tripal_project'][7200] = array(
  210. 'tripal_cv' => 7200
  211. );
  212. return $dependencies;
  213. }