You are here

public function FrxCrosstab::generate in Forena Reports 7.5

Generate the template from the configuration.

Parameters

string $data_block:

SimpleXMLElement $xml:

array $config:

Overrides RendererBase::generate

File

src/Renderer/FrxCrosstab.php, line 220

Class

FrxCrosstab

Namespace

Drupal\forena\Renderer

Code

public function generate($xml, &$config) {
  $config['class'] = get_class($this);
  $block = @$config['block'];
  $id = @$config['id'];
  if ($block) {
    $id = $this
      ->idFromBlock($block);
    $config['id'] = $id . '_block';
  }
  $config['class'] = @$config['class'] ? $config['class'] . ' FrxCrosstab' : 'FrxCrosstab';
  $div = $this
    ->blockDiv($config);

  // PUt on the header
  $this
    ->removeChildren($div);
  if (isset($config['header']['value'])) {
    $this
      ->addFragment($div, $config['header']['value']);
  }

  // Decide to inlcude columns
  $found_columns = $this
    ->columns($xml);
  if (!$found_columns) {
    $found_columns = $this
      ->columns($xml, '/*');
    $attrs = array();
  }
  $numeric_columns = $this->numeric_columns;
  $new_columns = @$config['crosstab_columns'] ? FALSE : TRUE;
  foreach ($found_columns as $column => $label) {
    $token = '{' . $column . '}';
    if ($new_columns) {
      $type = isset($numeric_columns[$column]) ? 'value' : 'heading';
    }
    else {
      $type = 'ignore';
    }
    if (!isset($config['crosstab_columns'][$column])) {
      $this
        ->addColumn($type, '{' . $column . '}', $column, $config);
    }
  }

  // Generate the grouping row
  $group = '';
  $dim = array();
  foreach ($config['crosstab_columns'] as $col) {
    if ($col['type'] == 'heading') {
      $group .= $col['contents'];
    }
    if ($col['type'] == 'crosstab') {
      $dim = $col['contents'];
    }
  }
  $r_id = $id . '-renderer';
  $table_frx['renderer'] = 'FrxCrosstab';
  $table_frx['group'] = $group;
  $table_frx['dim'] = $dim;
  $attrs[$id] = $r_id;

  //$attrs = array('foreach' => '*');
  $table = $this
    ->setFirstNode($div, 4, 'table', NULL, $attrs, $table_frx);
  $thead = $this
    ->setFirstNode($table, 6, 'thead');
  $throw = $this
    ->setFirstNode($thead, 8, 'tr');
  $tbody = $this
    ->setFirstNode($table, 6, 'tbody');
  $tdrow = $this
    ->setFirstNode($tbody, 8, 'tr', NULL, array(
    'id' => $id,
  ), $attrs);
  if ($config['crosstab_columns']) {
    foreach ($config['crosstab_columns'] as $key => $col) {
      if ($col['type'] !== 'ignore') {
        if ($col['type'] == 'heading') {
          $tag = 'th';
        }
        else {
          $tag = 'td';
        }
        if ($col['type'] != 'crosstab') {
          $this
            ->addNode($throw, 10, $tag, $col['label']);
          $this
            ->addNode($tdrow, 10, $tag, $col['contents']);
        }
      }
    }
  }
  if (isset($config['footer']['value'])) {
    $this
      ->addFragment($div, $config['footer']['value']);
  }
}