summaryrefslogtreecommitdiffstats
path: root/application/forms/Config/UserBackendReorderForm.php
blob: 019c0329080116677d7c88108e60a8f4d6e2ecbd (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Forms\Config;

use Icinga\Application\Config;
use Icinga\Forms\ConfigForm;
use Icinga\Exception\NotFoundError;
use Icinga\Web\Notification;

class UserBackendReorderForm extends ConfigForm
{
    /**
     * Initialize this form
     */
    public function init()
    {
        $this->setName('form_reorder_authbackend');
        $this->setViewScript('form/reorder-authbackend.phtml');
    }

    /**
     * Return the ordered backend names
     *
     * @return  array
     */
    public function getBackendOrder()
    {
        return $this->config->keys();
    }

    /**
     * Return the ordered backend configuration
     *
     * @return Config
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * Create and add elements to this form
     *
     * @param   array   $formData
     */
    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', 'backend_newpos');
    }

    /**
     * Update the user backend order and save the configuration
     */
    public function onSuccess()
    {
        $newPosData = $this->getValue('backend_newpos');
        if ($newPosData) {
            $configForm = $this->getConfigForm();
            list($backendName, $position) = explode('|', $newPosData, 2);

            try {
                if ($configForm->move($backendName, $position)->save()) {
                    Notification::success($this->translate('Authentication order updated'));
                } else {
                    return false;
                }
            } catch (NotFoundError $_) {
                 Notification::error(sprintf($this->translate('User backend "%s" not found'), $backendName));
            }
        }
    }

    /**
     * Return the config form for user backends
     *
     * @return  ConfigForm
     */
    protected function getConfigForm()
    {
        $form = new UserBackendConfigForm();
        $form->setIniConfig($this->config);
        return $form;
    }
}