icingaweb2-module-businessp.../library/Businessprocess/BpNode.php

274 lines
6.5 KiB
PHP
Raw Normal View History

<?php
2014-11-30 09:56:58 -05:00
namespace Icinga\Module\Businessprocess;
2015-02-06 19:02:53 -05:00
use Icinga\Exception\ConfigurationError;
class BpNode extends Node
{
const OP_AND = '&';
const OP_OR = '|';
protected $operator = '&';
protected $url;
protected $info_command;
protected $display = 0;
protected $children;
protected $child_names = array();
protected $alias;
protected $counters;
public function __construct(
BusinessProcess $bp,
$object
/*
$name,
$operator,
$child_names
*/
) {
$this->bp = $bp;
$this->name = $object->name;
$this->operator = $object->operator;
$this->setChildNames($object->child_names);
}
public function getStateSummary()
{
if ($this->counters === null) {
$this->getState();
$this->counters = array(0, 0, 0, 0);
foreach ($this->children as $child) {
if ($child instanceof BpNode) {
$counters = $child->getStateSummary();
foreach ($counters as $k => $v) {
$this->counters[$k] += $v;
}
} else {
$state = $child->getState();
$this->counters[$state]++;
}
}
}
return $this->counters;
}
public function getOperator()
{
return $this->operator;
}
2014-11-30 05:56:14 -05:00
public function setInfoUrl($url)
{
$this->url = $url;
2014-11-30 05:56:14 -05:00
return $this;
}
2014-11-30 05:56:14 -05:00
public function hasInfoUrl()
{
return $this->url !== null;
}
2014-11-30 05:56:14 -05:00
public function getInfoUrl()
{
return $this->url;
}
public function setInfoCommand($cmd)
{
$this->info_command = $cmd;
}
public function hasInfoCommand()
{
return $this->info_command !== null;
}
public function getInfoCommand()
{
return $this->info_command;
}
2014-12-01 13:32:09 -05:00
public function hasAlias()
{
return $this->alias !== null;
}
public function getAlias()
{
2014-12-01 13:32:09 -05:00
return $this->alias ? preg_replace('~_~', ' ', $this->alias) : $this->name;
}
public function setAlias($name)
{
$this->alias = $name;
return $this;
}
public function getState()
{
if ($this->state === null) {
$this->calculateState();
}
return $this->state;
}
protected function calculateState()
{
$sort_states = array();
foreach ($this->getChildren() as $child) {
$sort_states[] = $child->getSortingState();
}
switch ($this->operator) {
case self::OP_AND:
$state = max($sort_states);
break;
case self::OP_OR:
$state = min($sort_states);
break;
default:
// MIN:
if (! is_numeric($this->operator)) {
2015-02-06 19:02:53 -05:00
throw new ConfigurationError(
'Got invalid operator: %s',
$this->operator
);
}
sort($sort_states);
// default -> unknown
$state = 2 << self::SHIFT_FLAGS;
for ($i = 1; $i <= $this->operator; $i++) {
$state = array_shift($sort_states);
}
}
if ($state & self::FLAG_DOWNTIME) {
$this->setDowntime(true);
}
if ($state & self::FLAG_ACK) {
$this->setAck(true);
}
$state = $state >> self::SHIFT_FLAGS;
if ($state === 3) {
$this->state = 2;
} elseif ($state === 2) {
$this->state = 3;
} else {
$this->state = $state;
}
}
public function countChildren()
{
return count($this->getChildren());
}
public function hasChildren()
{
return $this->countChildren() > 0;
}
public function setDisplay($display)
{
$this->display = $display;
return $this;
}
public function setChildNames($names)
{
$this->child_names = $names;
$this->children = null;
return $this;
}
public function getChildren()
{
if ($this->children === null) {
$this->children = array();
natsort($this->child_names);
foreach ($this->child_names as $name) {
$this->children[$name] = $this->bp->getNode($name);
}
}
return $this->children;
}
2014-11-30 05:57:47 -05:00
protected function assertNumericOperator()
{
if (! is_numeric($this->operator)) {
2015-02-06 19:02:53 -05:00
throw new ConfigurationError('Got invalid operator: %s', $this->operator);
2014-11-30 05:57:47 -05:00
}
}
public function renderLink($view)
{
if (! $this->bp->isEditMode()) {
return parent::renderLink($view);
}
2014-11-30 09:56:58 -05:00
return $view->qlink($this->name, 'businessprocess/node/edit', array(
'node' => $this->name,
'processName' => $this->bp->getName()
2014-11-30 05:57:47 -05:00
));
}
public function toLegacyConfigString()
{
$cfg = '';
$children = array();
foreach ($this->getChildren() as $name => $child) {
$children[] = (string) $child;
if ($child instanceof BpNode) {
$cfg .= $child->toLegacyConfigString() . "\n";
}
}
$eq = '=';
$op = $this->operator;
if (is_numeric($op)) {
$eq = '= ' . $op . ' of:';
$op = '+';
}
$cfg .= sprintf(
"%s %s %s\n",
$this->name,
$eq,
implode(' ' . $op . ' ', $children)
);
if ($this->hasAlias()/* || $this->hasPrio()*/) {
2015-02-06 18:59:53 -05:00
$prio = $this->display; // TODO: $this->getPrio() ??
$cfg .= sprintf(
"display_name %s;%s;%s\n",
$prio,
$this->name,
$this->getAlias()
);
}
if ($this->hasInfoUrl()) {
$cfg .= sprintf(
"info_url;%s;%s\n",
$this->name,
$this->getInfoUrl()
);
}
return $cfg;
}
2014-11-30 05:57:47 -05:00
public function operatorHtml()
{
switch ($this->operator) {
case self::OP_AND:
return 'and';
break;
case self::OP_OR:
return 'or';
break;
default:
// MIN
$this->assertNumericOperator();
return 'min:' . $this->operator;
}
}
}