1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
<?php
namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Authentication\Auth;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
class BpConfigForm extends BpConfigBaseForm
{
protected $deleteButtonName;
public function setup()
{
$this->addElement('text', 'name', array(
'label' => $this->translate('ID'),
'required' => true,
'validators' => array(
array(
'validator' => 'StringLength',
'options' => array(
'min' => 2,
'max' => 40
)
),
[
'validator' => 'Regex',
'options' => [
'pattern' => '/^[a-zA-Z0-9](?:[\w\h._-]*)?\w$/',
'messages' => [
'regexNotMatch' => $this->translate(
'Id must only consist of alphanumeric characters.'
. ' Underscore at the beginning and space, dot and hyphen at the beginning'
. ' and end are not allowed.'
)
]
]
]
),
'description' => $this->translate(
'This is the unique identifier of this process'
),
));
$this->addElement('text', 'Title', array(
'label' => $this->translate('Display Name'),
'description' => $this->translate(
'Usually this name will be shown for this process. Equals ID'
. ' if not given'
),
));
$this->addElement('textarea', 'Description', array(
'label' => $this->translate('Description'),
'description' => $this->translate(
'A slightly more detailed description for this process, about 100-150 characters long'
),
'rows' => 4,
));
if (! empty($this->listAvailableBackends())) {
$this->addElement('select', 'Backend', array(
'label' => $this->translate('Backend'),
'description' => $this->translate(
'Icinga Web Monitoring Backend where current object states for'
. ' this process should be retrieved from'
),
'multiOptions' => array(
null => $this->translate('Use the configured default backend'),
) + $this->listAvailableBackends()
));
}
$this->addElement('select', 'Statetype', 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(
'soft' => $this->translate('Use SOFT states'),
'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'),
)
));
$this->addElement('text', 'AllowedUsers', array(
'label' => $this->translate('Allowed Users'),
'description' => $this->translate(
'Allowed Users (comma-separated)'
),
));
$this->addElement('text', 'AllowedGroups', array(
'label' => $this->translate('Allowed Groups'),
'description' => $this->translate(
'Allowed Groups (comma-separated)'
),
));
$this->addElement('text', 'AllowedRoles', array(
'label' => $this->translate('Allowed Roles'),
'description' => $this->translate(
'Allowed Roles (comma-separated)'
),
));
if ($this->bp === null) {
$this->setSubmitLabel(
$this->translate('Add')
);
} else {
$config = $this->bp;
$meta = $config->getMetadata();
foreach ($meta->getProperties() as $k => $v) {
if ($el = $this->getElement($k)) {
$el->setValue($v);
}
}
$this->getElement('name')
->setValue($config->getName())
->setAttrib('readonly', true);
$this->setSubmitLabel(
$this->translate('Store')
);
$label = $this->translate('Delete');
$el = $this->createElement('submit', $label, array(
'data-base-target' => '_main'
))->setLabel($label)->setDecorators(array('ViewHelper'));
$this->deleteButtonName = $el->getName();
$this->addElement($el);
}
}
protected function onSetup()
{
$this->getElement($this->getSubmitLabel())->setAttrib('data-base-target', '_main');
}
protected function onRequest()
{
$name = $this->getValue('name');
if ($this->shouldBeDeleted()) {
if ($this->bp->isReferenced()) {
$this->addError(sprintf(
$this->translate('Process "%s" cannot be deleted as it has been referenced in other processes'),
$name
));
} else {
$this->bp->clearAppliedChanges();
$this->storage->deleteProcess($name);
$this->setSuccessUrl('businessprocess');
$this->redirectOnSuccess(sprintf($this->translate('Process %s has been deleted'), $name));
}
}
}
public function onSuccess()
{
$name = $this->getValue('name');
if ($this->bp === null) {
if ($this->storage->hasProcess($name)) {
$this->addError(sprintf(
$this->translate('A process named "%s" already exists'),
$name
));
return;
}
// New config
$config = new BpConfig();
$config->setName($name);
if (! $this->prepareMetadata($config)) {
return;
}
$this->setSuccessUrl(
$this->getSuccessUrl()->setParams(
array('config' => $name, 'unlocked' => true)
)
);
$this->setSuccessMessage(sprintf($this->translate('Process %s has been created'), $name));
} else {
$config = $this->bp;
$this->setSuccessMessage(sprintf($this->translate('Process %s has been stored'), $name));
}
$meta = $config->getMetadata();
foreach ($this->getValues() as $key => $value) {
if (! in_array($key, ['Title', 'Description', 'Backend'], true)
&& ($value === null || $value === '')) {
continue;
}
if ($meta->hasKey($key)) {
$meta->set($key, $value);
}
}
$this->storage->storeProcess($config);
$config->clearAppliedChanges();
parent::onSuccess();
}
public function hasDeleteButton()
{
return $this->deleteButtonName !== null;
}
public function shouldBeDeleted()
{
if (! $this->hasDeleteButton()) {
return false;
}
$name = $this->deleteButtonName;
return $this->getSentValue($name) === $this->getElement($name)->getLabel();
}
}
|