chado_linker__dbxref.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * This class provides fields for adding database cross-references.
  4. *
  5. * It supports two types of cross-references. The primary cross reference
  6. * which is usually in the base table as an FK to the dbxref table, and
  7. * secondary cross references which are in a linking table that connects
  8. * the base table with the dbxref table.
  9. *
  10. */
  11. class chado_linker__dbxref extends TripalField {
  12. /**
  13. * Implements hook_info() for fields.
  14. *
  15. * This is a hook provided by the tripal_chado module for offloading the
  16. * hook_field_info() hook for each field to specify.
  17. */
  18. function field_info() {
  19. return array(
  20. 'label' => t('Cross references'),
  21. 'description' => t('This record can be cross referenced with a record in
  22. another online database. This field is intended for one or more
  23. references. At a minimum, the database and accession must be provided.'),
  24. 'default_widget' => 'chado_linker__dbxref_widget',
  25. 'default_formatter' => 'chado_linker__dbxref_formatter',
  26. 'settings' => array(),
  27. 'storage' => array(
  28. 'type' => 'field_chado_storage',
  29. 'module' => 'tripal_chado',
  30. 'active' => TRUE
  31. ),
  32. );
  33. }
  34. /**
  35. * @see TripalField::attach_info()
  36. */
  37. function attach_info($entity_type, $bundle, $settings) {
  38. $field_info = array();
  39. $table_name = $settings['data_table'];
  40. $type_table = $settings['type_table'];
  41. $type_field = $settings['field'];
  42. $cv_id = $settings['cv_id'];
  43. $cvterm_id = $settings['cvterm_id'];
  44. // If the linker table does not exists then we don't want to add attach.
  45. $dbxref_table = $table_name . '_dbxref';
  46. if (chado_table_exists($dbxref_table)) {
  47. // We already have a dbxref_id field.
  48. $schema = chado_get_schema($dbxref_table);
  49. $pkey = $schema['primary key'][0];
  50. // Initialize the field array.
  51. $field_info = array(
  52. 'field_name' => $dbxref_table,
  53. 'field_type' => 'chado_linker__dbxref',
  54. 'widget_type' => 'chado_linker__dbxref_widget',
  55. 'widget_settings' => array('display_label' => 1),
  56. 'description' => 'This field specifies the IDs where this
  57. record may be available in other external online databases.',
  58. 'label' => 'Cross References',
  59. 'is_required' => 0,
  60. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  61. 'storage' => 'field_chado_storage',
  62. 'field_settings' => array(
  63. 'chado_table' => $dbxref_table,
  64. 'chado_column' => $pkey,
  65. 'base_table' => $table_name,
  66. 'semantic_web' => 'SBO:0000554',
  67. ),
  68. );
  69. }
  70. return $field_info;
  71. }
  72. /**
  73. * @see TripalField::widget_info()
  74. */
  75. function widget_info() {
  76. return array(
  77. 'label' => t('Cross references'),
  78. 'field types' => array('chado_linker__dbxref'),
  79. 'description' => t('This record can be cross referenced with a record
  80. in another online database. This field is intended for the most
  81. prominent reference. At a minimum, the database and accession
  82. must be provided.'),
  83. );
  84. }
  85. /**
  86. * @see TripalField::formatter_info()
  87. */
  88. function formatter_info() {
  89. return array(
  90. 'label' => t('Cross references'),
  91. 'field types' => array('chado_linker__dbxref'),
  92. 'settings' => array(
  93. ),
  94. );
  95. }
  96. /**
  97. * @see TripalField::formatter_view()
  98. */
  99. function formatter_view(&$element, $entity_type, $entity, $field,
  100. $instance, $langcode, $items, $display) {
  101. $chado_table = $field['settings']['chado_table'];
  102. foreach ($items as $delta => $item) {
  103. if ($item['value']) {
  104. $content = $item['value']['vocabulary'] . ':' . $item['value']['accession'];
  105. if ($item['value']['URL']) {
  106. $content = l($item['value']['URL'], $item['value']['URL']);
  107. }
  108. $element[$delta] = array(
  109. '#type' => 'markup',
  110. '#markup' => $content,
  111. );
  112. }
  113. }
  114. }
  115. /**
  116. * @see TripalField::widget_form()
  117. */
  118. function widget_form(&$widget, $form, $form_state, $field, $instance,
  119. $langcode, $items, $delta, $element) {
  120. $field_name = $field['field_name'];
  121. $field_type = $field['type'];
  122. $field_table = $field['settings']['chado_table'];
  123. $field_column = $field['settings']['chado_column'];
  124. // Get the FK column that links to the base table.
  125. $chado_table = $field['settings']['chado_table'];
  126. $base_table = $field['settings']['base_table'];
  127. $schema = chado_get_schema($chado_table);
  128. $pkey = $schema['primary key'][0];
  129. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  130. $fkey = $fkeys[0];
  131. // Get the field defaults.
  132. $record_id = '';
  133. $fkey_value = '';
  134. $dbxref_id = '';
  135. $db_id = '';
  136. $accession = '';
  137. $version = '';
  138. $description = '';
  139. // If the field already has a value then it will come through the $items
  140. // array. This happens when editing an existing record.
  141. if (array_key_exists($delta, $items)) {
  142. $record_id = $items[$delta][$field_table . '__' . $pkey];
  143. $fkey_value = $items[$delta][$field_table . '__' . $fkey];
  144. $dbxref_id = $items[$delta][$field_table . '__dbxref_id'];
  145. $db_id = $items[$delta][$field_table . '__dbxref_id--db_id'];
  146. $accession = $items[$delta][$field_table . '__dbxref_id--accession'];
  147. $version = $items[$delta][$field_table . '__dbxref_id--version'];
  148. $description = $items[$delta][$field_table . '__dbxref_id--description'];
  149. }
  150. // Check $form_state['values'] to see if an AJAX call set the values.
  151. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  152. $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $pkey);
  153. $fkey_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $fkey);
  154. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id');
  155. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--db_id');
  156. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--accession');
  157. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--version');
  158. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--description');
  159. }
  160. $schema = chado_get_schema('dbxref');
  161. $options = tripal_get_db_select_options();
  162. $widget['#table_name'] = $chado_table;
  163. $widget['#fkey_field'] = $fkey;
  164. $widget['#element_validate'] = array('chado_linker__dbxref_widget_validate');
  165. $widget['#theme'] = 'chado_linker__dbxref_widget';
  166. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id-$delta'>";
  167. $widget['#suffix'] = "</span>";
  168. $widget['value'] = array(
  169. '#type' => 'value',
  170. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  171. );
  172. $widget[$field_table . '__' . $pkey] = array(
  173. '#type' => 'value',
  174. '#default_value' => $record_id,
  175. );
  176. $widget[$field_table . '__' . $fkey] = array(
  177. '#type' => 'value',
  178. '#default_value' => $fkey_value,
  179. );
  180. $widget[$field_table . '__dbxref_id'] = array(
  181. '#type' => 'value',
  182. '#default_value' => $dbxref_id,
  183. );
  184. $widget[$field_table . '__dbxref_id--dbxref_id'] = array(
  185. '#type' => 'value',
  186. '#default_value' => $dbxref_id,
  187. );
  188. $widget[$field_table . '__dbxref_id--db_id'] = array(
  189. '#type' => 'select',
  190. '#title' => t('Database'),
  191. '#options' => $options,
  192. '#required' => $element['#required'],
  193. '#default_value' => $db_id,
  194. '#ajax' => array(
  195. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  196. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  197. 'effect' => 'fade',
  198. 'method' => 'replace'
  199. ),
  200. );
  201. $widget[$field_table . '__dbxref_id--accession'] = array(
  202. '#type' => 'textfield',
  203. '#title' => t('Accession'),
  204. '#default_value' => $accession,
  205. '#required' => $element['#required'],
  206. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  207. '#size' => 15,
  208. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  209. '#ajax' => array(
  210. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  211. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  212. 'effect' => 'fade',
  213. 'method' => 'replace'
  214. ),
  215. '#disabled' => $db_id ? FALSE : TRUE,
  216. );
  217. $widget[$field_table . '__dbxref_id--version'] = array(
  218. '#type' => 'textfield',
  219. '#title' => t('Version'),
  220. '#default_value' => $version,
  221. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  222. '#size' => 5,
  223. '#disabled' => $db_id ? FALSE : TRUE,
  224. );
  225. $widget[$field_table . '__dbxref_id--description'] = array(
  226. '#type' => 'textfield',
  227. '#title' => t('Description'),
  228. '#default_value' => $description,
  229. '#size' => 20,
  230. '#disabled' => $db_id ? FALSE : TRUE,
  231. );
  232. if (!$db_id) {
  233. $widget['links'] = array(
  234. '#type' => 'item',
  235. '#markup' => l('Add a database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  236. );
  237. }
  238. }
  239. /**
  240. * @see TripalField::load()
  241. */
  242. function load($field, $entity, $details) {
  243. $record = $details['record'];
  244. $field_name = $field['field_name'];
  245. $field_type = $field['type'];
  246. $field_table = $field['settings']['chado_table'];
  247. $field_column = $field['settings']['chado_column'];
  248. $base_table = $record->tablename;
  249. $schema = chado_get_schema($field_table);
  250. $pkey = $schema['primary key'][0];
  251. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  252. $fkey = $fkeys[0];
  253. // Set some defaults for the empty record.
  254. $entity->{$field_name}['und'][0] = array(
  255. 'value' => array(),
  256. $field_table . '__' . $pkey => '',
  257. $field_table . '__' . $fkey => '',
  258. $field_table . '__dbxref_id' => '',
  259. $field_table . '__dbxref_id--dbxref_id' => '',
  260. $field_table . '__dbxref_id--db_id' => '',
  261. $field_table . '__dbxref_id--accession' => '',
  262. $field_table . '__dbxref_id--version' => '',
  263. $field_table . '__dbxref_id--description' => '',
  264. );
  265. $linker_table = $base_table . '_dbxref';
  266. $options = array('return_array' => 1);
  267. $record = chado_expand_var($record, 'table', $linker_table, $options);
  268. if (count($record->$linker_table) > 0) {
  269. $i = 0;
  270. foreach ($record->$linker_table as $index => $linker) {
  271. $dbxref = $linker->dbxref_id;
  272. $entity->{$field_name}['und'][$i] = array(
  273. 'value' => array(
  274. 'vocabulary' => $dbxref->db_id->name,
  275. 'accession' => $dbxref->accession,
  276. 'URL' => $URL,
  277. ),
  278. $field_table . '__' . $pkey => $linker->$pkey,
  279. $field_table . '__' . $fkey => $linker->$fkey->$fkey,
  280. $field_table . '__dbxref_id' => $dbxref->dbxref_id,
  281. $field_table . '__dbxref_id--dbxref_id' => $dbxref->dbxref_id,
  282. $field_table . '__dbxref_id--db_id' => $dbxref->db_id->db_id,
  283. $field_table . '__dbxref_id--accession' => $dbxref->accession,
  284. $field_table . '__dbxref_id--version' => $dbxref->version,
  285. $field_table . '__dbxref_id--description' => $dbxref->description,
  286. );
  287. $i++;
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Theme function for the dbxref_id_widget.
  294. *
  295. * @param $variables
  296. */
  297. function theme_chado_linker__dbxref_widget($variables) {
  298. $element = $variables['element'];
  299. // These two fields were added to the widget to help identify the fields
  300. // for layout.
  301. $table_name = $element['#table_name'];
  302. $fkey = $element['#fkey_field'];
  303. $layout = "
  304. <div class=\"secondary-dbxref-widget\">
  305. <div class=\"secondary-dbxref-widget-item\">" .
  306. drupal_render($element[$table_name . '__dbxref_id--db_id']) . "
  307. </div>
  308. <div class=\"secondary-dbxref-widget-item\">" .
  309. drupal_render($element[$table_name . '__dbxref_id--accession']) . "
  310. </div>
  311. <div class=\"secondary-dbxref-widget-item\">" .
  312. drupal_render($element[$table_name . '__dbxref_id--version']) . "
  313. </div>
  314. <div class=\"secondary-dbxref-widget-item\">" .
  315. drupal_render($element[$table_name . '__dbxref_id--description']) . "
  316. </div>
  317. <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  318. </div>
  319. ";
  320. return $layout;
  321. }
  322. /**
  323. * Callback function for validating the chado_linker__dbxref_widget.
  324. */
  325. function chado_linker__dbxref_widget_validate($element, &$form_state) {
  326. $field_name = $element['#field_name'];
  327. $delta = $element['#delta'];
  328. $table_name = $element['#table_name'];
  329. $fkey = $element['#fkey_field'];
  330. $field = field_info_field($field_name);
  331. $field_type = $field['type'];
  332. $field_table = $field['settings']['chado_table'];
  333. $field_column = $field['settings']['chado_column'];
  334. $field_prefix = $field_table . '__dbxref_id';
  335. // If the form ID is field_ui_field_edit_form, then the user is editing the
  336. // field's values in the manage fields form of Drupal. We don't want
  337. // to validate it as if it were being used in a data entry form.
  338. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  339. return;
  340. }
  341. // Get the field values.
  342. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id');
  343. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--db_id');
  344. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--accession');
  345. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--version');
  346. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--description');
  347. // Make sure that if a database ID is provided that an accession is also
  348. // provided. Here we use the form_set_error function rather than the
  349. // form_error function because the form_error will add a red_highlight
  350. // around all of the fields in the fieldset which is confusing as it's not
  351. // clear to the user what field is required and which isn't. Therefore,
  352. // we borrow the code from the 'form_error' function and append the field
  353. // so that the proper field is highlighted on error.
  354. if (!$db_id and $accession) {
  355. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided."));
  356. }
  357. if ($db_id and !$accession) {
  358. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--accession', t("A database and the accession must both be provided."));
  359. }
  360. if (!$db_id and !$accession and ($version or $description)) {
  361. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided."));
  362. }
  363. // If the dbxref_id does not match the db_id + accession then the user
  364. // has selected a new dbxref record and we need to update the hidden
  365. // value accordingly.
  366. if ($db_id and $accession) {
  367. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  368. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  369. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $table_name . '__dbxref_id');
  370. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $field_prefix . '--dbxref_id');
  371. }
  372. }
  373. else {
  374. // If the db_id and accession are not set, then remove the linker FK value to the base table.
  375. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  376. }
  377. }
  378. /**
  379. * An Ajax callback for the dbxref widget.
  380. */
  381. function chado_linker__dbxref_widget_form_ajax_callback($form, $form_state) {
  382. $field_name = $form_state['triggering_element']['#parents'][0];
  383. $delta = $form_state['triggering_element']['#parents'][2];
  384. $field = field_info_field($field_name);
  385. $field_type = $field['type'];
  386. $field_table = $field['settings']['chado_table'];
  387. $field_column = $field['settings']['chado_column'];
  388. $field_prefix = $field_table . '__dbxref_id';
  389. // Check to see if this dbxref already exists. If not then
  390. // give a notice to the user that the dbxref will be added.
  391. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--db_id');
  392. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--accession');
  393. if ($db_id and $accession) {
  394. $values = array(
  395. 'db_id' => $db_id,
  396. 'accession' => $accession,
  397. );
  398. $options = array('is_duplicate' => TRUE);
  399. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  400. if (!$has_duplicate) {
  401. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  402. }
  403. }
  404. return $form[$field_name]['und'][$delta];
  405. }