dbxref.inc 14 KB

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