dbxref.inc 11 KB

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