Add form classes to handle the configuration

Add a tri-state form element to handle the configuration flags and add the form
to the controllers

refs #3788
This commit is contained in:
Matthias Jentsch 2013-10-16 18:50:19 +02:00
parent df0cb01a27
commit 1d07a766d9
8 changed files with 405 additions and 30 deletions

View File

@ -0,0 +1,58 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
/**
* Icinga 2 Web - Head for multiple monitoring frontends
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
use \Zend_View_Helper_FormElement;
/**
* Helper to generate a "datetime" element
*/
class Zend_View_Helper_FormTriStateCheckbox extends Zend_View_Helper_FormElement
{
/**
* Generate a tri-state checkbox
*
* @param string $name The element name
* @param int $value The checkbox value
* @param array $attribs Attributes for the element tag
*
* @return string The element XHTML
*/
public function formTriStateCheckbox($name, $value = null, $attribs = null)
{
$class = "";
$xhtml = '<div data-icinga-component="app/triStateCheckbox" class="form-group">'
. '<div>' . ($value == 1 ? '{{ICON_ENABLED}}' : ($value == 0 ? '{{ICON_DISABLED}}' : '{{ICON_BLANK}}' )) . '</div>'
. '<input class="' . $class . '" type="radio" value=1 name="'
. $name . '" ' . ($value == 1 ? 'checked' : '') . ' ">On</input> '
. '<input class="' . $class . '" type="radio" value=0 name="'
. $name . '" ' . ($value == 0 ? 'checked' : '') . ' ">Off</input> '
. '<input class="' . $class . '" type="radio" value="unchanged" name="'
. $name . '" ' . ($value === 'unchanged' ? 'checked' : ' disabled="disabled" ') . ' . "> Mixed </input>'
. '</div>';
return $xhtml;
}
}

View File

@ -0,0 +1,57 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Form\Element;
use \Icinga\Web\Form\Validator\TriStateValidator;
use \Zend_Form_Element_Xhtml;
/**
* A checkbox that can display three different states:
* true, false and mixed. When there is no JavaScript
* available to display the checkbox properly, a radio
* button-group with all three possible states will be
* displayed.
*/
class TriStateCheckbox extends Zend_Form_Element_Xhtml
{
/**
* Name of the view helper
*
* @var string
*/
public $helper = 'formTriStateCheckbox';
public function __construct($spec, $options = null)
{
parent::__construct($spec, $options);
$this->triStateValidator = new TriStateValidator($this->patterns);
$this->addValidator($this->triStateValidator);
}
}

View File

@ -0,0 +1,79 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Form\Validator;
use \Zend_Validate_Abstract;
class TriStateValidator extends Zend_Validate_Abstract {
/**
* @var null
*/
private $validPattern = null;
/**
* Validate the input value and set the value of @see validPattern if the input machtes
* a state description like '0', '1' or 'unchanged'
*
* @param string $value The value to validate
* @param null $context The form context (ignored)
*
* @return bool True when the input is valid, otherwise false
*
* @see Zend_Validate_Abstract::isValid()
*/
public function isValid($value, $context = null)
{
if (!is_string($value) && !is_int($value)) {
$this->error('INVALID_TYPE');
return false;
}
if (is_string($value)) {
$value = intval($value);
if ($value === 'unchanged') {
$this->validPattern = null;
return true;
}
}
if (is_int($value)) {
if ($value === 1 || $value === 0) {
$this->validPattern = $value;
return true;
}
}
return false;
}
public function getValidPattern()
{
return $this->validPattern;
}
}

View File

