primary_dbxref.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  43. $field_name = $field['field_name'];
  44. // Get the field defaults from the database if a record exists.
  45. $dbxref_id = '';
  46. $db_id = '';
  47. $accession = '';
  48. $version = '';
  49. $description = '';
  50. if (count($items) > 0 and $items[0]['value']) {
  51. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
  52. $dbxref_id = $dbxref->dbxref_id;
  53. $db_id = $dbxref->db_id->db_id;
  54. $accession = $dbxref->accession;
  55. $version = $dbxref->version;
  56. $description = $dbxref->description;
  57. }
  58. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
  59. // If we are here because our parent was triggered in a form submit
  60. // then that means an ajax call was made and we don't want the fieldset to
  61. // be closed when it returns from the ajax call.
  62. $collapsed = TRUE;
  63. if (array_key_exists('triggering_element', $form_state) and
  64. $form_state['triggering_element']['#parents'][0] == $field_name) {
  65. $collapsed = FALSE;
  66. }
  67. if ($dbxref_id) {
  68. $collapsed = FALSE;
  69. }
  70. $schema = chado_get_schema('dbxref');
  71. $options = tripal_get_db_select_options();
  72. $widget['#element_validate'] = array('tripal_fields_primary_dbxref_widget_validate');
  73. $widget['#theme'] = 'tripal_fields_primary_dbxref_widget';
  74. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  75. $widget['#suffix'] = "</span>";
  76. // A temporary element used for theming the fieldset.
  77. $widget['#theme_settings'] = array(
  78. '#title' => $element['#title'],
  79. '#description' => $element['#description'],
  80. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  81. '#theme' => 'tripal_fields_primary_dbxref_widget',
  82. '#collapsible' => TRUE,
  83. '#collapsed' => $collapsed,
  84. );
  85. $widget['value'] = array(
  86. '#type' => 'hidden',
  87. '#default_value' => $dbxref_id,
  88. );
  89. $widget['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. $widget['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. $widget['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. $widget['dbxref__description'] = array(
  125. '#type' => 'textfield',
  126. '#title' => t('Description'),
  127. '#default_value' => $description,
  128. '#size' => 20,
  129. );
  130. $widget['links'] = array(
  131. '#type' => 'item',
  132. '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  133. );
  134. }
  135. /**
  136. * An Ajax callback for the tripal_fields_admin_publish_form..
  137. */
  138. function tripal_fields_primary_dbxref_widget_form_ajax_callback($form, $form_state) {
  139. $field_name = $form_state['triggering_element']['#parents'][0];
  140. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
  141. $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
  142. if ($db_id and $accession) {
  143. $values = array(
  144. 'db_id' => $db_id,
  145. 'accession' => $accession,
  146. );
  147. $options = array('is_duplicate' => TRUE);
  148. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  149. if (!$has_duplicate) {
  150. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  151. }
  152. }
  153. return $form[$field_name];
  154. }
  155. /**
  156. * Callback function for validating the tripal_fields_organism_select_widget.
  157. */
  158. function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
  159. $field_name = $element['#parents'][0];
  160. // If the form ID is field_ui_field_edit_form, then the user is editing the
  161. // field's values in the manage fields form of Drupal. We don't want
  162. // to validate it as if it were being used in a data entry form.
  163. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  164. return;
  165. }
  166. // Get the field values.
  167. $dbxref_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, $field_name);
  168. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
  169. $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
  170. $version = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
  171. $description = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
  172. // Make sure that if a database ID is provided that an accession is also
  173. // provided. Here we use the form_set_error function rather than the
  174. // form_error function because the form_error will add a red_highlight
  175. // around all of the fields in the fieldset which is confusing as it's not
  176. // clear to the user what field is required and which isn't. Therefore,
  177. // we borrow the code from the 'form_error' function and append the field
  178. // so that the proper field is highlighted on error.
  179. if (!$db_id and $accession) {
  180. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  181. }
  182. if ($db_id and !$accession) {
  183. form_set_error(implode('][', $element ['#parents']) . '][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  184. }
  185. if (!$db_id and !$accession and ($version or $description)) {
  186. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  187. }
  188. // If user did not select a database, we want to remove dbxref_id from the
  189. // field.
  190. if (!$db_id) {
  191. tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__');
  192. }
  193. // If the dbxref_id does not match the db_id + accession then the user
  194. // has selected a new dbxref record and we need to update the hidden
  195. // value accordingly.
  196. if ($db_id and $accession) {
  197. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  198. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  199. tripal_fields_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id);
  200. }
  201. }
  202. }
  203. /**
  204. * Theme function for the primary_dbxref_widget.
  205. *
  206. * @param $variables
  207. */
  208. function theme_tripal_fields_primary_dbxref_widget($variables) {
  209. $element = $variables['element'];
  210. $layout = "
  211. <div class=\"primary-dbxref-widget\">
  212. <div class=\"primary-dbxref-widget-item\">" .
  213. drupal_render($element['dbxref__db_id']) . "
  214. </div>
  215. <div class=\"primary-dbxref-widget-item\">" .
  216. drupal_render($element['dbxref__accession']) . "
  217. </div>
  218. <div class=\"primary-dbxref-widget-item\">" .
  219. drupal_render($element['dbxref__version']) . "
  220. </div>
  221. <div class=\"primary-dbxref-widget-item\">" .
  222. drupal_render($element['dbxref__description']) . "
  223. </div>
  224. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  225. </div>
  226. ";
  227. $classes = array();
  228. $classes[] = 'collapsible';
  229. $theme_settings = $element['#theme_settings'];
  230. if ($theme_settings['#collapsed'] == TRUE) {
  231. $classes[] = 'collapsed';
  232. }
  233. $fieldset = array(
  234. '#title' => $element['#title'],
  235. '#value' => '',
  236. '#description' => $element['#description'],
  237. '#children' => $layout,
  238. '#attributes' => array('class' => $classes),
  239. );
  240. return theme('fieldset', array('element' => $fieldset));
  241. }