tripal_contact.install

  1. 2.x tripal_contact/tripal_contact.install
  2. 3.x legacy/tripal_contact/tripal_contact.install
  3. 1.x tripal_contact/tripal_contact.install

Handles install, uninstall, disable and enable functionality including database tables.

File

tripal_contact/tripal_contact.install
View source
  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. // First we add the cv.
  228. // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
  229. $cv = tripal_insert_cv(
  230. 'tripal_contact',
  231. '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.'
  232. );
  233. if ($cv) {
  234. $cv_id = $cv->cv_id;
  235. // Set as Default CV for contact types.
  236. $is_set = tripal_get_default_cv('contact', 'type_id');
  237. if (!$is_set) {
  238. tripal_set_default_cv('contact','type_id', 'tripal_contact', $cv_id);
  239. }
  240. // Set as Default CV for contact properties.
  241. $is_set = tripal_get_default_cv('contactprop', 'type_id');
  242. if (!$is_set) {
  243. tripal_set_default_cv('contactprop','type_id', 'tripal_contact', $cv_id);
  244. }
  245. }
  246. else {
  247. throw new DrupalUpdateException('Failed to add tripal_contact vocabulary.');
  248. }
  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. // First we add the cv.
  257. // Notice that tripal_insert_cv() will only add it if it doesn't exist already.
  258. $cv = tripal_insert_cv(
  259. 'contact_relationship',
  260. 'Contains types of relationships between contacts.'
  261. );
  262. if ($cv) {
  263. $cv_id = $cv->cv_id;
  264. // Set as Default CV for contact relationships.
  265. $is_set = tripal_get_default_cv('contact_relationship', 'type_id');
  266. if (!$is_set) {
  267. tripal_set_default_cv('contact_relationship','type_id', 'contact_relationship', $cv_id);
  268. }
  269. }
  270. else {
  271. throw new DrupalUpdateException('Failed to add contact_relationship vocabulary.');
  272. }
  273. }
  274. catch (\PDOException $e) {
  275. $error = $e->getMessage();
  276. throw new DrupalUpdateException('Failed to add contact_relationship vocabulary: '. $error);
  277. }
  278. // Add the contact_type CV (not default).
  279. try {
  280. $cv = tripal_insert_cv(
  281. 'contact_type',
  282. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  283. );
  284. }
  285. catch (\PDOException $e) {
  286. $error = $e->getMessage();
  287. throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
  288. }
  289. // Add the contact_property CV (not default).
  290. try {
  291. $cv = tripal_insert_cv(
  292. 'contact_property',
  293. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  294. );
  295. }
  296. catch (\PDOException $e) {
  297. $error = $e->getMessage();
  298. throw new DrupalUpdateException('Failed to add contact_property vocabulary: '. $error);
  299. }
  300. }
  301. /**
  302. * Implementation of hook_update_dependencies().
  303. *
  304. * It specifies a list of other modules whose updates must be run prior to
  305. * this one. It also ensures the the Tripal API is in scope for site
  306. * upgrades when tripal_core is disabled.
  307. */
  308. function tripal_contact_update_dependencies() {
  309. $dependencies = array();
  310. // the tripal_cv update 7200 must run prior to update 7200 of this module
  311. $dependencies['tripal_contact'][7200] = array(
  312. 'tripal_cv' => 7200
  313. );
  314. return $dependencies;
  315. }
  316. /**
  317. * Adds missing foreign key constraints
  318. *
  319. */
  320. function tripal_contact_update_7201() {
  321. // Make sure we have the full API loaded this will help during a
  322. // site upgrade when the tripal_core module is disabled.
  323. module_load_include('module', 'tripal_core', 'tripal_core');
  324. tripal_core_import_api();
  325. // there was a bug in the function for creating a custom table that
  326. // kept foreign key constraints from being added. So, we need to add those
  327. // to keep from error messages appear, we will drop the FK if it already
  328. // exists and then re-add it.
  329. try {
  330. $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'contactprop_type_id_fkey'))->fetchField();
  331. if ($fkey_exists) {
  332. chado_query('
  333. ALTER TABLE {contactprop}
  334. DROP CONSTRAINT contactprop_type_id_fkey CASCADE
  335. ');
  336. chado_query('
  337. ALTER TABLE {contactprop}
  338. DROP CONSTRAINT contactprop_contact_id_fkey CASCADE
  339. ');
  340. }
  341. chado_query('
  342. ALTER TABLE {contactprop}
  343. ADD CONSTRAINT contactprop_type_id_fkey
  344. FOREIGN KEY (type_id) REFERENCES {cvterm} (cvterm_id)
  345. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  346. ');
  347. chado_query('
  348. ALTER TABLE {contactprop}
  349. ADD CONSTRAINT contactprop_contact_id_fkey
  350. FOREIGN KEY (contact_id) REFERENCES {contact} (contact_id)
  351. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  352. ');
  353. }
  354. catch (\PDOException $e) {
  355. $error = $e->getMessage();
  356. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  357. }
  358. }
  359. /**
  360. * Updates path of tripal_contact OBO to be relative.
  361. */
  362. function tripal_contact_update_7202() {
  363. // Make sure we have the full API loaded this will help during a
  364. // site upgrade when the tripal_core module is disabled.
  365. module_load_include('module', 'tripal_core', 'tripal_core');
  366. tripal_core_import_api();
  367. module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');
  368. try {
  369. // Remove duplicates.
  370. db_delete('tripal_cv_obo')
  371. ->condition('name', 'Tripal Contacts')
  372. ->execute();
  373. // Add in the updated path.
  374. $obo_path = '{tripal_contact}/files/tcontact.obo';
  375. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  376. }
  377. catch (\PDOException $e) {
  378. $error = $e->getMessage();
  379. throw new DrupalUpdateException('Failed to update tripal_contact OBO path: '. $error);
  380. }
  381. }

Related topics