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