Source of file CsvReportFormatter.php
Size: 1,352 Bytes - Last Modified: 2021-12-23T10:25:16+00:00
/var/www/docs.ssmods.com/process/src/code/formatters/CsvReportFormatter.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 | <?php /** * * @author marcus@silverstripe.com.au * @license http://silverstripe.org/bsd-license/ */ class CsvReportFormatter extends ReportFormatter { protected function getOutputFormat() { return 'csv'; } /** * Create a header for the report */ protected function createHeader($tableName) { $header = array(); // just join it all up foreach ($this->headers as $field => $display) { $header[] = $display; } return '"'.implode('","', $header).'"'; } /** * Create a body for the report */ protected function createBody($tableName, $tableData) { $body = array(); $formatting = $this->getFieldFormatters(); foreach ($tableData as $row) { $csvRow = array(); foreach ($row as $field => $value) { if (isset($formatting[$field])) { $value = $formatting[$field]->format($value); } $csvRow[] = str_replace('"', '""', $value); } $body[] = '"'.implode('","', $csvRow).'"'; } return implode("\n", $body); } /** * Format the header and body into a complete report output. */ protected function formatReport($reportPieces) { $bits = ''; foreach ($reportPieces as $tableName => $table) { if ($tableName != ReportFormatter::DEFAULT_TABLE_NAME) { $bits .= '"'.$tableName.'",'; } $bits .= $table['Header']."\n".$table['Body']."\n,\n,\n"; } return $bits; } } |