dbxref_id.inc 12 KB

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