tripal_chado.term_storage.inc 6.0 KB

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