tripal_contact.install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. // Make sure we have the full API loaded this will help during a
  221. // site upgrade when the tripal_core module is disabled.
  222. module_load_include('module', 'tripal_core', 'tripal_core');
  223. tripal_core_import_api();
  224. module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');
  225. // add the contact_type CV
  226. try {
  227. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_contact'")->fetchField();
  228. if (!$cv_id) {
  229. // add the vocabulary
  230. $cv_id = db_insert('chado.cv')
  231. ->fields(array(
  232. 'name' => 'tripal_contact',
  233. '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.'
  234. ))
  235. ->execute();
  236. }
  237. // make this CV the defaults for the contact properties and contact types
  238. // If a record already exists then don't change it.
  239. $cdi = db_select('tripal_cv_defaults', 't')
  240. ->fields('t', array('cv_default_id'))
  241. ->condition('table_name', 'contact')
  242. ->condition('field_name', 'type_id')
  243. ->execute()
  244. ->fetchField();
  245. if (!$cdi) {
  246. db_insert('tripal_cv_defaults')
  247. ->fields(array(
  248. 'table_name' => 'contact',
  249. 'field_name' => 'type_id',
  250. 'cv_id' => $cv_id
  251. ))
  252. ->execute();
  253. }
  254. $cdi = db_select('tripal_cv_defaults', 't')
  255. ->fields('t', array('cv_default_id'))
  256. ->condition('table_name', 'contactprop')
  257. ->condition('field_name', 'type_id')
  258. ->execute()
  259. ->fetchField();
  260. if (!$cdi) {
  261. db_insert('tripal_cv_defaults')
  262. ->fields(array(
  263. 'table_name' => 'contactprop',
  264. 'field_name' => 'type_id',
  265. 'cv_id' => $cv_id
  266. ))
  267. ->execute();
  268. }
  269. }
  270. catch (\PDOException $e) {
  271. $error = $e->getMessage();
  272. throw new DrupalUpdateException('Failed to add tripal_contact vocabulary: '. $error);
  273. }
  274. // add the contact_relationship CV
  275. try {
  276. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_relationship'")->fetchField();
  277. if (!$cv_id) {
  278. // add the vocabulary
  279. $cv_id = db_insert('chado.cv')
  280. ->fields(array(
  281. 'name' => 'contact_relationship',
  282. 'definition' => 'Contains types of relationships between contacts.'
  283. ))
  284. ->execute();
  285. }
  286. $cdi = db_select('tripal_cv_defaults', 't')
  287. ->fields('t', array('cv_default_id'))
  288. ->condition('table_name', 'contact_relationship')
  289. ->condition('field_name', 'type_id')
  290. ->execute()
  291. ->fetchField();
  292. if (!$cdi) {
  293. // add the default
  294. db_insert('tripal_cv_defaults')
  295. ->fields(array(
  296. 'table_name' => 'contact_relationship',
  297. 'field_name' => 'type_id',
  298. 'cv_id' => $cv_id
  299. ))
  300. ->execute();
  301. }
  302. }
  303. catch (\PDOException $e) {
  304. $error = $e->getMessage();
  305. throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
  306. }
  307. // add the contact_type CV
  308. try {
  309. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_type'")->fetchField();
  310. if (!$cv_id) {
  311. // add the vocabulary
  312. $cv_id = db_insert('chado.cv')
  313. ->fields(array(
  314. 'name' => 'contact_type',
  315. 'definition' => 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  316. ))
  317. ->execute();
  318. }
  319. }
  320. catch (\PDOException $e) {
  321. $error = $e->getMessage();
  322. throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
  323. }
  324. // add the contact_property CV
  325. try {
  326. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_property'")->fetchField();
  327. if (!$cv_id) {
  328. // add the vocabulary
  329. $cv_id = db_insert('chado.cv')
  330. ->fields(array(
  331. 'name' => 'contact_property',
  332. 'definition' => 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  333. ))
  334. ->execute();
  335. }
  336. }
  337. catch (\PDOException $e) {
  338. $error = $e->getMessage();
  339. throw new DrupalUpdateException('Failed to add contact_property vocabulary: '. $error);
  340. }
  341. }
  342. /**
  343. * Implementation of hook_update_dependencies().
  344. *
  345. * It specifies a list of other modules whose updates must be run prior to
  346. * this one. It also ensures the the Tripal API is in scope for site
  347. * upgrades when tripal_core is disabled.
  348. */
  349. function tripal_contact_update_dependencies() {
  350. $dependencies = array();
  351. // the tripal_cv update 7200 must run prior to update 7200 of this module
  352. $dependencies['tripal_contact'][7200] = array(
  353. 'tripal_cv' => 7200
  354. );
  355. return $dependencies;
  356. }
  357. /**
  358. * Adds missing foreign key constraints
  359. *
  360. */
  361. function tripal_contact_update_7201() {
  362. // Make sure we have the full API loaded this will help during a
  363. // site upgrade when the tripal_core module is disabled.
  364. module_load_include('module', 'tripal_core', 'tripal_core');
  365. tripal_core_import_api();
  366. // there was a bug in the function for creating a custom table that
  367. // kept foreign key constraints from being added. So, we need to add those
  368. // to keep from error messages appear, we will drop the FK if it already
  369. // exists and then re-add it.
  370. try {
  371. $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'contactprop_type_id_fkey'))->fetchField();
  372. if ($fkey_exists) {
  373. db_query('
  374. ALTER TABLE chado.contactprop
  375. DROP CONSTRAINT contactprop_type_id_fkey CASCADE
  376. ');
  377. db_query('
  378. ALTER TABLE chado.contactprop
  379. DROP CONSTRAINT contactprop_contact_id_fkey CASCADE
  380. ');
  381. }
  382. db_query('
  383. ALTER TABLE chado.contactprop
  384. ADD CONSTRAINT contactprop_type_id_fkey
  385. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  386. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  387. ');
  388. db_query('
  389. ALTER TABLE chado.contactprop
  390. ADD CONSTRAINT contactprop_contact_id_fkey
  391. FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
  392. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  393. ');
  394. }
  395. catch (\PDOException $e) {
  396. $error = $e->getMessage();
  397. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  398. }
  399. }
  400. /**
  401. * Updates path of tripal_contact OBO to be relative.
  402. */
  403. function tripal_contact_update_7202() {
  404. // Make sure we have the full API loaded this will help during a
  405. // site upgrade when the tripal_core module is disabled.
  406. module_load_include('module', 'tripal_core', 'tripal_core');
  407. tripal_core_import_api();
  408. module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');
  409. try {
  410. // Remove duplicates.
  411. db_delete('tripal_cv_obo')
  412. ->condition('name', 'Tripal Contacts')
  413. ->execute();
  414. // Add in the updated path.
  415. $obo_path = '{tripal_contact}/files/tcontact.obo';
  416. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  417. }
  418. catch (\PDOException $e) {
  419. $error = $e->getMessage();
  420. throw new DrupalUpdateException('Failed to update tripal_contact OBO path: '. $error);
  421. }
  422. }