tripal_chado.term_storage.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * This hooks is called by the tripal_entity module to retrieve information
  19. * about the term from the storage backend. It must return an array with
  20. * a set of keys.
  21. *
  22. * @param $namespace
  23. * The namespace of the vocabulary in which the term is found.
  24. * @param $term_id
  25. * The unique identifier (accession) for this term.
  26. *
  27. * @return
  28. * An array with at least the following keys:
  29. * id : The name unique ID of the term.
  30. * name : The name of the term.
  31. * definition : The term's description.
  32. * any other keys may be added as desired. Returns NULL if the term
  33. * cannot be found.
  34. */
  35. function tripal_chado_vocab_get_term($namespace, $term_id) {
  36. $match = array(
  37. 'dbxref_id' => array(
  38. 'db_id' => array(
  39. 'name' => $namespace,
  40. ),
  41. 'accession' => $term_id
  42. ),
  43. );
  44. $cvterm = chado_generate_var('cvterm', $match);
  45. if (!$cvterm) {
  46. return NULL;
  47. }
  48. $cvterm = chado_expand_var($cvterm, 'field', 'cveterm.definition');
  49. return array(
  50. 'id' => $cvterm->dbxref_id->accession,
  51. 'name' => $cvterm->name,
  52. 'definition' => $cvterm->definition,
  53. 'cvterm' => $cvterm,
  54. );
  55. }
  56. /**
  57. * Implements hook_vocab_select_term_form().
  58. */
  59. function tripal_chado_vocab_select_term_form($form, &$form_state) {
  60. $term_name = '';
  61. $num_terms = 0;
  62. $cv_id = '';
  63. $terms = array();
  64. // Set defaults using the form state.
  65. if (array_key_exists('storage', $form_state)) {
  66. if (array_key_exists('terms', $form_state['storage'])) {
  67. $terms = $form_state['storage']['terms'];
  68. }
  69. }
  70. $num_terms = count($terms);
  71. // If no term has been selected yet then provide the auto complete field.
  72. if ($num_terms == 0) {
  73. $form['term_name'] = array(
  74. '#title' => t('Content Type'),
  75. '#type' => 'textfield',
  76. '#description' => t("The content type must be the name of a term in
  77. a controlled vocabulary and the controlled vocabulary should
  78. already be loaded into Tripal. For example, to create a content
  79. type for storing 'genes', use the 'gene' term from the
  80. Sequence Ontology (SO)."),
  81. '#required' => TRUE,
  82. '#default_value' => $term_name,
  83. '#autocomplete_path' => "admin/tripal/vocab/cvterm/auto_name/$cv_id/",
  84. );
  85. }
  86. // If the term belongs to more than one vocabulary then add additional fields
  87. // to let the user select the vocabulary.
  88. if ($num_terms > 1) {
  89. $form['term_name'] = array(
  90. '#type' => 'hidden',
  91. '#value' => $term_name,
  92. );
  93. $cvs = array();
  94. foreach ($terms as $term) {
  95. $cvs[$term->cv_id->cv_id] = 'Vocabulary: <b>' . $term->cv_id->name . '</b> (' . $term->cv_id->definition . ')<br>' . $term->name . ': ' . $term->definition;
  96. }
  97. $form['cv_id'] = array(
  98. '#type' => 'radios',
  99. '#title' => t('Select the appropriate vocabulary'),
  100. '#options' => $cvs,
  101. );
  102. }
  103. // Add in the button for the cases of no terms or too many.
  104. $form['select_button'] = array(
  105. '#type' => 'submit',
  106. '#value' => t('Use this term'),
  107. '#name' => 'select_cvterm'
  108. );
  109. return $form;
  110. }
  111. /**
  112. * Implements hook_vocab_select_term_form_validate().
  113. */
  114. function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {
  115. if (array_key_exists('clicked_button', $form_state) and
  116. $form_state['clicked_button']['#name'] =='select_cvterm') {
  117. // First, make sure the term is unique. If not then we can't check it.
  118. $term_name = NULL;
  119. $cv_id = NULL;
  120. $cvterm = NULL;
  121. $term_id = NULL;
  122. if (array_key_exists('term_name', $form_state['values'])) {
  123. $term_name = $form_state['input']['term_name'];
  124. }
  125. if (array_key_exists('cv_id', $form_state['input'])) {
  126. $cv_id = $form_state['input']['cv_id'];
  127. }
  128. // If a term and $cv_id are provided then we can look for the term using
  129. // both and we should find a unique term. If only ther term is provided
  130. // we can still look for a unique term but there must only be one.
  131. if ($term_name and !$cv_id) {
  132. $match = array(
  133. 'name' => $term_name,
  134. );
  135. }
  136. else {
  137. $match = array(
  138. 'name' => $term_name,
  139. 'cv_id' => $cv_id,
  140. );
  141. }
  142. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  143. $form_state['storage']['terms'] = $terms;
  144. // If we do not have any terms then the term provided by the user does not
  145. // exists and we need to provide an error message.
  146. if (count($terms) == 0) {
  147. form_set_error('term_name', t('The term does not exist in this database.'));
  148. }
  149. // If we have more than one term then we need to set an error so that the
  150. // form can provide a list of vocabularies to select from.
  151. if (count($terms) > 1) {
  152. form_set_error('term_name', t('The term is not unique. A list of vocabularies
  153. that contain this term. Please select the most appropriate vocabulary.'));
  154. }
  155. // If we have a unique term then set the namespace, term_id and name.
  156. if (count($terms) == 1) {
  157. $form_state['storage']['namespace'] = $terms[0]->dbxref_id->db_id->name;
  158. $form_state['storage']['term_id'] = $terms[0]->dbxref_id->accession;
  159. $form_state['storage']['term_name'] = $terms[0]->name;
  160. }
  161. }
  162. }