2016-11-28 18:34:28 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Businessprocess\Renderer;
|
|
|
|
|
|
|
|
|
|
use Icinga\Module\Businessprocess\BpNode;
|
2017-01-11 08:04:45 -05:00
|
|
|
use Icinga\Module\Businessprocess\BpConfig;
|
2016-11-28 18:34:28 -05:00
|
|
|
use Icinga\Module\Businessprocess\Node;
|
2019-01-17 07:21:46 -05:00
|
|
|
use ipl\Html\BaseHtmlElement;
|
|
|
|
|
use ipl\Html\Html;
|
2016-11-28 18:34:28 -05:00
|
|
|
|
|
|
|
|
class TreeRenderer extends Renderer
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
2017-01-11 08:33:35 -05:00
|
|
|
$bp = $this->config;
|
2019-01-17 07:21:46 -05:00
|
|
|
$this->add(Html::tag(
|
|
|
|
|
'div',
|
|
|
|
|
[
|
2016-11-28 18:34:28 -05:00
|
|
|
'id' => $bp->getHtmlId(),
|
|
|
|
|
'class' => 'bp'
|
2019-01-17 07:21:46 -05:00
|
|
|
],
|
2016-11-29 09:04:11 -05:00
|
|
|
$this->renderBp($bp)
|
2016-11-28 18:34:28 -05:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
return parent::render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-11 08:04:45 -05:00
|
|
|
* @param BpConfig $bp
|
2016-11-28 18:34:28 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-01-11 08:04:45 -05:00
|
|
|
public function renderBp(BpConfig $bp)
|
2016-11-28 18:34:28 -05:00
|
|
|
{
|
2016-11-29 09:04:11 -05:00
|
|
|
$html = array();
|
2016-11-28 19:52:44 -05:00
|
|
|
if ($this->wantsRootNodes()) {
|
|
|
|
|
$nodes = $bp->getChildren();
|
|
|
|
|
} else {
|
|
|
|
|
$nodes = $this->parent->getChildren();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($nodes as $name => $node) {
|
2016-11-29 09:04:11 -05:00
|
|
|
$html[] = $this->renderNode($bp, $node);
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
* @param $path
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getId(Node $node, $path)
|
|
|
|
|
{
|
|
|
|
|
return md5(implode(';', $path) . (string) $node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getStateClassNames(Node $node)
|
|
|
|
|
{
|
|
|
|
|
$state = strtolower($node->getStateName());
|
|
|
|
|
|
|
|
|
|
if ($node->isMissing()) {
|
|
|
|
|
return array('missing');
|
|
|
|
|
} elseif ($state === 'ok') {
|
|
|
|
|
if ($node->hasMissingChildren()) {
|
|
|
|
|
return array('ok', 'missing-children');
|
|
|
|
|
} else {
|
|
|
|
|
return array('ok');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return array('problem', $state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
2019-01-17 07:21:46 -05:00
|
|
|
* @return BaseHtmlElement[]
|
2016-11-28 18:34:28 -05:00
|
|
|
*/
|
|
|
|
|
public function getNodeIcons(Node $node)
|
|
|
|
|
{
|
|
|
|
|
$icons = array();
|
|
|
|
|
if ($node->isInDowntime()) {
|
2019-01-17 07:21:46 -05:00
|
|
|
$icons[] = Html::tag('i', ['class' => 'icon icon-moon']);
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
|
|
|
|
if ($node->isAcknowledged()) {
|
2019-01-17 07:21:46 -05:00
|
|
|
$icons[] = Html::tag('i', ['class' => 'icon icon-ok']);
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
|
|
|
|
return $icons;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-11 08:04:45 -05:00
|
|
|
* @param BpConfig $bp
|
2016-11-28 18:34:28 -05:00
|
|
|
* @param Node $node
|
2016-11-28 19:52:44 -05:00
|
|
|
* @param array $path
|
|
|
|
|
*
|
2016-11-28 18:34:28 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-01-11 08:04:45 -05:00
|
|
|
public function renderNode(BpConfig $bp, Node $node, $path = array())
|
2016-11-28 18:34:28 -05:00
|
|
|
{
|
2019-01-17 07:21:46 -05:00
|
|
|
$table = Html::tag(
|
2016-11-28 18:34:28 -05:00
|
|
|
'table',
|
2019-01-17 07:21:46 -05:00
|
|
|
[
|
2016-11-28 18:34:28 -05:00
|
|
|
'id' => $this->getId($node, $path),
|
|
|
|
|
'class' => array(
|
|
|
|
|
'bp',
|
|
|
|
|
$node->getObjectClassName()
|
|
|
|
|
)
|
2019-01-17 07:21:46 -05:00
|
|
|
]
|
2016-11-28 18:34:28 -05:00
|
|
|
);
|
2019-01-17 07:21:46 -05:00
|
|
|
$attributes = $table->getAttributes();
|
2016-11-28 18:34:28 -05:00
|
|
|
$attributes->add('class', $this->getStateClassNames($node));
|
|
|
|
|
if ($node->isHandled()) {
|
|
|
|
|
$attributes->add('class', 'handled');
|
|
|
|
|
}
|
2017-01-26 09:59:43 -05:00
|
|
|
if ($node instanceof BpNode) {
|
2016-11-28 18:34:28 -05:00
|
|
|
$attributes->add('class', 'operator');
|
|
|
|
|
} else {
|
|
|
|
|
$attributes->add('class', 'node');
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 07:21:46 -05:00
|
|
|
$tbody = Html::tag('tbody');
|
|
|
|
|
$table->add($tbody);
|
|
|
|
|
$tr = Html::tag('tr');
|
|
|
|
|
$tbody->add($tr);
|
2016-11-28 18:34:28 -05:00
|
|
|
|
2017-01-26 09:59:43 -05:00
|
|
|
if ($node instanceof BpNode) {
|
2019-01-17 07:21:46 -05:00
|
|
|
$tr->add(Html::tag(
|
2016-11-28 18:34:28 -05:00
|
|
|
'th',
|
2019-01-17 07:21:46 -05:00
|
|
|
['rowspan' => $node->countChildren() + 1 + ($this->isLocked() ? 0 : 1)],
|
|
|
|
|
Html::tag('span', ['class' => 'op'], $node->operatorHtml())
|
|
|
|
|
));
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
2019-01-17 07:21:46 -05:00
|
|
|
$td = Html::tag('td');
|
|
|
|
|
$tr->add($td);
|
2016-11-28 18:34:28 -05:00
|
|
|
|
2016-11-28 19:52:44 -05:00
|
|
|
if ($node instanceof BpNode && $node->hasInfoUrl()) {
|
2016-11-28 18:34:28 -05:00
|
|
|
$td->add($this->createInfoAction($node));
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 07:46:58 -05:00
|
|
|
if (! $this->isLocked()) {
|
2019-01-17 07:21:46 -05:00
|
|
|
$td->add($this->getActionIcons($bp, $node));
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$link = $node->getLink();
|
2019-01-17 07:21:46 -05:00
|
|
|
$link->getAttributes()->set('data-base-target', '_next');
|
|
|
|
|
$link->add($this->getNodeIcons($node));
|
2016-11-28 19:52:44 -05:00
|
|
|
|
|
|
|
|
if ($node->hasChildren()) {
|
2019-01-17 07:21:46 -05:00
|
|
|
$link->add($this->renderStateBadges($node->getStateSummary()));
|
2016-11-28 19:52:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($time = $node->getLastStateChange()) {
|
2019-01-17 07:21:46 -05:00
|
|
|
$since = $this->timeSince($time)->prepend(
|
2016-11-28 19:52:44 -05:00
|
|
|
sprintf(' (%s ', $node->getStateName())
|
2019-01-17 07:21:46 -05:00
|
|
|
)->add(')');
|
|
|
|
|
$link->add($since);
|
2016-11-28 19:52:44 -05:00
|
|
|
}
|
|
|
|
|
|
2019-01-17 07:21:46 -05:00
|
|
|
$td->add($link);
|
2016-11-28 18:34:28 -05:00
|
|
|
|
|
|
|
|
foreach ($node->getChildren() as $name => $child) {
|
2019-01-17 07:21:46 -05:00
|
|
|
$tbody->add(Html::tag(
|
|
|
|
|
'tr',
|
|
|
|
|
null,
|
|
|
|
|
Html::tag(
|
|
|
|
|
'td',
|
|
|
|
|
null,
|
|
|
|
|
$this->renderNode($bp, $child, $this->getCurrentPath())
|
|
|
|
|
)
|
|
|
|
|
));
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-08 11:59:03 -05:00
|
|
|
if (! $this->isLocked() && $node instanceof BpNode && $bp->getMetadata()->canModify()) {
|
2019-01-17 07:21:46 -05:00
|
|
|
$tbody->add(Html::tag(
|
|
|
|
|
'tr',
|
|
|
|
|
null,
|
|
|
|
|
Html::tag(
|
|
|
|
|
'td',
|
|
|
|
|
null,
|
|
|
|
|
$this->renderAddNewNode($node)
|
|
|
|
|
)
|
|
|
|
|
));
|
2016-11-29 10:58:03 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-28 18:34:28 -05:00
|
|
|
return $table;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 08:04:45 -05:00
|
|
|
protected function getActionIcons(BpConfig $bp, Node $node)
|
2016-11-28 18:34:28 -05:00
|
|
|
{
|
|
|
|
|
if ($node instanceof BpNode) {
|
2017-02-08 11:59:03 -05:00
|
|
|
if ($bp->getMetadata()->canModify()) {
|
|
|
|
|
return $this->createEditAction($bp, $node);
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2016-11-28 18:34:28 -05:00
|
|
|
} else {
|
|
|
|
|
return $this->createSimulationAction($bp, $node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 08:04:45 -05:00
|
|
|
protected function createEditAction(BpConfig $bp, BpNode $node)
|
2016-11-28 18:34:28 -05:00
|
|
|
{
|
|
|
|
|
return $this->actionIcon(
|
|
|
|
|
'wrench',
|
2017-02-17 09:38:36 -05:00
|
|
|
$this->getUrl()->with(array(
|
|
|
|
|
'action' => 'edit',
|
|
|
|
|
'editnode' => $node->getName()
|
2016-11-28 18:34:28 -05:00
|
|
|
)),
|
2019-01-17 07:21:46 -05:00
|
|
|
mt('businessprocess', 'Modify this node')
|
2016-11-28 18:34:28 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 08:04:45 -05:00
|
|
|
protected function createSimulationAction(BpConfig $bp, Node $node)
|
2016-11-28 18:34:28 -05:00
|
|
|
{
|
|
|
|
|
return $this->actionIcon(
|
|
|
|
|
'magic',
|
2016-11-30 08:35:13 -05:00
|
|
|
$this->getUrl()->with(array(
|
|
|
|
|
//'config' => $bp->getName(),
|
|
|
|
|
'action' => 'simulation',
|
|
|
|
|
'simulationnode' => $node->getName()
|
2016-11-28 18:34:28 -05:00
|
|
|
)),
|
2019-01-17 07:21:46 -05:00
|
|
|
mt('businessprocess', 'Simulate a specific state')
|
2016-11-28 18:34:28 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function createInfoAction(BpNode $node)
|
|
|
|
|
{
|
|
|
|
|
$url = $node->getInfoUrl();
|
|
|
|
|
return $this->actionIcon(
|
|
|
|
|
'help',
|
|
|
|
|
$url,
|
2019-01-17 07:21:46 -05:00
|
|
|
sprintf('%s: %s', mt('businessprocess', 'More information'), $url)
|
2016-11-28 18:34:28 -05:00
|
|
|
);
|
|
|
|
|
}
|
2017-02-17 09:38:36 -05:00
|
|
|
|
2016-11-28 18:34:28 -05:00
|
|
|
protected function actionIcon($icon, $url, $title)
|
|
|
|
|
{
|
2019-01-17 07:21:46 -05:00
|
|
|
return Html::tag(
|
|
|
|
|
'a',
|
|
|
|
|
[
|
|
|
|
|
'href' => $url,
|
2016-11-28 18:34:28 -05:00
|
|
|
'title' => $title,
|
2019-01-17 07:21:46 -05:00
|
|
|
'style' => 'float: right'
|
|
|
|
|
],
|
|
|
|
|
Html::tag('i', ['class' => 'icon icon-' . $icon])
|
2016-11-28 18:34:28 -05:00
|
|
|
);
|
|
|
|
|
}
|
2016-11-29 10:58:03 -05:00
|
|
|
|
|
|
|
|
protected function renderAddNewNode($parent)
|
|
|
|
|
{
|
2019-01-17 07:21:46 -05:00
|
|
|
return Html::tag(
|
|
|
|
|
'a',
|
|
|
|
|
[
|
|
|
|
|
'href' => $this->getUrl()
|
|
|
|
|
->with('action', 'add')
|
|
|
|
|
->with('node', $parent->getName()),
|
|
|
|
|
'title' => mt('businessprocess', 'Add a new business process node'),
|
|
|
|
|
'class' => 'addnew icon-plus'
|
|
|
|
|
],
|
|
|
|
|
mt('businessprocess', 'Add')
|
2016-11-29 10:58:03 -05:00
|
|
|
);
|
|
|
|
|
}
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|