primary_dbxref.inc 8.6 KB

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