2016-11-28 18:34:28 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Businessprocess\Renderer;
|
|
|
|
|
|
2019-01-22 05:44:05 -05:00
|
|
|
use Icinga\Date\DateFormatter;
|
2017-01-11 08:04:45 -05:00
|
|
|
use Icinga\Module\Businessprocess\BpConfig;
|
2019-02-21 01:29:32 -05:00
|
|
|
use Icinga\Module\Businessprocess\BpNode;
|
2019-01-28 02:50:42 -05:00
|
|
|
use Icinga\Module\Businessprocess\ImportedNode;
|
2016-11-28 18:34:28 -05:00
|
|
|
use Icinga\Module\Businessprocess\Node;
|
2019-02-21 01:29:32 -05:00
|
|
|
use Icinga\Module\Businessprocess\Web\Component\StateBall;
|
2018-12-20 04:56:05 -05:00
|
|
|
use Icinga\Module\Businessprocess\Web\Form\CsrfToken;
|
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-22 05:44:05 -05:00
|
|
|
$htmlId = $bp->getHtmlId();
|
2019-02-22 03:12:07 -05:00
|
|
|
$tree = Html::tag(
|
2019-01-14 02:43:10 -05:00
|
|
|
'ul',
|
2019-01-17 07:21:46 -05:00
|
|
|
[
|
2019-01-22 05:44:05 -05:00
|
|
|
'id' => $htmlId,
|
2019-02-22 03:14:04 -05:00
|
|
|
'class' => ['bp', 'sortable', $this->wantsRootNodes() ? '' : 'process'],
|
2019-01-22 05:32:14 -05:00
|
|
|
'data-sortable-disabled' => $this->isLocked() ? 'true' : 'false',
|
2018-12-20 04:56:05 -05:00
|
|
|
'data-sortable-data-id-attr' => 'id',
|
|
|
|
|
'data-sortable-direction' => 'vertical',
|
2019-01-14 02:49:10 -05:00
|
|
|
'data-sortable-group' => json_encode([
|
2019-01-22 05:44:05 -05:00
|
|
|
'name' => $this->wantsRootNodes() ? 'root' : $htmlId,
|
2019-01-14 02:49:10 -05:00
|
|
|
'put' => 'function:rowPutAllowed'
|
|
|
|
|
]),
|
|
|
|
|
'data-sortable-invert-swap' => 'true',
|
2019-01-22 05:44:05 -05:00
|
|
|
'data-is-root-config' => $this->wantsRootNodes() ? 'true' : 'false',
|
2019-02-22 03:12:07 -05:00
|
|
|
'data-csrf-token' => CsrfToken::generate()
|
2019-01-17 07:21:46 -05:00
|
|
|
],
|
2016-11-29 09:04:11 -05:00
|
|
|
$this->renderBp($bp)
|
2019-02-22 03:12:07 -05:00
|
|
|
);
|
|
|
|
|
if ($this->wantsRootNodes()) {
|
|
|
|
|
$tree->getAttributes()->add(
|
|
|
|
|
'data-action-url',
|
|
|
|
|
$this->getUrl()->setParams(['config' => $bp->getName()])->getAbsoluteUrl()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$nodeName = $this->parent instanceof ImportedNode
|
|
|
|
|
? $this->parent->getNodeName()
|
|
|
|
|
: $this->parent->getName();
|
|
|
|
|
$tree->getAttributes()
|
|
|
|
|
->add('data-node-name', $nodeName)
|
|
|
|
|
->add('data-action-url', $this->getUrl()
|
|
|
|
|
->setParams([
|
|
|
|
|
'config' => $this->parent->getBpConfig()->getName(),
|
|
|
|
|
'node' => $nodeName
|
|
|
|
|
])
|
|
|
|
|
->getAbsoluteUrl());
|
|
|
|
|
}
|
2016-11-28 18:34:28 -05:00
|
|
|
|
2019-02-22 03:12:07 -05:00
|
|
|
$this->add($tree);
|
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) {
|
2019-01-22 05:40:33 -05:00
|
|
|
if ($node instanceof BpNode) {
|
|
|
|
|
$html[] = $this->renderNode($bp, $node);
|
|
|
|
|
} else {
|
|
|
|
|
$html[] = $this->renderChild($bp, $node);
|
|
|
|
|
}
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-22 05:44:05 -05:00
|
|
|
* @param array $path
|
2019-01-17 07:21:46 -05:00
|
|
|
* @return BaseHtmlElement[]
|
2016-11-28 18:34:28 -05:00
|
|
|
*/
|
2019-01-22 05:44:05 -05:00
|
|
|
public function getNodeIcons(Node $node, array $path = null)
|
2016-11-28 18:34:28 -05:00
|
|
|
{
|
2019-01-22 05:44:05 -05:00
|
|
|
$icons = [];
|
2019-02-22 03:14:54 -05:00
|
|
|
if (empty($path) && $node instanceof BpNode) {
|
2019-01-22 05:44:05 -05:00
|
|
|
$icons[] = Html::tag('i', ['class' => 'icon icon-sitemap']);
|
|
|
|
|
} else {
|
|
|
|
|
$icons[] = $node->getIcon();
|
|
|
|
|
}
|
|
|
|
|
$icons[] = (new StateBall(strtolower($node->getStateName())))->addAttributes([
|
|
|
|
|
'title' => sprintf(
|
|
|
|
|
'%s %s',
|
|
|
|
|
$node->getStateName(),
|
|
|
|
|
DateFormatter::timeSince($node->getLastStateChange())
|
|
|
|
|
)
|
|
|
|
|
]);
|
2016-11-28 18:34:28 -05:00
|
|
|
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-16 07:38:30 -05:00
|
|
|
$htmlId = $this->getId($node, $path);
|
2019-01-22 05:44:05 -05:00
|
|
|
$li = Html::tag(
|
2019-01-14 02:43:10 -05:00
|
|
|
'li',
|
2019-01-17 07:21:46 -05:00
|
|
|
[
|
2019-01-16 07:38:30 -05:00
|
|
|
'id' => $htmlId,
|
2018-12-21 07:58:45 -05:00
|
|
|
'class' => ['bp', 'movable', $node->getObjectClassName()],
|
2018-12-20 04:56:05 -05:00
|
|
|
'data-node-name' => $node->getName()
|
2019-01-17 07:21:46 -05:00
|
|
|
]
|
2016-11-28 18:34:28 -05:00
|
|
|
);
|
2019-01-22 05:44:05 -05:00
|
|
|
$attributes = $li->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-22 05:44:05 -05:00
|
|
|
$div = Html::tag('div');
|
|
|
|
|
$li->add($div);
|
2018-12-21 07:58:45 -05:00
|
|
|
|
2019-01-22 05:44:05 -05:00
|
|
|
$div->add($node->getLink());
|
|
|
|
|
$div->add($this->getNodeIcons($node, $path));
|
2016-11-28 18:34:28 -05:00
|
|
|
|
2019-01-22 05:44:05 -05:00
|
|
|
$div->add(Html::tag('span', null, $node->getAlias()));
|
2016-11-28 19:52:44 -05:00
|
|
|
|
2019-01-22 05:44:05 -05:00
|
|
|
if ($node instanceof BpNode) {
|
|
|
|
|
$div->add(Html::tag('span', ['class' => 'op'], $node->operatorHtml()));
|
2016-11-28 19:52:44 -05:00
|
|
|
}
|
|
|
|
|
|
2019-02-22 04:17:54 -05:00
|
|
|
if ($node instanceof BpNode && $node->hasInfoUrl()) {
|
|
|
|
|
$div->add($this->createInfoAction($node));
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-25 07:58:18 -05:00
|
|
|
$differentConfig = $node->getBpConfig()->getName() !== $this->getBusinessProcess()->getName();
|
|
|
|
|
if (! $this->isLocked() && !$differentConfig) {
|
2019-01-22 05:44:05 -05:00
|
|
|
$div->add($this->getActionIcons($bp, $node));
|
2016-11-28 19:52:44 -05:00
|
|
|
}
|
|
|
|
|
|
2019-01-22 05:44:05 -05:00
|
|
|
$ul = Html::tag('ul', [
|
|
|
|
|
'class' => ['bp', 'sortable'],
|
2019-02-25 07:58:18 -05:00
|
|
|
'data-sortable-disabled' => ($this->isLocked() || $differentConfig) ? 'true' : 'false',
|
2019-01-22 05:44:05 -05:00
|
|
|
'data-sortable-invert-swap' => 'true',
|
2018-12-21 07:58:45 -05:00
|
|
|
'data-sortable-data-id-attr' => 'id',
|
|
|
|
|
'data-sortable-draggable' => '.movable',
|
|
|
|
|
'data-sortable-direction' => 'vertical',
|
2019-01-14 02:49:10 -05:00
|
|
|
'data-sortable-group' => json_encode([
|
2019-01-16 07:38:30 -05:00
|
|
|
'name' => $htmlId, // Unique, so that the function below is the only deciding factor
|
2019-01-14 02:49:10 -05:00
|
|
|
'put' => 'function:rowPutAllowed'
|
|
|
|
|
]),
|
2018-12-21 07:58:45 -05:00
|
|
|
'data-csrf-token' => CsrfToken::generate(),
|
|
|
|
|
'data-action-url' => $this->getUrl()
|
2019-02-22 03:12:07 -05:00
|
|
|
->setParams([
|
2019-02-04 02:37:36 -05:00
|
|
|
'config' => $node->getBpConfig()->getName(),
|
2019-01-28 02:50:42 -05:00
|
|
|
'node' => $node instanceof ImportedNode
|
|
|
|
|
? $node->getNodeName()
|
2019-02-21 05:32:32 -05:00
|
|
|
: $node->getName()
|
2019-01-28 02:50:42 -05:00
|
|
|
])
|
2018-12-21 07:58:45 -05:00
|
|
|
->getAbsoluteUrl()
|
|
|
|
|
]);
|
2019-01-22 05:44:05 -05:00
|
|
|
$li->add($ul);
|
2018-12-21 07:58:45 -05:00
|
|
|
|
2019-02-25 07:58:18 -05:00
|
|
|
$path[] = $differentConfig ? $node->getIdentifier() : $node->getName();
|
2016-11-28 18:34:28 -05:00
|
|
|
foreach ($node->getChildren() as $name => $child) {
|
2019-01-15 04:35:02 -05:00
|
|
|
if ($child instanceof BpNode) {
|
2019-01-22 05:44:05 -05:00
|
|
|
$ul->add($this->renderNode($bp, $child, $path));
|
2018-12-21 07:58:45 -05:00
|
|
|
} else {
|
2019-01-22 05:44:05 -05:00
|
|
|
$ul->add($this->renderChild($bp, $child, $path));
|
2018-12-21 07:58:45 -05:00
|
|
|
}
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
|
|
|
|
|
2019-01-22 05:44:05 -05:00
|
|
|
return $li;
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|
|
|
|
|
|
2019-01-22 05:40:33 -05:00
|
|
|
protected function renderChild($bp, Node $node, $path = null)
|
2018-12-21 07:58:45 -05:00
|
|
|
{
|
|
|
|
|
$li = Html::tag('li', [
|
|
|
|
|
'class' => 'movable',
|
|
|
|
|
'id' => $this->getId($node, $path ?: []),
|
2019-02-21 05:32:32 -05:00
|
|
|
'data-node-name' => $node->getName()
|
2018-12-21 07:58:45 -05:00
|
|
|
]);
|
|
|
|
|
|
2019-01-22 05:44:05 -05:00
|
|
|
$li->add($this->getNodeIcons($node, $path));
|
2018-12-21 07:58:45 -05:00
|
|
|
|
|
|
|
|
$link = $node->getLink();
|
|
|
|
|
$link->getAttributes()->set('data-base-target', '_next');
|
2019-01-22 05:44:05 -05:00
|
|
|
$li->add($link);
|
2018-12-21 07:58:45 -05:00
|
|
|
|
2019-02-04 02:37:36 -05:00
|
|
|
if (! $this->isLocked() && $node->getBpConfig()->getName() === $this->getBusinessProcess()->getName()) {
|
2019-01-22 05:44:05 -05:00
|
|
|
$li->add($this->getActionIcons($bp, $node));
|
2018-12-21 07:58:45 -05:00
|
|
|
}
|
|
|
|
|
|
2019-01-22 05:40:33 -05:00
|
|
|
return $li;
|
2018-12-21 07:58:45 -05:00
|
|
|
}
|
|
|
|
|
|
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()) {
|
2019-01-14 02:46:32 -05:00
|
|
|
return [$this->createEditAction($bp, $node), $this->renderAddNewNode($node)];
|
2017-02-08 11:59:03 -05:00
|
|
|
} 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(
|
2019-01-22 05:44:05 -05:00
|
|
|
'edit',
|
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-22 05:44:05 -05:00
|
|
|
'class' => 'action-link'
|
2019-01-17 07:21:46 -05:00
|
|
|
],
|
|
|
|
|
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-14 02:46:32 -05:00
|
|
|
return $this->actionIcon(
|
|
|
|
|
'plus',
|
|
|
|
|
$this->getUrl()
|
|
|
|
|
->with('action', 'add')
|
|
|
|
|
->with('node', $parent->getName()),
|
|
|
|
|
mt('businessprocess', 'Add a new business process node')
|
2016-11-29 10:58:03 -05:00
|
|
|
);
|
|
|
|
|
}
|
2016-11-28 18:34:28 -05:00
|
|
|
}
|