sio__annotation_formatter.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. dpm($items);
  70. $field_table = $this->instance['settings']['chado_table'];
  71. $schema = chado_get_schema($field_table);
  72. $vocabulary_term = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  73. $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
  74. $definition_term = tripal_get_chado_semweb_term('cvterm', 'definition');
  75. if (array_key_exists('is_not', $schema['fields'])) {
  76. $negation_term = tripal_get_chado_semweb_term($field_table, 'is_not');
  77. }
  78. $chado_table = $this->instance['settings']['chado_table'];
  79. foreach ($items as $delta => $item) {
  80. if ($item['chado-' . $chado_table . '__cvterm_id']) {
  81. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $item['chado-' . $chado_table . '__cvterm_id']));
  82. $dbxref = $cvterm->dbxref_id;
  83. // Build the accession.
  84. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  85. if ($dbxref->db_id->urlprefix) {
  86. $accession = l($accession, tripal_get_dbxref_url($dbxref), array('attributes' => array('target' => '_blank')));
  87. }
  88. $row = array(
  89. $item['value'][$vocabulary_term] . ':' . $item['value'][$accession_term],
  90. $item['value'][$definition_term],
  91. );
  92. if (array_key_exists('is_not', $schema['fields'])) {
  93. if ($negation_term == FALSE) {
  94. $row[1] = 'NOT ' . $row[1];
  95. }
  96. }
  97. $rows[] = $row;
  98. }
  99. }
  100. // Theme the results in a talbe.
  101. $caption = 'This record is associated with the following annotations.';
  102. $table = array(
  103. 'header' => $headers,
  104. 'rows' => $rows,
  105. 'attributes' => array(
  106. 'id' => "$chado_table-table-terms",
  107. 'class' => 'tripal-data-table'
  108. ),
  109. 'caption' => $caption,
  110. 'sticky' => FALSE,
  111. 'colgroups' => array(),
  112. 'empty' => 'There are no annotations of this type',
  113. );
  114. if (count($items) > 0) {
  115. $element[0] = array(
  116. '#type' => 'markup',
  117. '#markup' => theme_table($table),
  118. );
  119. }
  120. }
  121. }