summaryrefslogtreecommitdiffstats
path: root/application/forms/DeleteNodeForm.php
blob: dada9d31c10dc6d6c66fa855fc16215820fe72cf (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
155
156
157
158
159
160
161
162
163
164
165
<?php

namespace Icinga\Module\Businessprocess\Forms;

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

class DeleteNodeForm extends QuickForm
{
    /** @var MonitoringBackend|IcingaDbConnection */
    protected $backend;

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

    /** @var Node */
    protected $node;

    /** @var BpNode */
    protected $parentNode;

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

    public function setup()
    {
        $node = $this->node;
        $view = $this->getView();
        $this->addHtml(
            '<h2>' . $view->escape(
                sprintf($this->translate('Delete "%s"'), $node->getAlias())
            ) . '</h2>'
        );

        $biLink = $view->qlink(
            $node->getAlias(),
            'businessprocess/node/impact',
            array('name' => $node->getName()),
            array('data-base-target' => '_next')
        );
        $this->addHtml(
            '<p>' . sprintf(
                $view->escape(
                    $this->translate('Unsure? Show business impact of "%s"')
                ),
                $biLink
            ) . '</p>'
        );

        if ($this->parentNode) {
            $yesMsg = sprintf(
                $this->translate('Delete from %s'),
                $this->parentNode->getAlias()
            );
        } else {
            $yesMsg = sprintf(
                $this->translate('Delete root node "%s"'),
                $this->node->getAlias()
            );
        }

        $this->addElement('select', 'confirm', array(
            'label'        => $this->translate('Are you sure?'),
            'required'     => true,
            'description'  => $this->translate(
                'Do you really want to delete this node?'
            ),
            'multiOptions' => $this->optionalEnum(array(
                'no'  => $this->translate('No'),
                'yes' => $yesMsg,
                'all' => sprintf($this->translate('Delete all occurrences of %s'), $node->getAlias()),
            ))
        ));
    }

    /**
     * @param MonitoringBackend|IcingaDbConnection $backend
     * @return $this
     */
    public function setBackend($backend)
    {
        $this->backend = $backend;
        return $this;
    }

    /**
     * @param BpConfig $process
     * @return $this
     */
    public function setProcess(BpConfig $process)
    {
        $this->bp = $process;
        $this->setBackend($process->getBackend());
        return $this;
    }

    /**
     * @param Node $node
     * @return $this
     */
    public function setNode(Node $node)
    {
        $this->node = $node;
        return $this;
    }

    /**
     * @param BpNode|null $node
     * @return $this
     */
    public function setParentNode(BpNode $node = null)
    {
        $this->parentNode = $node;
        return $this;
    }

    /**
     * @param SessionNamespace $session
     * @return $this
     */
    public function setSession(SessionNamespace $session)
    {
        $this->session = $session;
        return $this;
    }

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

        $confirm = $this->getValue('confirm');
        switch ($confirm) {
            case 'yes':
                $changes->deleteNode($this->node, $this->parentNode === null ? null : $this->parentNode->getName());
                break;
            case 'all':
                $changes->deleteNode($this->node);
                break;
            case 'no':
                $this->setSuccessMessage($this->translate('Well, maybe next time'));
        }

        switch ($confirm) {
            case 'yes':
            case 'all':
                if ($this->successUrl === null) {
                    $this->successUrl = clone $this->getRequest()->getUrl();
                }

                $this->successUrl->getParams()->remove(array('action', 'deletenode'));
        }

        // Trigger session desctruction to make sure it get's stored.
        // TODO: figure out why this is necessary, might be an unclean shutdown on redirect
        unset($changes);

        parent::onSuccess();
    }
}