chado_linker__dbxref.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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 base table has a 'dbxref_id' then we want to attach
  45. $attach = FALSE;
  46. $cardinality = 1;
  47. $field_name = $table_name . '__dbxref_id';
  48. $chado_table = $table_name;
  49. $chado_column = 'dbxref_id';
  50. $schema = chado_get_schema($table_name);
  51. if (array_key_exists('dbxref_id', $schema['fields'])){
  52. $attach = TRUE;
  53. }
  54. // If the linker table exists then we want to attach.
  55. $dbxref_table = $table_name . '_dbxref';
  56. if (chado_table_exists($dbxref_table)) {
  57. $schema = chado_get_schema($dbxref_table);
  58. $pkey = $schema['primary key'][0];
  59. $attach = TRUE;
  60. $field_name = $dbxref_table;
  61. $cardinality = FIELD_CARDINALITY_UNLIMITED;
  62. $chado_table = $dbxref_table;
  63. $chado_column = $pkey;
  64. }
  65. if ($attach) {
  66. // Initialize the field array.
  67. $field_info = array(
  68. 'field_name' => $field_name,
  69. 'field_type' => 'chado_linker__dbxref',
  70. 'widget_type' => 'chado_linker__dbxref_widget',
  71. 'widget_settings' => array(
  72. 'display_label' => 1
  73. ),
  74. 'description' => '',
  75. 'label' => 'Cross References',
  76. 'is_required' => 0,
  77. 'cardinality' => $cardinality,
  78. 'storage' => 'field_chado_storage',
  79. 'field_settings' => array(
  80. 'chado_table' => $chado_table,
  81. 'chado_column' => $chado_column,
  82. 'base_table' => $table_name,
  83. 'semantic_web' => array(
  84. 'name' => 'database cross reference',
  85. 'accession' => '0000554',
  86. 'ns' => 'SBO',
  87. 'nsurl' => 'http://www.ebi.ac.uk/sbo/main/',
  88. ),
  89. ),
  90. );
  91. }
  92. return $field_info;
  93. }
  94. /**
  95. * @see TripalField::widget_info()
  96. */
  97. function widget_info() {
  98. return array(
  99. 'label' => t('Cross references'),
  100. 'field types' => array('chado_linker__dbxref'),
  101. 'description' => t('This record can be cross referenced with a record
  102. in another online database. This field is intended for the most
  103. prominent reference. At a minimum, the database and accession
  104. must be provided.'),
  105. );
  106. }
  107. /**
  108. * @see TripalField::formatter_info()
  109. */
  110. function formatter_info() {
  111. return array(
  112. 'label' => t('Cross references'),
  113. 'field types' => array('chado_linker__dbxref'),
  114. 'settings' => array(
  115. ),
  116. );
  117. }
  118. /**
  119. * @see TripalField::formatter_view()
  120. */
  121. function formatter_view(&$element, $entity_type, $entity, $field,
  122. $instance, $langcode, $items, $display) {
  123. dpm($items);
  124. foreach ($items as $delta => $item) {
  125. if ($item['value']) {
  126. $content = $item['value']['namespace'] . ':' . $item['value']['accession'];
  127. if ($item['value']['URL']) {
  128. $content = l($item['value']['URL'], $item['value']['URL']);
  129. }
  130. $element[$delta] = array(
  131. '#type' => 'markup',
  132. '#markup' => $content,
  133. );
  134. }
  135. }
  136. }
  137. function widget_form(&$widget, $form, $form_state, $field, $instance,
  138. $langcode, $items, $delta, $element) {
  139. $field_name = $field['field_name'];
  140. $field_type = $field['type'];
  141. $field_table = $field['settings']['chado_table'];
  142. $field_column = $field['settings']['chado_column'];
  143. dpm($items[$delta]);
  144. $record = $element['#entity']->chado_record;
  145. $base_table = $record->tablename;
  146. // The first form element should be for the local dbxref record.
  147. if (property_exists($record, 'dbxref_id') and $delta == 0) {
  148. $this->base_widget_form($widget, $form, $form_state, $field, $instance,
  149. $langcode, $items, $delta, $element);
  150. }
  151. else {
  152. // The remainindg form elements should be for the linking dbxref table.
  153. $this->linker_widget_form($widget, $form, $form_state, $field, $instance,
  154. $langcode, $items, $delta, $element);
  155. }
  156. }
  157. /**
  158. * @see TripalField::widget_form()
  159. */
  160. private function base_widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  161. $field_name = $field['field_name'];
  162. $field_type = $field['type'];
  163. $base_table = $element['#entity']->chado_record->tablename;
  164. $field_table = $base_table;
  165. $field_column = 'dbxref_id';
  166. // Get the field defaults.
  167. $fk_val = '';
  168. $dbxref_id = '';
  169. $db_id = '';
  170. $accession = '';
  171. $version = '';
  172. $description = '';
  173. // If the field already has a value then it will come through the $items
  174. // array. This happens when editing an existing record.
  175. if (array_key_exists($delta, $items)) {
  176. $fk_val = $items[$delta][$field_table .'__' .$field_column];
  177. $dbxref_id = $items[$delta]['dbxref__dbxref_id'];
  178. $db_id = $items[$delta]['dbxref__db_id'];
  179. $accession = $items[$delta]['dbxref__accession'];
  180. $version = $items[$delta]['dbxref__version'];
  181. $description = $items[$delta]['dbxref__description'];
  182. }
  183. // Check $form_state['values'] to see if an AJAX call set the values.
  184. if (array_key_exists('values', $form_state)) {
  185. $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table .'__' .$field_column);
  186. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__dbxref_id');
  187. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
  188. $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
  189. $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
  190. $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
  191. }
  192. // If we are here because our parent was triggered in a form submit
  193. // then that means an ajax call was made and we don't want the fieldset to
  194. // be closed when it returns from the ajax call.
  195. // $collapsed = TRUE;
  196. // if (array_key_exists('triggering_element', $form_state) and
  197. // $form_state['triggering_element']['#parents'][0] == $field_name) {
  198. // $collapsed = FALSE;
  199. // }
  200. // if ($dbxref_id) {
  201. // $collapsed = FALSE;
  202. // }
  203. $schema = chado_get_schema('dbxref');
  204. $options = tripal_get_db_select_options();
  205. $widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
  206. $widget['#theme'] = 'chado_base__dbxref_id_widget';
  207. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  208. $widget['#suffix'] = "</span>";
  209. // A temporary element used for theming the fieldset.
  210. $widget['#theme_settings'] = array(
  211. '#title' => $element['#title'],
  212. '#description' => $element['#description'],
  213. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  214. '#theme' => 'chado_base__dbxref_id_widget'
  215. // '#collapsible' => TRUE,
  216. // '#collapsed' => $collapsed,
  217. );
  218. $widget['value'] = array(
  219. '#type' => 'value',
  220. '#value' => $items[0]['value']
  221. );
  222. $widget[$field_table .'__' .$field_column] = array(
  223. '#type' => 'value',
  224. '#default_value' => $fk_val
  225. );
  226. $widget['dbxref__dbxref_id'] = array(
  227. '#type' => 'value',
  228. '#default_value' => $dbxref_id
  229. );
  230. $widget['dbxref__db_id'] = array(
  231. '#type' => 'select',
  232. '#title' => t('Database'),
  233. '#options' => $options,
  234. '#required' => $element['#required'],
  235. '#default_value' => $db_id,
  236. '#ajax' => array(
  237. 'callback' => "chado_base__dbxref_id_widget_form_ajax_callback",
  238. 'wrapper' => "$field_name-dbxref--db-id",
  239. 'effect' => 'fade',
  240. 'method' => 'replace')
  241. );
  242. $widget['dbxref__accession'] = array(
  243. '#type' => 'textfield',
  244. '#title' => t('Accession'),
  245. '#default_value' => $accession,
  246. '#required' => $element['#required'],
  247. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  248. '#size' => 15,
  249. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' .$db_id,
  250. '#ajax' => array(
  251. 'callback' => "tripal_chado_dbxref_widget_form_ajax_callback",
  252. 'wrapper' => "$field_name-dbxref--db-id",
  253. 'effect' => 'fade','method' => 'replace'
  254. ),
  255. '#disabled' => $db_id ? FALSE : TRUE
  256. );
  257. $widget['dbxref__version'] = array(
  258. '#type' => 'textfield',
  259. '#title' => t('Version'),
  260. '#default_value' => $version,
  261. '#maxlength' => array_key_exists(
  262. 'length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  263. '#size' => 5,
  264. '#disabled' => $db_id ? FALSE : TRUE
  265. );
  266. $widget['dbxref__description'] = array(
  267. '#type' => 'textfield',
  268. '#title' => t('Description'),
  269. '#default_value' => $description,
  270. '#size' => 20,
  271. '#disabled' => $db_id ? FALSE : TRUE
  272. );
  273. // $widget['links'] = array(
  274. // '#type' => 'item',
  275. // '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  276. // );
  277. }
  278. /**
  279. * @see TripalField::widget_form()
  280. */
  281. private function linker_widget_form(&$widget, $form, $form_state, $field, $instance,
  282. $langcode, $items, $delta, $element) {
  283. $field_name = $field['field_name'];
  284. $field_type = $field['type'];
  285. $field_table = $field['settings']['chado_table'];
  286. $field_column = $field['settings']['chado_column'];
  287. // Get the FK column that links to the base table.
  288. $chado_table = $field['settings']['chado_table'];
  289. $base_table = $field['settings']['base_table'];
  290. $schema = chado_get_schema($chado_table);
  291. $pkey = $schema['primary key'][0];
  292. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  293. $fkey = $fkeys[0];
  294. // Get the field defaults.
  295. $record_id = '';
  296. $fkey_value = '';
  297. $dbxref_id = '';
  298. $db_id = '';
  299. $accession = '';
  300. $version = '';
  301. $description = '';
  302. // If the field already has a value then it will come through the $items
  303. // array. This happens when editing an existing record.
  304. if (array_key_exists($delta, $items)) {
  305. $record_id = $items[$delta]['value'];
  306. $fkey_value = $items[$delta][$field_name . '__' . $fkey];
  307. $dbxref_id = $items[$delta][$field_name . '__dbxref_id'];
  308. $db_id = $items[$delta][$field_name . '--dbxref__db_id'];
  309. $accession = $items[$delta][$field_name . '--dbxref__accession'];
  310. $version = $items[$delta][$field_name . '--dbxref__version'];
  311. $description = $items[$delta][$field_name . '--dbxref__description'];
  312. }
  313. // Check $form_state['values'] to see if an AJAX call set the values.
  314. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  315. $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name);
  316. $fkey_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '__' . $fkey);
  317. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '__dbxref_id');
  318. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__db_id');
  319. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__accession');
  320. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__version');
  321. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__description');
  322. }
  323. $schema = chado_get_schema('dbxref');
  324. $options = tripal_get_db_select_options();
  325. $widget['#table_name'] = $chado_table;
  326. $widget['#fkey_field'] = $fkey;
  327. $widget['#element_validate'] = array('chado_linker__dbxref_widget_validate');
  328. $widget['#theme'] = 'chado_linker__dbxref_widget';
  329. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id-$delta'>";
  330. $widget['#suffix'] = "</span>";
  331. $widget['value'] = array(
  332. '#type' => 'value',
  333. '#value' => array_key_exists($delta, $items)? $items[$delta]['value'] : '',
  334. );
  335. $widget[$field_table . '__' . $pkey] = array(
  336. '#type' => 'value',
  337. '#default_value' => $record_id,
  338. );
  339. $widget[$field_table . '__dbxref_id'] = array(
  340. '#type' => 'value',
  341. '#default_value' => $dbxref_id,
  342. );
  343. $widget[$field_table . '__' . $fkey] = array(
  344. '#type' => 'value',
  345. '#default_value' => $fkey_value,
  346. );
  347. $widget[$field_table . '--dbxref__dbxref_id'] = array(
  348. '#type' => 'value',
  349. '#default_value' => $dbxref_id,
  350. );
  351. $widget[$field_table . '--dbxref__db_id'] = array(
  352. '#type' => 'select',
  353. '#title' => t('Database'),
  354. '#options' => $options,
  355. '#required' => $element['#required'],
  356. '#default_value' => $db_id,
  357. '#ajax' => array(
  358. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  359. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  360. 'effect' => 'fade',
  361. 'method' => 'replace'
  362. ),
  363. );
  364. $widget[$field_table . '--dbxref__accession'] = array(
  365. '#type' => 'textfield',
  366. '#title' => t('Accession'),
  367. '#default_value' => $accession,
  368. '#required' => $element['#required'],
  369. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  370. '#size' => 15,
  371. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  372. '#ajax' => array(
  373. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  374. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  375. 'effect' => 'fade',
  376. 'method' => 'replace'
  377. ),
  378. '#disabled' => $db_id ? FALSE : TRUE,
  379. );
  380. $widget[$field_table . '--dbxref__version'] = array(
  381. '#type' => 'textfield',
  382. '#title' => t('Version'),
  383. '#default_value' => $version,
  384. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  385. '#size' => 5,
  386. '#disabled' => $db_id ? FALSE : TRUE,
  387. );
  388. $widget[$field_table . '--dbxref__description'] = array(
  389. '#type' => 'textfield',
  390. '#title' => t('Description'),
  391. '#default_value' => $description,
  392. '#size' => 20,
  393. '#disabled' => $db_id ? FALSE : TRUE,
  394. );
  395. if (!$db_id) {
  396. $widget['links'] = array(
  397. '#type' => 'item',
  398. '#markup' => l('Add a database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  399. );
  400. }
  401. }
  402. /**
  403. * @see TripalField::load()
  404. */
  405. function load($field, $entity, $details) {
  406. $record = $details['record'];
  407. $field_name = $field['field_name'];
  408. $field_type = $field['type'];
  409. $field_table = $field['settings']['chado_table'];
  410. $field_column = $field['settings']['chado_column'];
  411. $base_table = $record->tablename;
  412. $num_dbxrefs = 0;
  413. // Set some defauls for the empty record
  414. if (property_exists($record, 'dbxref_id')) {
  415. $entity->{$field_name}['und'][0] = array(
  416. 'value' => array(),
  417. $base_table . '__dbxref_id' => '',
  418. 'dbxref__dbxref_id' => '',
  419. 'dbxref__db_id' => '',
  420. 'dbxref__accession' => '',
  421. 'dbxref__version' => '',
  422. 'dbxref__description' => '',
  423. );
  424. }
  425. else {
  426. $entity->{$field_name}['und'][0] = array(
  427. 'value' => array(),
  428. $field_table . '__' . $pkey => '',
  429. $field_table . '__' . $fkey => '',
  430. $field_table . '__' . 'dbxref_id' => '',
  431. $field_table . '--' . 'dbxref__dbxref_id' => '',
  432. $field_table . '--' . 'dbxref__db_id' => '',
  433. $field_table . '--' . 'dbxref__accession' => '',
  434. $field_table . '--' . 'dbxref__version' => '',
  435. $field_table . '--' . 'dbxref__description' => '',
  436. );
  437. }
  438. // First check to see if a local dbxref_id field has values, if it does
  439. // then add it.
  440. if (property_exists($record, 'dbxref_id')) {
  441. $dbxref = $record->dbxref_id;
  442. $value = $dbxref->db_id->name . ':' . $dbxref->accession;
  443. $URL = '';
  444. if ($dbxref->db_id->urlprefix) {
  445. $URL = l($value, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  446. }
  447. $entity->{$field_name}['und'][$num_dbxrefs] = array(
  448. 'value' => array(
  449. 'namespace' => $dbxref->db_id->name,
  450. 'accession' => $dbxref->accession,
  451. 'URL' => $URL,
  452. ),
  453. $base_table . '__dbxref_id' => $dbxref->dbxref_id,
  454. 'dbxref__dbxref_id' => $dbxref->dbxref_id,
  455. 'dbxref__db_id' => $dbxref->db_id->db_id,
  456. 'dbxref__accession' => $dbxref->accession,
  457. 'dbxref__version' => $dbxref->version,
  458. 'dbxref__description' => $dbxref->description,
  459. );
  460. $num_dbxrefs++;
  461. }
  462. // Second, check in the dbxref linker table for values.
  463. $schema = chado_get_schema($field_table);
  464. $pkey = $schema['primary key'][0];
  465. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  466. $fkey = $fkeys[0];
  467. $linker_table = $base_table . '_dbxref';
  468. $options = array('return_array' => 1);
  469. $record = chado_expand_var($record, 'table', $linker_table, $options);
  470. if (count($record->$linker_table) > 0) {
  471. foreach ($record->$linker_table as $index => $linker) {
  472. $dbxref = $linker->dbxref_id;
  473. $entity->{$field_name}['und'][$num_dbxrefs] = array(
  474. 'value' => array(
  475. 'namespace' => $dbxref->db_id->name,
  476. 'accession' => $dbxref->accession,
  477. 'URL' => $URL,
  478. ),
  479. $field_table . '__' . $pkey => $linker->$pkey,
  480. $field_table . '__' . $fkey => $linker->$fkey->$fkey,
  481. $field_table . '__' . 'dbxref_id' => $dbxref->dbxref_id,
  482. $field_table . '--' . 'dbxref__dbxref_id' => $dbxref->dbxref_id,
  483. $field_table . '--' . 'dbxref__db_id' => $dbxref->db_id->db_id,
  484. $field_table . '--' . 'dbxref__accession' => $dbxref->accession,
  485. $field_table . '--' . 'dbxref__version' => $dbxref->version,
  486. $field_table . '--' . 'dbxref__description' => $dbxref->description,
  487. );
  488. $num_dbxrefs++;
  489. }
  490. }
  491. }
  492. }
  493. /**
  494. * Theme function for the linker table dbxrefs.
  495. *
  496. * @param $variables
  497. */
  498. function theme_chado_linker__dbxref_widget($variables) {
  499. $element = $variables['element'];
  500. // These two fields were added to the widget to help identify the fields
  501. // for layout.
  502. $table_name = $element['#table_name'];
  503. $fkey = $element['#fkey_field'];
  504. $layout = "
  505. <div>
  506. <div class=\"secondary-dbxref-widget-item\">" .
  507. drupal_render($element[$table_name . '--dbxref__db_id']) . "
  508. </div>
  509. <div class=\"secondary-dbxref-widget-item\">" .
  510. drupal_render($element[$table_name . '--dbxref__accession']) . "
  511. </div>
  512. <div class=\"secondary-dbxref-widget-item\">" .
  513. drupal_render($element[$table_name . '--dbxref__version']) . "
  514. </div>
  515. <div class=\"secondary-dbxref-widget-item\">" .
  516. drupal_render($element[$table_name . '--dbxref__description']) . "
  517. </div>
  518. <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  519. </div>
  520. ";
  521. return $layout;
  522. }
  523. /**
  524. * Callback function for validating the chado_linker__dbxref_widget.
  525. */
  526. function chado_linker__dbxref_widget_validate($element, &$form_state) {
  527. $field_name = $element['#field_name'];
  528. $delta = $element['#delta'];
  529. $table_name = $element['#table_name'];
  530. $fkey = $element['#fkey_field'];
  531. // If the form ID is field_ui_field_edit_form, then the user is editing the
  532. // field's values in the manage fields form of Drupal. We don't want
  533. // to validate it as if it were being used in a data entry form.
  534. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  535. return;
  536. }
  537. // Get the field values.
  538. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '__dbxref_id');
  539. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__db_id');
  540. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__accession');
  541. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__version');
  542. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__description');
  543. // Make sure that if a database ID is provided that an accession is also
  544. // provided. Here we use the form_set_error function rather than the
  545. // form_error function because the form_error will add a red_highlight
  546. // around all of the fields in the fieldset which is confusing as it's not
  547. // clear to the user what field is required and which isn't. Therefore,
  548. // we borrow the code from the 'form_error' function and append the field
  549. // so that the proper field is highlighted on error.
  550. if (!$db_id and $accession) {
  551. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--dbxref__db_id', t("A database and the accession must both be provided."));
  552. }
  553. if ($db_id and !$accession) {
  554. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--dbxref__accession', t("A database and the accession must both be provided."));
  555. }
  556. if (!$db_id and !$accession and ($version or $description)) {
  557. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--dbxref__db_id', t("A database and the accession must both be provided."));
  558. }
  559. // If the dbxref_id does not match the db_id + accession then the user
  560. // has selected a new dbxref record and we need to update the hidden
  561. // value accordingly.
  562. if ($db_id and $accession) {
  563. $fkey_value = $element['#entity']->chado_record_id;
  564. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  565. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  566. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  567. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $table_name . '__dbxref_id');
  568. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $table_name . '--dbxref__dbxref_id');
  569. }
  570. }
  571. else {
  572. // If the db_id and accession are not set, then remove the linker FK value to the base table.
  573. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  574. }
  575. }
  576. /**
  577. * An Ajax callback for the dbxref widget.
  578. */
  579. function chado_linker__dbxref_widget_form_ajax_callback($form, $form_state) {
  580. $field_name = $form_state['triggering_element']['#parents'][0];
  581. $delta = $form_state['triggering_element']['#parents'][2];
  582. // Check to see if this dbxref already exists. If not then
  583. // give a notice to the user that the dbxref will be added.
  584. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__db_id');
  585. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_name . '--dbxref__accession');
  586. if ($db_id and $accession) {
  587. $values = array(
  588. 'db_id' => $db_id,
  589. 'accession' => $accession,
  590. );
  591. $options = array('is_duplicate' => TRUE);
  592. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  593. if (!$has_duplicate) {
  594. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  595. }
  596. }
  597. return $form[$field_name]['und'][$delta];
  598. }
  599. /**
  600. * Theme function for the local base table dbxrefs.
  601. */
  602. function theme_chado_base__dbxref_id_widget($variables) {
  603. $element = $variables['element'];
  604. $layout = "
  605. <div>
  606. <div class=\"primary-dbxref-widget-item\">" .
  607. drupal_render($element['dbxref__db_id']) . "
  608. </div>
  609. <div class=\"primary-dbxref-widget-item\">" .
  610. drupal_render($element['dbxref__accession']) . "
  611. </div>
  612. <div class=\"primary-dbxref-widget-item\">" .
  613. drupal_render($element['dbxref__version']) . "
  614. </div>
  615. <div class=\"primary-dbxref-widget-item\">" .
  616. drupal_render($element['dbxref__description']) . "
  617. </div>
  618. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  619. </div>
  620. ";
  621. return $layout;
  622. }