tripal_chado.term_storage.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Implements hook_vocab_storage_info().
  4. */
  5. function tripal_chado_vocab_storage_info() {
  6. return array(
  7. 'term_chado_storage' => array(
  8. 'label' => t('Chado storage'),
  9. 'module' => 'tripal_chado',
  10. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  11. 'settings' => array(),
  12. ),
  13. );
  14. }
  15. /**
  16. * Implements hook_vocab_get_term().
  17. */
  18. function tripal_chado_vocab_get_term($namespace, $accession) {
  19. $match = array(
  20. 'dbxref_id' => array(
  21. 'db_id' => array(
  22. 'name' => $namespace,
  23. ),
  24. 'accession' => $accession,
  25. ),
  26. );
  27. $cvterm = chado_generate_var('cvterm', $match);
  28. if (!$cvterm) {
  29. return NULL;
  30. }
  31. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  32. return array(
  33. 'namespace' => $cvterm->dbxref_id->db_id->name,
  34. 'accession' => $cvterm->dbxref_id->accession,
  35. 'name' => $cvterm->name,
  36. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  37. 'urlprefix' => $cvterm->dbxref_id->db_id->urlprefix,
  38. // The following are not required for the returned array but we'll
  39. // add these for convenience later when we look at the TripalTerm
  40. // objects and these will be there.
  41. 'cvterm' => $cvterm,
  42. );
  43. }
  44. /**
  45. * Implements hook_vocab_select_term_form().
  46. */
  47. function tripal_chado_vocab_select_term_form($form, &$form_state) {
  48. $term_name = '';
  49. $num_terms = 0;
  50. $cv_id = '';
  51. $terms = array();
  52. // Set defaults using the form state.
  53. if (array_key_exists('storage', $form_state)) {
  54. if (array_key_exists('terms', $form_state['storage'])) {
  55. $terms = $form_state['storage']['terms'];
  56. }
  57. }
  58. $num_terms = count($terms);
  59. // If no term has been selected yet then provide the auto complete field.
  60. if ($num_terms == 0) {
  61. $form['term_name'] = array(
  62. '#title' => t('Content Type'),
  63. '#type' => 'textfield',
  64. '#description' => t("The content type must be the name of a term in
  65. a controlled vocabulary and the controlled vocabulary should
  66. already be loaded into Tripal. For example, to create a content
  67. type for storing 'genes', use the 'gene' term from the
  68. Sequence Ontology (SO)."),
  69. '#required' => TRUE,
  70. '#default_value' => $term_name,
  71. '#autocomplete_path' => "admin/tripal/storage/term/$cv_id",
  72. );
  73. }
  74. // If the term belongs to more than one vocabulary then add additional fields
  75. // to let the user select the vocabulary.
  76. if ($num_terms > 1) {
  77. $form['term_name'] = array(
  78. '#type' => 'hidden',
  79. '#value' => $term_name,
  80. );
  81. $cvs = array();
  82. foreach ($terms as $term) {
  83. $cvs[$term->cv_id->cv_id] = 'Vocabulary: <b>' . $term->cv_id->name . '</b> (' . $term->cv_id->definition . ')<br>' . $term->name . ': ' . $term->definition;
  84. }
  85. $form['cv_id'] = array(
  86. '#type' => 'radios',
  87. '#title' => t('Select the appropriate vocabulary'),
  88. '#options' => $cvs,
  89. );
  90. }
  91. // Add in the button for the cases of no terms or too many.
  92. $form['select_button'] = array(
  93. '#type' => 'submit',
  94. '#value' => t('Use this term'),
  95. '#name' => 'select_cvterm'
  96. );
  97. return $form;
  98. }
  99. /**
  100. * Implements hook_vocab_select_term_form_validate().
  101. */
  102. function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {
  103. if (array_key_exists('clicked_button', $form_state) and
  104. $form_state['clicked_button']['#name'] =='select_cvterm') {
  105. // First, make sure the term is unique. If not then we can't check it.
  106. $term_name = NULL;
  107. $cv_id = NULL;
  108. $cvterm = NULL;
  109. if (array_key_exists('term_name', $form_state['values'])) {
  110. $term_name = $form_state['input']['term_name'];
  111. }
  112. if (array_key_exists('cv_id', $form_state['input'])) {
  113. $cv_id = $form_state['input']['cv_id'];
  114. }
  115. // If a term and $cv_id are provided then we can look for the term using
  116. // both and we should find a unique term. If only ther term is provided
  117. // we can still look for a unique term but there must only be one.
  118. if ($term_name and !$cv_id) {
  119. $match = array(
  120. 'name' => $term_name,
  121. );
  122. }
  123. else {
  124. $match = array(
  125. 'name' => $term_name,
  126. 'cv_id' => $cv_id,
  127. );
  128. }
  129. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  130. $form_state['storage']['terms'] = $terms;
  131. // If we do not have any terms then the term provided by the user does not
  132. // exists and we need to provide an error message.
  133. if (count($terms) == 0) {
  134. form_set_error('term_name', t('The term does not exist in this database.'));
  135. }
  136. // If we have more than one term then we need to set an error so that the
  137. // form can provide a list of vocabularies to select from.
  138. if (count($terms) > 1) {
  139. form_set_error('term_name', t('The term is not unique. A list of vocabularies
  140. that contain this term. Please select the most appropriate vocabulary.'));
  141. }
  142. // If we have a unique term then set the namespace, accession and name.
  143. if (count($terms) == 1) {
  144. $form_state['storage']['namespace'] = $terms[0]->dbxref_id->db_id->name;
  145. $form_state['storage']['accession'] = $terms[0]->dbxref_id->accession;
  146. $form_state['storage']['term_name'] = $terms[0]->name;
  147. }
  148. }
  149. }