tripal_pub.install 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the publication module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_legacy_pub
  11. */
  12. function tripal_pub_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_pub.views_default.inc");
  15. $views = tripal_pub_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_disable_view($view_name, FALSE);
  18. }
  19. }
  20. /**
  21. * Implementation of hook_requirements().
  22. *
  23. * @ingroup tripal_legacy_pub
  24. */
  25. function tripal_pub_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_pub'] = array(
  31. 'title' => "tripal_pub",
  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_legacy_pub
  43. */
  44. function tripal_pub_install() {
  45. global $base_path;
  46. // set the default vocabularies
  47. tripal_set_default_cv('pub', 'type_id', 'tripal_pub');
  48. tripal_set_default_cv('pubprop', 'type_id', 'tripal_pub');
  49. tripal_set_default_cv('pub_relationship', 'type_id', 'pub_relationship');
  50. }
  51. /**
  52. * Implementation of hook_uninstall().
  53. *
  54. * @ingroup tripal_legacy_pub
  55. */
  56. function tripal_pub_uninstall() {
  57. }
  58. /**
  59. * Implements hook_enable().
  60. *
  61. * @ingroup tripal_legacy_pub
  62. */
  63. function tripal_pub_enable() {
  64. }
  65. /**
  66. * Implementation of hook_schema().
  67. *
  68. * @ingroup tripal_legacy_pub
  69. */
  70. function tripal_pub_schema() {
  71. $schema['chado_pub'] = array(
  72. 'fields' => array(
  73. 'vid' => array(
  74. 'type' => 'int',
  75. 'unsigned' => TRUE,
  76. 'not null' => TRUE, 'default' => 0
  77. ),
  78. 'nid' => array(
  79. 'type' => 'int',
  80. 'unsigned' => TRUE,
  81. 'not null' => TRUE,
  82. 'default' => 0
  83. ),
  84. 'pub_id' => array(
  85. 'type' => 'int',
  86. 'not null' => TRUE,
  87. 'default' => 0
  88. ),
  89. 'sync_date' => array(
  90. 'type' => 'int',
  91. 'not null' => FALSE,
  92. 'description' => 'UNIX integer sync date/time'
  93. ),
  94. ),
  95. 'indexes' => array(
  96. 'pub_id' => array('pub_id')
  97. ),
  98. 'unique keys' => array(
  99. 'nid_vid' => array('nid', 'vid'),
  100. 'vid' => array('vid')
  101. ),
  102. 'primary key' => array('nid'),
  103. );
  104. return $schema;
  105. }
  106. /**
  107. * This is the required update for tripal_pub when upgrading from Drupal core API 6.x.
  108. *
  109. */
  110. function tripal_pub_update_7200() {
  111. // add the tripal_pub CV and set it to be the default for pub types and pub properties
  112. try {
  113. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_pub'")->fetchField();
  114. if (!$cv_id) {
  115. // add the vocabulary
  116. $cv_id = db_insert('chado.cv')
  117. ->fields(array(
  118. 'name' => 'tripal_pub',
  119. 'definition' => 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
  120. ))
  121. ->execute();
  122. }
  123. // use the new pub_property CV we just added
  124. db_insert('tripal_cv_defaults')
  125. ->fields(array(
  126. 'table_name' => 'pub',
  127. 'field_name' => 'type_id',
  128. 'cv_id' => $cv_id
  129. ))
  130. ->execute();
  131. // use the new pub_property CV we just added
  132. db_insert('tripal_cv_defaults')
  133. ->fields(array(
  134. 'table_name' => 'pubprop',
  135. 'field_name' => 'type_id',
  136. 'cv_id' => $cv_id
  137. ))
  138. ->execute();
  139. }
  140. catch (\PDOException $e) {
  141. $error = $e->getMessage();
  142. throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
  143. }
  144. // add the pub_property CV
  145. try {
  146. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_property'")->fetchField();
  147. if (!$cv_id) {
  148. // add the vocabulary
  149. $cv_id = db_insert('chado.cv')
  150. ->fields(array(
  151. 'name' => 'pub_property',
  152. 'definition' => 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  153. ))
  154. ->execute();
  155. }
  156. }
  157. catch (\PDOException $e) {
  158. $error = $e->getMessage();
  159. throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
  160. }
  161. // add the pub_type CV
  162. try {
  163. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_type'")->fetchField();
  164. if (!$cv_id) {
  165. // add the vocabulary
  166. $cv_id = db_insert('chado.cv')
  167. ->fields(array(
  168. 'name' => 'pub_type',
  169. 'definition' => 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  170. ))
  171. ->execute();
  172. }
  173. }
  174. catch (\PDOException $e) {
  175. $error = $e->getMessage();
  176. throw new DrupalUpdateException('Failed to add pub_type vocabulary: '. $error);
  177. }
  178. // add the pub_relationship CV
  179. try {
  180. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_relationship'")->fetchField();
  181. if (!$cv_id) {
  182. // add the vocabulary
  183. $cv_id = db_insert('chado.cv')
  184. ->fields(array(
  185. 'name' => 'pub_relationship',
  186. 'definition' => 'Contains types of relationships between publications.'
  187. ))
  188. ->execute();
  189. }
  190. // use the new pub_property CV we just added
  191. db_insert('tripal_cv_defaults')
  192. ->fields(array(
  193. 'table_name' => 'pub_relationship',
  194. 'field_name' => 'type_id',
  195. 'cv_id' => $cv_id
  196. ))
  197. ->execute();
  198. }
  199. catch (\PDOException $e) {
  200. $error = $e->getMessage();
  201. throw new DrupalUpdateException('Failed to add pub_relationship vocabulary: '. $error);
  202. }
  203. }
  204. /**
  205. * Implementation of hook_update_dependencies(). It specifies a list of
  206. * other modules whose updates must be run prior to this one.
  207. */
  208. function tripal_pub_update_dependencies() {
  209. $dependencies = array();
  210. // the tripal_cv update 7200 must run prior to update 7200 of this module
  211. $dependencies['tripal_pub'][7200] = array(
  212. 'tripal_cv' => 7200
  213. );
  214. return $dependencies;
  215. }
  216. /**
  217. * Adds missing foreign key constraints
  218. *
  219. */
  220. function tripal_pub_update_7201() {
  221. // there was a bug in the function for creating a custom table that
  222. // kept foreign key constraints from being added. So, we need to add those
  223. // to keep from error messages appear, we will drop the FK if it already
  224. // exists and then re-add it.
  225. try {
  226. $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'pubauthor_contact_pubauthor_id_fkey'))->fetchField();
  227. if ($fkey_exists) {
  228. db_query('
  229. ALTER TABLE chado.pubauthor_contact
  230. DROP CONSTRAINT pubauthor_contact_pubauthor_id_fkey CASCADE
  231. ');
  232. db_query('
  233. ALTER TABLE chado.pubauthor_contact
  234. DROP CONSTRAINT pubauthor_contact_contact_id_fkey CASCADE
  235. ');
  236. }
  237. db_query('
  238. ALTER TABLE chado.pubauthor_contact
  239. ADD CONSTRAINT pubauthor_contact_pubauthor_id_fkey
  240. FOREIGN KEY (pubauthor_id) REFERENCES chado.pubauthor (pubauthor_id)
  241. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  242. ');
  243. db_query('
  244. ALTER TABLE chado.pubauthor_contact
  245. ADD CONSTRAINT pubauthor_contact_contact_id_fkey
  246. FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
  247. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  248. ');
  249. }
  250. catch (\PDOException $e) {
  251. $error = $e->getMessage();
  252. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  253. }
  254. }
  255. /**
  256. * Updates path of tripal_pub OBO to be relative.
  257. */
  258. function tripal_pub_update_7202() {
  259. try {
  260. // Remove duplicates.
  261. db_delete('tripal_cv_obo')
  262. ->condition('name', 'Tripal Publication')
  263. ->execute();
  264. // Add in the updated path.
  265. $obo_path = '{tripal_pub}/files/tpub.obo';
  266. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  267. }
  268. catch (\PDOException $e) {
  269. $error = $e->getMessage();
  270. throw new DrupalUpdateException('Failed to update tripal_pub OBO path: '. $error);
  271. }
  272. }