summaryrefslogtreecommitdiffstats
path: root/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php
blob: cf14db8a4263653d814b07def5cfdd1fa42ab69e (plain)
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
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Forms\Command\Instance;

use Icinga\Module\Icingadb\Command\Instance\ToggleInstanceFeatureCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Web\Notification;
use ipl\Web\FormDecorator\IcingaFormDecorator;
use Traversable;

class ToggleInstanceFeaturesForm extends CommandForm
{
    protected $features;

    protected $featureStatus;

    /**
     * ToggleFeature(s) being used to submit this form
     *
     * @var ToggleInstanceFeatureCommand[]
     */
    protected $submittedFeatures = [];

    public function __construct(array $featureStatus)
    {
        $this->featureStatus = $featureStatus;
        $this->features = [
            ToggleInstanceFeatureCommand::FEATURE_ACTIVE_HOST_CHECKS =>
                t('Active Host Checks'),
            ToggleInstanceFeatureCommand::FEATURE_ACTIVE_SERVICE_CHECKS =>
                t('Active Service Checks'),
            ToggleInstanceFeatureCommand::FEATURE_EVENT_HANDLERS =>
                t('Event Handlers'),
            ToggleInstanceFeatureCommand::FEATURE_FLAP_DETECTION =>
                t('Flap Detection'),
            ToggleInstanceFeatureCommand::FEATURE_NOTIFICATIONS =>
                t('Notifications'),
            ToggleInstanceFeatureCommand::FEATURE_PERFORMANCE_DATA =>
                t('Performance Data')
        ];

        $this->getAttributes()->add('class', 'instance-features');

        $this->on(self::ON_SUCCESS, function () {
            if ($this->errorOccurred) {
                return;
            }

            foreach ($this->submittedFeatures as $feature) {
                $enabled = $feature->getEnabled();
                switch ($feature->getFeature()) {
                    case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_HOST_CHECKS:
                        if ($enabled) {
                            $message = t('Enabled active host checks successfully');
                        } else {
                            $message = t('Disabled active host checks successfully');
                        }

                        break;
                    case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_SERVICE_CHECKS:
                        if ($enabled) {
                            $message = t('Enabled active service checks successfully');
                        } else {
                            $message = t('Disabled active service checks successfully');
                        }

                        break;
                    case ToggleInstanceFeatureCommand::FEATURE_EVENT_HANDLERS:
                        if ($enabled) {
                            $message = t('Enabled event handlers successfully');
                        } else {
                            $message = t('Disabled event handlers checks successfully');
                        }

                        break;
                    case ToggleInstanceFeatureCommand::FEATURE_FLAP_DETECTION:
                        if ($enabled) {
                            $message = t('Enabled flap detection successfully');
                        } else {
                            $message = t('Disabled flap detection successfully');
                        }

                        break;
                    case ToggleInstanceFeatureCommand::FEATURE_NOTIFICATIONS:
                        if ($enabled) {
                            $message = t('Enabled notifications successfully');
                        } else {
                            $message = t('Disabled notifications successfully');
                        }

                        break;
                    case ToggleInstanceFeatureCommand::FEATURE_PERFORMANCE_DATA:
                        if ($enabled) {
                            $message = t('Enabled performance data successfully');
                        } else {
                            $message = t('Disabled performance data successfully');
                        }

                        break;
                    default:
                        $message = t('Invalid feature option');
                        break;
                }

                Notification::success($message);
            }
        });
    }

    protected function assembleElements()
    {
        $disabled = ! $this->getAuth()->hasPermission('icingadb/command/feature/instance');
        $decorator = new IcingaFormDecorator();

        foreach ($this->features as $feature => $label) {
            $this->addElement(
                'checkbox',
                $feature,
                [
                    'class'     => 'autosubmit',
                    'label'     => $label,
                    'disabled'  => $disabled,
                    'value'     => (bool) $this->featureStatus[$feature]
                ]
            );
            $decorator->decorate($this->getElement($feature));
        }
    }

    protected function assembleSubmitButton()
    {
    }

    protected function getCommands(Traversable $objects): Traversable
    {
        foreach ($this->features as $feature => $spec) {
            $featureState = $this->getElement($feature)->isChecked();

            if ((int) $featureState === (int) $this->featureStatus[$feature]) {
                continue;
            }

            $command = new ToggleInstanceFeatureCommand();
            $command->setFeature($feature);
            $command->setEnabled($featureState);

            $this->submittedFeatures[] = $command;

            yield $command;
        }
    }
}