tripal_chado.bundle.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Implements hook_chado_bundle_create().
  4. *
  5. * This is a Tripal hook. It allows any module to perform tasks after
  6. * a bundle has been created.
  7. *
  8. * @param $bundle
  9. * The TripalBundle object.
  10. */
  11. function tripal_chado_bundle_create($bundle, $storage_args) {
  12. $entity_type = $bundle->type;
  13. if (!array_key_exists('data_table', $storage_args)) {
  14. throw new Exception('Cannot create content type. Missing the "data_table" for this bundle.');
  15. }
  16. $type_id = '';
  17. if (array_key_exists('type_id', $storage_args)) {
  18. $type_id = $storage_args['type_id'];
  19. }
  20. else {
  21. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  22. $vocab = tripal_load_vocab_entity(array('vocab_id' => $term->vocab_id));
  23. $cvterm = chado_generate_var('cvterm', array(
  24. 'dbxref_id' => array(
  25. 'db_id' => array(
  26. 'name' => $vocab->vocabulary,
  27. ),
  28. 'accession' => $term->accession
  29. ),
  30. ));
  31. $type_id = $cvterm->cvterm_id;
  32. }
  33. // Before adding fields to this bundle, let's make sure we associate
  34. // what table in Chado this bundle is mapped to
  35. $chado_bundle = db_select('chado_bundle', 'cb')
  36. ->fields('cb')
  37. ->condition('bundle_id', $bundle->id)
  38. ->execute()
  39. ->fetchObject();
  40. if (!$chado_bundle) {
  41. $record = array(
  42. 'bundle_id' => $bundle->id,
  43. 'data_table' => $storage_args['data_table'],
  44. 'type_id' => $type_id,
  45. );
  46. if (array_key_exists('type_linker_table', $storage_args)) {
  47. $record['type_linker_table'] = $storage_args['type_linker_table'];
  48. }
  49. if (array_key_exists('type_column', $storage_args)) {
  50. $record['type_column'] = $storage_args['type_column'];
  51. }
  52. if (array_key_exists('type_value', $storage_args)) {
  53. $record['type_value'] = $storage_args['type_value'];
  54. }
  55. $success = drupal_write_record('chado_bundle', $record);
  56. if (!$success) {
  57. throw new Exception('Cannot create content type. Problem associating type with Chado.');
  58. }
  59. }
  60. tripal_chado_create_bundle_table($bundle);
  61. }
  62. /**
  63. * Creates the table that tripal_chado uses to link Chado records with entities.
  64. *
  65. * @param $bundle
  66. * A bundle object (as retrieved from tripal_load_bundle_entity().
  67. */
  68. function tripal_chado_create_bundle_table($bundle) {
  69. // Now create the table where the bundle's records will go
  70. $schema = array(
  71. 'description' => 'The linker table that associates TripalEntities with Chado records for entities of type ' . $bundle->name . '.',
  72. 'fields' => array(
  73. 'mapping_id' => array(
  74. 'type' => 'serial',
  75. 'not null' => TRUE
  76. ),
  77. 'entity_id' => array(
  78. 'description' => 'The unique entity id.',
  79. 'type' => 'int',
  80. 'not null' => TRUE,
  81. ),
  82. 'record_id' => array(
  83. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  84. 'type' => 'int',
  85. 'not null' => TRUE,
  86. ),
  87. 'nid' => array(
  88. 'description' => 'Optional. For linking nid to the entity when migrating Tripal v2 content',
  89. 'type' => 'int',
  90. )
  91. ),
  92. 'primary key' => array(
  93. 'mapping_id',
  94. ),
  95. 'indexes' => array(
  96. 'record_id' => array('record_id'),
  97. 'entity_id' => array('entity_id'),
  98. 'nid' => array('nid'),
  99. ),
  100. 'unique keys' => array(
  101. 'table_record' => array('record_id'),
  102. 'entity_id' => array('entity_id'),
  103. ),
  104. );
  105. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  106. db_create_table($chado_entity_table, $schema);
  107. }
  108. /**
  109. * Implements hook_bundle_delete().
  110. *
  111. */
  112. function tripal_chado_bundle_delete($bundle) {
  113. // Remove the entries in the appropriate chado entity table
  114. // and tripal_entity
  115. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  116. $sql = "DROP TABLE {$chado_entity_table}";
  117. db_query($sql);
  118. // Remove the entry from the chado_bundle table.
  119. db_delete('chado_bundle')
  120. ->condition('bundle_id', $bundle->id)
  121. ->execute();
  122. }