You are here

public function FrxControls::csvDocument in Forena Reports 6.2

Same name and namespace in other branches
  1. 6 plugins/FrxControls.inc \FrxControls::csvdocument()
  2. 7 plugins/FrxControls.inc \FrxControls::csvdocument()
  3. 7.2 plugins/FrxControls.inc \FrxControls::csvDocument()

Generate the CSV document.

Parameters

XML $body:

unknown_type $options:

File

plugins/FrxControls.inc, line 157
contains various methods for extending report formating, layout, transformation and design.

Class

FrxControls
@file contains various methods for extending report formating, layout, transformation and design.

Code

public function csvDocument($body, $options) {
  $doc = new DOMDocument();
  $doc->strictErrorChecking = FALSE;
  $xmlBody = '<?xml version="1.0" encoding="UTF-8"?>' . $body;
  @$doc
    ->loadHTML($xmlBody);
  $xml = simplexml_import_dom($doc);
  $output = '';
  $rows = $xml
    ->xpath('//tr');
  $rowspans = array();
  if ($rows) {
    foreach ($rows as $row) {
      $c = 0;
      foreach ($row as $column) {
        $c++;
        if (@$rowspans[$c]) {
          $cont = true;
          while ($rowspans[$c] && $cont) {
            $rowspans[$c]--;
            $output .= ',';
            $c++;
          }
        }
        $value = $column
          ->asXML();
        $value = strip_tags($value);
        $value = str_replace('"', '""', $value);
        $value = str_replace(array(
          "\n",
        ), '', $value);
        $value = '"' . $value . '",';
        $output .= $value;

        // Add Column span elements
        if ((int) $column['colspan'] > 1) {
          for ($i = 2; $i <= (int) $column['colspan']; $i++) {
            $c++;
            $output .= ',';
          }
        }

        // Check to see if we have some rowspans that we need to save
        if ((int) $column['rowspan'] > 1) {
          $rowspans[$c] = (int) $column['rowspan'] - 1;
        }
      }
      $output .= "\n";
    }
  }
  return $this
    ->convertCharset($output);
}