Codestyle and Test fixes for SortBox

refs #4601
This commit is contained in:
Jannis Moßhammer 2013-09-03 19:01:21 +02:00 committed by Eric Lippmann
parent 57e66c247a
commit d27e34cef8
6 changed files with 180 additions and 8 deletions

View File

@ -328,7 +328,6 @@ namespace Icinga\Test {
if ($token !== null) {
$requestData[$form->getTokenElementName()] = $token;
}
$request = $this->getRequest();
$request->setMethod('POST');
$request->setPost($requestData);

View File

@ -35,7 +35,7 @@ use \Zend_View_Interface;
use \Icinga\Web\Form\Element\Note;
use \Icinga\Exception\ProgrammingError;
use \Icinga\Web\Form\Decorator\HelpText;
use \Icinga\Web\Form\Decorator\BoostrapFormDecorator;
use \Icinga\Web\Form\Decorator\BootstrapForm;
use \Icinga\Web\Form\InvalidCSRFTokenException;
use \Icinga\Application\Config as IcingaConfig;
@ -127,7 +127,7 @@ class Form extends Zend_Form
/**
* Decorator that replaces the DtDd Zend-Form default
*
* @var Form\Decorator\BoostrapFormDecorator
* @var Form\Decorator\BootstrapFormDecorator
*/
private $formDecorator;
@ -586,7 +586,7 @@ class Form extends Zend_Form
$el->removeDecorator('HtmlTag');
$el->removeDecorator('Label');
$el->removeDecorator('DtDdWrapper');
$el->addDecorator(new BoostrapFormDecorator());
$el->addDecorator(new BootstrapForm());
$el->setAttrib('class', $el->getAttrib('class') . ' form-control input-sm');
}

View File

@ -30,7 +30,7 @@ namespace Icinga\Web\Form\Decorator;
use Zend_Form_Decorator_Abstract;
/**
/**
* Decorator that styles forms in the DOM boostrap wants for it's forms
*
* This component replaces the dt/dd wrapping of elements with the approach used by bootstrap.
@ -40,7 +40,7 @@ use Zend_Form_Decorator_Abstract;
* cases where you want to put inputs with and inputs without labels on the same line and don't
* want buttons to 'jump'
*/
class BoostrapFormDecorator extends Zend_Form_Decorator_Abstract
class BootstrapForm extends Zend_Form_Decorator_Abstract
{
/**
* An array of elements that won't get a <label> dom added per default

View File

@ -0,0 +1,173 @@
<?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\Widget;
use Icinga\Web\Form;
use Icinga\Web\Request;
use Zend_View_Abstract;
use Icinga\Web\Form\Decorator\ConditionalHidden;
use Zend_Form_Element_Submit;
/**
* Sortbox widget
*
* The "SortBox" Widget allows you to create a generic sort input for sortable views.
* It automatically creates a form containing a select box with all sort options and a
* dropbox with the sort direction. It also handles automatic submission of sorting changes and draws an additional
* submit button when JavaScript is disabled.
*
* The constructor takes an string for the component name ad an array containing the select options, where the key is
* the value to be submitted and the value is the label that will be shown. You then should call applyRequest in order
* to make sure the form is correctly populated when a request with a sort parameter is being made.
*
* Example:
* <pre><code>
* $this->view->sortControl = new SortBox(
* $this->getRequest()->getActionName(),
* $columns
* );
* $this->view->sortControl->applyRequest($this->getRequest());
* </code></pre>
* By default the sortBox uses the GET parameter 'sort' for the sorting key and 'dir' for the sorting direction
*
*/
class SortBox implements Widget
{
/**
* An array containing all sort columns with their associated labels
*
* @var array
*/
private $sortFields;
/**
* The name of the form that will be created
*
* @var string
*/
private $name;
/**
* A request object used for initial form population
*
* @var Icinga\Web\Request
*/
private $request;
/**
* Create a SortBox with the entries from $sortFields
*
* @param string $name The name of the sort form
* @param array $sortFields An array containing the columns and their labels to be displayed
* in the sort select box
*/
public function __construct($name, array $sortFields)
{
$this->name = $name;
$this->sortFields = $sortFields;
}
/**
* Apply the parameters from the given request on this SortBox
*
* @param Request $request The request to use for populating the form
*/
public function applyRequest($request)
{
$this->request = $request;
}
/**
* Create a submitbutton that is hidden via the @see ConditionalDecorator
* in order to allow sorting changes to be sumbitted in a JavaScript-less environment
*
* @return Zend_Form_Element_Submit The submit button that is hidden by default
*/
private function createFallbackSubmitButton()
{
$manualSubmitButton = new Zend_Form_Element_Submit(
array(
'name' => 'submit_' . $this->name,
'label' => 'Sort',
'class' => 'btn btn-default',
'condition' => 0,
'value' => '{{SUBMIT_ICON}}'
)
);
$manualSubmitButton->addDecorator(new ConditionalHidden());
$manualSubmitButton->setAttrib('addLabelPlaceholder', true);
return $manualSubmitButton;
}
/**
* Renders this widget via the given view and returns the
* HTML as a string
*
* @param Zend_View_Abstract $view
* @return string
*/
public function render(Zend_View_Abstract $view)
{
$form = new Form();
$form->setAttrib('class', 'form-inline');
$form->setMethod('GET');
$form->setTokenDisabled();
$form->setName($this->name);
$form->addElement(
'select',
'sort_' . $this->name,
array(
'name' => 'sort',
'label' => 'Sort By',
'multiOptions' => $this->sortFields
)
);
$form->addElement(
'select',
'dir_' . $this->name,
array(
'name' => 'dir',
'multiOptions' => array(
'desc' => 'Desc',
'asc' => 'Asc'
)
)
);
$form->enableAutoSubmit(array('sort_' . $this->name, 'dir_' . $this->name));
$form->addElement($this->createFallbackSubmitButton());
if ($this->request) {
$form->populate($this->request->getParams());
}
return $form->render($view);
}
}

View File

@ -119,7 +119,7 @@ abstract class MonitoringControllerTest extends Zend_Test_PHPUnit_ControllerTest
require_once('Data/Db/Connection.php');
require_once('Data/Db/Query.php');
require_once('Exception/ProgrammingError.php');
require_once('Web/Widget/SortBox.php');
require_once('library/Monitoring/Backend/AbstractBackend.php');
}

View File

@ -65,7 +65,7 @@ class GeneralFormTest extends BaseTestCase
if ($child->hasAttributes() === false) {
continue;
}
if (strpos($child->attributes->item(0)->value, $value) !== false) {
if (strpos($child->attributes->getNamedItem('id')->value, $value . '-element') !== false) {
return true;
}
}