chado_views_handler_field_aggregate.inc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class chado_views_handler_field_aggregate extends chado_views_handler_field {
  3. function init(&$view, $options) {
  4. parent::init($view, $options);
  5. if (!isset($this->chado_table_description)) {
  6. $this->chado_table_description = module_invoke_all('chado_'.$this->table.'_schema');
  7. foreach($this->chado_table_description['foreign keys'] as $defn) {
  8. if ($defn['table'] != $this->view->base_table) {
  9. $join_table = module_invoke_all('chado_'.$defn['table'].'_schema');
  10. foreach ($join_table['fields'] as $fname => $f) {
  11. $this->chado_table_description['fields'][$defn['table'] .'_'. $fname] = $f;
  12. }
  13. }
  14. }
  15. }
  16. }
  17. /**
  18. * Defines the options form (form available to admin when they add a field to a view)
  19. */
  20. function options_form(&$form, &$form_state) {
  21. parent::options_form($form, $form_state);
  22. $form['format'] = array(
  23. '#type' => 'fieldset',
  24. '#title' => 'Format Output',
  25. '#description' => t('The following fields specify how a single result of this field will be
  26. displayed. When there are multiple results of this field due to aggregation, each result
  27. will be rendered according to the following rules and then all results will be joined
  28. together based on the "Display Type" indicated.')
  29. );
  30. $this->tokens = array();
  31. $value = array();
  32. foreach( array_keys($this->chado_table_description['fields']) as $field ) {
  33. $t = '[' . $this->options['id'] . '-' . $field . ']';
  34. $this->tokens[$t] = t($field);
  35. $value[] = $t . ' == ' . $field;
  36. }
  37. $form['format']['format_string'] = array(
  38. '#type' => 'textfield',
  39. '#title' => t('Format String'),
  40. '#description' => 'Use any of the format tokens below to indicate what fields you want displayed.',
  41. '#default_value' => ($this->options['format']['format_string']) ? $this->options['format']['format_string'] : implode(', ', array_keys($this->tokens)),
  42. );
  43. $form['format']['tokens'] = array(
  44. '#type' => 'item',
  45. '#title' => 'Format Tokens',
  46. '#value' => implode("<br />",$value),
  47. );
  48. }
  49. function query() {
  50. parent::query();
  51. $this->table_definition = $this->query->get_table_info($this->table);
  52. }
  53. function pre_render (&$values) {
  54. if ($this->aggregated) {
  55. foreach($values as $k => $v) {
  56. $values[$k]->{$this->field_alias} = $this->split_array_agg_results($v->{$this->field_alias});
  57. foreach($values[$k]->{$this->field_alias} as &$val) {
  58. // First, get the token values
  59. $subparts = explode(',',$val);
  60. $token_values = array();
  61. foreach($subparts as $ssk => $ssv) {
  62. if(preg_match('/(.*)::(.*)/',$ssv,$matches)) {
  63. $token_values[ '[all-'.$matches[1].']' ] = $matches[2];
  64. }
  65. }
  66. // Now manually sub them in
  67. $val = str_replace(array_keys($token_values),$token_values,$this->options['format']['format_string']);
  68. }
  69. }
  70. }
  71. }
  72. }