dbxref_id.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_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_chado_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 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. }
  143. /**
  144. * An Ajax callback for the tripal_chado_admin_publish_form..
  145. */
  146. function tripal_chado_dbxref_id_widget_form_ajax_callback($form, $form_state) {
  147. $field_name = $form_state['triggering_element']['#parents'][0];
  148. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
  149. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
  150. if ($db_id and $accession) {
  151. $values = array(
  152. 'db_id' => $db_id,
  153. 'accession' => $accession,
  154. );
  155. $options = array('is_duplicate' => TRUE);
  156. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  157. if (!$has_duplicate) {
  158. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  159. }
  160. }
  161. return $form[$field_name];
  162. }
  163. /**
  164. * Callback function for validating the tripal_chado_organism_select_widget.
  165. */
  166. function tripal_chado_dbxref_id_widget_validate($element, &$form_state) {
  167. $field_name = $element['#parents'][0];
  168. // If the form ID is field_ui_field_edit_form, then the user is editing the
  169. // field's values in the manage fields form of Drupal. We don't want
  170. // to validate it as if it were being used in a data entry form.
  171. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  172. return;
  173. }
  174. // Get the field values.
  175. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_name);
  176. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
  177. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
  178. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
  179. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
  180. // Make sure that if a database ID is provided that an accession is also
  181. // provided. Here we use the form_set_error function rather than the
  182. // form_error function because the form_error will add a red_highlight
  183. // around all of the fields in the fieldset which is confusing as it's not
  184. // clear to the user what field is required and which isn't. Therefore,
  185. // we borrow the code from the 'form_error' function and append the field
  186. // so that the proper field is highlighted on error.
  187. if (!$db_id and $accession) {
  188. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  189. }
  190. if ($db_id and !$accession) {
  191. form_set_error(implode('][', $element ['#parents']) . '][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  192. }
  193. if (!$db_id and !$accession and ($version or $description)) {
  194. form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  195. }
  196. // If user did not select a database, we want to remove dbxref_id from the
  197. // field.
  198. if (!$db_id) {
  199. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  200. }
  201. // If the dbxref_id does not match the db_id + accession then the user
  202. // has selected a new dbxref record and we need to update the hidden
  203. // value accordingly.
  204. if ($db_id and $accession) {
  205. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  206. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  207. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id);
  208. }
  209. }
  210. }
  211. /**
  212. * Theme function for the dbxref_id_widget.
  213. *
  214. * @param $variables
  215. */
  216. function theme_tripal_chado_dbxref_id_widget($variables) {
  217. $element = $variables['element'];
  218. $layout = "
  219. <div class=\"primary-dbxref-widget\">
  220. <div class=\"primary-dbxref-widget-item\">" .
  221. drupal_render($element['dbxref__db_id']) . "
  222. </div>
  223. <div class=\"primary-dbxref-widget-item\">" .
  224. drupal_render($element['dbxref__accession']) . "
  225. </div>
  226. <div class=\"primary-dbxref-widget-item\">" .
  227. drupal_render($element['dbxref__version']) . "
  228. </div>
  229. <div class=\"primary-dbxref-widget-item\">" .
  230. drupal_render($element['dbxref__description']) . "
  231. </div>
  232. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  233. </div>
  234. ";
  235. $classes = array();
  236. $classes[] = 'collapsible';
  237. $theme_settings = $element['#theme_settings'];
  238. if ($theme_settings['#collapsed'] == TRUE) {
  239. $classes[] = 'collapsed';
  240. }
  241. $fieldset = array(
  242. '#title' => $element['#title'],
  243. '#value' => '',
  244. '#description' => $element['#description'],
  245. '#children' => $layout,
  246. '#attributes' => array('class' => $classes),
  247. );
  248. return theme('fieldset', array('element' => $fieldset));
  249. }
  250. /**
  251. * Loads the field values with appropriate data.
  252. *
  253. * This function is called by the tripal_chado_field_storage_load() for
  254. * each property managed by the field_chado_storage storage type. This is
  255. * an optional hook function that is only needed if the field has
  256. * multiple form elements.
  257. *
  258. * @param $field
  259. * @param $entity
  260. * @param $base_table
  261. * @param $record
  262. */
  263. function tripal_chado_dbxref_id_field_load($field, $entity, $base_table, $record) {
  264. $field_name = $field['field_name'];
  265. $field_type = $field['type'];
  266. $field_table = $field['settings']['chado_table'];
  267. $field_column = $field['settings']['chado_column'];
  268. // Set some defauls 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. // Get the primary dbxref record. Because we have a dbxref_id passed in
  277. // by the base record, we will only have one record.
  278. $columns = array('*');
  279. $match = array('dbxref_id' => $record->$field_column);
  280. $options = array('return_array' => TRUE);
  281. $records = chado_select_record('dbxref', $columns, $match, $options);
  282. if (count($records) > 0) {
  283. $dbxref = $records[0];
  284. $entity->{$field_name}['und'][0] = array(
  285. 'value' => $dbxref->dbxref_id,
  286. 'dbxref__db_id' => $dbxref->db_id,
  287. 'dbxref__accession' => $dbxref->accession,
  288. 'dbxref__version' => $dbxref->version,
  289. 'dbxref__description' => $dbxref->description,
  290. );
  291. }
  292. }