2015-07-26 15:39:37 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-16 14:55:34 +02:00
|
|
|
namespace Icinga\Module\Director\Web\Table;
|
2015-07-26 15:39:37 +02:00
|
|
|
|
2019-05-02 13:23:06 +02:00
|
|
|
use ipl\Html\Html;
|
|
|
|
use ipl\Html\ValidHtml;
|
|
|
|
use gipfl\IcingaWeb2\Table\SimpleQueryBasedTable;
|
2018-03-11 13:30:52 +01:00
|
|
|
use Icinga\Data\DataArray\ArrayDatasource;
|
|
|
|
use Icinga\Module\Director\Objects\ImportRun;
|
|
|
|
use Icinga\Module\Director\PlainObjectRenderer;
|
2015-07-26 15:39:37 +02:00
|
|
|
|
2017-08-16 14:55:34 +02:00
|
|
|
class ImportedrowsTable extends SimpleQueryBasedTable
|
2015-07-26 15:39:37 +02:00
|
|
|
{
|
2016-07-13 21:36:45 +02:00
|
|
|
protected $columns;
|
2015-07-26 15:39:37 +02:00
|
|
|
|
2016-11-09 14:21:04 +01:00
|
|
|
/** @var ImportRun */
|
2016-07-13 21:36:45 +02:00
|
|
|
protected $importRun;
|
|
|
|
|
2019-09-25 16:29:44 +02:00
|
|
|
protected $keyColumn;
|
|
|
|
|
2017-08-16 14:55:34 +02:00
|
|
|
public static function load(ImportRun $run)
|
|
|
|
{
|
|
|
|
$table = new static();
|
|
|
|
$table->setImportRun($run);
|
|
|
|
return $table;
|
|
|
|
}
|
|
|
|
|
2016-07-13 21:36:45 +02:00
|
|
|
public function setImportRun(ImportRun $run)
|
2015-07-26 15:39:37 +02:00
|
|
|
{
|
2016-07-13 21:36:45 +02:00
|
|
|
$this->importRun = $run;
|
|
|
|
return $this;
|
2015-07-26 15:39:37 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 21:36:45 +02:00
|
|
|
public function setColumns($columns)
|
2015-07-26 15:39:37 +02:00
|
|
|
{
|
2016-07-13 21:36:45 +02:00
|
|
|
$this->columns = $columns;
|
2015-07-26 15:39:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-09-25 16:29:44 +02:00
|
|
|
protected function getKeyColumn()
|
|
|
|
{
|
|
|
|
if ($this->keyColumn === null) {
|
|
|
|
$this->keyColumn = $this->importRun->importSource()->get('key_column');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->keyColumn;
|
|
|
|
}
|
|
|
|
|
2016-07-13 21:36:45 +02:00
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
if ($this->columns === null) {
|
|
|
|
$cols = $this->importRun->listColumnNames();
|
2016-11-09 14:21:04 +01:00
|
|
|
|
2019-09-25 16:29:44 +02:00
|
|
|
$keyColumn = $this->getKeyColumn();
|
2016-11-09 14:21:04 +01:00
|
|
|
if ($keyColumn !== null && ($pos = array_search($keyColumn, $cols)) !== false) {
|
|
|
|
unset($cols[$pos]);
|
|
|
|
array_unshift($cols, $keyColumn);
|
|
|
|
}
|
2016-07-13 21:36:45 +02:00
|
|
|
} else {
|
|
|
|
$cols = $this->columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_combine($cols, $cols);
|
|
|
|
}
|
|
|
|
|
2018-03-11 13:30:52 +01:00
|
|
|
public function renderRow($row)
|
|
|
|
{
|
|
|
|
// Find a better place!
|
|
|
|
if ($row === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$tr = $this::tr();
|
|
|
|
|
|
|
|
foreach ($this->getColumnsToBeRendered() as $column) {
|
|
|
|
$td = $this::td();
|
|
|
|
if (property_exists($row, $column)) {
|
|
|
|
if (is_string($row->$column) || $row->$column instanceof ValidHtml) {
|
|
|
|
$td->setContent($row->$column);
|
|
|
|
} else {
|
|
|
|
$html = Html::tag('pre', null, PlainObjectRenderer::render($row->$column));
|
|
|
|
$td->setContent($html);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$tr->add($td);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tr;
|
|
|
|
}
|
|
|
|
|
2017-08-16 14:55:34 +02:00
|
|
|
public function getColumnsToBeRendered()
|
2015-12-02 04:23:00 +01:00
|
|
|
{
|
2017-08-16 14:55:34 +02:00
|
|
|
return $this->getColumns();
|
2015-07-26 15:39:37 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 14:55:34 +02:00
|
|
|
public function prepareQuery()
|
2015-07-26 15:39:37 +02:00
|
|
|
{
|
2015-12-16 20:27:10 +01:00
|
|
|
$ds = new ArrayDatasource(
|
2017-08-16 14:55:34 +02:00
|
|
|
$this->importRun->fetchRows($this->columns)
|
2015-12-16 20:27:10 +01:00
|
|
|
);
|
|
|
|
|
2019-09-25 16:29:44 +02:00
|
|
|
return $ds->select()->order($this->getKeyColumn());
|
2015-07-26 15:39:37 +02:00
|
|
|
}
|
|
|
|
}
|