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

namespace Icinga\Module\Eventdb\Forms\Config;

use Exception;
use Icinga\Data\ResourceFactory;
use Icinga\Forms\ConfigForm;

/**
 * Form for managing the connection to the EventDB backend
 */
class BackendConfigForm extends ConfigForm
{
    /**
     * {@inheritdoc}
     */
    public function init()
    {
        $this->setSubmitLabel($this->translate('Save'));
    }

    /**
     * {@inheritdoc}
     */
    public function createElements(array $formData)
    {
        $resources = array();
        foreach (ResourceFactory::getResourceConfigs() as $name => $config) {
            if ($config->type === 'db') {
                $resources[] = $name;
            }
        }

        $this->addElement(
            'select',
            'backend_resource',
            array(
                'description'   => $this->translate('The resource to use'),
                'label'         => $this->translate('Resource'),
                'multiOptions'  => array_combine($resources, $resources),
                'required'      => true
            )
        );

        if (isset($formData['skip_validation']) && $formData['skip_validation']) {
            $this->addSkipValidationCheckbox();
        }
    }

    /**
     * Return whether the given values are valid
     *
     * @param   array   $formData   The data to validate
     *
     * @return  bool
     */
    public function isValid($formData)
    {
        if (! parent::isValid($formData)) {
            return false;
        }

        if (($el = $this->getElement('skip_validation')) === null || ! $el->isChecked()) {
            $resourceConfig = ResourceFactory::getResourceConfig($this->getValue('backend_resource'));

            if (! $this->isValidEventDbSchema($resourceConfig)) {
                if ($el === null) {
                    $this->addSkipValidationCheckbox();
                }

                return false;
            }
        }

        return true;
    }

    public function isValidEventDbSchema($resourceConfig)
    {
        try {
            $db = ResourceFactory::createResource($resourceConfig);
            $db->select()->from('event', array('id'))->fetchOne();
        } catch (Exception $_) {
            $this->error($this->translate(
                'Cannot find the EventDB schema. Please verify that the given database '
                . 'contains the schema and that the configured user has access to it.'
            ));
            return false;
        }
        return true;
    }

    /**
     * Add a checkbox to the form by which the user can skip the schema validation
     */
    protected function addSkipValidationCheckbox()
    {
        $this->addElement(
            'checkbox',
            'skip_validation',
            array(
                'description'   => $this->translate(
                    'Check this to not to validate the EventDB schema of the chosen resource'
                ),
                'ignore'        => true,
                'label'         => $this->translate('Skip Validation'),
                'order'         => 0
            )
        );
    }
}