tripal_entities.api.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. else {
  119. $vocab_usage = (object) $values;
  120. }
  121. }
  122. // The organism table does not have a type_id so we won't ever find
  123. // a record for it in the tripal_cv_defaults table.
  124. elseif ($cvterm->name == 'organism') {
  125. $values = array(
  126. 'vocabulary_id' => $vocab->vocabulary_id,
  127. 'data_table' => 'organism',
  128. 'type_table' => 'organism',
  129. 'field' => '',
  130. );
  131. $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $values);
  132. if (count($vocab_usage) == 0) {
  133. $values = chado_insert_record('tripal_vocabulary_usage', $values);
  134. if (!$values) {
  135. $error = 'Could not add vocabulary to tripal_vocabulary_usage table.';
  136. return FALSE;
  137. }
  138. $vocab_usage = new stdClass();
  139. $vocab_usage->vocabulary_id = $values['vocabulary_id'];
  140. $vocab_usage->data_table = $values['data_table'];
  141. $vocab_usage->type_table = $values['type_table'];
  142. $vocab_usage->field = $values['field'];
  143. }
  144. else {
  145. $vocab_usage = (object) $values;
  146. }
  147. }
  148. // The analysis table does not have a type_id so we won't ever find
  149. // a record for it in the tripalcv_defaults table.
  150. elseif ($cvterm->name == 'analysis') {
  151. $values = array(
  152. 'vocabulary_id' => $vocab->vocabulary_id,
  153. 'data_table' => 'analysis',
  154. 'type_table' => 'analysis',
  155. 'field' => '',
  156. );
  157. $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $values);
  158. if (count($vocab_usage) == 0) {
  159. $values = chado_insert_record('tripal_vocabulary_usage', $values);
  160. if (!$values) {
  161. $error = 'Could not add vocabulary to tripal_vocabulary_usage table.';
  162. return FALSE;
  163. }
  164. $vocab_usage = new stdClass();
  165. $vocab_usage->vocabulary_id = $values['vocabulary_id'];
  166. $vocab_usage->data_table = $values['data_table'];
  167. $vocab_usage->type_table = $values['type_table'];
  168. $vocab_usage->field = $values['field'];
  169. }
  170. else {
  171. $vocab_usage = (object) $values;
  172. }
  173. }
  174. // If there is no default table then we have an error, and we should
  175. // set a variable so that the form can help the user deal with the problem.
  176. else {
  177. $error = t('There is no default mapping of this term\'s ' .
  178. 'vocabulary to a table in Chado. Therefore, it is not possible to ' .
  179. 'determine how to store data of this type.');
  180. return FALSE;
  181. }
  182. // Now add the tripal_term record if it doesn't already exist.
  183. $match = array(
  184. 'vocabulary_id' => $vocab->vocabulary_id,
  185. 'cvterm_id' => $cvterm->cvterm_id,
  186. );
  187. $term = chado_select_record('tripal_term', array('*'), $match);
  188. if (count($term) == 0) {
  189. $values = array(
  190. 'vocabulary_id' => $vocab->vocabulary_id,
  191. 'cvterm_id' => $cvterm->cvterm_id,
  192. 'publish' => 1
  193. );
  194. $values = chado_insert_record('tripal_term', $values);
  195. if (!$values) {
  196. $error = 'Could not add term to tripal_term table.';
  197. return FALSE;
  198. }
  199. $term = new stdClass();
  200. $term->term_id = $values['term_id'];
  201. }
  202. else {
  203. $values = array('publish' => 1);
  204. chado_update_record('tripal_term', $match, $values);
  205. $term = $term[0];
  206. }
  207. // Finally, add the tripal_term_usage record if it doesn't already exist.
  208. $match = array('term_id' => $term->term_id);
  209. $options = array('has_record' => TRUE);
  210. if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
  211. $values = array(
  212. 'term_id' => $term->term_id,
  213. 'data_table' => $vocab_usage->data_table,
  214. 'type_table' => $vocab_usage->type_table,
  215. 'field' => $vocab_usage->field,
  216. );
  217. $values = chado_insert_record('tripal_term_usage', $values);
  218. if (!$values) {
  219. $error = 'Could not add term to tripal_term table.';
  220. return FALSE;
  221. }
  222. }
  223. // Clear the entity cache so that Drupal will read our
  224. // hook_entity_info() implementation which now will have the entities
  225. // described because we set the publish column to 1 in the tripal_term
  226. // table.
  227. global $language;
  228. $langcode = $language->language;
  229. cache_clear_all("entity_info:$langcode", 'cache');
  230. // Create the bundle name and entity type name. The bundle name is the
  231. // dbxref ID. This isn't very human readable, but the alternative is to
  232. // use the accession which may not always be alpha-numeric.
  233. $bundle_name = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
  234. // Check to see if this bundle exists. If not then create it
  235. $bundle = db_select('tripal_bundle', 't')
  236. ->fields('t')
  237. ->condition('type', 'BioData')
  238. ->condition('bundle', $bundle_name)
  239. ->execute()
  240. ->fetchObject();
  241. if (!$bundle) {
  242. // The TripalBundle Entity manages the bundles we have available.
  243. // Therefore, we need to add a new entity for each bundle "type".
  244. $vals = array(
  245. 'label' => $cvterm->name,
  246. 'type' => 'BioData',
  247. 'bundle' => $bundle_name,
  248. 'data' => serialize(array()),
  249. 'module' => 'tripal_entities'
  250. );
  251. $tripal_bundle = new TripalBundle($vals, 'BioData_bundles');
  252. $tripal_bundle->save();
  253. }
  254. // Allow modules to now add fields to the bundle
  255. module_invoke_all('add_bundle_fields', 'BioData', $bundle_name, $cvterm);
  256. return TRUE;
  257. }