2013-09-03 19:01:21 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-09-03 19:01:21 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-09-03 19:01:21 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* @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>
|
|
|
|
*
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
|
|
|
// {{{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
|
|
|
|
*
|
|
|
|
*/
|
2014-03-09 02:03:06 +01:00
|
|
|
class SortBox extends AbstractWidget
|
2013-09-03 19:01:21 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2013-10-17 19:48:46 +02:00
|
|
|
* @var \Icinga\Web\Request
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2013-09-04 17:12:44 +02:00
|
|
|
* @param Request $request The request to use for populating the form
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
|
|
|
public function applyRequest($request)
|
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-04 17:12:44 +02:00
|
|
|
* Create a submit button that is hidden via the ConditionalDecorator
|
|
|
|
* in order to allow sorting changes to be submitted in a JavaScript-less environment
|
2013-09-03 19:01:21 +02:00
|
|
|
*
|
2013-09-04 17:12:44 +02:00
|
|
|
* @return Zend_Form_Element_Submit The submit button that is hidden by default
|
|
|
|
* @see ConditionalDecorator
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
|
|
|
private function createFallbackSubmitButton()
|
|
|
|
{
|
|
|
|
$manualSubmitButton = new Zend_Form_Element_Submit(
|
|
|
|
array(
|
|
|
|
'name' => 'submit_' . $this->name,
|
|
|
|
'label' => 'Sort',
|
2014-03-09 02:03:06 +01:00
|
|
|
'class' => '',
|
2013-09-03 19:01:21 +02:00
|
|
|
'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
|
|
|
|
*
|
2013-09-04 17:12:44 +02:00
|
|
|
* @param Zend_View_Abstract $view
|
|
|
|
*
|
|
|
|
* @return string
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
|
|
|
public function render(Zend_View_Abstract $view)
|
|
|
|
{
|
|
|
|
$form = new Form();
|
2014-03-09 02:03:06 +01:00
|
|
|
$form->setAttrib('class', 'inline');
|
2013-09-03 19:01:21 +02:00
|
|
|
$form->setMethod('GET');
|
|
|
|
$form->setTokenDisabled();
|
|
|
|
$form->setName($this->name);
|
|
|
|
$form->addElement(
|
|
|
|
'select',
|
2013-09-04 18:21:10 +02:00
|
|
|
'sort',
|
2013-09-03 19:01:21 +02:00
|
|
|
array(
|
|
|
|
'label' => 'Sort By',
|
2014-03-09 02:03:06 +01:00
|
|
|
'multiOptions' => $this->sortFields,
|
|
|
|
'class' => 'autosubmit'
|
2013-09-03 19:01:21 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$form->addElement(
|
|
|
|
'select',
|
2013-09-04 18:21:10 +02:00
|
|
|
'dir',
|
2013-09-03 19:01:21 +02:00
|
|
|
array(
|
|
|
|
'multiOptions' => array(
|
|
|
|
'desc' => 'Desc',
|
2014-03-09 02:03:06 +01:00
|
|
|
'asc' => 'Asc',
|
2014-03-09 13:53:31 +01:00
|
|
|
),
|
2014-03-09 02:03:06 +01:00
|
|
|
'class' => 'autosubmit'
|
2013-09-03 19:01:21 +02:00
|
|
|
|
|
|
|
)
|
|
|
|
);
|
2014-03-09 02:03:06 +01:00
|
|
|
$sort = $form->getElement('sort')->setDecorators(array('ViewHelper'));
|
|
|
|
$dir = $form->getElement('dir')->setDecorators(array('ViewHelper'));
|
|
|
|
// $form->enableAutoSubmit(array('sort', 'dir'));
|
|
|
|
// $form->addElement($this->createFallbackSubmitButton());
|
2013-09-03 19:01:21 +02:00
|
|
|
|
|
|
|
if ($this->request) {
|
2013-10-17 19:48:46 +02:00
|
|
|
$form->setAction($this->request->getRequestUri());
|
2013-09-03 19:01:21 +02:00
|
|
|
$form->populate($this->request->getParams());
|
|
|
|
}
|
2014-03-09 02:03:06 +01:00
|
|
|
return $form;
|
2013-09-03 19:01:21 +02:00
|
|
|
}
|
|
|
|
}
|