so__transcript_formatter.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. class so__transcript_formatter extends ChadoFieldFormatter {
  3. // The default label for this field.
  4. public static $default_label = 'Transcript';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['so__transcript'];
  7. /**
  8. *
  9. * @see TripalFieldFormatter::view()
  10. */
  11. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  12. $mRNA_fieldset = [];
  13. // If there are no items return an empty element.
  14. if (count($items) == 0) {
  15. return $this->returnEmpty($element);
  16. }
  17. // Get the list of fields that are t o be shown
  18. $fields_to_show = [];
  19. if (array_key_exists('transcript_fields', $this->instance['settings'])) {
  20. $fields_to_show = $this->fieldsToShow($this->instance['settings']['transcript_fields']);
  21. }
  22. // For backwards compatibility if the 'transcript_fields' setting
  23. // is not available then return the default table.
  24. if (count($fields_to_show) == 0) {
  25. return $this->returnDefaultTable($element, $items);
  26. }
  27. // If any of the transcripts are not published then just provide the
  28. // default view
  29. foreach ($items as $delta => $item) {
  30. // Skip empty items;
  31. if (!$item['value']) {
  32. continue;
  33. }
  34. // If any transcript is not published (i.e. it doesn't have an entity)
  35. // then we want to just return the default table and stop.
  36. if (!array_key_exists('entity', $item['value'])) {
  37. return $this->returnDefaultTable($element, $items);
  38. }
  39. }
  40. // Iterate through the list of fields so that we can load the mRNA
  41. // entity with all of the field requested by the site admin. If we
  42. // don't do this first then auto_attach fields won't be added.
  43. $field_ids = [];
  44. foreach ($fields_to_show as $field_name) {
  45. $field_info = field_info_field($field_name);
  46. $field_ids[] = $field_info['id'];
  47. }
  48. // For backwards compatibility if no field IDs were provided then
  49. // return the original table of four rows.
  50. if (count($field_ids) == 0) {
  51. return $this->returnDefaultTable($element, $items);
  52. }
  53. #return $this->returnFieldsets($items, $element, $field_ids, $fields_to_show);
  54. return $this->returnDropDown($items, $element, $field_ids, $fields_to_show);
  55. }
  56. /**
  57. * Returns a list of fields names that the site admin wants to show.
  58. */
  59. private function fieldsToShow($transcript_fields) {
  60. $to_show = [];
  61. foreach($transcript_fields as $field_name => $details) {
  62. if ($details['show'] == 1) {
  63. $to_show[] = $field_name;
  64. }
  65. }
  66. return $to_show;
  67. }
  68. /**
  69. *
  70. */
  71. private function returnDropDown($items, &$element, $field_ids, $fields_to_show) {
  72. drupal_add_js(drupal_get_path ('module', 'tripal_chado') . '/theme/js/so__transcript.js');
  73. // Iterate through each mRNA (transcript).
  74. $options = [0 => '--Select a transcript to view--'];
  75. $transcripts = [];
  76. foreach ($items as $delta => $item) {
  77. // Skip empty items;
  78. if (!$item['value']) {
  79. continue;
  80. }
  81. list($entity_type, $mRNA_entity_id) = explode(':', $item['value']['entity']);
  82. $result = tripal_load_entity('TripalEntity', [$mRNA_entity_id], FALSE, $field_ids);
  83. reset($result);
  84. $mRNA_entity = $result[$mRNA_entity_id];
  85. $options[$mRNA_entity_id] = $mRNA_entity->title;
  86. // Create the fieldset for this transcript.
  87. $transcripts[$mRNA_entity_id] = [
  88. '#type' => 'markup',
  89. '#prefix' => '<div id="' . 'tripal-chado-so__transcript-' . $mRNA_entity_id . '" class="tripal-chado-so__transcript-box">',
  90. '#suffix' => '</div>',
  91. ];
  92. // Add a link to the mRNA page.
  93. $feature_name = $mRNA_entity->title;
  94. $feature_name = l($feature_name, "bio_data/" . $mRNA_entity->id, ['attributes' => ['target' => "_blank"]]);
  95. $transcripts[$mRNA_entity_id]['mRNA_link'] = [
  96. '#type' => 'markup',
  97. '#markup' => "<i>Click, " . $feature_name . ", for the full transcript page.</i>",
  98. '#weight' => -102
  99. ];
  100. // Now add all fields to the fieldset.
  101. $this->addFields($mRNA_entity, $fields_to_show, $transcripts[$mRNA_entity_id]);
  102. }
  103. $transcripts['transcript_dropdown'] = [
  104. '#type' => 'select',
  105. '#title' => 'Transcripts for this gene',
  106. '#options' => $options,
  107. '#weight' => -100,
  108. '#description' => 'Select a transcript to view more details.',
  109. '#attributes' => ['class' => ['tripal-chado-so__transcript-select']]
  110. ];
  111. $element[0] = $transcripts;
  112. }
  113. /**
  114. *
  115. */
  116. private function returnFieldsets($items, &$element, $field_ids, $fields_to_show) {
  117. // Iterate through each mRNA (transcript).
  118. $transcripts = [];
  119. foreach ($items as $delta => $item) {
  120. // Skip empty items;
  121. if (!$item['value']) {
  122. continue;
  123. }
  124. list($entity_type, $mRNA_entity_id) = explode(':', $item['value']['entity']);
  125. // Now load the mRNA entity with all of the fields.
  126. $result = tripal_load_entity('TripalEntity', [$mRNA_entity_id], FALSE, $field_ids);
  127. reset($result);
  128. $mRNA_entity = $result[$mRNA_entity_id];
  129. // Create the fieldset for this transcript.
  130. $transcripts[$mRNA_entity_id] = [
  131. '#type' => 'fieldset',
  132. '#title' => $mRNA_entity->title,
  133. '#description' => '',
  134. '#collapsible' => TRUE,
  135. '#collapsed' => TRUE,
  136. '#attributes' => ['class' => ['collapsible', 'collapsed']],
  137. ];
  138. drupal_add_library('system', 'drupal.collapse');
  139. // Add a link to the mRNA page.
  140. $feature_name = $mRNA_entity->title;
  141. $feature_name = l($feature_name, "bio_data/" . $mRNA_entity->id, ['attributes' => ['target' => "_blank"]]);
  142. $transcripts[$mRNA_entity_id]['mRNA_link'] = [
  143. '#type' => 'markup',
  144. '#markup' => "<i>Click, " . $feature_name . ", for the full transcript page.</i>",
  145. ];
  146. // Now add all fields to the fieldset.
  147. $this->addFields($mRNA_entity, $fields_to_show, $transcripts[$mRNA_entity_id]);
  148. }
  149. $element[0] = $transcripts;
  150. }
  151. /**
  152. * Builds a fieldset for the entity.
  153. */
  154. private function addFields($mRNA_entity, $fields, &$fieldset) {
  155. // Get some settings about the transcript content type.
  156. $mRNA_bundle = tripal_load_bundle_entity(['name'=> $mRNA_entity->bundle]);
  157. $hide_empty = tripal_get_bundle_variable('hide_empty_field', $mRNA_bundle->id);
  158. // Get the Entity ID for the transcript.
  159. $entity_id = $mRNA_entity->id;
  160. // We will use a summary table to house the fields that have a
  161. // cardinality of one. These will appear at the top of the fieldset.
  162. $summary_rows = [];
  163. // Iterate through the list of fields that the site admin has indicated
  164. // that they want to show in the transcript fieldset. They are provided
  165. // in order that they should be shown.
  166. foreach ($fields as $field_name) {
  167. // Load the field instance info. We'll need this to determine the
  168. // cardinality of the field and to render it for display.
  169. $field_info = field_info_field($field_name);
  170. $field_instance = field_info_instance('TripalEntity', $field_name, $mRNA_entity->bundle);
  171. // For the display we want to honor the site admin's wishes and not
  172. // show fields that are empty if they have that setting turned on.
  173. $field_items = field_get_items('TripalEntity', $mRNA_entity, $field_name);
  174. $field_is_empty = tripal_field_is_empty($field_info, $field_items);
  175. if ($field_is_empty and $hide_empty) {
  176. continue;
  177. }
  178. // If the default display is to hide this field then skip it too.
  179. if ($field_instance['display']['default']['type'] == 'hidden') {
  180. continue;
  181. }
  182. // Get the render array for this field.
  183. $field_element = field_view_field('TripalEntity', $mRNA_entity, $field_name, $field_instance['display']['default']);
  184. $field_element['#label_display'] = 'hidden';
  185. // We need to know the cardinality of this field. If it has a
  186. // cardinatliy of 1 we'll put it in the summary table for the
  187. // transcript that appears at the top of the fieldset. If not we'll
  188. // let it render as is, in the order it's provided to us here.
  189. $cardinality = array_key_exists('cardinality', $field_info) ? $field_info['cardinality'] : 1;
  190. if ($cardinality == 1) {
  191. // add field to a special transcripts table, where fields have cardinality of 1
  192. $summary_rows[] = [
  193. [
  194. 'data' => $field_instance['label'],
  195. 'header' => TRUE,
  196. ],
  197. [
  198. 'data' => drupal_render($field_element),
  199. ]
  200. ];
  201. }
  202. // Else add the field as is.
  203. else {
  204. $fieldset[$field_name . '_header'] = [
  205. '#type' => 'markup',
  206. '#markup' => '<h3 class="tripal-chado-gene-transcript-fieldset-item">' . $field_instance['label'] . '</h3>',
  207. ];
  208. $fieldset[$field_name] = [
  209. '#type' => 'markup',
  210. '#markup' => drupal_render($field_element),
  211. ];
  212. }
  213. } // End looping over fields.
  214. // If the summary table has values then
  215. if (count($summary_rows) > 0) {
  216. // display fields of single cardinality in a special transcripts table
  217. $summary_table = [
  218. 'header' => [],
  219. 'rows' => $summary_rows,
  220. 'attributes' => [
  221. 'id' => 'tripal_feature-table-transcript-fields-object',
  222. 'class' => 'tripal-data-table',
  223. ],
  224. 'sticky' => FALSE,
  225. 'caption' => "",
  226. 'colgroups' => [],
  227. 'empty' => 'This feature has no single cardinality transcript fields',
  228. ];
  229. $fieldset['summary_header'] = [
  230. '#type' => 'markup',
  231. '#markup' => '<h3>Transcript ' . $mRNA_entity->title . '</h3>',
  232. '#weight' => -101,
  233. ];
  234. $fieldset['summary_table'] = [
  235. '#type' => 'markup',
  236. '#markup' => theme_table($summary_table),
  237. '#weight' => -100,
  238. ];
  239. }
  240. }
  241. /**
  242. * Returns the default table.
  243. *
  244. * For backwards compatibility this function returns a table
  245. * of four columns and one row per transcript.
  246. */
  247. private function returnDefaultTable(&$element, $items) {
  248. $default_headers = ['Transcript Name', 'Identifier', 'Type', 'Location'];
  249. $default_rows = [];
  250. foreach ($items as $delta => $item) {
  251. if (!$item['value']) {
  252. continue;
  253. }
  254. // Get the field values.
  255. $feature_name = $item['value']['schema:name'];
  256. $feature_uname = $item['value']['data:0842'];
  257. $loc = $item['value']['SO:0000735'];
  258. $type = $item['value']['rdfs:type'];
  259. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  260. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  261. $feature_name = l($feature_name, "bio_data/" . $entity_id, ['attributes' => ['target' => "_blank"]]);
  262. }
  263. $default_rows[] = [$feature_name, $feature_uname, $type, $loc];
  264. }
  265. // Build the default table
  266. $default_table = [
  267. 'header' => $default_headers,
  268. 'rows' => $default_rows,
  269. 'attributes' => [
  270. 'id' => 'tripal_feature-table-transcripts-object',
  271. 'class' => 'tripal-data-table',
  272. ],
  273. 'sticky' => FALSE,
  274. 'caption' => "",
  275. 'colgroups' => [],
  276. 'empty' => 'This feature has no transcripts',
  277. ];
  278. $element[0] = [
  279. '#type' => 'markup',
  280. '#markup' => theme_table($default_table),
  281. ];
  282. }
  283. /**
  284. * Returns an empty element for the view.
  285. */
  286. private function returnEmpty(&$element){
  287. $element[0] = [
  288. '#type' => 'markup',
  289. '#markup' => '',
  290. ];
  291. }
  292. }