/
DbResourcePage.php
118 lines (108 loc) · 3.15 KB
/
DbResourcePage.php
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
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Setup\Form;
use PDOException;
use Icinga\Web\Form;
use Icinga\Web\Form\Element\Note;
use Icinga\Form\Config\Resource\DbResourceForm;
use Icinga\Module\Setup\Utils\DbTool;
/**
* Wizard page to define connection details for a database resource
*/
class DbResourcePage extends Form
{
/**
* Initialize this page
*/
public function init()
{
$this->setName('setup_db_resource');
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$this->addElement(
'hidden',
'type',
array(
'required' => true,
'value' => 'db'
)
);
$this->addElement(
new Note(
'description',
array(
'value' => t(
'Now please configure your database resource. Note that the database itself does not need to'
. ' exist at this time as it is going to be created once the wizard is about to be finished.'
)
)
)
);
if (isset($formData['skip_validation']) && $formData['skip_validation']) {
$this->addSkipValidationCheckbox();
} else {
$this->addElement(
'hidden',
'skip_validation',
array(
'required' => true,
'value' => 0
)
);
}
$resourceForm = new DbResourceForm();
$this->addElements($resourceForm->createElements($formData)->getElements());
$this->addElement(
'hidden',
'prefix',
array(
'required' => true,
'value' => 'icingaweb_'
)
);
}
/**
* Validate the given form data and check whether it's possible to connect to the database server
*
* @param array $data The data to validate
*
* @return bool
*/
public function isValid($data)
{
if (false === parent::isValid($data)) {
return false;
}
if (false === isset($data['skip_validation']) || $data['skip_validation'] == 0) {
try {
$db = new DbTool($this->getValues());
$db->checkConnectivity();
} catch (PDOException $e) {
$this->addError($e->getMessage());
$this->addSkipValidationCheckbox();
return false;
}
}
return true;
}
/**
* Add a checkbox to the form by which the user can skip the connection validation
*/
protected function addSkipValidationCheckbox()
{
$this->addElement(
'checkbox',
'skip_validation',
array(
'required' => true,
'label' => t('Skip Validation'),
'description' => t('Check this to not to validate connectivity with the given database server')
)
);
}
}