tripal_chado.term_storage.inc 5.7 KB

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