dbxref_id.inc 11 KB

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