primary_dbxref.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. *
  4. * @param unknown $entity_type
  5. * @param unknown $entity
  6. * @param unknown $field
  7. * @param unknown $instance
  8. * @param unknown $langcode
  9. * @param unknown $items
  10. * @param unknown $display
  11. */
  12. function tripal_fields_primary_dbxref_formatter($entity_type, $entity, $field,
  13. $instance, $langcode, $items, $display) {
  14. foreach ($items as $delta => $item) {
  15. $accession = '';
  16. if ($item['value']) {
  17. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $item['value']));
  18. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  19. if ($dbxref->db_id->urlprefix) {
  20. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  21. }
  22. }
  23. }
  24. return $accession;
  25. }
  26. /**
  27. *
  28. * @param unknown $field_name
  29. * @param unknown $widget
  30. * @param unknown $form
  31. * @param unknown $form_state
  32. * @param unknown $field
  33. * @param unknown $instance
  34. * @param unknown $langcode
  35. * @param unknown $items
  36. * @param unknown $delta
  37. * @param unknown $element
  38. */
  39. function tripal_fields_primary_dbxref_widget($field_name, &$widget,
  40. &$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  41. // Get the field defaults from the database if a record exists.
  42. $dbxref_id = '';
  43. $db_id = '';
  44. $accession = '';
  45. $version = '';
  46. $description = '';
  47. if (count($items) > 0 and $items[0]['value']) {
  48. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
  49. $dbxref_id = $dbxref->dbxref_id;
  50. $db_id = $dbxref->db_id->db_id;
  51. $accession = $dbxref->accession;
  52. $version = $dbxref->version;
  53. $description = $dbxref->description;
  54. }
  55. $temp = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
  56. if (count($temp) > 0) {
  57. $db_id = $temp[0];
  58. }
  59. $schema = chado_get_schema('dbxref');
  60. $options = tripal_get_db_select_options();
  61. $widget += array(
  62. '#element_validate' => array('tripal_fields_primary_dbxref_widget_validate'),
  63. '#type' => 'fieldset',
  64. '#title' => $element['#title'],
  65. '#description' => $element['#description'],
  66. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  67. '#delta' => $delta,
  68. '#theme' => 'tripal_fields_primary_dbxref_widget',
  69. # '#group' => 'entity_vetical_tabs',
  70. array(
  71. $element['#field_name'] => array(
  72. '#type' => 'hidden',
  73. '#default_value' => $dbxref_id,
  74. ),
  75. 'dbxref__db_id' => array(
  76. '#type' => 'select',
  77. '#title' => t('Database'),
  78. '#options' => $options,
  79. '#required' => $element['#required'],
  80. '#default_value' => $db_id,
  81. '#ajax' => array(
  82. 'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
  83. 'wrapper' => "$field_name-dbxref--db-id",
  84. 'effect' => 'fade',
  85. 'method' => 'replace'
  86. )
  87. ),
  88. 'dbxref__accession' => array(
  89. '#type' => 'textfield',
  90. '#title' => t('Accession'),
  91. '#default_value' => $accession,
  92. '#required' => $element['#required'],
  93. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  94. '#size' => 15,
  95. '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
  96. '#ajax' => array(
  97. 'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
  98. 'wrapper' => "$field_name-dbxref--db-id",
  99. 'effect' => 'fade',
  100. 'method' => 'replace'
  101. )
  102. ),
  103. 'dbxref__version' => array(
  104. '#type' => 'textfield',
  105. '#title' => t('Version'),
  106. '#default_value' => $version,
  107. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  108. '#size' => 5,
  109. ),
  110. 'dbxref__description' => array(
  111. '#type' => 'textfield',
  112. '#title' => t('Description'),
  113. '#default_value' => $description,
  114. '#size' => 20,
  115. ),
  116. ),
  117. '#prefix' => "<span id='$field_name-dbxref--db-id'>",
  118. '#suffix' => "</span>"
  119. );
  120. }
  121. /**
  122. * An Ajax callback for the tripal_fields_admin_publish_form..
  123. */
  124. function tripal_fields_primary_dbxref_widget_form_ajax_callback($form, $form_state) {
  125. $field_name = $form_state['triggering_element']['#parents'][0];
  126. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
  127. $accession = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__accession');
  128. if (count($db_id) > 0 and count($accession) > 0) {
  129. $values = array(
  130. 'db_id' => $db_id[0],
  131. 'accession' => $accession[0],
  132. );
  133. $options = array('is_duplicate' => TRUE);
  134. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  135. if (!$has_duplicate) {
  136. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  137. }
  138. }
  139. return $form[$field_name];
  140. }
  141. /**
  142. * Callback function for validating the tripal_fields_organism_select_widget.
  143. */
  144. function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
  145. $field_name = $element['#field_name'];
  146. // If the form ID is field_ui_field_edit_form, then the user is editing the
  147. // field's values in the manage fields form of Drupal. We don't want
  148. // to validate it as if it were being used in a data entry form.
  149. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  150. return;
  151. }
  152. // Get the field values.
  153. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__db_id");
  154. $accession = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__accession");
  155. $version = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__version");
  156. $description = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__description");
  157. // Make sure that if a database ID is provided that an accession is also
  158. // provided. Here we use the form_set_error function rather than the
  159. // form_error function because the form_error will add a red_highlight
  160. // around all of the fields in the fieldset which is confusing as it's not
  161. // clear to the user what field is required and which isn't. Therefore,
  162. // we borrow the code from the 'form_error' function and append the field
  163. // so that the proper field is highlighted on error.
  164. if (count($db_id) == 0 and count($accession) > 0) {
  165. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  166. }
  167. if (count($db_id) > 0 and count($accession) == 0) {
  168. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  169. }
  170. // If user did not select a database, we want to remove dbxref_id from the
  171. // field.
  172. if (count($db_id) == 0) {
  173. tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__', $field_name);
  174. }
  175. }