Merge branch 'feature/ido-resource-validation-9203'

fixes #9214
resolves #9203
This commit is contained in:
Alexander Fuhr 2015-05-15 15:59:18 +02:00
commit 2cbcea25cb
6 changed files with 292 additions and 6 deletions

View File

@ -38,6 +38,28 @@ class Form extends Zend_Form
*/
const DEFAULT_SUFFIX = '_default';
/**
* The type of the notification for the error
*/
const NOTIFICATION_ERROR = 0;
/**
* The type of the notification for the warning
*/
const NOTIFICATION_WARNING = 2;
/**
* The type of the notification for the info
*/
const NOTIFICATION_INFO = 4;
/**
* The notifications of the form
*
* @var array
*/
protected $notifications = array();
/**
* Whether this form has been created
*
@ -1009,6 +1031,7 @@ class Form extends Zend_Form
}
$this->addDecorator('FormErrors', array('onlyCustomFormErrors' => true))
->addDecorator('FormNotifications')
->addDecorator('FormDescriptions')
->addDecorator('FormElements')
//->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
@ -1208,4 +1231,57 @@ class Form extends Zend_Form
throw new SecurityException('No permission for %s', $permission);
}
}
/**
* Return all form notifications
*
* @return array
*/
public function getNotifications()
{
return $this->notifications;
}
/**
* Add a typed message to the notifications
*
* @param string $message The message which would be displayed to the user
*
* @param int $type The type of the message notification
*/
public function addNotification($message, $type = self::NOTIFICATION_ERROR)
{
$this->notifications[$message] = $type;
$this->markAsError();
}
/**
* Add a error message to notifications
*
* @param string $message
*/
public function error($message)
{
$this->addNotification($message, $type = self::NOTIFICATION_ERROR);
}
/**
* Add a warning message to notifications
*
* @param string $message
*/
public function warning($message)
{
$this->addNotification($message, $type = self::NOTIFICATION_WARNING);
}
/**
* Add a info message to notifications
*
* @param string $message
*/
public function info($message)
{
$this->addNotification($message, $type = self::NOTIFICATION_INFO);
}
}

View File

@ -0,0 +1,95 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Form\Decorator;
use Zend_Form_Decorator_Abstract;
use Icinga\Web\Form as Form;
/**
* Decorator to add a list of notifications at the top of a form
*/
class FormNotifications extends Zend_Form_Decorator_Abstract
{
/**
* Render form descriptions
*
* @param string $content The html rendered so far
*
* @return string The updated html
*/
public function render($content = '')
{
$form = $this->getElement();
if (! $form instanceof Form) {
return $content;
}
$view = $form->getView();
if ($view === null) {
return $content;
}
$notifications = $this->recurseForm($form);
if (empty($notifications)) {
return $content;
}
$html = '<ul class="form-notifications">';
asort($notifications);
foreach ($notifications as $message => $type) {
$html .= '<li class="'.self::getNotificationTypeName($type).'">' . $view->escape($message) . '</li>';
}
switch ($this->getPlacement()) {
case self::APPEND:
return $content . $html . '</ul>';
case self::PREPEND:
return $html . '</ul>' . $content;
}
}
/**
* Recurse the given form and return the notifications for it and all of its subforms
*
* @param Form $form The form to recurse
*
* @return array
*/
protected function recurseForm(Form $form)
{
$notifications = $form->getNotifications();
foreach ($form->getSubForms() as $subForm) {
$notifications = $notifications + $this->recurseForm($subForm);
}
return $notifications;
}
/**
* Get the readable type name of the notification
*
* @param $type Type of the message
*
* @return string
*/
public static function getNotificationTypeName($type)
{
switch ($type) {
case Form::NOTIFICATION_ERROR:
return 'error';
break;
case Form::NOTIFICATION_WARNING:
return 'warning';
break;
case Form::NOTIFICATION_INFO:
return 'info';
break;
default:
return 'unknown';
}
}
}

View File

