sbo__relationship_formatter.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. class sbo__relationship_formatter extends ChadoFieldFormatter {
  3. // The default label for this field.
  4. public static $default_label = 'Relationship Statements';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['sbo__relationship'];
  7. public static $default_settings = [
  8. 'title' => 'Relationship',
  9. 'empty' => 'There are no relationships',
  10. ];
  11. /**
  12. *
  13. * @see TripalFieldFormatter::settingsForm()
  14. */
  15. public function settingsForm($view_mode, $form, &$form_state) {
  16. $display = $this->instance['display'][$view_mode];
  17. $settings = $display['settings'];
  18. $element = [];
  19. $element['title'] = [
  20. '#type' => 'textfield',
  21. '#title' => 'Table Header',
  22. '#default_value' => array_key_exists('title', $settings) ? $settings['title'] : 'Relationship',
  23. ];
  24. $element['empty'] = [
  25. '#type' => 'textfield',
  26. '#title' => 'Empty text',
  27. '#default_value' => array_key_exists('empty', $settings) ? $settings['empty'] : 'There are no relationships',
  28. ];
  29. return $element;
  30. }
  31. /**
  32. * @see TripalFieldFormatter::settingsSummary()
  33. */
  34. public function settingsSummary($view_mode) {
  35. $display = $this->instance['display'][$view_mode];
  36. $settings = $display['settings'];
  37. $summary = t('Title: @title<br>Empty: @empty',
  38. [
  39. '@title' => $settings['title'],
  40. '@empty' => $settings['empty'],
  41. ]
  42. );
  43. return $summary;
  44. }
  45. /**
  46. *
  47. * @see TripalFieldFormatter::view()
  48. */
  49. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  50. // Load the Field class into scope for use below.
  51. tripal_load_include_field_class('sbo__relationship');
  52. // Get the settings
  53. $settings = $display['settings'];
  54. $rows = [];
  55. $headers = [$settings['title']];
  56. foreach ($items as $delta => $item) {
  57. if (empty($item['value'])) {
  58. continue;
  59. }
  60. $subject_name = $item['value']['local:relationship_subject']['schema:name'];
  61. $subject_type = $item['value']['local:relationship_subject']['rdfs:type'];
  62. $object_name = $item['value']['local:relationship_object']['schema:name'];
  63. $object_type = $item['value']['local:relationship_object']['rdfs:type'];
  64. $rel_type = $item['value']['local:relationship_type'];
  65. // Get some better human-readable formats for the type and verb.
  66. $verb = sbo__relationship::get_rel_verb($rel_type);
  67. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  68. // Handle some special cases.
  69. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  70. // we want to show the parent gene and the protein.
  71. if ($object_type == 'mRNA' and
  72. (in_array($subject_type, [
  73. 'CDS',
  74. 'exon',
  75. 'five_prime_UTR',
  76. 'three_prime_UTR',
  77. ]))) {
  78. continue;
  79. }
  80. // Convert the object/subject to a link if an entity exists for it.
  81. if (array_key_exists('entity', $item['value']['local:relationship_object'])) {
  82. list($entity_type, $object_entity_id) = explode(':', $item['value']['local:relationship_object']['entity']);
  83. if ($object_entity_id != $entity->id) {
  84. $object_name = l($object_name, 'bio_data/' . $object_entity_id);
  85. }
  86. }
  87. if (array_key_exists('entity', $item['value']['local:relationship_subject'])) {
  88. list($entity_type, $subject_entity_id) = explode(':', $item['value']['local:relationship_subject']['entity']);
  89. if ($subject_entity_id != $entity->id) {
  90. $subject_name = l($subject_name, 'bio_data/' . $subject_entity_id);
  91. }
  92. }
  93. // Add bold font to the subject and object names.
  94. $subject_name = '<b>' . $subject_name . '</b>';
  95. $object_name = '<b>' . $object_name . '</b>';
  96. // The $clause text here will match what is already in $item['value']['SIO:000493']
  97. // but we rebuild it here to incorporated links and formatting
  98. $clause = 'The ' . $subject_type . ', ' .
  99. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' ' .
  100. $object_type . ', ' . $object_name . '.';
  101. $rows[][] = [
  102. 'data' => $clause,
  103. //'class' => array('tripal-entity-unattached field-items')
  104. ];
  105. }
  106. // Build the pager
  107. $items_per_page = array_key_exists('items_per_page', $this->instance['settings']) ? $this->instance['settings']['items_per_page'] : 10;
  108. $total_records = count($rows);
  109. $total_pages = (int) ($total_records / $items_per_page) + 1;
  110. $pelement = 0;
  111. $current_page = pager_default_initialize($total_records, $items_per_page, $pelement);
  112. $pager = theme('pager', [
  113. 'tags' => [],
  114. 'element' => $pelement,
  115. 'parameters' => [],
  116. 'quantity' => $total_pages,
  117. ]);
  118. $pager = $this->ajaxifyPager($pager, $entity);
  119. $page_items = array_chunk($rows, $items_per_page);
  120. $caption = '';
  121. if ($total_records == 1) {
  122. $caption = 'There is ' . count($rows) . ' relationship.';
  123. }
  124. else {
  125. $caption = 'There are ' . count($rows) . ' relationships.';
  126. }
  127. $content = theme_table([
  128. 'header' => $headers,
  129. 'rows' => count($rows) > 0 ? $page_items[$current_page] : [],
  130. 'attributes' => [
  131. 'class' => 'tripal-data-table',
  132. ],
  133. 'sticky' => FALSE,
  134. 'caption' => $caption,
  135. 'colgroups' => [],
  136. 'empty' => $settings['empty'],
  137. ]);
  138. $element[0] = [
  139. '#type' => 'markup',
  140. '#markup' => $content . $pager,
  141. ];
  142. }
  143. }