sio__annotation_formatter.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // Build the publication reference.
  25. $pub_ref = '';
  26. $pub_id = $item['chado-' . $chado_table . '__pub_id'];
  27. if ($pub_id) {
  28. $pub = chado_generate_var('pub', array('pub_id' => $pub_id));
  29. $pub_ref = $pub->title;
  30. }
  31. $rows[] = array(
  32. $accession,
  33. $cvterm->definition,
  34. $item['chado-' . $chado_table . '__is_not'] ? 'Yes' : '',
  35. '',
  36. );
  37. }
  38. }
  39. // the $table array contains the headers and rows array as well as other
  40. // options for controlling the display of the table. Additional
  41. // documentation can be found here:
  42. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  43. $table = array(
  44. 'header' => $headers,
  45. 'rows' => $rows,
  46. 'attributes' => array(
  47. 'id' => "$chado_table-table-terms",
  48. 'class' => 'tripal-data-table'
  49. ),
  50. 'caption' => '',
  51. 'sticky' => FALSE,
  52. 'colgroups' => array(),
  53. 'empty' => 'There are no annotations of this type',
  54. );
  55. if (count($items) > 0) {
  56. $element[0] = array(
  57. '#type' => 'markup',
  58. '#markup' => theme_table($table),
  59. );
  60. }
  61. }
  62. /**
  63. *
  64. * @see TripalFieldFormatter::view()
  65. */
  66. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  67. $headers = array('Term', 'Definition');
  68. $rows = array();
  69. $field_table = $this->instance['settings']['chado_table'];
  70. $schema = chado_get_schema($field_table);
  71. if (array_key_exists('is_not', $schema['fields'])) {
  72. $headers[] = 'Negates';
  73. }
  74. $vocabulary_term = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  75. $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
  76. $definition_term = tripal_get_chado_semweb_term('cvterm', 'definition');
  77. if (array_key_exists('is_not', $schema['fields'])) {
  78. $negation_term = tripal_get_chado_semweb_term($field_table, 'is_not');
  79. }
  80. $chado_table = $this->instance['settings']['chado_table'];
  81. foreach ($items as $delta => $item) {
  82. if ($item['chado-' . $chado_table . '__cvterm_id']) {
  83. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $item['chado-' . $chado_table . '__cvterm_id']));
  84. $dbxref = $cvterm->dbxref_id;
  85. // Build the accession.
  86. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  87. if ($dbxref->db_id->urlprefix) {
  88. $accession = l($accession, tripal_get_dbxref_url($dbxref), array('attributes' => array('target' => '_blank')));
  89. }
  90. $row = array(
  91. $item['value'][$vocabulary_term] . ':' . $item['value'][$accession_term],
  92. $item['value'][$definition_term],
  93. );
  94. if (array_key_exists('is_not', $schema['fields'])) {
  95. $row[] = $item['value'][$negation_term];
  96. }
  97. $rows[] = $row;
  98. }
  99. }
  100. // Theme the results in a talbe.
  101. $caption = 'This record is associated with the following annotations.';
  102. if (array_key_exists('is_not', $schema['fields'])) {
  103. $caption .= 'The "Negates" column indicates if the term does NOT apply.';
  104. }
  105. $table = array(
  106. 'header' => $headers,
  107. 'rows' => $rows,
  108. 'attributes' => array(
  109. 'id' => "$chado_table-table-terms",
  110. 'class' => 'tripal-data-table'
  111. ),
  112. 'caption' => $caption,
  113. 'sticky' => FALSE,
  114. 'colgroups' => array(),
  115. 'empty' => 'There are no annotations of this type',
  116. );
  117. if (count($items) > 0) {
  118. $element[0] = array(
  119. '#type' => 'markup',
  120. '#markup' => theme_table($table),
  121. );
  122. }
  123. }
  124. }