tripal_project.install 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. tripal_project_add_cvs();
  46. tripal_project_add_cvterms();
  47. // set the default vocabularies
  48. tripal_set_default_cv('projectprop', 'type_id', 'project_property');
  49. tripal_set_default_cv('project_relationship', 'type_id', 'project_relationship');
  50. }
  51. /**
  52. * Implementation of hook_uninstall().
  53. *
  54. * @ingroup tripal_project
  55. */
  56. function tripal_project_uninstall() {
  57. }
  58. /**
  59. * Implementation of hook_schema().
  60. *
  61. * @ingroup tripal_project
  62. */
  63. function tripal_project_schema() {
  64. $schema['chado_project'] = array(
  65. 'fields' => array(
  66. 'nid' => array(
  67. 'type' => 'int',
  68. 'unsigned' => TRUE,
  69. 'not null' => TRUE,
  70. ),
  71. 'vid' => array(
  72. 'type' => 'int',
  73. 'not null' => TRUE,
  74. ),
  75. 'project_id' => array(
  76. 'type' => 'int',
  77. 'unsigned' => TRUE,
  78. 'not null' => TRUE,
  79. ),
  80. ),
  81. 'primary key' => array('nid', 'vid', 'project_id'),
  82. );
  83. return $schema;
  84. }
  85. /**
  86. * Add cvs pertaining to projects
  87. *
  88. * @ingroup tripal_project
  89. */
  90. function tripal_project_add_cvs() {
  91. // Add the cv for project properties
  92. tripal_insert_cv(
  93. 'project_property',
  94. 'Contains properties for projects'
  95. );
  96. // Add cv for relationship types
  97. tripal_insert_cv(
  98. 'project_relationship',
  99. 'Contains Types of relationships between projects.'
  100. );
  101. }
  102. /**
  103. * Add cvterms pertaining to projects
  104. *
  105. * @ingroup tripal_project
  106. */
  107. function tripal_project_add_cvterms() {
  108. // Insert cvterm 'Project Description' into cvterm table of chado
  109. // database. This CV term is used to keep track of the project
  110. // description in the projectprop table.
  111. tripal_insert_cvterm(
  112. array(
  113. 'name' => 'Project Description',
  114. 'definition' => 'Description of a project',
  115. 'cv_name' => 'project_property',
  116. 'is_relationship' => 0,
  117. 'db_name' => 'tripal'
  118. ),
  119. array('update_existing' => TRUE)
  120. );
  121. }
  122. /**
  123. * This is the required update for tripal_project when upgrading from Drupal core API 6.x.
  124. *
  125. */
  126. function tripal_project_update_7200() {
  127. // Make sure we have the full API loaded this will help during a
  128. // site upgrade when the tripal_core module is disabled.
  129. module_load_include('module', 'tripal_core', 'tripal_core');
  130. tripal_core_import_api();
  131. // add the project_property CV
  132. try {
  133. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_property'")->fetchField();
  134. if (!$cv_id) {
  135. // add the vocabulary
  136. $cv_id = db_insert('chado.cv')
  137. ->fields(array(
  138. 'name' => 'project_property',
  139. 'definition' => 'Contains properties for projects.'
  140. ))
  141. ->execute();
  142. }
  143. // use the new project_property CV we just added
  144. $cdi = db_select('tripal_cv_defaults', 't')
  145. ->fields('t', array('cv_default_id'))
  146. ->condition('table_name', 'projectprop')
  147. ->condition('field_name', 'type_id')
  148. ->execute()
  149. ->fetchField();
  150. if (!$cdi) {
  151. db_insert('tripal_cv_defaults')
  152. ->fields(array(
  153. 'table_name' => 'projectprop',
  154. 'field_name' => 'type_id',
  155. 'cv_id' => $cv_id
  156. ))
  157. ->execute();
  158. }
  159. }
  160. catch (\PDOException $e) {
  161. $error = $e->getMessage();
  162. throw new DrupalUpdateException('Failed to add project_property vocabulary: '. $error);
  163. }
  164. // add the project_relationship CV
  165. try {
  166. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_relationship'")->fetchField();
  167. if (!$cv_id) {
  168. // add the vocabulary
  169. $cv_id = db_insert('chado.cv')
  170. ->fields(array(
  171. 'name' => 'project_relationship',
  172. 'definition' => 'Contains types of relationships between projects.'
  173. ))
  174. ->execute();
  175. }
  176. // use the new project_property CV we just added
  177. $cdi = db_select('tripal_cv_defaults', 't')
  178. ->fields('t', array('cv_default_id'))
  179. ->condition('table_name', 'project_relationship')
  180. ->condition('field_name', 'type_id')
  181. ->execute()
  182. ->fetchField();
  183. if (!$cdi) {
  184. db_insert('tripal_cv_defaults')
  185. ->fields(array(
  186. 'table_name' => 'project_relationship',
  187. 'field_name' => 'type_id',
  188. 'cv_id' => $cv_id
  189. ))
  190. ->execute();
  191. }
  192. }
  193. catch (\PDOException $e) {
  194. $error = $e->getMessage();
  195. throw new DrupalUpdateException('Failed to add project_relationship vocabulary: '. $error);
  196. }
  197. // For Tripal in Drupal 6 the project_description cvterm was stored in the
  198. // 'tripal' CV. It should be stored in the new project_property CV that
  199. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  200. // reset the CV ID for that term and rename the term to 'Project Description'
  201. // We cannot use the Tripal API calls'because during upgrade the tripal_core
  202. // should also be disabled
  203. $sql = "
  204. UPDATE chado.cvterm CVT
  205. SET
  206. name = 'Project Description',
  207. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'project_property')
  208. WHERE
  209. name = 'project_description' AND
  210. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
  211. ";
  212. try {
  213. db_query($sql);
  214. }
  215. catch (\PDOException $e) {
  216. $error = $e->getMessage();
  217. throw new DrupalUpdateException('Failed to change project_description property type to the project_property CV and update the name: '. $error);
  218. }
  219. }
  220. /**
  221. * Implementation of hook_update_dependencies().
  222. *
  223. * It specifies a list of other modules whose updates must be run prior to
  224. * this one. It also ensures the the Tripal API is in scope for site
  225. * upgrades when tripal_core is disabled.
  226. */
  227. function tripal_project_update_dependencies() {
  228. $dependencies = array();
  229. // the tripal_cv update 7200 must run prior to update 7200 of this module
  230. $dependencies['tripal_project'][7200] = array(
  231. 'tripal_cv' => 7200
  232. );
  233. return $dependencies;
  234. }