tripal_contact.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. /**
  3. * @file
  4. * Handles install, uninstall, disable and enable functionality including database tables.
  5. *
  6. * @ingroup tripal_contact
  7. */
  8. /**
  9. * Implements hook_disable().
  10. * Disable default views when module is disabled
  11. *
  12. * @ingroup tripal_contact
  13. */
  14. function tripal_contact_disable() {
  15. // Disable all default views provided by this module
  16. require_once("tripal_contact.views_default.inc");
  17. $views = tripal_contact_views_default_views();
  18. foreach (array_keys($views) as $view_name) {
  19. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  20. }
  21. }
  22. /**
  23. * Implementation of hook_requirements().
  24. *
  25. * @ingroup tripal_contact
  26. */
  27. function tripal_contact_requirements($phase) {
  28. $requirements = array();
  29. if ($phase == 'install') {
  30. // make sure chado is installed
  31. if (!$GLOBALS["chado_is_installed"]) {
  32. $requirements ['tripal_contact'] = array(
  33. 'title' => "tripal_contact",
  34. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  35. 'severity' => REQUIREMENT_ERROR,
  36. );
  37. }
  38. }
  39. return $requirements;
  40. }
  41. /**
  42. * Implementation of hook_install().
  43. *
  44. * @ingroup tripal_contact
  45. */
  46. function tripal_contact_install() {
  47. // Add the contactprop table to Chado.
  48. tripal_contact_add_custom_tables();
  49. // Add loading of the the tripal contact ontology to the job queue.
  50. $obo_path = '{tripal_contact}/files/tcontact.obo';
  51. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  52. tripal_submit_obo_job(array('obo_id' => $obo_id));
  53. // Add cvterms for relationship types.
  54. tripal_contact_add_cvs();
  55. tripal_contact_add_cvterms();
  56. // Set the default vocabularies.
  57. tripal_set_default_cv('contact', 'type_id', 'tripal_contact');
  58. tripal_set_default_cv('contactprop', 'type_id', 'tripal_contact');
  59. tripal_set_default_cv('contact_relationship', 'type_id', 'contact_relationship');
  60. }
  61. /**
  62. * Implementation of hook_uninstall().
  63. *
  64. * @ingroup tripal_contact
  65. */
  66. function tripal_contact_uninstall() {
  67. /*
  68. // remove our custom block visibility settings per node type
  69. db_delete('block_node_type')
  70. ->condition('module', 'chado_contact')
  71. ->condition('delta', 'contbase')
  72. ->execute();
  73. */
  74. }
  75. /**
  76. * Adds any cvs needed by this module.
  77. *
  78. * @ingroup tripal_contact
  79. */
  80. function tripal_contact_add_cvs() {
  81. // Add the cv for contact properties. This is a default vocabulary in the event
  82. // that a user does not want to use the tripal_contact vocabulary
  83. tripal_insert_cv(
  84. 'contact_property',
  85. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  86. );
  87. // add the cv for the contact type. This is a default vocabulary in the event
  88. // that a user does not want to use the tripal_contact vocabulary
  89. tripal_insert_cv(
  90. 'contact_type',
  91. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  92. );
  93. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  94. tripal_insert_cv(
  95. 'tripal_contact',
  96. 'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
  97. );
  98. // add the cv for contact relationships
  99. tripal_insert_cv(
  100. 'contact_relationship',
  101. 'Contains types of relationships between contacts.'
  102. );
  103. }
  104. /**
  105. * Adds any cvterms needed by this module.
  106. *
  107. * @ingroup tripal_contact
  108. */
  109. function tripal_contact_add_cvterms() {
  110. }
  111. /**
  112. * Implementation of hook_schema().
  113. *
  114. * @ingroup tripal_contact
  115. */
  116. function tripal_contact_schema() {
  117. $schema['chado_contact'] = array(
  118. 'fields' => array(
  119. 'vid' => array(
  120. 'type' => 'int',
  121. 'unsigned' => TRUE,
  122. 'not null' => TRUE,
  123. 'default' => 0
  124. ),
  125. 'nid' => array(
  126. 'type' => 'int',
  127. 'unsigned' => TRUE,
  128. 'not null' => TRUE,
  129. 'default' => 0
  130. ),
  131. 'contact_id' => array(
  132. 'type' => 'int',
  133. 'not null' => TRUE,
  134. 'default' => 0
  135. )
  136. ),
  137. 'indexes' => array(
  138. 'contact_id' => array('contact_id')
  139. ),
  140. 'unique keys' => array(
  141. 'nid_vid' => array('nid', 'vid'),
  142. 'vid' => array('vid')
  143. ),
  144. 'primary key' => array('nid'),
  145. );
  146. return $schema;
  147. }
  148. /**
  149. * Add any custom tables needed by this module.
  150. * - Contactprop: keep track of properties of contact
  151. *
  152. * @ingroup tripal_contact
  153. */
  154. function tripal_contact_add_custom_tables(){
  155. $schema = array (
  156. 'table' => 'contactprop',
  157. 'fields' => array (
  158. 'contactprop_id' => array (
  159. 'type' => 'serial',
  160. 'not null' => true,
  161. ),
  162. 'contact_id' => array (
  163. 'type' => 'int',
  164. 'not null' => true,
  165. ),
  166. 'type_id' => array (
  167. 'type' => 'int',
  168. 'not null' => true,
  169. ),
  170. 'value' => array (
  171. 'type' => 'text',
  172. 'not null' => false,
  173. ),
  174. 'rank' => array (
  175. 'type' => 'int',
  176. 'not null' => true,
  177. 'default' => 0,
  178. ),
  179. ),
  180. 'primary key' => array (
  181. 0 => 'contactprop_id',
  182. ),
  183. 'unique keys' => array (
  184. 'contactprop_c1' => array (
  185. 0 => 'contact_id',
  186. 1 => 'type_id',
  187. 2 => 'rank',
  188. ),
  189. ),
  190. 'indexes' => array (
  191. 'contactprop_idx1' => array (
  192. 0 => 'contact_id',
  193. ),
  194. 'contactprop_idx2' => array (
  195. 0 => 'type_id',
  196. ),
  197. ),
  198. 'foreign keys' => array (
  199. 'cvterm' => array (
  200. 'table' => 'cvterm',
  201. 'columns' => array (
  202. 'type_id' => 'cvterm_id',
  203. ),
  204. ),
  205. 'contact' => array (
  206. 'table' => 'contact',
  207. 'columns' => array (
  208. 'contact_id' => 'contact_id',
  209. ),
  210. ),
  211. ),
  212. );
  213. chado_create_custom_table('contactprop', $schema, TRUE);
  214. }
  215. /**
  216. * This is the required update for tripal_contact when upgrading from Drupal core API 6.x.
  217. *
  218. */
  219. function tripal_contact_update_7200() {
  220. // We cannot use the Tripal API calls in the 7200 update
  221. // because during upgrade the tripal_core will be disabled
  222. // add the contact_type CV
  223. try {
  224. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_contact'")->fetchField();
  225. if (!$cv_id) {
  226. // add the vocabulary
  227. $cv_id = db_insert('chado.cv')
  228. ->fields(array(
  229. 'name' => 'tripal_contact',
  230. 'definition' => 'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
  231. ))
  232. ->execute();
  233. }
  234. // make this CV the defaults for the contact properties and contact types
  235. db_insert('tripal_cv_defaults')
  236. ->fields(array(
  237. 'table_name' => 'contact',
  238. 'field_name' => 'type_id',
  239. 'cv_id' => $cv_id
  240. ))
  241. ->execute();
  242. db_insert('tripal_cv_defaults')
  243. ->fields(array(
  244. 'table_name' => 'contactprop',
  245. 'field_name' => 'type_id',
  246. 'cv_id' => $cv_id
  247. ))
  248. ->execute();
  249. }
  250. catch (\PDOException $e) {
  251. $error = $e->getMessage();
  252. throw new DrupalUpdateException('Failed to add tripal_contact vocabulary: '. $error);
  253. }
  254. // add the contact_relationship CV
  255. try {
  256. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_relationship'")->fetchField();
  257. if (!$cv_id) {
  258. // add the vocabulary
  259. $cv_id = db_insert('chado.cv')
  260. ->fields(array(
  261. 'name' => 'contact_relationship',
  262. 'definition' => 'Contains types of relationships between contacts.'
  263. ))
  264. ->execute();
  265. }
  266. // add the default
  267. db_insert('tripal_cv_defaults')
  268. ->fields(array(
  269. 'table_name' => 'contact_relationship',
  270. 'field_name' => 'type_id',
  271. 'cv_id' => $cv_id
  272. ))
  273. ->execute();
  274. }
  275. catch (\PDOException $e) {
  276. $error = $e->getMessage();
  277. throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
  278. }
  279. // add the contact_type CV
  280. try {
  281. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_type'")->fetchField();
  282. if (!$cv_id) {
  283. // add the vocabulary
  284. $cv_id = db_insert('chado.cv')
  285. ->fields(array(
  286. 'name' => 'contact_type',
  287. 'definition' => 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  288. ))
  289. ->execute();
  290. }
  291. }
  292. catch (\PDOException $e) {
  293. $error = $e->getMessage();
  294. throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
  295. }
  296. // add the contact_property CV
  297. try {
  298. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_property'")->fetchField();
  299. if (!$cv_id) {
  300. // add the vocabulary
  301. $cv_id = db_insert('chado.cv')
  302. ->fields(array(
  303. 'name' => 'contact_property',
  304. 'definition' => 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  305. ))
  306. ->execute();
  307. }
  308. }
  309. catch (\PDOException $e) {
  310. $error = $e->getMessage();
  311. throw new DrupalUpdateException('Failed to add contact_property vocabulary: '. $error);
  312. }
  313. }
  314. /**
  315. * Implementation of hook_update_dependencies(). It specifies a list of
  316. * other modules whose updates must be run prior to this one.
  317. */
  318. function tripal_contact_update_dependencies() {
  319. $dependencies = array();
  320. // the tripal_cv update 7200 must run prior to update 7200 of this module
  321. $dependencies['tripal_contact'][7200] = array(
  322. 'tripal_cv' => 7200
  323. );
  324. return $dependencies;
  325. }
  326. /**
  327. * Adds missing foreign key constraints
  328. *
  329. */
  330. function tripal_contact_update_7201() {
  331. // there was a bug in the function for creating a custom table that
  332. // kept foreign key constraints from being added. So, we need to add those
  333. // to keep from error messages appear, we will drop the FK if it already
  334. // exists and then re-add it.
  335. try {
  336. $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'contactprop_type_id_fkey'))->fetchField();
  337. if ($fkey_exists) {
  338. db_query('
  339. ALTER TABLE chado.contactprop
  340. DROP CONSTRAINT contactprop_type_id_fkey CASCADE
  341. ');
  342. db_query('
  343. ALTER TABLE chado.contactprop
  344. DROP CONSTRAINT contactprop_contact_id_fkey CASCADE
  345. ');
  346. }
  347. db_query('
  348. ALTER TABLE chado.contactprop
  349. ADD CONSTRAINT contactprop_type_id_fkey
  350. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  351. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  352. ');
  353. db_query('
  354. ALTER TABLE chado.contactprop
  355. ADD CONSTRAINT contactprop_contact_id_fkey
  356. FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
  357. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  358. ');
  359. }
  360. catch (\PDOException $e) {
  361. $error = $e->getMessage();
  362. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  363. }
  364. }
  365. /**
  366. * Updates path of tripal_contact OBO to be relative.
  367. */
  368. function tripal_contact_update_7202() {
  369. try {
  370. // Remove duplicates.
  371. db_delete('tripal_cv_obo')
  372. ->condition('name', 'Tripal Contacts')
  373. ->execute();
  374. // Add in the updated path.
  375. $obo_path = '{tripal_contact}/files/tcontact.obo';
  376. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  377. }
  378. catch (\PDOException $e) {
  379. $error = $e->getMessage();
  380. throw new DrupalUpdateException('Failed to update tripal_contact OBO path: '. $error);
  381. }
  382. }