chado_linker__dbxref.inc 19 KB

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