primary_dbxref.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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, array('attributes' => array('target' => '_blank')));
  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. // If we are here because our parent was triggered in a form submit
  64. // then that means an ajax call was made and we don't want the fieldset to
  65. // be closed when it returns from the ajax call.
  66. $collapsed = TRUE;
  67. if (array_key_exists('triggering_element', $form_state) and
  68. $form_state['triggering_element']['#parents'][0] == $field_name) {
  69. $collapsed = FALSE;
  70. }
  71. if ($dbxref_id) {
  72. $collapsed = FALSE;
  73. }
  74. $schema = chado_get_schema('dbxref');
  75. $options = tripal_get_db_select_options();
  76. $widget += array(
  77. '#element_validate' => array('tripal_fields_primary_dbxref_widget_validate'),
  78. '#type' => 'fieldset',
  79. '#title' => $element['#title'],
  80. '#description' => $element['#description'],
  81. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  82. '#delta' => $delta,
  83. '#theme' => 'tripal_fields_primary_dbxref_widget',
  84. '#group' => 'entity_vetical_tabs',
  85. '#collapsible' => TRUE,
  86. '#collapsed' => $collapsed,
  87. array(
  88. $element['#field_name'] => array(
  89. '#type' => 'hidden',
  90. '#default_value' => $dbxref_id,
  91. ),
  92. 'dbxref__db_id' => array(
  93. '#type' => 'select',
  94. '#title' => t('Database'),
  95. '#options' => $options,
  96. '#required' => $element['#required'],
  97. '#default_value' => $db_id,
  98. '#ajax' => array(
  99. 'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
  100. 'wrapper' => "$field_name-dbxref--db-id",
  101. 'effect' => 'fade',
  102. 'method' => 'replace'
  103. )
  104. ),
  105. 'dbxref__accession' => array(
  106. '#type' => 'textfield',
  107. '#title' => t('Accession'),
  108. '#default_value' => $accession,
  109. '#required' => $element['#required'],
  110. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  111. '#size' => 15,
  112. '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
  113. '#ajax' => array(
  114. 'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
  115. 'wrapper' => "$field_name-dbxref--db-id",
  116. 'effect' => 'fade',
  117. 'method' => 'replace'
  118. )
  119. ),
  120. 'dbxref__version' => array(
  121. '#type' => 'textfield',
  122. '#title' => t('Version'),
  123. '#default_value' => $version,
  124. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  125. '#size' => 5,
  126. ),
  127. 'dbxref__description' => array(
  128. '#type' => 'textfield',
  129. '#title' => t('Description'),
  130. '#default_value' => $description,
  131. '#size' => 20,
  132. ),
  133. 'links' => array(
  134. '#type' => 'item',
  135. '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attrbutes', array('target' => '_blank')))
  136. )
  137. ),
  138. '#prefix' => "<span id='$field_name-dbxref--db-id'>",
  139. '#suffix' => "</span>"
  140. );
  141. return $widget;
  142. }
  143. /**
  144. * An Ajax callback for the tripal_fields_admin_publish_form..
  145. */
  146. function tripal_fields_primary_dbxref_widget_form_ajax_callback($form, $form_state) {
  147. $field_name = $form_state['triggering_element']['#parents'][0];
  148. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
  149. $accession = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__accession');
  150. if (count($db_id) > 0 and count($accession) > 0) {
  151. $values = array(
  152. 'db_id' => $db_id[0],
  153. 'accession' => $accession[0],
  154. );
  155. $options = array('is_duplicate' => TRUE);
  156. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  157. if (!$has_duplicate) {
  158. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  159. }
  160. }
  161. return $form[$field_name];
  162. }
  163. /**
  164. * Callback function for validating the tripal_fields_organism_select_widget.
  165. */
  166. function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
  167. $field_name = $element['#field_name'];
  168. // If the form ID is field_ui_field_edit_form, then the user is editing the
  169. // field's values in the manage fields form of Drupal. We don't want
  170. // to validate it as if it were being used in a data entry form.
  171. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  172. return;
  173. }
  174. // Get the field values.
  175. $dbxref_id = tripal_fields_get_field_form_values($field_name, $form_state, $field_name);
  176. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__db_id");
  177. $accession = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__accession");
  178. $version = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__version");
  179. $description = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__description");
  180. // Make sure that if a database ID is provided that an accession is also
  181. // provided. Here we use the form_set_error function rather than the
  182. // form_error function because the form_error will add a red_highlight
  183. // around all of the fields in the fieldset which is confusing as it's not
  184. // clear to the user what field is required and which isn't. Therefore,
  185. // we borrow the code from the 'form_error' function and append the field
  186. // so that the proper field is highlighted on error.
  187. if (count($db_id) == 0 and count($accession) > 0) {
  188. 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."));
  189. }
  190. if (count($db_id) > 0 and count($accession) == 0) {
  191. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  192. }
  193. // If user did not select a database, we want to remove dbxref_id from the
  194. // field.
  195. if (count($db_id) == 0) {
  196. tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__', $field_name);
  197. }
  198. // If the dbxref_id does not match the db_id + accession then the user
  199. // has selected a new dbxref record and we need to update the hidden
  200. // value accordingly.
  201. if (count($db_id) == 1 and count($accession) == 1) {
  202. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id[0], 'accession' => $accession[0]));
  203. if ($dbxref->dbxref_id != $dbxref_id[0]) {
  204. tripal_fields_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $field_name);
  205. }
  206. }
  207. }
  208. /**
  209. * Theme function for the primary_dbxref_widget.
  210. *
  211. * @param $variables
  212. */
  213. function theme_tripal_fields_primary_dbxref_widget($variables) {
  214. $element = $variables['element'];
  215. $layout = "
  216. <div class=\"primary-dbxref-widget\">
  217. <div class=\"primary-dbxref-widget-item\">" .
  218. drupal_render($element[0]['dbxref__db_id']) . "
  219. </div>
  220. <div class=\"primary-dbxref-widget-item\">" .
  221. drupal_render($element[0]['dbxref__accession']) . "
  222. </div>
  223. <div class=\"primary-dbxref-widget-item\">" .
  224. drupal_render($element[0]['dbxref__version']) . "
  225. </div>
  226. <div class=\"primary-dbxref-widget-item\">" .
  227. drupal_render($element[0]['dbxref__description']) . "
  228. </div>
  229. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element[0]['links']) . "</div>
  230. </div>
  231. ";
  232. return $layout;
  233. }