2013-09-03 19:01:21 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-09-03 19:01:21 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web\Widget;
|
|
|
|
|
2015-07-28 12:03:50 +02:00
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
use Icinga\Data\Sortable;
|
|
|
|
use Icinga\Data\SortRules;
|
2013-09-03 19:01:21 +02:00
|
|
|
use Icinga\Web\Form;
|
|
|
|
use Icinga\Web\Request;
|
|
|
|
|
|
|
|
/**
|
2015-04-15 14:20:36 +02:00
|
|
|
* SortBox widget
|
2013-09-03 19:01:21 +02:00
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* 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.
|
2013-09-03 19:01:21 +02:00
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* The constructor takes an string for the component name and an array containing the select options, where the key is
|
2015-05-11 14:07:57 +02:00
|
|
|
* the value to be submitted and the value is the label that will be shown. You then should call setRequest in order
|
2015-04-15 14:20:36 +02:00
|
|
|
* to make sure the form is correctly populated when a request with a sort parameter is being made.
|
2013-09-03 19:01:21 +02:00
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* Example:
|
|
|
|
* <pre><code>
|
2013-09-03 19:01:21 +02:00
|
|
|
* $this->view->sortControl = new SortBox(
|
2015-04-15 14:20:36 +02:00
|
|
|
* $this->getRequest()->getActionName(),
|
2013-09-03 19:01:21 +02:00
|
|
|
* $columns
|
|
|
|
* );
|
2015-05-11 14:07:57 +02:00
|
|
|
* $this->view->sortControl->setRequest($this->getRequest());
|
2015-04-15 14:20:36 +02:00
|
|
|
* </code></pre>
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
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
|
|
|
|
*/
|
2015-04-15 14:20:36 +02:00
|
|
|
protected $sortFields;
|
2013-09-03 19:01:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the form that will be created
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-04-15 14:20:36 +02:00
|
|
|
protected $name;
|
2013-09-03 19:01:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A request object used for initial form population
|
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* @var Request
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
2015-04-15 14:20:36 +02:00
|
|
|
protected $request;
|
2013-09-03 19:01:21 +02:00
|
|
|
|
2015-05-11 15:37:00 +02:00
|
|
|
/**
|
|
|
|
* What to apply sort parameters on
|
|
|
|
*
|
|
|
|
* @var Sortable
|
|
|
|
*/
|
|
|
|
protected $query = null;
|
|
|
|
|
2013-09-03 19:01:21 +02:00
|
|
|
/**
|
|
|
|
* Create a SortBox with the entries from $sortFields
|
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* @param string $name The name for the SortBox
|
|
|
|
* @param array $sortFields An array containing the columns and their labels to be displayed in the SortBox
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
|
|
|
public function __construct($name, array $sortFields)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
$this->sortFields = $sortFields;
|
|
|
|
}
|
|
|
|
|
2015-04-07 11:32:09 +02:00
|
|
|
/**
|
2015-04-15 14:20:36 +02:00
|
|
|
* Create a SortBox
|
2015-04-07 11:32:09 +02:00
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* @param string $name The name for the SortBox
|
|
|
|
* @param array $sortFields An array containing the columns and their labels to be displayed in the SortBox
|
2015-04-07 11:32:09 +02:00
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* @return SortBox
|
2015-04-07 11:32:09 +02:00
|
|
|
*/
|
|
|
|
public static function create($name, array $sortFields)
|
|
|
|
{
|
|
|
|
return new static($name, $sortFields);
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:01:21 +02:00
|
|
|
/**
|
|
|
|
* Apply the parameters from the given request on this SortBox
|
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* @param Request $request The request to use for populating the form
|
2015-04-07 11:25:32 +02:00
|
|
|
*
|
2015-04-15 14:20:36 +02:00
|
|
|
* @return $this
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
2015-05-11 14:07:57 +02:00
|
|
|
public function setRequest($request)
|
2013-09-03 19:01:21 +02:00
|
|
|
{
|
|
|
|
$this->request = $request;
|
2015-04-07 11:25:32 +02:00
|
|
|
return $this;
|
2013-09-03 19:01:21 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 15:37:00 +02:00
|
|
|
/**
|
|
|
|
* @param Sortable $query
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setQuery(Sortable $query)
|
|
|
|
{
|
|
|
|
$this->query = $query;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-05-11 16:09:20 +02:00
|
|
|
public function handleRequest(Request $request = null)
|
|
|
|
{
|
2015-05-12 10:29:50 +02:00
|
|
|
if ($this->query !== null) {
|
|
|
|
if ($request === null) {
|
|
|
|
$request = Icinga::app()->getFrontController()->getRequest();
|
|
|
|
}
|
2015-07-28 12:03:50 +02:00
|
|
|
|
|
|
|
if (($sort = $request->getParam('sort'))) {
|
2015-05-15 10:56:33 +02:00
|
|
|
$this->query->order($sort, $request->getParam('dir'));
|
2015-07-28 12:03:50 +02:00
|
|
|
} elseif (($dir = $request->getParam('dir'))) {
|
|
|
|
$this->query->order(null, $dir);
|
2015-05-12 10:29:50 +02:00
|
|
|
}
|
2015-05-11 16:09:20 +02:00
|
|
|
}
|
2015-07-28 12:03:50 +02:00
|
|
|
|
2015-05-11 16:09:20 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-07-28 12:03:50 +02:00
|
|
|
/**
|
|
|
|
* Return the default sort rule for the query
|
|
|
|
*
|
|
|
|
* @param string $column An optional column
|
|
|
|
*
|
|
|
|
* @return array An array of two values: $column, $direction
|
|
|
|
*/
|
|
|
|
protected function getSortDefaults($column = null)
|
|
|
|
{
|
|
|
|
$direction = null;
|
|
|
|
if ($this->query !== null && $this->query instanceof SortRules) {
|
|
|
|
$sortRules = $this->query->getSortRules();
|
|
|
|
if ($column === null) {
|
|
|
|
$column = key($sortRules);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($column !== null && isset($sortRules[$column]['order'])) {
|
|
|
|
$direction = strtoupper($sortRules[$column]['order']) === Sortable::SORT_DESC ? 'desc' : 'asc';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($column, $direction);
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:01:21 +02:00
|
|
|
/**
|
2015-04-15 14:20:36 +02:00
|
|
|
* Render this SortBox as HTML
|
2013-09-03 19:01:21 +02:00
|
|
|
*
|
2013-09-04 17:12:44 +02:00
|
|
|
* @return string
|
2013-09-03 19:01:21 +02:00
|
|
|
*/
|
2014-03-17 17:04:09 +01:00
|
|
|
public function render()
|
2013-09-03 19:01:21 +02:00
|
|
|
{
|
2015-07-27 11:43:47 +02:00
|
|
|
$columnForm = new Form();
|
|
|
|
$columnForm->setTokenDisabled();
|
|
|
|
$columnForm->setName($this->name . '-column');
|
|
|
|
$columnForm->setAttrib('class', 'inline');
|
|
|
|
$columnForm->addElement(
|
2015-04-15 14:20:36 +02:00
|
|
|
'select',
|
|
|
|
'sort',
|
|
|
|
array(
|
|
|
|
'autosubmit' => true,
|
|
|
|
'label' => $this->view()->translate('Sort by'),
|
2015-07-27 11:43:47 +02:00
|
|
|
'multiOptions' => $this->sortFields,
|
|
|
|
'decorators' => array(
|
|
|
|
array('ViewHelper'),
|
|
|
|
array('Label')
|
|
|
|
)
|
2015-04-15 14:20:36 +02:00
|
|
|
)
|
|
|
|
);
|
2015-07-27 11:43:47 +02:00
|
|
|
|
|
|
|
$orderForm = new Form();
|
|
|
|
$orderForm->setTokenDisabled();
|
|
|
|
$orderForm->setName($this->name . '-order');
|
|
|
|
$orderForm->setAttrib('class', 'inline');
|
|
|
|
$orderForm->addElement(
|
2015-04-15 14:20:36 +02:00
|
|
|
'select',
|
|
|
|
'dir',
|
|
|
|
array(
|
|
|
|
'autosubmit' => true,
|
2015-07-27 11:43:47 +02:00
|
|
|
'label' => $this->view()->translate('Direction', 'sort direction'),
|
2015-04-15 14:20:36 +02:00
|
|
|
'multiOptions' => array(
|
2015-07-27 11:43:47 +02:00
|
|
|
'asc' => $this->view()->translate('Ascending', 'sort direction'),
|
|
|
|
'desc' => $this->view()->translate('Descending', 'sort direction')
|
2015-04-15 14:20:36 +02:00
|
|
|
),
|
|
|
|
'decorators' => array(
|
2015-07-27 11:43:47 +02:00
|
|
|
array('ViewHelper'),
|
|
|
|
array('Label', array('class' => 'no-js'))
|
2015-04-15 14:20:36 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-07-28 12:03:50 +02:00
|
|
|
$column = null;
|
2013-09-03 19:01:21 +02:00
|
|
|
if ($this->request) {
|
2015-07-27 11:43:47 +02:00
|
|
|
$url = $this->request->getUrl();
|
|
|
|
if ($url->hasParam('sort')) {
|
2015-07-28 12:03:50 +02:00
|
|
|
$column = $url->getParam('sort');
|
|
|
|
|
|
|
|
if ($url->hasParam('dir')) {
|
|
|
|
$direction = $url->getParam('dir');
|
|
|
|
} else {
|
|
|
|
list($_, $direction) = $this->getSortDefaults($column);
|
|
|
|
}
|
|
|
|
} elseif ($url->hasParam('dir')) {
|
|
|
|
$direction = $url->getParam('dir');
|
|
|
|
list($column, $_) = $this->getSortDefaults();
|
2015-07-27 11:43:47 +02:00
|
|
|
}
|
2015-07-28 12:03:50 +02:00
|
|
|
}
|
2015-07-27 11:43:47 +02:00
|
|
|
|
2015-07-28 12:03:50 +02:00
|
|
|
if ($column === null) {
|
|
|
|
list($column, $direction) = $this->getSortDefaults();
|
2013-09-03 19:01:21 +02:00
|
|
|
}
|
2015-04-15 14:20:36 +02:00
|
|
|
|
2015-07-28 12:03:50 +02:00
|
|
|
$columnForm->populate(array('sort' => $column));
|
|
|
|
$orderForm->populate(array('dir' => $direction));
|
2015-07-27 11:43:47 +02:00
|
|
|
return '<div class="sort-control">' . $columnForm . $orderForm . '</div>';
|
2013-09-03 19:01:21 +02:00
|
|
|
}
|
|
|
|
}
|