示例#1
0
 /**
  * Add calculations
  *
  * @param   array  $a    of field elements $a
  * @param   string &$str to out put as csv file $str
  *
  * @return  null
  */
 protected function addCalculations($a, &$str)
 {
     $input = $this->app->input;
     if ($input->get('inccalcs') == 1) {
         $incRaw = $input->get('incraw', true);
         $calKeys = array('sums', 'avgs', 'medians', 'count');
         foreach ($calKeys as $calKey) {
             $calculations[$calKey] = FArrayHelper::array_fill(0, count($a) + 1, ' ');
             $calculations[$calKey][0] = $calKey;
             $calcs = $this->model->getCalculations();
             foreach ($calcs[$calKey] as $key => $cal) {
                 $x = 0;
                 $found = false;
                 // $$$rob if grouped data and calc split then get the formatted string as $cal['calc] wont exist below
                 foreach ($a as $aKey => $aVal) {
                     if (trim($aKey) == trim($key) && $x != 0) {
                         $json = $calcs[$calKey][$aKey . '_obj'];
                         unset($json['']);
                         if (count($json) == 1) {
                             $default = $json['Total']->value;
                         } else {
                             $default = json_encode($json);
                         }
                     }
                     $x++;
                 }
                 $x = 0;
                 foreach ($a as $aKey => $aVal) {
                     if ($aKey == JString::substr($key, 0, JString::strlen($key) - 4) && $x != 0) {
                         $found = true;
                         break;
                     }
                     $x++;
                 }
                 if ($found) {
                     if (array_key_exists('calc', $cal)) {
                         $calculations[$calKey][$x] = $cal['calc']->value;
                         if ($incRaw) {
                             $calculations[$calKey][$x + 1] = $cal['calc']->value;
                         }
                     } else {
                         $calculations[$calKey][$x] = $default;
                         if ($incRaw) {
                             $calculations[$calKey][$x + 1] = $default;
                         }
                     }
                 }
             }
             $str .= implode($this->delimiter, array_map(array($this, 'quote'), $calculations[$calKey]));
             $str .= "\n";
         }
     }
 }