1<?php
2
3namespace Icinga\Module\Businessprocess\Forms;
4
5use Icinga\Module\Businessprocess\BpNode;
6use Icinga\Module\Businessprocess\BpConfig;
7use Icinga\Module\Businessprocess\Modification\ProcessChanges;
8use Icinga\Module\Businessprocess\Node;
9use Icinga\Module\Businessprocess\Web\Form\QuickForm;
10use Icinga\Module\Monitoring\Backend\MonitoringBackend;
11use Icinga\Web\Session\SessionNamespace;
12
13class DeleteNodeForm extends QuickForm
14{
15    /** @var MonitoringBackend */
16    protected $backend;
17
18    /** @var BpConfig */
19    protected $bp;
20
21    /** @var Node */
22    protected $node;
23
24    /** @var BpNode */
25    protected $parentNode;
26
27    /** @var SessionNamespace */
28    protected $session;
29
30    public function setup()
31    {
32        $node = $this->node;
33        $view = $this->getView();
34        $this->addHtml(
35            '<h2>' . $view->escape(
36                sprintf($this->translate('Delete "%s"'), $node->getAlias())
37            ) . '</h2>'
38        );
39
40        $biLink = $view->qlink(
41            $node->getAlias(),
42            'businessprocess/node/impact',
43            array('name' => $node->getName()),
44            array('data-base-target' => '_next')
45        );
46        $this->addHtml(
47            '<p>' . sprintf(
48                $view->escape(
49                    $this->translate('Unsure? Show business impact of "%s"')
50                ),
51                $biLink
52            ) . '</p>'
53        );
54
55        if ($this->parentNode) {
56            $yesMsg = sprintf(
57                $this->translate('Delete from %s'),
58                $this->parentNode->getAlias()
59            );
60        } else {
61            $yesMsg = sprintf(
62                $this->translate('Delete root node "%s"'),
63                $this->node->getAlias()
64            );
65        }
66
67        $this->addElement('select', 'confirm', array(
68            'label'        => $this->translate('Are you sure?'),
69            'required'     => true,
70            'description'  => $this->translate(
71                'Do you really want to delete this node?'
72            ),
73            'multiOptions' => $this->optionalEnum(array(
74                'no'  => $this->translate('No'),
75                'yes' => $yesMsg,
76                'all' => sprintf($this->translate('Delete all occurrences of %s'), $node->getAlias()),
77            ))
78        ));
79    }
80
81    /**
82     * @param MonitoringBackend $backend
83     * @return $this
84     */
85    public function setBackend(MonitoringBackend $backend)
86    {
87        $this->backend = $backend;
88        return $this;
89    }
90
91    /**
92     * @param BpConfig $process
93     * @return $this
94     */
95    public function setProcess(BpConfig $process)
96    {
97        $this->bp = $process;
98        $this->setBackend($process->getBackend());
99        return $this;
100    }
101
102    /**
103     * @param Node $node
104     * @return $this
105     */
106    public function setNode(Node $node)
107    {
108        $this->node = $node;
109        return $this;
110    }
111
112    /**
113     * @param BpNode|null $node
114     * @return $this
115     */
116    public function setParentNode(BpNode $node = null)
117    {
118        $this->parentNode = $node;
119        return $this;
120    }
121
122    /**
123     * @param SessionNamespace $session
124     * @return $this
125     */
126    public function setSession(SessionNamespace $session)
127    {
128        $this->session = $session;
129        return $this;
130    }
131
132    public function onSuccess()
133    {
134        $changes = ProcessChanges::construct($this->bp, $this->session);
135
136        $confirm = $this->getValue('confirm');
137        switch ($confirm) {
138            case 'yes':
139                $changes->deleteNode($this->node, $this->parentNode === null ? null : $this->parentNode->getName());
140                break;
141            case 'all':
142                $changes->deleteNode($this->node);
143                break;
144            case 'no':
145                $this->setSuccessMessage($this->translate('Well, maybe next time'));
146        }
147
148        switch ($confirm) {
149            case 'yes':
150            case 'all':
151                if ($this->successUrl === null) {
152                    $this->successUrl = clone $this->getRequest()->getUrl();
153                }
154
155                $this->successUrl->getParams()->remove(array('action', 'deletenode'));
156        }
157
158        // Trigger session desctruction to make sure it get's stored.
159        // TODO: figure out why this is necessary, might be an unclean shutdown on redirect
160        unset($changes);
161
162        parent::onSuccess();
163    }
164}
165