primary_dbxref.inc 7.9 KB

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