chado_linker__dbxref.inc 17 KB

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