tripal_entities.api.inc 9.5 KB

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