sio__annotation_formatter.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. class sio__annotation_formatter extends ChadoFieldFormatter {
  3. // The default lable for this field.
  4. public static $default_label = 'Chado Annotation';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('chado_linker__cvterm');
  7. /**
  8. *
  9. * @see TripalFieldFormatter::settingsForm()
  10. */
  11. public function settingsForm($view_mode, $form, &$form_state) {
  12. $headers = array('Term', 'Definition', 'Is Not', 'Reference');
  13. $rows = array();
  14. $chado_table = $this->instance['settings']['chado_table'];
  15. foreach ($items as $delta => $item) {
  16. if ($item['chado-' . $chado_table . '__cvterm_id']) {
  17. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $item['chado-' . $chado_table . '__cvterm_id']));
  18. $dbxref = $cvterm->dbxref_id;
  19. // Build the accession.
  20. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  21. if ($dbxref->db_id->urlprefix) {
  22. $accession = l($accession, tripal_get_dbxref_url($dbxref), array('attributes' => array('target' => '_blank')));
  23. }
  24. $rows[] = array(
  25. $accession,
  26. $cvterm->definition,
  27. $item['chado-' . $chado_table . '__is_not'] ? 'Yes' : '',
  28. '',
  29. );
  30. }
  31. }
  32. // the $table array contains the headers and rows array as well as other
  33. // options for controlling the display of the table. Additional
  34. // documentation can be found here:
  35. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  36. $table = array(
  37. 'header' => $headers,
  38. 'rows' => $rows,
  39. 'attributes' => array(
  40. 'id' => "$chado_table-table-terms",
  41. 'class' => 'tripal-data-table'
  42. ),
  43. 'caption' => '',
  44. 'sticky' => FALSE,
  45. 'colgroups' => array(),
  46. 'empty' => 'There are no annotations of this type',
  47. );
  48. if (count($items) > 0) {
  49. $element[0] = array(
  50. '#type' => 'markup',
  51. '#markup' => theme_table($table),
  52. );
  53. }
  54. }
  55. /**
  56. *
  57. * @see TripalFieldFormatter::view()
  58. */
  59. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  60. $headers = array('Term', 'Definition');
  61. $rows = array();
  62. $field_table = $this->instance['settings']['chado_table'];
  63. $schema = chado_get_schema($field_table);
  64. $vocabulary_term = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  65. $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
  66. $definition_term = tripal_get_chado_semweb_term('cvterm', 'definition');
  67. if (array_key_exists('is_not', $schema['fields'])) {
  68. $negation_term = tripal_get_chado_semweb_term($field_table, 'is_not');
  69. }
  70. $chado_table = $this->instance['settings']['chado_table'];
  71. foreach ($items as $delta => $item) {
  72. if ($item['chado-' . $chado_table . '__cvterm_id']) {
  73. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $item['chado-' . $chado_table . '__cvterm_id']));
  74. $dbxref = $cvterm->dbxref_id;
  75. // Build the accession.
  76. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  77. if ($dbxref->db_id->urlprefix) {
  78. $accession = l($accession, tripal_get_dbxref_url($dbxref), array('attributes' => array('target' => '_blank')));
  79. }
  80. $row = array(
  81. $item['value'][$vocabulary_term] . ':' . $item['value'][$accession_term],
  82. $item['value'][$definition_term],
  83. );
  84. if (array_key_exists('is_not', $schema['fields'])) {
  85. if ($negation_term == FALSE) {
  86. $row[1] = 'NOT ' . $row[1];
  87. }
  88. }
  89. $rows[] = $row;
  90. }
  91. }
  92. // Theme the results in a talbe.
  93. $caption = 'This record is associated with the following annotations.';
  94. $table = array(
  95. 'header' => $headers,
  96. 'rows' => $rows,
  97. 'attributes' => array(
  98. 'id' => "$chado_table-table-terms",
  99. 'class' => 'tripal-data-table'
  100. ),
  101. 'caption' => $caption,
  102. 'sticky' => FALSE,
  103. 'colgroups' => array(),
  104. 'empty' => 'There are no annotations of this type',
  105. );
  106. if (count($items) > 0) {
  107. $element[0] = array(
  108. '#type' => 'markup',
  109. '#markup' => theme_table($table),
  110. );
  111. }
  112. }
  113. }