Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #7163
- Loading branch information
Johannes Meyer
committed
Sep 29, 2014
1 parent
61f0ce6
commit b782e79
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
// {{{ICINGA_LICENSE_HEADER}}} | ||
// {{{ICINGA_LICENSE_HEADER}}} | ||
|
||
namespace Icinga\Form\Setup; | ||
|
||
use PDOException; | ||
use Icinga\Web\Form; | ||
use Icinga\Web\Setup\DbTool; | ||
use Icinga\Form\Config\Resource\DbResourceForm; | ||
|
||
/** | ||
* 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( | ||
'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 when installing Icinga Web 2.' | ||
) | ||
) | ||
); | ||
|
||
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()); | ||
} | ||
|
||
/** | ||
* 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') | ||
) | ||
); | ||
} | ||
} |