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;
|
namespace Icinga\File;
|
||||||
|
|
||||||
|
use Icinga\Util\Buffer;
|
||||||
use Traversable;
|
use Traversable;
|
||||||
|
|
||||||
class Csv
|
class Csv
|
||||||
{
|
{
|
||||||
protected $query;
|
protected $query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache for {@link render()}
|
||||||
|
*
|
||||||
|
* @var Buffer|null
|
||||||
|
*/
|
||||||
|
protected $renderBuffer;
|
||||||
|
|
||||||
protected function __construct()
|
protected function __construct()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -22,26 +30,49 @@ class Csv
|
|||||||
|
|
||||||
public function dump()
|
public function dump()
|
||||||
{
|
{
|
||||||
header('Content-type: text/csv');
|
$this->render()->fpassthru();
|
||||||
echo (string) $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
{
|
{
|
||||||
$first = true;
|
return (string) $this->render();
|
||||||
$csv = '';
|
}
|
||||||
foreach ($this->query as $row) {
|
|
||||||
if ($first) {
|
/**
|
||||||
$csv .= implode(',', array_keys((array) $row)) . "\r\n";
|
* Return the rendered CSV
|
||||||
$first = false;
|
*
|
||||||
|
* @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)));
|
||||||
}
|
}
|
||||||
$out = array();
|
|
||||||
foreach ($row as & $val) {
|
|
||||||
$out[] = '"' . $val . '"';
|
|
||||||
}
|
|
||||||
$csv .= implode(',', $out) . "\r\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $csv;
|
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',
|
'Content-Disposition',
|
||||||
'attachment; filename=' . $this->getRequest()->getActionName() . '.csv'
|
'attachment; filename=' . $this->getRequest()->getActionName() . '.csv'
|
||||||
)
|
)
|
||||||
->appendBody((string) Csv::fromQuery($query))
|
->sendHeaders();
|
||||||
->sendResponse();
|
|
||||||
|
while (ob_get_level()) {
|
||||||
|
ob_end_clean();
|
||||||
|
}
|
||||||
|
Csv::fromQuery($query)->dump();
|
||||||
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user