File: CleanupNodeForm.php

package info (click to toggle)
icingaweb2-module-businessprocess 2.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: php: 10,971; javascript: 2,019; sh: 115; xml: 49; makefile: 18
file content (74 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download | duplicates (2)
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
<?php

namespace Icinga\Module\Businessprocess\Forms;

use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Session\SessionNamespace;
use ipl\Html\Html;
use ipl\Sql\Connection as IcingaDbConnection;

class CleanupNodeForm extends BpConfigBaseForm
{
    /** @var MonitoringBackend|IcingaDbConnection */
    protected $backend;

    /** @var BpConfig */
    protected $bp;

    /** @var SessionNamespace */
    protected $session;

    public function setup()
    {
        $this->addHtml(Html::tag('h2', $this->translate('Cleanup missing nodes')));

        $this->addElement('checkbox', 'cleanup_all', [
            'class'         => 'autosubmit',
            'label'         => $this->translate('Cleanup all missing nodes'),
            'description'   => $this->translate('Remove all missing nodes from config')
        ]);

        if ($this->getSentValue('cleanup_all') !== '1') {
            $this->addElement('multiselect', 'nodes', [
                    'label'        => $this->translate('Select nodes to cleanup'),
                    'required'     => true,
                    'size'         => 8,
                    'multiOptions' => $this->bp->getMissingChildren()
            ]);
        }
    }

    public function onSuccess()
    {
        $changes = ProcessChanges::construct($this->bp, $this->session);

        /** @var string[] $nodesToCleanup */
        $nodesToCleanup = $this->getValue('cleanup_all') === '1'
            ? array_keys($this->bp->getMissingChildren())
            : $this->getValue('nodes');

        $nodeName = null;
        foreach ($nodesToCleanup as $nodeName) {
            $node = $this->bp->getNode($nodeName);
            $changes->deleteNode($node);
        }


        $count = count($nodesToCleanup);
        $this->setSuccessMessage(sprintf(
            $this->translatePlural(
                'Successfully removed missing node %s',
                'Successfully removed %d missing nodes',
                $count
            ),
            $count === 1 ? $nodeName : $count
        ));

        unset($changes);

        parent::onSuccess();
    }
}