tripal_chado.term_storage.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_select_term_form().
  17. */
  18. function tripal_chado_vocab_select_term_form($form, &$form_state) {
  19. $term_name = '';
  20. $num_terms = 0;
  21. $cv_id = '';
  22. $terms = array();
  23. // Set defaults using the form state.
  24. if (array_key_exists('storage', $form_state)) {
  25. if (array_key_exists('terms', $form_state['storage'])) {
  26. $terms = $form_state['storage']['terms'];
  27. }
  28. }
  29. $num_terms = count($terms);
  30. // If no term has been selected yet then provide the auto complete field.
  31. if ($num_terms == 0) {
  32. $form['term_name'] = array(
  33. '#title' => t('Content Type'),
  34. '#type' => 'textfield',
  35. '#description' => t("The content type must be the name of a term in
  36. a controlled vocabulary and the controlled vocabulary should
  37. already be loaded into Tripal. For example, to create a content
  38. type for storing 'genes', use the 'gene' term from the
  39. Sequence Ontology (SO)."),
  40. '#required' => TRUE,
  41. '#default_value' => $term_name,
  42. '#autocomplete_path' => "admin/tripal/vocab/cvterm/auto_name/$cv_id/",
  43. );
  44. }
  45. // If the term belongs to more than one vocabulary then add additional fields
  46. // to let the user select the vocabulary.
  47. if ($num_terms > 1) {
  48. $form['term_name'] = array(
  49. '#type' => 'hidden',
  50. '#value' => $term_name,
  51. );
  52. $cvs = array();
  53. foreach ($terms as $term) {
  54. $cvs[$term->cv_id->cv_id] = 'Vocabulary: <b>' . $term->cv_id->name . '</b> (' . $term->cv_id->definition . ')<br>' . $term->name . ': ' . $term->definition;
  55. }
  56. $form['cv_id'] = array(
  57. '#type' => 'radios',
  58. '#title' => t('Select the appropriate vocabulary'),
  59. '#options' => $cvs,
  60. );
  61. }
  62. // Add in the button for the cases of no terms or too many.
  63. $form['select_button'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Use this term'),
  66. '#name' => 'select_cvterm'
  67. );
  68. return $form;
  69. }
  70. /**
  71. * Implements hook_vocab_select_term_form_validate().
  72. */
  73. function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {
  74. if (array_key_exists('clicked_button', $form_state) and
  75. $form_state['clicked_button']['#name'] =='select_cvterm') {
  76. // First, make sure the term is unique. If not then we can't check it.
  77. $term_name = NULL;
  78. $cv_id = NULL;
  79. $cvterm = NULL;
  80. $term_id = NULL;
  81. if (array_key_exists('term_name', $form_state['values'])) {
  82. $term_name = $form_state['input']['term_name'];
  83. }
  84. if (array_key_exists('cv_id', $form_state['input'])) {
  85. $cv_id = $form_state['input']['cv_id'];
  86. }
  87. // If a term and $cv_id are provided then we can look for the term using
  88. // both and we should find a unique term. If only ther term is provided
  89. // we can still look for a unique term but there must only be one.
  90. if ($term_name and !$cv_id) {
  91. $match = array(
  92. 'name' => $term_name,
  93. );
  94. }
  95. else {
  96. $match = array(
  97. 'name' => $term_name,
  98. 'cv_id' => $cv_id,
  99. );
  100. }
  101. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  102. $form_state['storage']['terms'] = $terms;
  103. // If we do not have any terms then the term provided by the user does not
  104. // exists and we need to provide an error message.
  105. if (count($terms) == 0) {
  106. form_set_error('term_name', t('The term does not exist in this database.'));
  107. }
  108. // If we have more than one term then we need to set an error so that the
  109. // form can provide a list of vocabularies to select from.
  110. if (count($terms) > 1) {
  111. form_set_error('term_name', t('The term is not unique. A list of vocabularies
  112. that contain this term. Please select the most appropriate vocabulary.'));
  113. }
  114. // If we have a unique term then set the namespace, term_id and name.
  115. if (count($terms) == 1) {
  116. $form_state['storage']['namespace'] = $terms[0]->dbxref_id->db_id->name;
  117. $form_state['storage']['term_id'] = $terms[0]->dbxref_id->accession;
  118. $form_state['storage']['term_name'] = $terms[0]->name;
  119. }
  120. }
  121. }