tripal_entities.api.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * TODO: The code for creating the title needs to be updated to not
  4. * use nodes but rather entities.
  5. *
  6. * @param unknown $node
  7. * @return mixed
  8. */
  9. function chado_get_entity_title($entity) {
  10. // Get the base table for the entity
  11. $details = db_select('chado_entity', 'ce')
  12. ->fields('ce')
  13. ->condition('entity_id', $entity->id)
  14. ->execute()
  15. ->fetchObject();
  16. $tablename = $details->data_table;
  17. $type_field = $details->field;
  18. $schema = chado_get_schema($tablename);
  19. $pkey_field = $schema['primary key'][0];
  20. $record_id = $details->record_id;
  21. $record = chado_generate_var($tablename, array($pkey_field => $record_id));
  22. // TODO: fix this so it's native for entities and doesn't expect nodes.
  23. // Fake a node
  24. $node = new stdClass();
  25. $node->$tablename = $record;
  26. // Get the tokens and format
  27. $tokens = array(); // this will be set by chado_node_get_title_format
  28. $title = chado_node_get_title_format('chado_' . $tablename, $tokens);
  29. // Determine which tokens were used in the format string
  30. if (preg_match_all('/\[[^]]+\]/', $title, $used_tokens)) {
  31. // Get the value for each token used
  32. foreach ($used_tokens[0] as $token) {
  33. $token_info = $tokens[$token];
  34. if (!empty($token_info)) {
  35. $value = chado_get_token_value($token_info, $node);
  36. $title = str_replace($token, $value, $title);
  37. }
  38. }
  39. }
  40. else {
  41. return $title;
  42. }
  43. return $title;
  44. }
  45. /**
  46. * Creates a new Tripal Entity type (i.e. bundle).
  47. *
  48. * @param $cvterm
  49. * A cvterm object created using the chado_generate_var() function.
  50. * @param $error
  51. * A string, passed by reference, that is filled with the error message
  52. * if the function fails.
  53. *
  54. * @return
  55. * TRUE if the entity type (bundle) was succesfully created. FALSE otherwise.
  56. */
  57. function tripal_create_entity_type($cvterm, &$error = '') {
  58. // Create the bundle name and entity type name. The bundle name is the
  59. // cvterm ID. This isn't very human readable, but the alternative is to
  60. // use the accession which may not always be alpha-numeric.
  61. $bundle_id = 'bio-data_' . $cvterm->cvterm_id;
  62. // The organism table does not have a type_id so we won't ever find
  63. // a record for it in the tripal_cv_defaults table.
  64. $bundle_data = array();
  65. if ($cvterm->name == 'organism') {
  66. $bundle_data = array(
  67. 'cv_id' => $cvterm->cv_id->cv_id,
  68. 'cvterm_id' => $cvterm->cvterm_id,
  69. 'data_table' => 'organism',
  70. 'type_table' => 'organism',
  71. 'field' => '',
  72. );
  73. }
  74. // The analysis table does not have a type_id so we won't ever find
  75. // a record for it in the tripalcv_defaults table.
  76. else if ($cvterm->name == 'analysis') {
  77. $bundle_data = array(
  78. 'cv_id' => $cvterm->cv_id->cv_id,
  79. 'cvterm_id' => $cvterm->cvterm_id,
  80. 'data_table' => 'analysis',
  81. 'type_table' => 'analysis',
  82. 'field' => '',
  83. );
  84. }
  85. else if ($cvterm->name == 'project') {
  86. $bundle_data = array(
  87. 'cv_id' => $cvterm->cv_id->cv_id,
  88. 'cvterm_id' => $cvterm->cvterm_id,
  89. 'data_table' => 'project',
  90. 'type_table' => 'project',
  91. 'field' => '',
  92. );
  93. }
  94. else {
  95. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  96. // TABLES.
  97. // Look to see if this vocabulary is used as a default for any table.
  98. $default = db_select('tripal_cv_defaults', 't')
  99. ->fields('t')
  100. ->condition('cv_id', $cvterm->cv_id->cv_id)
  101. ->execute()
  102. ->fetchObject();
  103. if ($default) {
  104. $bundle_data = array(
  105. 'cv_id' => $cvterm->cv_id->cv_id,
  106. 'cvterm_id' => $cvterm->cvterm_id,
  107. 'data_table' => $default->table_name,
  108. 'type_table' => $default->table_name,
  109. 'field' => $default->field_name,
  110. );
  111. }
  112. // If there is no default table then we have an error, and we should
  113. // set a variable so that the form can help the user deal with the problem.
  114. else {
  115. $error = t('There is no default mapping of this term\'s ' .
  116. 'vocabulary to a table in Chado. Therefore, it is not possible to ' .
  117. 'determine how to store data of this type.');
  118. return FALSE;
  119. }
  120. }
  121. // Check to see if this bundle exists. If not then create it
  122. $bundle = db_select('tripal_bundle', 't')
  123. ->fields('t')
  124. ->condition('type', 'TripalEntity')
  125. ->condition('bundle', $bundle_id)
  126. ->execute()
  127. ->fetchObject();
  128. if (!$bundle) {
  129. db_insert('tripal_bundle')
  130. ->fields(array(
  131. 'label' => $cvterm->name,
  132. 'type' => 'TripalEntity',
  133. 'bundle' => $bundle_id,
  134. 'data' => serialize($bundle_data),
  135. 'module' => 'tripal_entities'
  136. ))
  137. ->execute();
  138. }
  139. // Clear the entity cache so that Drupal will read our
  140. // hook_entity_info() implementation.
  141. global $language;
  142. $langcode = $language->language;
  143. cache_clear_all("entity_info:$langcode", 'cache');
  144. variable_set('menu_rebuild_needed', TRUE);
  145. // Allow modules to now add fields to the bundle
  146. module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle_id, $cvterm);
  147. return TRUE;
  148. }