tripal_chado.bundle.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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', array('chado_bundle_id'))
  37. ->condition('bundle_id', $bundle->id)
  38. ->execute()
  39. ->fetchField();
  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. $success = drupal_write_record('chado_bundle', $record);
  53. if (!$success) {
  54. throw new Exception('Cannot create content type. Problem associating type with Chado.');
  55. }
  56. }
  57. // Before adding fields to the bundle we need to story the Chado table that
  58. // this bundle maps to. That information is in the $args['form_values']
  59. // array. The $args['form_values'] array contains values that indicate the
  60. // Chado table.
  61. // TODO: we need to store the chado table and type field used for this
  62. // bundle.
  63. // Create/Add the new fields for this bundle.
  64. tripal_chado_bundle_create_fields($entity_type, $bundle, $record);
  65. // Create/Add the new field instances for this bundle.
  66. tripal_chado_bundle_create_instances($entity_type, $bundle, $record);
  67. }