Tripal v1.0 (6.x-1.0)
tripal_views_handler_field_aggregate.inc
Go to the documentation of this file.
00001 <?php
00002 
00011 class tripal_views_handler_field_aggregate extends chado_views_handler_field {
00012 
00013   function init(&$view, $options) {
00014     parent::init($view, $options);
00015 
00016     if (!isset($this->chado_table_description)) {
00017       $this->chado_table_description = tripal_core_get_chado_table_schema($this->table);
00018       foreach ($this->chado_table_description['foreign keys'] as $defn) {
00019         if ($defn['table'] != $this->view->base_table) {
00020           $join_table = tripal_core_get_chado_table_schema($defn['table']);
00021           foreach ($join_table['fields'] as $fname => $f) {
00022             $this->chado_table_description['fields'][$defn['table'] . '_' . $fname] = $f;
00023           }
00024         }
00025       }
00026     }
00027 
00028   }
00029 
00033   function options_form(&$form, &$form_state) {
00034     parent::options_form($form, $form_state);
00035 
00036     $form['format'] = array(
00037       '#type' => 'fieldset',
00038       '#title' => 'Format Output',
00039       '#description' => t('The following fields specify how a single result of this field will be
00040         displayed. When there are multiple results of this field due to aggregation, each result
00041         will be rendered according to the following rules and then all results will be joined
00042         together based on the "Display Type" indicated.')
00043     );
00044 
00045     $this->tokens = array();
00046     $value = array();
00047     foreach ( array_keys($this->chado_table_description['fields']) as $field ) {
00048       $t = '[' . $this->options['id'] . '-' . $field . ']';
00049       $this->tokens[$t] = t($field);
00050       $value[] = $t . ' == ' . $field;
00051     }
00052 
00053     $form['format']['format_string'] = array(
00054       '#type' => 'textfield',
00055       '#title' => t('Format String'),
00056       '#description' => 'Use any of the format tokens below to indicate what fields you want displayed.',
00057       '#default_value' => ($this->options['format']['format_string']) ? $this->options['format']['format_string'] : implode(', ', array_keys($this->tokens)),
00058     );
00059 
00060     $form['format']['tokens'] = array(
00061       '#type' => 'item',
00062       '#title' => 'Format Tokens',
00063       '#value' => implode("<br />", $value),
00064     );
00065 
00066   }
00067 
00068   function query() {
00069     parent::query();
00070 
00071     $this->table_definition = $this->query->get_table_info($this->table);
00072   }
00073 
00074 
00075   function pre_render(&$values) {
00076 
00077     if ($this->aggregated) {
00078       foreach ($values as $k => $v) {
00079         $values[$k]->{$this->field_alias} = $this->split_array_agg_results($v->{$this->field_alias});
00080 
00081         foreach ($values[$k]->{$this->field_alias} as &$val) {
00082 
00083           // First, get the token values
00084           $subparts = explode(',', $val);
00085           $token_values = array();
00086           foreach ($subparts as $ssk => $ssv) {
00087             if (preg_match('/(.*)::(.*)/', $ssv, $matches)) {
00088               $token_values[ '[all-' . $matches[1] . ']' ] = $matches[2];
00089             }
00090           }
00091 
00092           // Now manually sub them in
00093           $val = str_replace(array_keys($token_values), $token_values, $this->options['format']['format_string']);
00094 
00095         }
00096       }
00097 
00098     }
00099 
00100   }
00101 
00102 }
 All Classes Files Functions Variables