tripal_views_handler_field_aggregate.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if ($this->aggregated) {
  63. foreach ($values as $k => $v) {
  64. $values[$k]->{$this->field_alias} = $this->split_array_agg_results($v->{$this->field_alias});
  65. foreach ($values[$k]->{$this->field_alias} as &$val) {
  66. // First, get the token values
  67. $subparts = explode(',', $val);
  68. $token_values = array();
  69. foreach ($subparts as $ssk => $ssv) {
  70. if (preg_match('/(.*)::(.*)/', $ssv, $matches)) {
  71. $token_values[ '[all-' . $matches[1] . ']' ] = $matches[2];
  72. }
  73. }
  74. // Now manually sub them in
  75. $val = str_replace(array_keys($token_values), $token_values, $this->options['format']['format_string']);
  76. }
  77. }
  78. }
  79. }
  80. }