You are here

function views_crosstab_table::pre_render in Views Crosstab 7

Allow the style to do stuff before each row is rendered.

Parameters

array $result: The full array of results from the query.

Overrides views_plugin_style::pre_render

File

plugins/views_crosstab_table.inc, line 463
Plugin functions

Class

views_crosstab_table
Style plugin to transform a linear query into a crosstab table.

Code

function pre_render($values) {

  // This method should properly have the argument passed by referenced. An
  // attempt to have the views API corrected to do so was unsuccessful.
  // Declaring the argument by reference generates a "Declaration of X should
  // be compatible with Y" warning.
  //
  // It would be possible to suppress errors for this function, but that might
  // suppress actual errors.
  // See: https://stackoverflow.com/questions/36079651/silence-declaration-should-be-compatible-warnings-in-php-7
  // And in specific the solution by Andrea.
  //
  // The solution taken here is to modify the result in the passed view
  // object. This will not work if pre_render is called with something other
  // than ($view->result), which it currently is not.
  $result =& $this->view->result;

  // Get options array.
  $options = $this->view->style_plugin->options;

  // Generate summary row
  if (count($result) && $options['include_crosstab_operation_on_column']) {
    $summary_query = clone $this->view->build_info['query'];

    // Remove groupBy to summarize all rows
    $group_by =& $summary_query
      ->getGroupBy();
    $group_by = array();
    $summary_results = $summary_query
      ->execute()
      ->fetchObject();
    $result[] = $summary_results;
  }

  // Get attribute that defines numerical precision.
  $rounding = $options['crosstab_decimals'];

  // Loop over rows in $values array.
  foreach ($result as &$result_row) {

    // Loop over fields in current row.
    foreach ($result_row as &$field_value) {

      // Check if value is numeric.
      if (is_numeric($field_value)) {

        // Round result to appropriate precision.
        $field_value = number_format($field_value, $rounding, '.', '');
      }
    }
  }
}