monitoring: Prepare feature command form for multi-select

refs #8963
This commit is contained in:
Eric Lippmann 2016-01-27 17:37:15 +01:00
parent 16bd78ac30
commit 027aaacff8
3 changed files with 80 additions and 74 deletions

View File

@ -12,6 +12,20 @@ use Icinga\Web\Notification;
*/ */
class ToggleObjectFeaturesCommandForm extends ObjectsCommandForm class ToggleObjectFeaturesCommandForm extends ObjectsCommandForm
{ {
/**
* Feature to label map
*
* @var string[]
*/
protected $features;
/**
* Feature to feature status map
*
* @var int[]
*/
protected $featureStatus;
/** /**
* (non-PHPDoc) * (non-PHPDoc)
* @see \Zend_Form::init() For the method documentation. * @see \Zend_Form::init() For the method documentation.
@ -20,6 +34,21 @@ class ToggleObjectFeaturesCommandForm extends ObjectsCommandForm
{ {
$this->setUseFormAutosubmit(); $this->setUseFormAutosubmit();
$this->setAttrib('class', 'inline object-features'); $this->setAttrib('class', 'inline object-features');
$features = array(
ToggleObjectFeatureCommand::FEATURE_ACTIVE_CHECKS => $this->translate('Active Checks'),
ToggleObjectFeatureCommand::FEATURE_PASSIVE_CHECKS => $this->translate('Passive Checks'),
ToggleObjectFeatureCommand::FEATURE_OBSESSING => $this->translate('Obsessing'),
ToggleObjectFeatureCommand::FEATURE_NOTIFICATIONS => $this->translate('Notifications'),
ToggleObjectFeatureCommand::FEATURE_EVENT_HANDLER => $this->translate('Event Handler'),
ToggleObjectFeatureCommand::FEATURE_FLAP_DETECTION => $this->translate('Flap Detection')
);
if (preg_match('~^v2\.\d+\.\d+.*$~', $this->getIcingaVersion())) {
unset($features[ToggleObjectFeatureCommand::FEATURE_OBSESSING]);
}
$this->features = $features;
} }
/** /**
@ -30,84 +59,54 @@ class ToggleObjectFeaturesCommandForm extends ObjectsCommandForm
{ {
$toggleDisabled = $this->hasPermission('monitoring/command/feature/object') ? null : ''; $toggleDisabled = $this->hasPermission('monitoring/command/feature/object') ? null : '';
$this->addElement( foreach ($this->features as $feature => $label) {
'checkbox', $options = array(
ToggleObjectFeatureCommand::FEATURE_ACTIVE_CHECKS,
array(
'label' => $this->translate('Active Checks'),
'autosubmit' => true, 'autosubmit' => true,
'disabled' => $toggleDisabled 'disabled' => $toggleDisabled,
) 'label' => $label
);
$this->addElement(
'checkbox',
ToggleObjectFeatureCommand::FEATURE_PASSIVE_CHECKS,
array(
'label' => $this->translate('Passive Checks'),
'autosubmit' => true,
'disabled' => $toggleDisabled
)
);
if (! preg_match('~^v2\.\d+\.\d+.*$~', $this->getIcingaVersion())) {
$this->addElement(
'checkbox',
ToggleObjectFeatureCommand::FEATURE_OBSESSING,
array(
'label' => $this->translate('Obsessing'),
'autosubmit' => true,
'disabled' => $toggleDisabled
)
); );
if ($formData[$feature . '_changed']) {
$options['description'] = $this->translate('changed');
}
if ($formData[$feature] === 2) {
$options['multiOptions'] = array(
$this->translate('disable'),
$this->translate('enable'),
);
$options['separator'] = '';
$elementType = 'radio';
} else {
$elementType = 'checkbox';
$options['value'] = $formData[$feature];
}
$this->addElement($elementType, $feature, $options);
} }
$this->addElement(
'checkbox',
ToggleObjectFeatureCommand::FEATURE_NOTIFICATIONS,
array(
'label' => $this->translate('Notifications'),
'autosubmit' => true,
'disabled' => $toggleDisabled
)
);
$this->addElement(
'checkbox',
ToggleObjectFeatureCommand::FEATURE_EVENT_HANDLER,
array(
'label' => $this->translate('Event Handler'),
'autosubmit' => true,
'disabled' => $toggleDisabled
)
);
$this->addElement(
'checkbox',
ToggleObjectFeatureCommand::FEATURE_FLAP_DETECTION,
array(
'label' => $this->translate('Flap Detection'),
'autosubmit' => true,
'disabled' => $toggleDisabled
)
);
} }
/** /**
* Load feature status * Load feature status
* *
* @param MonitoredObject $object * @param MonitoredObject|object $object
* *
* @return $this * @return $this
*/ */
public function load(MonitoredObject $object) public function load($object)
{ {
$this->create(); $featureStatus = array();
foreach ($this->getValues() as $feature => $enabled) {
$element = $this->getElement($feature); foreach (array_keys($this->features) as $feature) {
$element->setChecked($object->{$feature}); $featureStatus[$feature] = $object->{$feature};
if ((bool) $object->{$feature . '_changed'} === true) { if (isset($object->{$feature . '_changed'})) {
$element->setDescription($this->translate('changed')); $featureStatus[$feature . '_changed'] = (bool) $object->{$feature . '_changed'};
} else {
$featureStatus[$feature . '_changed'] = false;
} }
} }
$this->create($featureStatus);
$this->featureStatus = $featureStatus;
return $this; return $this;
} }
@ -146,9 +145,15 @@ class ToggleObjectFeaturesCommandForm extends ObjectsCommandForm
) )
); );
foreach ($this->objects as $object) { foreach ($this->getValues() as $feature => $enabled) {
/** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */ if ($enabled === null
foreach ($this->getValues() as $feature => $enabled) { || (int) $enabled === (int) $this->featureStatus[$feature]
) {
// Ignore unchanged features
continue;
}
foreach ($this->objects as $object) {
/** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */
if ((bool) $object->{$feature} !== (bool) $enabled) { if ((bool) $object->{$feature} !== (bool) $enabled) {
$toggleFeature = new ToggleObjectFeatureCommand(); $toggleFeature = new ToggleObjectFeatureCommand();
$toggleFeature $toggleFeature
@ -156,12 +161,11 @@ class ToggleObjectFeaturesCommandForm extends ObjectsCommandForm
->setObject($object) ->setObject($object)
->setEnabled($enabled); ->setEnabled($enabled);
$this->getTransport($this->request)->send($toggleFeature); $this->getTransport($this->request)->send($toggleFeature);
Notification::success(
$notifications[$feature][$enabled ? 0 : 1]
);
} }
} }
Notification::success(
$notifications[$feature][$enabled ? 0 : 1]
);
} }
return true; return true;

View File

@ -75,11 +75,12 @@ abstract class MonitoredObjectController extends Controller
} }
} }
$this->object->populate(); $this->object->populate();
$toggleFeaturesForm = new ToggleObjectFeaturesCommandForm(); $toggleFeaturesForm = new ToggleObjectFeaturesCommandForm(array(
'backend' => $this->backend,
'objects' => $this->object
));
$toggleFeaturesForm $toggleFeaturesForm
->setBackend($this->backend)
->load($this->object) ->load($this->object)
->setObjects($this->object)
->handleRequest(); ->handleRequest();
$this->view->toggleFeaturesForm = $toggleFeaturesForm; $this->view->toggleFeaturesForm = $toggleFeaturesForm;
if (! empty($this->object->comments) && $auth->hasPermission('monitoring/command/comment/delete')) { if (! empty($this->object->comments) && $auth->hasPermission('monitoring/command/comment/delete')) {

View File

@ -417,7 +417,8 @@ form.instance-features span.description, form.object-features span.description {
} }
} }
input[type="checkbox"] { input[type="checkbox"],
input[type="radio"] {
margin: @table-column-padding; margin: @table-column-padding;
} }
} }