tripal_contact.install 12 KB

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