2015-03-16 04:08:00 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Businessprocess\Forms;
|
|
|
|
|
|
|
|
|
|
use Icinga\Application\Config;
|
2016-12-09 08:05:58 -05:00
|
|
|
use Icinga\Authentication\Auth;
|
2015-03-16 04:08:00 -04:00
|
|
|
use Icinga\Module\Businessprocess\BusinessProcess;
|
2016-12-09 08:05:58 -05:00
|
|
|
use Icinga\Module\Businessprocess\Storage\Storage;
|
2015-11-17 09:45:13 -05:00
|
|
|
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
|
2015-03-16 04:08:00 -04:00
|
|
|
|
2015-11-17 09:45:13 -05:00
|
|
|
class BpConfigForm extends QuickForm
|
2015-03-16 04:08:00 -04:00
|
|
|
{
|
2016-12-09 08:05:58 -05:00
|
|
|
/** @var Storage */
|
2015-03-16 04:08:00 -04:00
|
|
|
protected $storage;
|
|
|
|
|
|
|
|
|
|
protected $backend;
|
|
|
|
|
|
2016-12-09 08:05:58 -05:00
|
|
|
/** @var BusinessProcess */
|
2015-03-16 04:08:00 -04:00
|
|
|
protected $config;
|
|
|
|
|
|
|
|
|
|
protected $node;
|
|
|
|
|
|
|
|
|
|
protected $objectList = array();
|
|
|
|
|
|
|
|
|
|
protected $processList = array();
|
|
|
|
|
|
2015-11-17 09:45:13 -05:00
|
|
|
protected $deleteButtonName;
|
|
|
|
|
|
2015-03-16 04:08:00 -04:00
|
|
|
public function setup()
|
|
|
|
|
{
|
|
|
|
|
$this->addElement('text', 'name', array(
|
2016-12-09 08:05:58 -05:00
|
|
|
'label' => $this->translate('Name'),
|
2015-03-16 04:08:00 -04:00
|
|
|
'required' => true,
|
|
|
|
|
'description' => $this->translate(
|
|
|
|
|
'This is the unique identifier of this process'
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
|
2016-12-09 08:05:58 -05:00
|
|
|
$this->addElement('text', 'Title', array(
|
2015-03-16 04:08:00 -04:00
|
|
|
'label' => $this->translate('Title'),
|
|
|
|
|
'description' => $this->translate(
|
|
|
|
|
'Usually this title will be shown for this process. Equals name'
|
2016-11-23 06:35:17 -05:00
|
|
|
. ' if not given'
|
2015-03-16 04:08:00 -04:00
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
|
2016-12-09 08:05:58 -05:00
|
|
|
$this->addElement('textarea', 'Description', array(
|
|
|
|
|
'label' => $this->translate('Description'),
|
|
|
|
|
'description' => $this->translate(
|
2016-12-16 13:24:27 -05:00
|
|
|
'A slightly more detailed description for this process, about 100-150 characters long'
|
2016-12-09 08:05:58 -05:00
|
|
|
),
|
|
|
|
|
'rows' => 4,
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$this->addElement('select', 'Backend', array(
|
2015-03-16 04:08:00 -04:00
|
|
|
'label' => $this->translate('Backend'),
|
|
|
|
|
'description' => $this->translate(
|
|
|
|
|
'Icinga Web Monitoring Backend where current object states for'
|
2016-11-23 06:35:17 -05:00
|
|
|
. ' this process should be retrieved from'
|
2015-03-16 04:08:00 -04:00
|
|
|
),
|
|
|
|
|
'multiOptions' => array(
|
|
|
|
|
null => $this->translate('Use the configured default backend'),
|
|
|
|
|
) + $this->listAvailableBackends()
|
|
|
|
|
));
|
|
|
|
|
|
2016-12-09 08:05:58 -05:00
|
|
|
$this->addElement('select', 'Statetype', array(
|
2015-03-16 04:08:00 -04:00
|
|
|
'label' => $this->translate('State Type'),
|
|
|
|
|
'required' => true,
|
|
|
|
|
'description' => $this->translate(
|
|
|
|
|
'Whether this process should be based on Icinga hard or soft states'
|
|
|
|
|
),
|
|
|
|
|
'multiOptions' => array(
|
|
|
|
|
'soft' => $this->translate('Use SOFT states'),
|
2016-12-09 08:05:58 -05:00
|
|
|
'hard' => $this->translate('Use HARD states'),
|
|
|
|
|
)
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$this->addElement('select', 'AddToMenu', array(
|
|
|
|
|
'label' => $this->translate('Add to menu'),
|
|
|
|
|
'required' => true,
|
|
|
|
|
'description' => $this->translate(
|
|
|
|
|
'Whether this process should be linked in the main Icinga Web 2 menu'
|
|
|
|
|
),
|
|
|
|
|
'multiOptions' => array(
|
|
|
|
|
'yes' => $this->translate('Yes'),
|
|
|
|
|
'no' => $this->translate('No'),
|
2015-03-16 04:08:00 -04:00
|
|
|
)
|
|
|
|
|
));
|
2015-11-17 09:45:13 -05:00
|
|
|
|
|
|
|
|
if ($this->config === null) {
|
|
|
|
|
$this->setSubmitLabel(
|
|
|
|
|
$this->translate('Add')
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$config = $this->config;
|
|
|
|
|
|
2016-12-09 08:05:58 -05:00
|
|
|
$meta = $config->getMetadata();
|
|
|
|
|
foreach ($meta->getProperties() as $k => $v) {
|
|
|
|
|
if ($el = $this->getElement($k)) {
|
|
|
|
|
$el->setValue($v);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-17 09:45:13 -05:00
|
|
|
$this->getElement('name')
|
|
|
|
|
->setValue($config->getName())
|
|
|
|
|
->setAttrib('readonly', true);
|
|
|
|
|
|
|
|
|
|
$this->setSubmitLabel(
|
|
|
|
|
$this->translate('Store')
|
|
|
|
|
);
|
2016-12-09 08:05:58 -05:00
|
|
|
|
2015-11-17 09:45:13 -05:00
|
|
|
$label = $this->translate('Delete');
|
2017-01-03 05:36:43 -05:00
|
|
|
$el = $this->createElement('submit', $label, array(
|
|
|
|
|
'data-base-target' => '_main'
|
|
|
|
|
))->setLabel($label)->setDecorators(array('ViewHelper'));
|
2015-11-17 09:45:13 -05:00
|
|
|
$this->deleteButtonName = $el->getName();
|
|
|
|
|
$this->addElement($el);
|
|
|
|
|
}
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function listAvailableBackends()
|
|
|
|
|
{
|
|
|
|
|
$keys = array_keys(Config::module('monitoring', 'backends')->toArray());
|
|
|
|
|
return array_combine($keys, $keys);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setStorage($storage)
|
|
|
|
|
{
|
|
|
|
|
$this->storage = $storage;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setProcessConfig($config)
|
|
|
|
|
{
|
|
|
|
|
$this->config = $config;
|
2015-11-17 09:45:13 -05:00
|
|
|
return $this;
|
|
|
|
|
}
|
2015-03-16 04:08:00 -04:00
|
|
|
|
2015-11-17 09:45:13 -05:00
|
|
|
protected function onRequest()
|
|
|
|
|
{
|
|
|
|
|
$name = $this->getValue('name');
|
2015-03-16 04:08:00 -04:00
|
|
|
|
2015-11-17 09:45:13 -05:00
|
|
|
if ($this->shouldBeDeleted()) {
|
|
|
|
|
$this->config->clearAppliedChanges();
|
|
|
|
|
$this->storage->deleteProcess($name);
|
|
|
|
|
$this->setSuccessUrl('businessprocess');
|
|
|
|
|
$this->redirectOnSuccess(sprintf('Process %s has been deleted', $name));
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function onSuccess()
|
|
|
|
|
{
|
2016-12-09 08:05:58 -05:00
|
|
|
$name = $this->getValue('name');
|
2015-03-16 04:08:00 -04:00
|
|
|
|
|
|
|
|
if ($this->config === null) {
|
|
|
|
|
// New config
|
|
|
|
|
$config = new BusinessProcess();
|
|
|
|
|
$config->setName($name);
|
2016-12-09 08:05:58 -05:00
|
|
|
$config->getMetadata()->set('Owner', Auth::getInstance()->getUser()->getUsername());
|
2015-11-17 09:45:13 -05:00
|
|
|
$this->setSuccessUrl(
|
|
|
|
|
$this->getSuccessUrl()->setParams(
|
2015-03-16 04:08:00 -04:00
|
|
|
array('config' => $name, 'unlocked' => true)
|
|
|
|
|
)
|
|
|
|
|
);
|
2016-12-09 08:05:58 -05:00
|
|
|
$this->setSuccessMessage(sprintf('Process %s has been created', $name));
|
2015-03-16 04:08:00 -04:00
|
|
|
|
|
|
|
|
} else {
|
2016-11-29 09:32:56 -05:00
|
|
|
$config = $this->config;
|
2016-12-09 08:05:58 -05:00
|
|
|
$this->setSuccessMessage(sprintf('Process %s has been stored', $name));
|
|
|
|
|
}
|
|
|
|
|
$meta = $config->getMetadata();
|
|
|
|
|
foreach ($this->getValues() as $key => $value) {
|
|
|
|
|
if ($value === null || $value === '') {
|
|
|
|
|
continue;
|
2015-03-16 08:19:05 -04:00
|
|
|
}
|
2016-12-09 08:05:58 -05:00
|
|
|
if ($meta->hasKey($key)) {
|
|
|
|
|
$meta->set($key, $value);
|
2015-03-16 08:19:05 -04:00
|
|
|
}
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
2016-12-09 08:05:58 -05:00
|
|
|
|
|
|
|
|
$this->storage->storeProcess($config);
|
|
|
|
|
$config->clearAppliedChanges();
|
|
|
|
|
$this->setSuccessUrl('businessprocess/process/show', array('config' => $name));
|
|
|
|
|
parent::onSuccess();
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
2015-11-17 09:45:13 -05:00
|
|
|
|
|
|
|
|
public function hasDeleteButton()
|
|
|
|
|
{
|
|
|
|
|
return $this->deleteButtonName !== null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function shouldBeDeleted()
|
|
|
|
|
{
|
2016-11-23 06:35:17 -05:00
|
|
|
if (! $this->hasDeleteButton()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-17 09:45:13 -05:00
|
|
|
|
|
|
|
|
$name = $this->deleteButtonName;
|
|
|
|
|
return $this->getSentValue($name) === $this->getElement($name)->getLabel();
|
|
|
|
|
}
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|