tripal_contact.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. // create the module's data directory
  48. tripal_create_files_dir('tripal_contact');
  49. // add the contactprop table to Chado
  50. tripal_contact_add_custom_tables();
  51. // add loading of the the tripal contact ontology to the job queue
  52. $obo_path = drupal_realpath(drupal_get_path('module', 'tripal_contact') . '/files/tcontact.obo');
  53. $obo_id = tripal_cv_add_obo_ref('Tripal Contacts', $obo_path);
  54. tripal_cv_submit_obo_job($obo_id);
  55. // Add cvterms for relationship types
  56. tripal_contact_add_cvs();
  57. tripal_contact_add_cvterms();
  58. // set the default vocabularies
  59. tripal_set_default_cv('contact', 'type_id', 'tripal_contact');
  60. tripal_set_default_cv('contactprop', 'type_id', 'tripal_contact');
  61. tripal_set_default_cv('contact_relationship', 'type_id', 'contact_relationship');
  62. }
  63. /**
  64. * Implementation of hook_uninstall().
  65. *
  66. * @ingroup tripal_contact
  67. */
  68. function tripal_contact_uninstall() {
  69. /*
  70. // remove our custom block visibility settings per node type
  71. db_delete('block_node_type')
  72. ->condition('module', 'chado_contact')
  73. ->condition('delta', 'contbase')
  74. ->execute();
  75. */
  76. }
  77. /**
  78. * Adds any cvs needed by this module.
  79. *
  80. * @ingroup tripal_contact
  81. */
  82. function tripal_contact_add_cvs() {
  83. // Add the cv for contact properties. This is a default vocabulary in the event
  84. // that a user does not want to use the tripal_contact vocabulary
  85. tripal_cv_add_cv(
  86. 'contact_property',
  87. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  88. );
  89. // add the cv for the contact type. This is a default vocabulary in the event
  90. // that a user does not want to use the tripal_contact vocabulary
  91. tripal_cv_add_cv(
  92. 'contact_type',
  93. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  94. );
  95. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  96. tripal_cv_add_cv(
  97. 'tripal_contact',
  98. '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.'
  99. );
  100. // add the cv for contact relationships
  101. tripal_cv_add_cv(
  102. 'contact_relationship',
  103. 'Contains types of relationships between contacts.'
  104. );
  105. }
  106. /**
  107. * Adds any cvterms needed by this module.
  108. *
  109. * @ingroup tripal_contact
  110. */
  111. function tripal_contact_add_cvterms() {
  112. }
  113. /**
  114. * Implementation of hook_schema().
  115. *
  116. * @ingroup tripal_contact
  117. */
  118. function tripal_contact_schema() {
  119. $schema['chado_contact'] = array(
  120. 'fields' => array(
  121. 'vid' => array(
  122. 'type' => 'int',
  123. 'unsigned' => TRUE,
  124. 'not null' => TRUE,
  125. 'default' => 0
  126. ),
  127. 'nid' => array(
  128. 'type' => 'int',
  129. 'unsigned' => TRUE,
  130. 'not null' => TRUE,
  131. 'default' => 0
  132. ),
  133. 'contact_id' => array(
  134. 'type' => 'int',
  135. 'not null' => TRUE,
  136. 'default' => 0
  137. )
  138. ),
  139. 'indexes' => array(
  140. 'contact_id' => array('contact_id')
  141. ),
  142. 'unique keys' => array(
  143. 'nid_vid' => array('nid', 'vid'),
  144. 'vid' => array('vid')
  145. ),
  146. 'primary key' => array('nid'),
  147. );
  148. return $schema;
  149. }
  150. /**
  151. * Add any custom tables needed by this module.
  152. * - Contactprop: keep track of properties of contact
  153. *
  154. * @ingroup tripal_contact
  155. */
  156. function tripal_contact_add_custom_tables(){
  157. $schema = array (
  158. 'table' => 'contactprop',
  159. 'fields' => array (
  160. 'contactprop_id' => array (
  161. 'type' => 'serial',
  162. 'not null' => true,
  163. ),
  164. 'contact_id' => array (
  165. 'type' => 'int',
  166. 'not null' => true,
  167. ),
  168. 'type_id' => array (
  169. 'type' => 'int',
  170. 'not null' => true,
  171. ),
  172. 'value' => array (
  173. 'type' => 'text',
  174. 'not null' => false,
  175. ),
  176. 'rank' => array (
  177. 'type' => 'int',
  178. 'not null' => true,
  179. 'default' => 0,
  180. ),
  181. ),
  182. 'primary key' => array (
  183. 0 => 'contactprop_id',
  184. ),
  185. 'unique keys' => array (
  186. 'contactprop_c1' => array (
  187. 0 => 'contact_id',
  188. 1 => 'type_id',
  189. 2 => 'rank',
  190. ),
  191. ),
  192. 'indexes' => array (
  193. 'contactprop_idx1' => array (
  194. 0 => 'contact_id',
  195. ),
  196. 'contactprop_idx2' => array (
  197. 0 => 'type_id',
  198. ),
  199. ),
  200. 'foreign keys' => array (
  201. 'cvterm' => array (
  202. 'table' => 'cvterm',
  203. 'columns' => array (
  204. 'type_id' => 'cvterm_id',
  205. ),
  206. ),
  207. 'contact' => array (
  208. 'table' => 'contact',
  209. 'columns' => array (
  210. 'contact_id' => 'contact_id',
  211. ),
  212. ),
  213. ),
  214. );
  215. chado_create_custom_table('contactprop', $schema, TRUE);
  216. }
  217. /**
  218. * This is the required update for tripal_contact when upgrading from Drupal core API 6.x.
  219. *
  220. */
  221. function tripal_contact_update_7200() {
  222. // We cannot use the Tripal API calls in the 7200 update
  223. // because during upgrade the tripal_core will be disabled
  224. // add the contact_type CV
  225. try {
  226. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_contact'")->fetchField();
  227. if (!$cv_id) {
  228. // add the vocabulary
  229. $cv_id = db_insert('chado.cv')
  230. ->fields(array(
  231. 'name' => 'tripal_contact',
  232. '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.'
  233. ))
  234. ->execute();
  235. }
  236. // make this CV the defaults for the contact properties and contact types
  237. db_insert('tripal_cv_defaults')
  238. ->fields(array(
  239. 'table_name' => 'contact',
  240. 'field_name' => 'type_id',
  241. 'cv_id' => $cv_id
  242. ))
  243. ->execute();
  244. db_insert('tripal_cv_defaults')
  245. ->fields(array(
  246. 'table_name' => 'contactprop',
  247. 'field_name' => 'type_id',
  248. 'cv_id' => $cv_id
  249. ))
  250. ->execute();
  251. }
  252. catch (\PDOException $e) {
  253. $error = $e->getMessage();
  254. throw new DrupalUpdateException('Failed to add tripal_contact vocabulary: '. $error);
  255. }
  256. // add the contact_relationship CV
  257. try {
  258. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_relationship'")->fetchField();
  259. if (!$cv_id) {
  260. // add the vocabulary
  261. $cv_id = db_insert('chado.cv')
  262. ->fields(array(
  263. 'name' => 'contact_relationship',
  264. 'definition' => 'Contains types of relationships between contacts.'
  265. ))
  266. ->execute();
  267. }
  268. // add the default
  269. db_insert('tripal_cv_defaults')
  270. ->fields(array(
  271. 'table_name' => 'contact_relationship',
  272. 'field_name' => 'type_id',
  273. 'cv_id' => $cv_id
  274. ))
  275. ->execute();
  276. }
  277. catch (\PDOException $e) {
  278. $error = $e->getMessage();
  279. throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
  280. }
  281. // add the contact_type CV
  282. try {
  283. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_type'")->fetchField();
  284. if (!$cv_id) {
  285. // add the vocabulary
  286. $cv_id = db_insert('chado.cv')
  287. ->fields(array(
  288. 'name' => 'contact_type',
  289. 'definition' => 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  290. ))
  291. ->execute();
  292. }
  293. }
  294. catch (\PDOException $e) {
  295. $error = $e->getMessage();
  296. throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
  297. }
  298. // add the contact_property CV
  299. try {
  300. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_property'")->fetchField();
  301. if (!$cv_id) {
  302. // add the vocabulary
  303. $cv_id = db_insert('chado.cv')
  304. ->fields(array(
  305. 'name' => 'contact_property',
  306. 'definition' => 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  307. ))
  308. ->execute();
  309. }
  310. }
  311. catch (\PDOException $e) {
  312. $error = $e->getMessage();
  313. throw new DrupalUpdateException('Failed to add contact_property vocabulary: '. $error);
  314. }
  315. }
  316. /**
  317. * Implementation of hook_update_dependencies(). It specifies a list of
  318. * other modules whose updates must be run prior to this one.
  319. */
  320. function tripal_contact_update_dependencies() {
  321. $dependencies = array();
  322. // the tripal_cv update 7200 must run prior to update 7200 of this module
  323. $dependencies['tripal_contact'][7200] = array(
  324. 'tripal_cv' => 7200
  325. );
  326. return $dependencies;
  327. }
  328. /**
  329. * Adds missing foreign key constraints
  330. *
  331. */
  332. function tripal_contact_update_7201() {
  333. // there was a bug in the function for creating a custom table that
  334. // kept foreign key constraints from being added. So, we need to add those
  335. // to keep from error messages appear, we will drop the FK if it already
  336. // exists and then re-add it.
  337. try {
  338. db_query('
  339. ALTER TABLE chado.contactprop
  340. DROP CONSTRAINT IF EXISTS contactprop_type_id_fkey CASCADE
  341. ');
  342. db_query('
  343. ALTER TABLE chado.contactprop
  344. DROP CONSTRAINT IF EXISTS contactprop_contact_id_fkey CASCADE
  345. ');
  346. db_query('
  347. ALTER TABLE chado.contactprop
  348. ADD CONSTRAINT contactprop_type_id_fkey
  349. FOREIGN KEY (type_id) REFERENCES chado.cvterm (cvterm_id)
  350. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  351. ');
  352. db_query('
  353. ALTER TABLE chado.contactprop
  354. ADD CONSTRAINT contactprop_contact_id_fkey
  355. FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
  356. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  357. ');
  358. }
  359. catch (\PDOException $e) {
  360. $error = $e->getMessage();
  361. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  362. }
  363. }