summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/forms/Config/TransportReorderForm.php
blob: f3efe4c488d68b0c5a86aab7dfead37b38788fd0 (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
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Forms\Config;

use Icinga\Application\Config;
use Icinga\Web\Form;
use Icinga\Web\Notification;

/**
 * Form for reordering command transports
 */
class TransportReorderForm extends Form
{
    /**
     * Initialize this form
     */
    public function init()
    {
        $this->setName('form_reorder_command_transports');
        $this->setViewScript('form/reorder-command-transports.phtml');
    }

    /**
     * {@inheritdoc}
     */
    public function createElements(array $formData)
    {
        // This adds just a dummy element to be able to utilize Form::getValue as part of onSuccess()
        $this->addElement(
            'hidden',
            'transport_newpos',
            array(
                'required' => true,
                'validators' => array(
                    array(
                        'validator' => 'regex',
                        'options' => array(
                            'pattern' => '/\A\d+\|/'
                        )
                    )
                )
            )
        );
    }

    /**
     * Update the command transport order and save the configuration
     */
    public function onSuccess()
    {
        list($position, $transportName) = explode('|', $this->getValue('transport_newpos'), 2);
        $config = $this->getConfig();
        if (! $config->hasSection($transportName)) {
            Notification::error(sprintf($this->translate('Command transport "%s" not found'), $transportName));
            return false;
        }

        if ($config->count() > 1) {
            $sections = $config->keys();
            array_splice($sections, array_search($transportName, $sections, true), 1);
            array_splice($sections, $position, 0, array($transportName));

            $sectionsInNewOrder = array();
            foreach ($sections as $section) {
                $sectionsInNewOrder[$section] = $config->getSection($section);
                $config->removeSection($section);
            }
            foreach ($sectionsInNewOrder as $name => $options) {
                $config->setSection($name, $options);
            }

            $config->saveIni();
            Notification::success($this->translate('Command transport order updated'));
        }
    }

    /**
     * Get the command transports config
     *
     * @return Config
     */
    public function getConfig()
    {
        return Config::module('monitoring', 'commandtransports');
    }
}