dbxref_id.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_dbxref_id_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_dbxref_id_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  43. $field_name = $field['field_name'];
  44. // Get the field defaults.
  45. $dbxref_id = $items[0]['value'];
  46. $db_id = $items[0]['dbxref__db_id'];
  47. $accession = $items[0]['dbxref__accession'];
  48. $version = $items[0]['dbxref__version'];
  49. $description = $items[0]['dbxref__description'];
  50. // If we are here because our parent was triggered in a form submit
  51. // then that means an ajax call was made and we don't want the fieldset to
  52. // be closed when it returns from the ajax call.
  53. $collapsed = TRUE;
  54. if (array_key_exists('triggering_element', $form_state) and
  55. $form_state['triggering_element']['#parents'][0] == $field_name) {
  56. $collapsed = FALSE;
  57. }
  58. if ($dbxref_id) {
  59. $collapsed = FALSE;
  60. }
  61. $schema = chado_get_schema('dbxref');
  62. $options = tripal_get_db_select_options();
  63. $widget['#element_validate'] = array('tripal_fields_dbxref_id_widget_validate');
  64. $widget['#theme'] = 'tripal_fields_dbxref_id_widget';
  65. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  66. $widget['#suffix'] = "</span>";
  67. // A temporary element used for theming the fieldset.
  68. $widget['#theme_settings'] = array(
  69. '#title' => $element['#title'],
  70. '#description' => $element['#description'],
  71. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  72. '#theme' => 'tripal_fields_dbxref_id_widget',
  73. '#collapsible' => TRUE,
  74. '#collapsed' => $collapsed,
  75. );
  76. $widget['value'] = array(
  77. '#type' => 'hidden',
  78. '#default_value' => $dbxref_id,
  79. );
  80. $widget['dbxref__db_id'] = array(
  81. '#type' => 'select',
  82. '#title' => t('Database'),
  83. '#options' => $options,
  84. '#required' => $element['#required'],
  85. '#default_value' => $db_id,
  86. '#ajax' => array(
  87. 'callback' => "tripal_fields_dbxref_id_widget_form_ajax_callback",
  88. 'wrapper' => "$field_name-dbxref--db-id",
  89. 'effect' => 'fade',
  90. 'method' => 'replace'
  91. ),
  92. );
  93. $widget['dbxref__accession'] = array(
  94. '#type' => 'textfield',
  95. '#title' => t('Accession'),
  96. '#default_value' => $accession,
  97. '#required' => $element['#required'],
  98. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  99. '#size' => 15,
  100. '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
  101. '#ajax' => array(
  102. 'callback' => "tripal_fields_dbxref_id_widget_form_ajax_callback",
  103. 'wrapper' => "$field_name-dbxref--db-id",
  104. 'effect' => 'fade',
  105. 'method' => 'replace'
  106. )
  107. );
  108. $widget['dbxref__version'] = array(
  109. '#type' => 'textfield',
  110. '#title' => t('Version'),
  111. '#default_value' => $version,
  112. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  113. '#size' => 5,
  114. );
  115. $widget['dbxref__description'] = array(
  116. '#type' => 'textfield',
  117. '#title' => t('Description'),
  118. '#default_value' => $description,
  119. '#size' => 20,
  120. );
  121. $widget['links'] = array(
  122. '#type' => 'item',
  123. '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  124. );
  125. }
  126. /**
  127. * An Ajax callback for the tripal_fields_admin_publish_form..
  128. */
  129. function tripal_fields_dbxref_id_widget_form_ajax_callback($form, $form_state) {
  130. $field_name = $form_state['triggering_element']['#parents'][0];
  131. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
  132. $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
  133. if ($db_id and $accession) {
  134. $values = array(
  135. 'db_id' => $db_id,
  136. 'accession' => $accession,
  137. );
  138. $options = array('is_duplicate' => TRUE);
  139. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  140. if (!$has_duplicate) {
  141. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  142. }
  143. }
  144. return $form[$field_name];
  145. }
  146. /**
  147. * Callback function for validating the tripal_fields_organism_select_widget.
  148. */
  149. function tripal_fields_dbxref_id_widget_validate($element, &$form_state) {
  150. $field_name = $element['#parents'][0];
  151. // If the form ID is field_ui_field_edit_form, then the user is editing the
  152. // field's values in the manage fields form of Drupal. We don't want
  153. // to validate it as if it were being used in a data entry form.
  154. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  155. return;
  156. }
  157. // Get the field values.
  158. $dbxref_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, $field_name);
  159. $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
  160. $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
  161. $version = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
  162. $description = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
  163. // Make sure that if a database ID is provided that an accession is also
  164. // provided. Here we use the form_set_error function rather than the
  165. // form_error function because the form_error will add a red_highlight
  166. // around all of the fields in the fieldset which is confusing as it's not
  167. // clear to the user what field is required and which isn't. Therefore,
  168. // we borrow the code from the 'form_error' function and append the field
  169. // so that the proper field is highlighted on error.
  170. if (!$db_id and $accession) {
  171. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  172. }
  173. if ($db_id and !$accession) {
  174. form_set_error(implode('][', $element ['#parents']) . '][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  175. }
  176. if (!$db_id and !$accession and ($version or $description)) {
  177. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  178. }
  179. // If user did not select a database, we want to remove dbxref_id from the
  180. // field.
  181. if (!$db_id) {
  182. tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__');
  183. }
  184. // If the dbxref_id does not match the db_id + accession then the user
  185. // has selected a new dbxref record and we need to update the hidden
  186. // value accordingly.
  187. if ($db_id and $accession) {
  188. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  189. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  190. tripal_fields_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id);
  191. }
  192. }
  193. }
  194. /**
  195. * Theme function for the dbxref_id_widget.
  196. *
  197. * @param $variables
  198. */
  199. function theme_tripal_fields_dbxref_id_widget($variables) {
  200. $element = $variables['element'];
  201. $layout = "
  202. <div class=\"primary-dbxref-widget\">
  203. <div class=\"primary-dbxref-widget-item\">" .
  204. drupal_render($element['dbxref__db_id']) . "
  205. </div>
  206. <div class=\"primary-dbxref-widget-item\">" .
  207. drupal_render($element['dbxref__accession']) . "
  208. </div>
  209. <div class=\"primary-dbxref-widget-item\">" .
  210. drupal_render($element['dbxref__version']) . "
  211. </div>
  212. <div class=\"primary-dbxref-widget-item\">" .
  213. drupal_render($element['dbxref__description']) . "
  214. </div>
  215. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  216. </div>
  217. ";
  218. $classes = array();
  219. $classes[] = 'collapsible';
  220. $theme_settings = $element['#theme_settings'];
  221. if ($theme_settings['#collapsed'] == TRUE) {
  222. $classes[] = 'collapsed';
  223. }
  224. $fieldset = array(
  225. '#title' => $element['#title'],
  226. '#value' => '',
  227. '#description' => $element['#description'],
  228. '#children' => $layout,
  229. '#attributes' => array('class' => $classes),
  230. );
  231. return theme('fieldset', array('element' => $fieldset));
  232. }
  233. /**
  234. * Loads the field values with appropriate data.
  235. *
  236. * This function is called by the tripal_fields_field_storage_load() for
  237. * each property managed by the field_chado_storage storage type.
  238. *
  239. * @param $field
  240. * @param $entity
  241. * @param $record
  242. */
  243. function tripal_fields_dbxref_id_field_load($field, $entity, $record) {
  244. $field_name = $field['field_name'];
  245. $field_type = $field['type'];
  246. $field_table = $field['settings']['chado_table'];
  247. $field_column = $field['settings']['chado_column'];
  248. // Get the primary dbxref record. Because we have a dbxref_id passed in
  249. // by the base record, we will only have one record.
  250. $columns = array('*');
  251. $match = array('dbxref_id' => $record->$field_column);
  252. $options = array('return_array' => TRUE);
  253. $records = chado_select_record('dbxref', $columns, $match, $options);
  254. if (count($records) > 0) {
  255. $dbxref = $records[0];
  256. $entity->{$field_name}['und'][0] = array(
  257. 'value' => $dbxref->dbxref_id,
  258. 'dbxref__db_id' => $dbxref->db_id,
  259. 'dbxref__accession' => $dbxref->accession,
  260. 'dbxref__version' => $dbxref->version,
  261. 'dbxref__description' => $dbxref->description,
  262. );
  263. }
  264. }