@ -1,5 +1,4 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
@ -31,6 +30,9 @@ use \Icinga\Web\Controller\ActionController;
use \Icinga\Module\Monitoring\Backend;
use \Icinga\Module\Monitoring\Object\Host;
use \Icinga\Module\Monitoring\Object\Service;
use \Icinga\Module\Monitoring\Form\Command\MultiCommandFlagForm;
use \Icinga\Web\Form;
/**
* Displays aggregations collections of multiple objects.
*/
@ -56,7 +58,6 @@ class Monitoring_MultiController extends ActionController
$errors[] = 'Query ' . $index . ' misses property host.';
continue;
}
$host = Host::fetch($this->backend, $query['host']);
foreach ($host->comments as $comment) {
$comments[$comment->comment_id] = null;
@ -73,6 +74,9 @@ class Monitoring_MultiController extends ActionController
$this->view->hostnames = $hostnames;
$this->view->downtimes = $downtimes;
$this->view->errors = $errors;
$this->handleConfigurationForm();
$this->view->form->setAction('/icinga2-web/monitoring/multi/host');
}
public function serviceAction()
@ -107,6 +111,9 @@ class Monitoring_MultiController extends ActionController
$this->view->comments = array_keys($comments);
$this->view->downtimes = $downtimes;
$this->view->errors = $errors;
$this->handleConfigurationForm();
$this->view->form->setAction('/icinga2-web/monitoring/multi/service');
}
public function notificationAction()
@ -119,6 +126,31 @@ class Monitoring_MultiController extends ActionController
}
/**
* Handle the form to configure configuration flags.
*/
private function handleConfigurationForm()
{
$this->view->form = $form = new MultiCommandFlagForm(
array(
'passive_checks_enabled' => 'Passive Checks',
'active_checks_enabled' => 'Active Checks',
'obsessing' => 'Obsessing',
'notifications_enabled' => 'Notifications',
'event_handler_enabled' => 'Event Handler',
'flap_detection_enabled' => 'Flap Detection'
)
);
$form->setRequest($this->_request);
if ($form->isSubmittedAndValid()) {
// TODO: Handle commands
$changed = $form->getChangedValues();
}
if ($this->_request->isPost() === false) {
$this->view->form->initFromItems($this->view->objects);
}
}
/**
* Fetch all requests from the 'detail' parameter.
*
@ -142,6 +174,4 @@ class Monitoring_MultiController extends ActionController
}
return $objects;
}
}

View File

@ -0,0 +1,171 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Command;
use \Icinga\Web\Form\Element\TriStateCheckbox;
use \Icinga\Web\Form;
use \Zend_Form_Element_Hidden;
/**
* A form to edit multiple command flags of multiple commands at once. When some commands have
* different flag values, these flags will be displayed as an undefined state,
* in a tri-state checkbox.
*/
class MultiCommandFlagForm extends Form {
/**
* The Suffix used to mark the old values in the form-field
*
* @var string
*/
const OLD_VALUE_MARKER = '_&&old';
/**
* The object properties to change
*
* @var array
*/
private $flags;
/**
* The current values of this form
*
* @var array
*/
private $values;
/**
* Create a new MultiCommandFlagForm
*
* @param array $flags The flags that will be used. Should contain the
* names of the used property keys.
*/
public function __construct(array $flags)
{
$this->flags = $flags;
parent::__construct();
}
/**
* Initialise the form values with the array of items to configure.
*
* @param array $items The items that will be edited with this form.
*/
public function initFromItems(array $items)
{
$this->values = $this->valuesFromObjects($items);
$this->buildForm();
$this->populate($this->values);
}
/**
* Return only the values that have been updated.
*/
public function getChangedValues()
{
$values = $this->getValues();
$changed = array();
foreach ($values as $key => $value) {
$oldKey = $key . self::OLD_VALUE_MARKER;
if (array_key_exists($oldKey, $values)) {
if ($values[$oldKey] !== $value) {
$changed[$key] = $value;
}
}
}
return $changed;
}
/**
* Extract the values from a set of items.
*
* @param array $items The items
*/
private function valuesFromObjects(array $items)
{
$values = array();
foreach ($items as $item) {
foreach ($this->flags as $key => $unused) {
if (isset($item->{$key})) {
$value = $item->{$key};
// convert strings
if ($value === '1' || $value === '0') {
$value = intval($value);
}
// init key with first value
if (!array_key_exists($key, $values)) {
$values[$key] = $value;
continue;
}
// already a mixed state ?
if ($values[$key] === 'unchanged') {
continue;
}
// values differ?
if ($values[$key] ^ $value) {
$values[$key] = 'unchanged';
}
}
}
}
$old = array();
foreach ($values as $key => $value) {
$old[$key . self::OLD_VALUE_MARKER] = $key;
}
return array_merge($values, $old);
}
/**
* Create the multi flag form
*
* @see Form::create()
*/
public function create()
{
$this->setName('form_flag_configuration');
foreach ($this->flags as $flag => $description) {
$this->addElement(new TriStateCheckbox(
$flag,
array(
'label' => $description,
'required' => true
)
));
$old = new Zend_Form_Element_Hidden($flag . self::OLD_VALUE_MARKER);
$this->addElement($old);
}
$this->setSubmitLabel('Save Configuration');
}
}

View File

@ -1,15 +0,0 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: mjentsch
* Date: 15.10.13
* Time: 17:47
* To change this template use File | Settings | File Templates.
*/
namespace Icinga\Module\Monitoring\Form\Command;
class MultiFlagForm extends Form {
}

View File

@ -2,14 +2,8 @@
<div class="panel-heading">
<span> Configuration </span>
</div>
<div class="panel-body">
Change settings:
<form>
Active Checks {{CHECKBOX}}
Passive Checks {{CHECKBOX}}
Flap Detection {{CHECKBOX}}
</form>
Change configuration for <?= count($this->objects) ?> objects.
<?php echo $this->form->render($this); ?>
</div>
</div>

View File

@ -2,15 +2,16 @@
$this->is_service = true;
?>
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-heading">
{{SERVICE_ICON}}
<h1> Services </h1>
<h1>Services</h1>
</div>
<div class="panel-body">
<?= $this->render('multi/components/summary.phtml'); ?>
</div>
</div>
<?= $this->render('multi/components/downtimes.phtml'); ?>