so__transcript_formatter.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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',
  106. '#options' => $options,
  107. '#weight' => -100,
  108. '#attributes' => ['class' => ['tripal-chado-so__transcript-select']]
  109. ];
  110. $element[0] = $transcripts;
  111. }
  112. /**
  113. *
  114. */
  115. private function returnFieldsets($items, &$element, $field_ids, $fields_to_show) {
  116. // Iterate through each mRNA (transcript).
  117. $transcripts = [];
  118. foreach ($items as $delta => $item) {
  119. // Skip empty items;
  120. if (!$item['value']) {
  121. continue;
  122. }
  123. list($entity_type, $mRNA_entity_id) = explode(':', $item['value']['entity']);
  124. // Now load the mRNA entity with all of the fields.
  125. $result = tripal_load_entity('TripalEntity', [$mRNA_entity_id], FALSE, $field_ids);
  126. reset($result);
  127. $mRNA_entity = $result[$mRNA_entity_id];
  128. // Create the fieldset for this transcript.
  129. $transcripts[$mRNA_entity_id] = [
  130. '#type' => 'fieldset',
  131. '#title' => $mRNA_entity->title,
  132. '#description' => '',
  133. '#collapsible' => TRUE,
  134. '#collapsed' => TRUE,
  135. '#attributes' => ['class' => ['collapsible', 'collapsed']],
  136. ];
  137. drupal_add_library('system', 'drupal.collapse');
  138. // Add a link to the mRNA page.
  139. $feature_name = $mRNA_entity->title;
  140. $feature_name = l($feature_name, "bio_data/" . $mRNA_entity->id, ['attributes' => ['target' => "_blank"]]);
  141. $transcripts[$mRNA_entity_id]['mRNA_link'] = [
  142. '#type' => 'markup',
  143. '#markup' => "<i>Click, " . $feature_name . ", for the full transcript page.</i>",
  144. ];
  145. // Now add all fields to the fieldset.
  146. $this->addFields($mRNA_entity, $fields_to_show, $transcripts[$mRNA_entity_id]);
  147. }
  148. $element[0] = $transcripts;
  149. }
  150. /**
  151. * Builds a fieldset for the entity.
  152. */
  153. private function addFields($mRNA_entity, $fields, &$fieldset) {
  154. // Get some settings about the transcript content type.
  155. $mRNA_bundle = tripal_load_bundle_entity(['name'=> $mRNA_entity->bundle]);
  156. $hide_empty = tripal_get_bundle_variable('hide_empty_field', $mRNA_bundle->id);
  157. // Get the Entity ID for the transcript.
  158. $entity_id = $mRNA_entity->id;
  159. // We will use a summary table to house the fields that have a
  160. // cardinality of one. These will appear at the top of the fieldset.
  161. $summary_rows = [];
  162. // Iterate through the list of fields that the site admin has indicated
  163. // that they want to show in the transcript fieldset. They are provided
  164. // in order that they should be shown.
  165. foreach ($fields as $field_name) {
  166. // Load the field instance info. We'll need this to determine the
  167. // cardinality of the field and to render it for display.
  168. $field_info = field_info_field($field_name);
  169. $field_instance = field_info_instance('TripalEntity', $field_name, $mRNA_entity->bundle);
  170. // For the display we want to honor the site admin's wishes and not
  171. // show fields that are empty if they have that setting turned on.
  172. $field_items = field_get_items('TripalEntity', $mRNA_entity, $field_name);
  173. $field_is_empty = tripal_field_is_empty($field_info, $field_items);
  174. if ($field_is_empty and $hide_empty) {
  175. continue;
  176. }
  177. // If the default display is to hide this field then skip it too.
  178. if ($field_instance['display']['default']['type'] == 'hidden') {
  179. continue;
  180. }
  181. // Get the render array for this field.
  182. $field_element = field_view_field('TripalEntity', $mRNA_entity, $field_name, $field_instance['display']['default']);
  183. $field_element['#label_display'] = 'hidden';
  184. // We need to know the cardinality of this field. If it has a
  185. // cardinatliy of 1 we'll put it in the summary table for the
  186. // transcript that appears at the top of the fieldset. If not we'll
  187. // let it render as is, in the order it's provided to us here.
  188. $cardinality = array_key_exists('cardinality', $field_info) ? $field_info['cardinality'] : 1;
  189. if ($cardinality == 1) {
  190. // add field to a special transcripts table, where fields have cardinality of 1
  191. $summary_rows[] = [
  192. [
  193. 'data' => $field_instance['label'],
  194. 'header' => TRUE,
  195. ],
  196. [
  197. 'data' => drupal_render($field_element),
  198. ]
  199. ];
  200. }
  201. // Else add the field as is.
  202. else {
  203. $fieldset[$field_name . '_header'] = [
  204. '#type' => 'markup',
  205. '#markup' => '<h3 class="tripal-chado-gene-transcript-fieldset-item">' . $field_instance['label'] . '</h3>',
  206. ];
  207. $fieldset[$field_name] = [
  208. '#type' => 'markup',
  209. '#markup' => drupal_render($field_element),
  210. ];
  211. }
  212. } // End looping over fields.
  213. // If the summary table has values then
  214. if (count($summary_rows) > 0) {
  215. // display fields of single cardinality in a special transcripts table
  216. $summary_table = [
  217. 'header' => [],
  218. 'rows' => $summary_rows,
  219. 'attributes' => [
  220. 'id' => 'tripal_feature-table-transcript-fields-object',
  221. 'class' => 'tripal-data-table',
  222. ],
  223. 'sticky' => FALSE,
  224. 'caption' => "",
  225. 'colgroups' => [],
  226. 'empty' => 'This feature has no single cardinality transcript fields',
  227. ];
  228. $fieldset['summary_header'] = [
  229. '#type' => 'markup',
  230. '#markup' => '<h3>Transcript ' . $mRNA_entity->title . '</h3>',
  231. '#weight' => -101,
  232. ];
  233. $fieldset['summary_table'] = [
  234. '#type' => 'markup',
  235. '#markup' => theme_table($summary_table),
  236. '#weight' => -100,
  237. ];
  238. }
  239. }
  240. /**
  241. * Returns the default table.
  242. *
  243. * For backwards compatibility this function returns a table
  244. * of four columns and one row per transcript.
  245. */
  246. private function returnDefaultTable(&$element, $items) {
  247. $default_headers = ['Transcript Name', 'Identifier', 'Type', 'Location'];
  248. $default_rows = [];
  249. foreach ($items as $delta => $item) {
  250. if (!$item['value']) {
  251. continue;
  252. }
  253. // Get the field values.
  254. $feature_name = $item['value']['schema:name'];
  255. $feature_uname = $item['value']['data:0842'];
  256. $loc = $item['value']['SO:0000735'];
  257. $type = $item['value']['rdfs:type'];
  258. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  259. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  260. $feature_name = l($feature_name, "bio_data/" . $entity_id, ['attributes' => ['target' => "_blank"]]);
  261. }
  262. $default_rows[] = [$feature_name, $feature_uname, $type, $loc];
  263. }
  264. // Build the default table
  265. $default_table = [
  266. 'header' => $default_headers,
  267. 'rows' => $default_rows,
  268. 'attributes' => [
  269. 'id' => 'tripal_feature-table-transcripts-object',
  270. 'class' => 'tripal-data-table',
  271. ],
  272. 'sticky' => FALSE,
  273. 'caption' => "",
  274. 'colgroups' => [],
  275. 'empty' => 'This feature has no transcripts',
  276. ];
  277. $element[0] = [
  278. '#type' => 'markup',
  279. '#markup' => theme_table($default_table),
  280. ];
  281. }
  282. /**
  283. * Returns an empty element for the view.
  284. */
  285. private function returnEmpty(&$element){
  286. $element[0] = [
  287. '#type' => 'markup',
  288. '#markup' => '',
  289. ];
  290. }
  291. }