tripal_entities.api.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. * An entity is created by first adding a record to the tripal_term and
  49. * tripal_vocabulary tables. This helps Tripal keep track of which terms
  50. * are used in which tables. Second, records are added to the tripal_bundle
  51. * table which is where Tripal keeps track of all of the entity_types. Finally,
  52. * the hook_add_bundle_fields() is called that allows other modules to
  53. * add fields to the entity.
  54. *
  55. * @param $cvterm
  56. * A cvterm object created using the chado_generate_var() function.
  57. * @param $error
  58. * A string, passed by reference, that is filled with the error message
  59. * if the function fails.
  60. *
  61. * @return
  62. * TRUE if the entity type (bundle) was succesfully created. FALSE otherwise.
  63. */
  64. function tripal_create_entity_type($cvterm, &$error = '') {
  65. // Before creating the entity we must add records to the tripal_vocabulary
  66. // tripal_vocabulary_usage, tripal_term, and tripal_term_usage tables.
  67. $match = array('cv_id' => $cvterm->cv_id->cv_id);
  68. $vocab = chado_select_record('tripal_vocabulary', array('*'), $match);
  69. if (count($vocab) == 0) {
  70. $values = array(
  71. 'cv_id' => $cvterm->cv_id->cv_id,
  72. 'db_id' => $cvterm->dbxref_id->db_id->db_id,
  73. 'publish' => 1,
  74. );
  75. $values = chado_insert_record('tripal_vocabulary', $values);
  76. if (!$values) {
  77. $error = 'Could not add vocabulary to tripal_vocabluary table.';
  78. return FALSE;
  79. }
  80. // Convert the values array into an object.
  81. $vocab = new stdClass();
  82. $vocab->vocabulary_id = $values['vocabulary_id'];
  83. $vocab->cv_id = $values['cv_id'];
  84. }
  85. else {
  86. // Make sure the vocabulary is set to publish
  87. $values = array('publish' => 1);
  88. chado_update_record('tripal_vocabulary', $match, $values);
  89. $vocab = $vocab[0];
  90. }
  91. // Look to see if this vocabulary is used as a default for any table. If
  92. // so then we can use that to populate the tripal_vocabulary_usage table.
  93. $default = db_select('tripal_cv_defaults', 't')
  94. ->fields('t')
  95. ->condition('cv_id', $vocab->cv_id)
  96. ->execute()
  97. ->fetchObject();
  98. if ($default) {
  99. $values = array(
  100. 'vocabulary_id' => $vocab->vocabulary_id,
  101. 'data_table' => $default->table_name,
  102. 'type_table' => $default->table_name,
  103. 'field' => $default->field_name,
  104. );
  105. $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $values);
  106. if (count($vocab_usage) == 0) {
  107. $values = chado_insert_record('tripal_vocabulary_usage', $values);
  108. if (!$values) {
  109. $error = 'Could not add vocabulary to tripal_vocabulary_usage table.';
  110. return FALSE;
  111. }
  112. $vocab_usage = new stdClass();
  113. $vocab_usage->vocabulary_id = $values['vocabulary_id'];
  114. $vocab_usage->data_table = $values['data_table'];
  115. $vocab_usage->type_table = $values['type_table'];
  116. $vocab_usage->field = $values['field'];
  117. }
  118. }
  119. // The organism table does not have a type_id so we won't ever find
  120. // a record for it in the tripal_cv_defaults table.
  121. else if ($cvterm->name == 'organism') {
  122. $values = array(
  123. 'vocabulary_id' => $vocab->vocabulary_id,
  124. 'data_table' => 'organism',
  125. 'type_table' => 'organism',
  126. 'field' => '',
  127. );
  128. $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $values);
  129. if (count($vocab_usage) == 0) {
  130. $values = chado_insert_record('tripal_vocabulary_usage', $values);
  131. if (!$values) {
  132. $error = 'Could not add vocabulary to tripal_vocabulary_usage table.';
  133. return FALSE;
  134. }
  135. $vocab_usage = new stdClass();
  136. $vocab_usage->vocabulary_id = $values['vocabulary_id'];
  137. $vocab_usage->data_table = $values['data_table'];
  138. $vocab_usage->type_table = $values['type_table'];
  139. $vocab_usage->field = $values['field'];
  140. }
  141. }
  142. // The analysis table does not have a type_id so we won't ever find
  143. // a record for it in the tripalcv_defaults table.
  144. else if ($cvterm->name == 'analysis') {
  145. $values = array(
  146. 'vocabulary_id' => $vocab->vocabulary_id,
  147. 'data_table' => 'analysis',
  148. 'type_table' => 'analysis',
  149. 'field' => '',
  150. );
  151. $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $values);
  152. if (count($vocab_usage) == 0) {
  153. $values = chado_insert_record('tripal_vocabulary_usage', $values);
  154. if (!$values) {
  155. $error = 'Could not add vocabulary to tripal_vocabulary_usage table.';
  156. return FALSE;
  157. }
  158. $vocab_usage = new stdClass();
  159. $vocab_usage->vocabulary_id = $values['vocabulary_id'];
  160. $vocab_usage->data_table = $values['data_table'];
  161. $vocab_usage->type_table = $values['type_table'];
  162. $vocab_usage->field = $values['field'];
  163. }
  164. }
  165. // If there is no default table then we have an error, and we should
  166. // set a variable so that the form can help the user deal with the problem.
  167. else {
  168. $error = t('There is no default mapping of this term\'s ' .
  169. 'vocabulary to a table in Chado. Therefore, it is not possible to ' .
  170. 'determine how to store data of this type.');
  171. return FALSE;
  172. }
  173. // Now add the tripal_term record if it doesn't already exist.
  174. $match = array(
  175. 'vocabulary_id' => $vocab->vocabulary_id,
  176. 'cvterm_id' => $cvterm->cvterm_id,
  177. );
  178. $term = chado_select_record('tripal_term', array('*'), $match);
  179. if (count($term) == 0) {
  180. $values = array(
  181. 'vocabulary_id' => $vocab->vocabulary_id,
  182. 'cvterm_id' => $cvterm->cvterm_id,
  183. 'publish' => 1
  184. );
  185. $values = chado_insert_record('tripal_term', $values);
  186. if (!$values) {
  187. $error = 'Could not add term to tripal_term table.';
  188. return FALSE;
  189. }
  190. $term = new stdClass();
  191. $term->term_id = $values['term_id'];
  192. }
  193. else {
  194. $values = array('publish' => 1);
  195. chado_update_record('tripal_term', $match, $values);
  196. $term = $term[0];
  197. }
  198. // Finally, add the tripal_term_usage record if it doesn't already exist.
  199. $match = array('term_id' => $term->term_id);
  200. $options = array('has_record' => TRUE);
  201. if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
  202. $values = array(
  203. 'term_id' => $term->term_id,
  204. 'data_table' => $vocab_usage->data_table,
  205. 'type_table' => $vocab_usage->type_table,
  206. 'field' => $vocab_usage->field,
  207. );
  208. $values = chado_insert_record('tripal_term_usage', $values);
  209. if (!$values) {
  210. $error = 'Could not add term to tripal_term table.';
  211. return FALSE;
  212. }
  213. }
  214. // Clear the entity cache so that Drupal will read our
  215. // hook_entity_info() implementation which now will have the entities
  216. // described because we set the publish column to 1 in the tripal_term
  217. // table.
  218. global $language;
  219. $langcode = $language->language;
  220. cache_clear_all("entity_info:$langcode", 'cache');
  221. // Create the bundle name and entity type name. The bundle name is the
  222. // dbxref ID. This isn't very human readable, but the alternative is to
  223. // use the accession which may not always be alpha-numeric.
  224. $bundle_name = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
  225. // Check to see if this bundle exists. If not then create it
  226. $bundle = db_select('tripal_bundle', 't')
  227. ->fields('t')
  228. ->condition('type', 'BioData')
  229. ->condition('bundle', $bundle_name)
  230. ->execute()
  231. ->fetchObject();
  232. if (!$bundle) {
  233. // The TripalBundle Entity manages the bundles we have available.
  234. // Therefore, we need to add a new entity for each bundle "type".
  235. $vals = array(
  236. 'label' => $cvterm->name,
  237. 'type' => 'BioData',
  238. 'bundle' => $bundle_name,
  239. 'data' => serialize(array()),
  240. 'module' => 'tripal_entities'
  241. );
  242. $tripal_bundle = new TripalBundle($vals, 'BioData_bundles');
  243. $tripal_bundle->save();
  244. }
  245. // Allow modules to now add fields to the bundle
  246. module_invoke_all('add_bundle_fields', 'BioData', $bundle_name, $cvterm);
  247. return TRUE;
  248. }