primary_dbxref.inc 7.8 KB

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