2015-07-26 15:46:59 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-16 11:15:39 +02:00
|
|
|
namespace Icinga\Module\Director\Web\Table;
|
2015-07-26 15:46:59 +02:00
|
|
|
|
|
|
|
use Icinga\Data\DataArray\ArrayDatasource;
|
2016-02-17 11:49:56 +01:00
|
|
|
use Icinga\Module\Director\Hook\ImportSourceHook;
|
2015-07-26 15:46:59 +02:00
|
|
|
use Icinga\Module\Director\Objects\ImportSource;
|
2017-08-18 21:48:28 +02:00
|
|
|
use Icinga\Module\Director\PlainObjectRenderer;
|
2017-08-16 11:15:39 +02:00
|
|
|
use ipl\Web\Table\SimpleQueryBasedTable;
|
2015-07-26 15:46:59 +02:00
|
|
|
|
2017-08-16 11:15:39 +02:00
|
|
|
class ImportsourceHookTable extends SimpleQueryBasedTable
|
2015-07-26 15:46:59 +02:00
|
|
|
{
|
2016-11-09 14:21:04 +01:00
|
|
|
/** @var ImportSource */
|
2015-07-26 15:46:59 +02:00
|
|
|
protected $source;
|
|
|
|
|
|
|
|
protected $dataCache;
|
|
|
|
|
|
|
|
protected $columnCache;
|
|
|
|
|
|
|
|
protected $sourceHook;
|
|
|
|
|
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
if ($this->columnCache === null) {
|
2016-07-20 15:21:07 +02:00
|
|
|
$this->columnCache = array_merge(
|
|
|
|
$this->sourceHook()->listColumns(),
|
|
|
|
$this->source->listModifierTargetProperties()
|
|
|
|
);
|
|
|
|
|
|
|
|
sort($this->columnCache);
|
2016-11-09 14:21:04 +01:00
|
|
|
|
|
|
|
// prioritize key column
|
|
|
|
$keyColumn = $this->source->get('key_column');
|
|
|
|
if ($keyColumn !== null && ($pos = array_search($keyColumn, $this->columnCache)) !== false) {
|
|
|
|
unset($this->columnCache[$pos]);
|
|
|
|
array_unshift($this->columnCache, $keyColumn);
|
|
|
|
}
|
2015-07-26 15:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->columnCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setImportSource(ImportSource $source)
|
|
|
|
{
|
|
|
|
$this->source = $source;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-16 11:15:39 +02:00
|
|
|
public function getColumnsToBeRendered()
|
2015-07-26 15:46:59 +02:00
|
|
|
{
|
2017-08-16 11:15:39 +02:00
|
|
|
return $this->getColumns();
|
2015-07-26 15:46:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-18 21:48:28 +02:00
|
|
|
public function renderRow($row)
|
|
|
|
{
|
|
|
|
$tr = $this::tr();
|
|
|
|
|
|
|
|
foreach ($this->getColumnsToBeRendered() as $column) {
|
|
|
|
$td = $this::td();
|
|
|
|
if (property_exists($row, $column)) {
|
|
|
|
$td->setContent(PlainObjectRenderer::render($row->$column));
|
|
|
|
}
|
|
|
|
$tr->add($td);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tr;
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:46:59 +02:00
|
|
|
protected function sourceHook()
|
|
|
|
{
|
|
|
|
if ($this->sourceHook === null) {
|
2017-08-16 11:15:39 +02:00
|
|
|
$this->sourceHook = ImportSourceHook::forImportSource(
|
|
|
|
$this->source
|
2015-07-26 15:46:59 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sourceHook;
|
|
|
|
}
|
|
|
|
|
2017-08-16 11:15:39 +02:00
|
|
|
public function fetchQueryRows()
|
2015-07-26 15:46:59 +02:00
|
|
|
{
|
|
|
|
if ($this->dataCache === null) {
|
2017-08-16 11:15:39 +02:00
|
|
|
$this->dataCache = parent::fetchQueryRows();
|
2016-07-20 14:25:18 +02:00
|
|
|
$this->source->applyModifiers($this->dataCache);
|
2016-02-19 02:01:53 +01:00
|
|
|
}
|
2016-07-20 14:19:20 +02:00
|
|
|
|
|
|
|
return $this->dataCache;
|
2016-02-19 02:01:53 +01:00
|
|
|
}
|
|
|
|
|
2017-08-16 11:15:39 +02:00
|
|
|
public function prepareQuery()
|
2015-07-26 15:46:59 +02:00
|
|
|
{
|
2016-02-26 12:42:21 +01:00
|
|
|
$ds = new ArrayDatasource($this->sourceHook()->fetchData());
|
2015-07-26 15:46:59 +02:00
|
|
|
return $ds->select();
|
|
|
|
}
|
|
|
|
}
|