icingaweb2-module-businessp.../application/forms/BpConfigForm.php

205 lines
6.1 KiB
PHP
Raw Normal View History

2015-03-16 04:08:00 -04:00
<?php
namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Application\Config;
use Icinga\Module\Businessprocess\BusinessProcess;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
2015-03-16 04:08:00 -04:00
use Icinga\Web\Notification;
use Icinga\Web\Request;
use Icinga\Web\Url;
class BpConfigForm extends QuickForm
2015-03-16 04:08:00 -04:00
{
protected $storage;
protected $backend;
protected $config;
protected $node;
protected $objectList = array();
protected $processList = array();
protected $deleteButtonName;
2015-03-16 04:08:00 -04:00
public function setup()
{
2015-03-16 04:08:00 -04:00
$this->addElement('text', 'name', array(
'label' => $this->translate('Name'),
'required' => true,
'description' => $this->translate(
'This is the unique identifier of this process'
),
));
$this->addElement('text', 'title', array(
'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
),
));
$this->addElement('select', 'backend_name', array(
'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()
));
$this->addElement('select', 'state_type', array(
'label' => $this->translate('State Type'),
'required' => true,
'description' => $this->translate(
'Whether this process should be based on Icinga hard or soft states'
),
'multiOptions' => array(
'hard' => $this->translate('Use HARD states'),
'soft' => $this->translate('Use SOFT states'),
)
));
if ($this->config === null) {
$this->setSubmitLabel(
$this->translate('Add')
);
} else {
$config = $this->config;
$this->getElement('name')
->setValue($config->getName())
->setAttrib('readonly', true);
if ($config->hasTitle()) {
$this->getElement('title')->setValue($config->getTitle());
}
if ($config->hasBackend()) {
$this->getElement('backend_name')->setValue(
$config->getBackend()->getName()
);
}
if ($config->usesSoftStates()) {
$this->getElement('state_type')->setValue('soft');
} else {
$this->getElement('state_type')->setValue('hard');
}
$this->setSubmitLabel(
$this->translate('Store')
);
$label = $this->translate('Delete');
$el = $this->createElement('submit', $label)
->setLabel($label)
->setDecorators(array('ViewHelper'));
$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;
return $this;
return $this;
}
2015-03-16 04:08:00 -04:00
protected function onRequest()
{
$name = $this->getValue('name');
2015-03-16 04:08:00 -04: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()
{
$name = $this->getValue('name');
$title = $this->getValue('title');
$backend = $this->getValue('backend');
if ($this->config === null) {
// New config
$config = new BusinessProcess();
$config->setName($name);
if ($title) {
$config->setTitle($title);
}
if ($backend) {
$config->setBackendName($backend);
}
if ($this->getValue('state_type') === 'soft') {
$config->useSoftStates();
} else {
$config->useHardStates();
}
$this->storage->storeProcess($config);
$config->clearAppliedChanges();
$this->setSuccessUrl(
$this->getSuccessUrl()->setParams(
2015-03-16 04:08:00 -04:00
array('config' => $name, 'unlocked' => true)
)
);
$this->redirectOnSuccess(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;
2015-03-16 08:19:05 -04:00
if ($title) {
$config->setTitle($title);
}
if ($backend) {
$config->setBackendName($backend);
}
if ($this->getValue('state_type') === 'soft') {
$config->useSoftStates();
} else {
$config->useHardStates();
}
$this->storage->storeProcess($config);
$config->clearAppliedChanges();
$this->getSuccessUrl()->setParam('config', $name);
2015-03-16 08:19:05 -04:00
Notification::success(sprintf('Process %s has been stored', $name));
2015-03-16 04:08:00 -04:00
}
}
public function hasDeleteButton()
{
return $this->deleteButtonName !== null;
}
public function shouldBeDeleted()
{
2016-11-23 06:35:17 -05:00
if (! $this->hasDeleteButton()) {
return false;
}
$name = $this->deleteButtonName;
return $this->getSentValue($name) === $this->getElement($name)->getLabel();
}
2015-03-16 04:08:00 -04:00
}