You are here

public function FrxCrosstab::render in Forena Reports 7.4

Render the crosstab

Overrides FrxRenderer::render

File

renderers/FrxCrosstab.inc, line 14

Class

FrxCrosstab

Code

public function render() {
  $variables = $this
    ->mergedAttributes();
  $attributes = $this
    ->replacedAttributes();
  if (!empty($variables['hidden']) && $this->format != 'csv' && $this->format != 'xls') {
    return '';
  }
  $path = isset($variables['path']) ? $variables['path'] : '*';
  if (!$path) {
    $path = "*";
  }
  $group = $variables['group'];
  $dim = $variables['dim'];
  $sum = (array) @$variables['sum'];

  // Get the current context
  $data = Frx::Data()
    ->currentContext();

  // Generate the data nodes.
  if (is_object($data)) {
    if (method_exists($data, 'xpath')) {
      $nodes = $data
        ->xpath($path);
    }
    else {
      $nodes = $data;
    }
  }
  else {
    $nodes = (array) $data;
  }

  // Group the data.
  $data = $this->frxReport
    ->group($nodes, $group, $sum);
  $this->dim_headers = array();
  $this->dim_rows = array();
  $this->dim_columns = array();
  $this->group_columns = array();
  $this->group_headers = array();
  $dim_values = array();
  $rows = array();
  foreach ($data as $gk => $group_rows) {
    $row_copy = array_values($group_rows);
    $dims = $this->frxReport
      ->group($group_rows, $dim);
    $rows[$gk] = $group_rows[0];
    foreach ($dims as $dk => $r) {
      $dims = array_values($r);
      $dim_values[$dk] = $dk;
      $dim_rows[$gk][$dk] = $r[0];
    }
  }

  // Default controling attributes
  $this
    ->defaultHeaders($dim_values);
  $hrow = array();
  foreach ($this->group_headers as $col) {
    $cell = $col;
    if (count($this->dim_columns) > 1) {
      $cell['rowspan'] = 2;
    }
    $hrow[] = $cell;
  }

  // Add the dimension headers.
  foreach ($dim_values as $dk) {
    foreach ($this->dim_headers as $i => $col) {
      $cell = $col;
      $cell['data'] = $dk;
      if (count($this->dim_columns) > 1) {
        $cell['data'] = $i ? $col['data'] : $dk . ' ' . $col['data'];
      }
      $hrow[] = $cell;
    }
  }
  $trows = array();
  foreach ($rows as $k => $row) {
    Frx::Data()
      ->push($row, '_group');
    $trow = array();

    // Base group
    foreach ($this->group_columns as $col) {
      $cell = $col;
      foreach ($col as $key => $v) {
        $cell[$key] = $this->teng
          ->replace($v);
      }
      $trow[] = $cell;
    }
    Frx::Data()
      ->pop();

    // Dimensions
    $dim_data = $dim_rows[$k];
    foreach ($dim_values as $dk) {
      $dim_row = isset($dim_data[$dk]) ? $dim_data[$dk] : array();
      frx::Data()
        ->push($dim_row, '_dim');
      foreach ($this->dim_columns as $col) {
        $cell = $col;
        foreach ($col as $k => $v) {
          $cell[$k] = $this->teng
            ->replace($v);
        }
        $trow[] = $cell;
      }
      frx::Data()
        ->pop();
    }
    $trows[] = $trow;
  }
  $class = 'crosstab-table';
  if (isset($attributes['class'])) {
    $class .= ' ' . $attributes['class'];
  }
  $vars = array(
    'header' => $hrow,
    'rows' => $trows,
    'attributes' => array(
      'class' => array(
        $class,
      ),
    ),
  );
  $output = theme('table', $vars);
  return $output;
}