chado_linker__dbxref.inc 17 KB

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