mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-09-23 18:07:42 +02:00
parent
2f46929ac7
commit
78ba3dc4d4
@ -3,12 +3,20 @@
|
||||
|
||||
namespace Icinga\File;
|
||||
|
||||
use Icinga\Util\Buffer;
|
||||
use Traversable;
|
||||
|
||||
class Csv
|
||||
{
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* Cache for {@link render()}
|
||||
*
|
||||
* @var Buffer|null
|
||||
*/
|
||||
protected $renderBuffer;
|
||||
|
||||
protected function __construct()
|
||||
{
|
||||
}
|
||||
@ -22,26 +30,49 @@ class Csv
|
||||
|
||||
public function dump()
|
||||
{
|
||||
header('Content-type: text/csv');
|
||||
echo (string) $this;
|
||||
$this->render()->fpassthru();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
$first = true;
|
||||
$csv = '';
|
||||
foreach ($this->query as $row) {
|
||||
if ($first) {
|
||||
$csv .= implode(',', array_keys((array) $row)) . "\r\n";
|
||||
$first = false;
|
||||
}
|
||||
$out = array();
|
||||
foreach ($row as & $val) {
|
||||
$out[] = '"' . $val . '"';
|
||||
}
|
||||
$csv .= implode(',', $out) . "\r\n";
|
||||
return (string) $this->render();
|
||||
}
|
||||
|
||||
return $csv;
|
||||
/**
|
||||
* Return the rendered CSV
|
||||
*
|
||||
* @return Buffer
|
||||
*/
|
||||
protected function render()
|
||||
{
|
||||
if ($this->renderBuffer === null) {
|
||||
$this->renderBuffer = new Buffer();
|
||||
$first = true;
|
||||
foreach ($this->query as $row) {
|
||||
if ($first) {
|
||||
$this->renderBuffer->append($this->renderRow(array_keys((array)$row)));
|
||||
$first = false;
|
||||
}
|
||||
$this->renderBuffer->append($this->renderRow(array_values((array)$row)));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->renderBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a single CSV row string representing the given columns
|
||||
*
|
||||
* @param array $columns
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function renderRow(array $columns)
|
||||
{
|
||||
$quoted = array();
|
||||
foreach ($columns as $column) {
|
||||
$quoted[] = '"' . str_replace('"', '""', $column) . '"';
|
||||
}
|
||||
return implode(',', $quoted) . "\r\n";
|
||||
}
|
||||
}
|
||||
|
@ -72,8 +72,13 @@ class Controller extends IcingaWebController
|
||||
'Content-Disposition',
|
||||
'attachment; filename=' . $this->getRequest()->getActionName() . '.csv'
|
||||
)
|
||||
->appendBody((string) Csv::fromQuery($query))
|
||||
->sendResponse();
|
||||
->sendHeaders();
|
||||
|
||||
while (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
Csv::fromQuery($query)->dump();
|
||||
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user