@ -3,6 +3,10 @@
namespace Icinga\Module\Monitoring\Forms\Config;
use Exception;
use Icinga\Data\ConfigObject;
use Icinga\Data\ResourceFactory;
use Icinga\Web\Form;
use InvalidArgumentException;
use Icinga\Application\Config;
use Icinga\Exception\ConfigurationError;
@ -271,4 +275,63 @@ class BackendConfigForm extends ConfigForm
)
);
}
/**
* Validate the ido instance schema resource
*
* @param Form $form
* @param ConfigObject $resourceConfig
*
* @return bool Whether validation succeeded or not
*/
public static function isValidIdoSchema(Form $form, ConfigObject $resourceConfig)
{
try {
$resource = ResourceFactory::createResource($resourceConfig);
$result = $resource->select()->from('icinga_dbversion', array('version'));
$result->fetchOne();
} catch (Exception $e) {
$form->addError(
$form->translate(
'IDO schema validation failed, it looks like that the IDO schema is missing in the given database.'
)
);
return false;
}
return true;
}
/**
* Validate the ido instance availability
*
* @param Form $form
* @param ConfigObject $resourceConfig
*
* @return bool Whether validation succeeded or not
*/
public static function isValidIdoInstance(Form $form, ConfigObject $resourceConfig)
{
$resource = ResourceFactory::createResource($resourceConfig);
$result = $resource->select()->from('icinga_instances', array('instance_name'));
$instances = $result->fetchAll();
if (count($instances) === 1) {
return true;
} elseif (count($instances) > 1) {
$form->warning(
$form->translate(
'IDO instance validation failed, because there are multiple instances available.'
)
);
return false;
}
$form->error(
$form->translate(
'IDO instance validation failed, because there is no IDO instance available.'
)
);
return false;
}
}

View File

@ -3,6 +3,8 @@
namespace Icinga\Module\Monitoring\Forms\Setup;
use Icinga\Data\ConfigObject;
use Icinga\Module\Monitoring\Forms\Config\BackendConfigForm;
use Icinga\Web\Form;
use Icinga\Forms\Config\Resource\DbResourceForm;
@ -53,8 +55,21 @@ class IdoResourcePage extends Form
}
if (false === isset($data['skip_validation']) || $data['skip_validation'] == 0) {
if (false === DbResourceForm::isValidResource($this)) {
$this->addSkipValidationCheckbox();
$configObject = new ConfigObject($this->getValues());
if (false === DbResourceForm::isValidResource($this, $configObject)) {
$this->addSkipValidationCheckbox($this->translate(
'Check this to not to validate connectivity with the given database server'
));
return false;
} elseif (false === BackendConfigForm::isValidIdoSchema($this, $configObject)) {
$this->addSkipValidationCheckbox($this->translate(
'Check this to not to validate the ido schema'
));
return false;
} elseif (false === BackendConfigForm::isValidIdoInstance($this, $configObject)) {
$this->addSkipValidationCheckbox($this->translate(
'Check this to not to validate the ido instance'
));
return false;
}
}
@ -65,17 +80,21 @@ class IdoResourcePage extends Form
/**
* Add a checkbox to the form by which the user can skip the connection validation
*/
protected function addSkipValidationCheckbox()
protected function addSkipValidationCheckbox($description = '')
{
if (empty($description)) {
$description = $this->translate(
'Proceed without any further (custom) validation'
);
}
$this->addElement(
'checkbox',
'skip_validation',
array(
'required' => true,
'label' => $this->translate('Skip Validation'),
'description' => $this->translate(
'Check this to not to validate connectivity with the given database server'
)
'description' => $description
)
);
}

View File

@ -23,6 +23,9 @@
@colorUnreachableHandled: #cc77ff;
@colorPending: #77aaff;
@colorInvalid: #999;
@colorFormNotificationInfo: #77aaff;
@colorFormNotificationWarning: #ffaa44;
@colorFormNotificationError: #ff5566;
/* Mixins */

View File

@ -87,6 +87,10 @@ button::-moz-focus-inner {
outline: 0;
}
button {
cursor: pointer;
}
select::-moz-focus-inner {
border: 0;
outline: 0;
@ -165,6 +169,32 @@ form ul.form-errors {
}
}
form ul.form-notifications {
.non-list-like-list;
margin-bottom: 1em;
padding: 0em;
li.info {
background: @colorFormNotificationInfo;
}
li.warning {
background: @colorFormNotificationWarning;
}
li.error {
background: @colorFormNotificationError;
}
li {
color: white;
font-weight: bold;
line-height: 1.5em;
padding: 0.5em;
margin-bottom: 0.5em;
}
}
form div.element {
margin: 0.5em 0;
}