tripal_views_handler_field_aggregate.inc

A chado wrapper for the views_handler_field.

Handles fields which may be aggregated during the chado join process. This field will render an aggregated field as a pre_rendered list and will dynamically detect whether the field is aggregated or not.

File

tripal_views/views/handlers/tripal_views_handler_field_aggregate.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * A chado wrapper for the views_handler_field.
  5. *
  6. * Handles fields which may be aggregated during the chado join process. This field
  7. * will render an aggregated field as a pre_rendered list and will dynamically detect
  8. * whether the field is aggregated or not.
  9. */
  10. class tripal_views_handler_field_aggregate extends chado_views_handler_field {
  11. function init(&$view, $options) {
  12. parent::init($view, $options);
  13. if (!isset($this->chado_table_description)) {
  14. $this->chado_table_description = tripal_core_get_chado_table_schema($this->table);
  15. foreach ($this->chado_table_description['foreign keys'] as $defn) {
  16. if ($defn['table'] != $this->view->base_table) {
  17. $join_table = tripal_core_get_chado_table_schema($defn['table']);
  18. foreach ($join_table['fields'] as $fname => $f) {
  19. $this->chado_table_description['fields'][$defn['table'] . '_' . $fname] = $f;
  20. }
  21. }
  22. }
  23. }
  24. }
  25. /**
  26. * Defines the options form (form available to admin when they add a field to a view)
  27. */
  28. function options_form(&$form, &$form_state) {
  29. parent::options_form($form, $form_state);
  30. $form['format'] = array(
  31. '#type' => 'fieldset',
  32. '#title' => 'Format Output',
  33. '#description' => t('The following fields specify how a single result of this field will be
  34. displayed. When there are multiple results of this field due to aggregation, each result
  35. will be rendered according to the following rules and then all results will be joined
  36. together based on the "Display Type" indicated.')
  37. );
  38. $this->tokens = array();
  39. $value = array();
  40. foreach ( array_keys($this->chado_table_description['fields']) as $field ) {
  41. $t = '[' . $this->options['id'] . '-' . $field . ']';
  42. $this->tokens[$t] = t($field);
  43. $value[] = $t . ' == ' . $field;
  44. }
  45. $form['format']['format_string'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Format String'),
  48. '#description' => 'Use any of the format tokens below to indicate what fields you want displayed.',
  49. '#default_value' => ($this->options['format']['format_string']) ? $this->options['format']['format_string'] : implode(', ', array_keys($this->tokens)),
  50. );
  51. $form['format']['tokens'] = array(
  52. '#type' => 'item',
  53. '#title' => 'Format Tokens',
  54. '#value' => implode("<br />", $value),
  55. );
  56. }
  57. function query() {
  58. parent::query();
  59. $this->table_definition = $this->query->get_table_info($this->table);
  60. }
  61. function pre_render(&$values) {
  62. // further check the results to see if this field is a postgresql array
  63. $this->aggregated = chado_wrapper_is_aggregated_by_result($this, $values);
  64. if ($this->aggregated) {
  65. // Split Aggregated Results
  66. chado_wrapper_split_array_agg_results($this, $values);
  67. // Now each result is of the following form:
  68. // stockprop_id::16554,stock_id::12037,type_id::3650,value::Sm2008-P13,rank::1...
  69. // we need to split it further
  70. foreach ($values as $k => $v) {
  71. $this->split_aggregated_result_with_keys($values[$k]->{$this->field_alias});
  72. }
  73. }
  74. }
  75. // Split a single value in the $values array into an associative array
  76. // $value will be an array where each value is of the following form:
  77. // stockprop_id::16554,stock_id::12037,type_id::3650,value::Sm2008-P13,rank::1...
  78. function split_aggregated_result_with_keys(&$value) {
  79. foreach ($value as $k => $v) {
  80. if (!is_array($v)) {
  81. if (preg_match('/.*::.*/',$v)) {
  82. $subparts = explode(',', $v);
  83. $token_values = array();
  84. foreach ($subparts as $ssk => $ssv) {
  85. if (preg_match('/(.*)::(.*)/', $ssv, $matches)) {
  86. $values[ $matches[1] ] = $matches[2];
  87. $tokens[ '[' . $this->options['id'] . '-' . $matches[1] . ']' ] = $matches[2];
  88. }
  89. }
  90. if ($this->options['format']['format_string']) {
  91. $value[$k] = str_replace(array_keys($tokens), $tokens, $this->options['format']['format_string']);
  92. }
  93. else {
  94. $value[$k] = $values;
  95. }
  96. }
  97. else {
  98. if ($this->options['format']['format_string']) {
  99. $value[$k] = '';
  100. }
  101. else {
  102. $value[$k] = array();
  103. }
  104. }
  105. $value = array_filter($value);
  106. }
  107. }
  108. }
  109. }