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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\CustomVariable\CustomVariables;
use Icinga\Module\Director\Hook\DataTypeHook;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Application\Hook;
use Exception;
class DirectorDatafieldForm extends DirectorObjectForm
{
protected $objectName = 'Data field';
protected $listUrl = 'director/data/fields';
protected function onRequest()
{
if ($this->hasBeenSent()) {
if ($this->shouldBeDeleted()) {
$varname = $this->getSentValue('varname');
if ($cnt = CustomVariables::countAll($varname, $this->getDb())) {
$this->askForVariableDeletion($varname, $cnt);
}
} elseif ($this->shouldBeRenamed()) {
$varname = $this->object()->getOriginalProperty('varname');
if ($cnt = CustomVariables::countAll($varname, $this->getDb())) {
$this->askForVariableRename(
$varname,
$this->getSentValue('varname'),
$cnt
);
}
}
}
parent::onRequest();
}
protected function askForVariableDeletion($varname, $cnt)
{
$msg = $this->translate(
'Leaving custom variables in place while removing the related field is'
. ' perfectly legal and might be a desired operation. This way you can'
. ' no longer modify related custom variables in the Director GUI, but'
. ' the variables themselves will stay there and continue to be deployed.'
. ' When you re-add a field for the same variable later on, everything'
. ' will continue to work as before'
);
$this->addBoolean('wipe_vars', array(
'label' => $this->translate('Wipe related vars'),
'description' => sprintf($msg, $this->getSentValue('varname')),
'required' => true,
));
if ($wipe = $this->getSentValue('wipe_vars')) {
if ($wipe === 'y') {
CustomVariables::deleteAll($varname, $this->getDb());
}
} else {
$this->abortDeletion();
$this->addError(
sprintf(
$this->translate('Also wipe all "%s" custom variables from %d objects?'),
$varname,
$cnt
)
);
$this->getElement('wipe_vars')->addError(
sprintf(
$this->translate(
'There are %d objects with a related property. Should I also'
. ' remove the "%s" property from them?'
),
$cnt,
$varname
)
);
}
}
protected function askForVariableRename($oldname, $newname, $cnt)
{
$msg = $this->translate(
'Leaving custom variables in place while renaming the related field is'
. ' perfectly legal and might be a desired operation. This way you can'
. ' no longer modify related custom variables in the Director GUI, but'
. ' the variables themselves will stay there and continue to be deployed.'
. ' When you re-add a field for the same variable later on, everything'
. ' will continue to work as before'
);
$this->addBoolean('rename_vars', array(
'label' => $this->translate('Rename related vars'),
'description' => sprintf($msg, $this->getSentValue('varname')),
'required' => true,
));
if ($wipe = $this->getSentValue('rename_vars')) {
if ($wipe === 'y') {
CustomVariables::renameAll($oldname, $newname, $this->getDb());
}
} else {
$this->abortDeletion();
$this->addError(
sprintf(
$this->translate('Also rename all "%s" custom variables to "%s" on %d objects?'),
$oldname,
$newname,
$cnt
)
);
$this->getElement('rename_vars')->addError(
sprintf(
$this->translate(
'There are %d objects with a related property. Should I also'
. ' rename the "%s" property to "%s" on them?'
),
$cnt,
$oldname,
$newname
)
);
}
}
public function setup()
{
$this->addHtmlHint(
$this->translate(
'Data fields allow you to customize input controls for Icinga custom'
. ' variables. Once you defined them here, you can provide them through'
. ' your defined templates. This gives you a granular control over what'
. ' properties your users should be allowed to configure in which way.'
)
);
$this->addElement('text', 'varname', array(
'label' => $this->translate('Field name'),
'description' => $this->translate(
'This will be the name of the custom variable in the rendered Icinga configuration.'
),
'required' => true,
));
$this->addElement('text', 'caption', array(
'label' => $this->translate('Caption'),
'required' => true,
'description' => $this->translate(
'The caption which should be displayed to your users when this field'
. ' is shown'
)
));
$this->addElement('textarea', 'description', array(
'label' => $this->translate('Description'),
'description' => $this->translate(
'An extended description for this field. Will be shown as soon as a'
. ' user puts the focus on this field'
),
'rows' => '3',
));
$this->addElement('select', 'category_id', [
'label' => $this->translate('Data Field Category'),
'multiOptions' => $this->optionalEnum($this->enumCategories()),
]);
$error = false;
try {
$types = $this->enumDataTypes();
} catch (Exception $e) {
$error = $e->getMessage();
$types = $this->optionalEnum(array());
}
$this->addElement('select', 'datatype', array(
'label' => $this->translate('Data type'),
'description' => $this->translate('Field type'),
'required' => true,
'multiOptions' => $types,
'class' => 'autosubmit',
));
if ($error) {
$this->getElement('datatype')->addError($error);
}
$object = $this->object();
try {
if ($class = $this->getSentValue('datatype')) {
if ($class && array_key_exists($class, $types)) {
$this->addSettings($class);
}
} elseif ($class = $object->get('datatype')) {
$this->addSettings($class);
}
// TODO: next line looks like obsolete duplicate code to me
$this->addSettings();
} catch (Exception $e) {
$this->getElement('datatype')->addError($e->getMessage());
}
foreach ($object->getSettings() as $key => $val) {
if ($el = $this->getElement($key)) {
$el->setValue($val);
}
}
$this->setButtons();
}
public function shouldBeRenamed()
{
$object = $this->object();
return $object->hasBeenLoadedFromDb()
&& $object->getOriginalProperty('varname') !== $this->getSentValue('varname');
}
protected function addSettings($class = null)
{
if ($class === null) {
$class = $this->getValue('datatype');
}
if ($class !== null) {
if (! class_exists($class)) {
throw new ConfigurationError(
'The hooked class "%s" for this data field does no longer exist',
$class
);
}
$class::addSettingsFormFields($this);
}
}
protected function clearOutdatedSettings()
{
$names = array();
$object = $this->object();
$global = array('varname', 'description', 'caption', 'datatype');
/** @var \Zend_Form_Element $el */
foreach ($this->getElements() as $el) {
if ($el->getIgnore()) {
continue;
}
$name = $el->getName();
if (in_array($name, $global)) {
continue;
}
$names[$name] = $name;
}
foreach ($object->getSettings() as $setting => $value) {
if (! array_key_exists($setting, $names)) {
unset($object->$setting);
}
}
}
public function onSuccess()
{
$this->clearOutdatedSettings();
if ($class = $this->getValue('datatype')) {
if (array_key_exists($class, $this->enumDataTypes())) {
$this->addHidden('format', $class::getFormat());
}
}
parent::onSuccess();
}
protected function enumDataTypes()
{
$hooks = Hook::all('Director\\DataType');
$enum = [null => $this->translate('- please choose -')];
/** @var DataTypeHook $hook */
foreach ($hooks as $hook) {
$enum[get_class($hook)] = $hook->getName();
}
return $enum;
}
protected function enumCategories()
{
$db = $this->getDb()->getDbAdapter();
return $db->fetchPairs(
$db->select()->from('director_datafield_category', ['id', 'category_name'])
);
}
}
|