chado_linker__dbxref.inc 18 KB

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