2015-04-24 14:27:22 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Table;
|
|
|
|
|
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
use Icinga\Data\Selectable;
|
|
|
|
use Icinga\Web\Request;
|
|
|
|
use Icinga\Web\Url;
|
|
|
|
|
|
|
|
abstract class QuickTable
|
|
|
|
{
|
|
|
|
protected $view;
|
|
|
|
|
|
|
|
protected $connection;
|
|
|
|
|
2015-06-01 12:42:40 +02:00
|
|
|
protected function renderRow($row, $actionColumn = false)
|
2015-04-24 14:27:22 +02:00
|
|
|
{
|
|
|
|
$htm = " <tr>\n";
|
|
|
|
$idKey = key($row);
|
2015-06-01 12:42:40 +02:00
|
|
|
$firstRow = true;
|
2015-04-24 14:27:22 +02:00
|
|
|
|
|
|
|
foreach ($row as $key => $val) {
|
2015-06-01 12:42:40 +02:00
|
|
|
$value = null;
|
|
|
|
if ($key === $idKey) continue;
|
|
|
|
|
|
|
|
if ($firstRow) {
|
|
|
|
if ($val !== null && $url = $this->getActionUrl($row)) {
|
|
|
|
$value = $this->view()->qlink($val, $this->getActionUrl($row));
|
|
|
|
}
|
|
|
|
$firstRow = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value === null) {
|
|
|
|
$value = $val === null ? '-' : $this->view()->escape($val);
|
|
|
|
}
|
|
|
|
|
|
|
|
$htm .= ' <td>' . $value . "</td>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->hasAdditionalActions()) {
|
|
|
|
$htm .= ' <td class="actions">' . $this->renderAdditionalActions($row) . "</td>\n";
|
2015-04-24 14:27:22 +02:00
|
|
|
}
|
2015-06-01 12:42:40 +02:00
|
|
|
|
2015-04-24 14:27:22 +02:00
|
|
|
return $htm . " </tr>\n";
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:42:40 +02:00
|
|
|
protected function getActionUrl($row)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:22 +02:00
|
|
|
public function setConnection(Selectable $connection)
|
|
|
|
{
|
|
|
|
$this->connection = $connection;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:42:40 +02:00
|
|
|
public function hasAdditionalActions()
|
|
|
|
{
|
|
|
|
return method_exists($this, 'renderAdditionalActions');
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:22 +02:00
|
|
|
protected function connection()
|
|
|
|
{
|
|
|
|
// TODO: Fail if missing? Require connection in constructor?
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderTitles($row)
|
|
|
|
{
|
|
|
|
$view = $this->view;
|
|
|
|
$htm = "<thead>\n <tr>\n";
|
2015-06-01 12:42:40 +02:00
|
|
|
|
2015-04-24 14:27:22 +02:00
|
|
|
foreach ($row as $title) {
|
|
|
|
$htm .= ' <th>' . $view->escape($title) . "</th>\n";
|
|
|
|
}
|
2015-06-01 12:42:40 +02:00
|
|
|
|
|
|
|
if ($this->hasAdditionalActions()) {
|
|
|
|
$htm .= ' <th class="actions">' . $view->translate('Actions') . "</th>\n";
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:22 +02:00
|
|
|
return $htm . " </tr>\n</thead>\n";
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:42:40 +02:00
|
|
|
protected function url($url, $params)
|
|
|
|
{
|
|
|
|
return Url::fromPath($url, $params);
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:22 +02:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$data = $this->fetchData();
|
|
|
|
|
|
|
|
$htm = '<table class="simple action">' . "\n"
|
|
|
|
. $this->renderTitles($this->getTitles())
|
|
|
|
. "<tbody>\n";
|
|
|
|
foreach ($data as $row) {
|
|
|
|
$htm .= $this->renderRow($row);
|
|
|
|
}
|
|
|
|
return $htm . "</tbody>\n</table>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function view()
|
|
|
|
{
|
|
|
|
if ($this->view === null) {
|
|
|
|
$this->view = Icinga::app()->getViewRenderer()->view;
|
|
|
|
}
|
|
|
|
return $this->view;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function setView($view)
|
|
|
|
{
|
|
|
|
$this->view = $view;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->render();
|
|
|
|
}
|
|
|
|
}
|