chado_linker__dbxref.inc 18 KB

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