Merge branch 'bugfix/Centralize-submission-and-apply-handling-of-sort-rules-9220'

fixes #9220
This commit is contained in:
Alexander A. Klimov 2015-05-12 10:32:24 +02:00
commit 4bda1cf6d6
3 changed files with 79 additions and 86 deletions

View File

@ -15,24 +15,50 @@ use Icinga\Web\Widget\Limiter;
*/ */
class Controller extends ModuleActionController class Controller extends ModuleActionController
{ {
/**
* @see ActionController::init
*/
public function init()
{
parent::init();
$request = $this->getRequest();
$url = Url::fromRequest();
if ($request->isPost() && ($sort = $request->getPost('sort'))) {
$url->setParam('sort', $sort);
if ($dir = $request->getPost('dir')) {
$url->setParam('dir', $dir);
} else {
$url->removeParam('dir');
}
$this->redirectNow($url);
}
}
/** /**
* Create a SortBox widget at the `sortBox' view property * Create a SortBox widget at the `sortBox' view property
* *
* In case the current view has been requested as compact this method does nothing. * In case the current view has been requested as compact this method does nothing.
* *
* @param array $columns An array containing the sort columns, with the * @param array $columns An array containing the sort columns, with the
* submit value as the key and the label as the value * submit value as the key and the label as the value
* @param Sortable $query Query to set on the newly created SortBox
* *
* @return $this * @return $this
*/ */
protected function setupSortControl(array $columns) protected function setupSortControl(array $columns, Sortable $query = null)
{ {
if (! $this->view->compact) { if (! $this->view->compact) {
$req = $this->getRequest(); $req = $this->getRequest();
$this->view->sortBox = SortBox::create( $this->view->sortBox = $sortBox = SortBox::create(
'sortbox-' . $req->getActionName(), 'sortbox-' . $req->getActionName(),
$columns $columns
)->applyRequest($req); )->setRequest($req);
if ($query !== null) {
$sortBox->setQuery($query);
}
$sortBox->handleRequest();
} }
return $this; return $this;

View File

@ -5,6 +5,8 @@ namespace Icinga\Web\Widget;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Web\Request; use Icinga\Web\Request;
use Icinga\Data\Sortable;
use Icinga\Application\Icinga;
/** /**
* SortBox widget * SortBox widget
@ -14,7 +16,7 @@ use Icinga\Web\Request;
* submission of sorting changes and draws an additional submit button when JavaScript is disabled. * submission of sorting changes and draws an additional submit button when JavaScript is disabled.
* *
* The constructor takes an string for the component name and an array containing the select options, where the key is * The constructor takes an string for the component name and 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 * the value to be submitted and the value is the label that will be shown. You then should call setRequest in order
* to make sure the form is correctly populated when a request with a sort parameter is being made. * to make sure the form is correctly populated when a request with a sort parameter is being made.
* *
* Example: * Example:
@ -23,7 +25,7 @@ use Icinga\Web\Request;
* $this->getRequest()->getActionName(), * $this->getRequest()->getActionName(),
* $columns * $columns
* ); * );
* $this->view->sortControl->applyRequest($this->getRequest()); * $this->view->sortControl->setRequest($this->getRequest());
* </code></pre> * </code></pre>
*/ */
class SortBox extends AbstractWidget class SortBox extends AbstractWidget
@ -49,6 +51,13 @@ class SortBox extends AbstractWidget
*/ */
protected $request; protected $request;
/**
* What to apply sort parameters on
*
* @var Sortable
*/
protected $query = null;
/** /**
* Create a SortBox with the entries from $sortFields * Create a SortBox with the entries from $sortFields
* *
@ -81,12 +90,36 @@ class SortBox extends AbstractWidget
* *
* @return $this * @return $this
*/ */
public function applyRequest($request) public function setRequest($request)
{ {
$this->request = $request; $this->request = $request;
return $this; return $this;
} }
/**
* @param Sortable $query
*
* @return $this
*/
public function setQuery(Sortable $query)
{
$this->query = $query;
return $this;
}
public function handleRequest(Request $request = null)
{
if ($this->query !== null) {
if ($request === null) {
$request = Icinga::app()->getFrontController()->getRequest();
}
if ($sort = $request->getParam('sort')) {
$this->query->order($sort, $request->getParam('dir'));
}
}
return $this;
}
/** /**
* Render this SortBox as HTML * Render this SortBox as HTML
* *

View File

@ -20,6 +20,7 @@ class Monitoring_ListController extends Controller
*/ */
public function init() public function init()
{ {
parent::init();
$this->createTabs(); $this->createTabs();
} }
@ -41,25 +42,6 @@ class Monitoring_ListController extends Controller
return $query; return $query;
} }
protected function hasBetterUrl()
{
$request = $this->getRequest();
$url = Url::fromRequest();
if ($this->getRequest()->isPost()) {
if ($request->getPost('sort')) {
$url->setParam('sort', $request->getPost('sort'));
if ($request->getPost('dir')) {
$url->setParam('dir', $request->getPost('dir'));
} else {
$url->removeParam('dir');
}
return $url;
}
}
return false;
}
/** /**
* Overwrite the backend to use (used for testing) * Overwrite the backend to use (used for testing)
* *
@ -75,10 +57,6 @@ class Monitoring_ListController extends Controller
*/ */
public function hostsAction() public function hostsAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
// Handle soft and hard states // Handle soft and hard states
if (strtolower($this->params->shift('stateType', 'soft')) === 'hard') { if (strtolower($this->params->shift('stateType', 'soft')) === 'hard') {
$stateColumn = 'host_hard_state'; $stateColumn = 'host_hard_state';
@ -141,7 +119,7 @@ class Monitoring_ListController extends Controller
'host_display_name' => $this->translate('Hostname'), 'host_display_name' => $this->translate('Hostname'),
'host_address' => $this->translate('Address'), 'host_address' => $this->translate('Address'),
'host_last_check' => $this->translate('Last Check') 'host_last_check' => $this->translate('Last Check')
)); ), $query);
} }
/** /**
@ -149,10 +127,6 @@ class Monitoring_ListController extends Controller
*/ */
public function servicesAction() public function servicesAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
// Handle soft and hard states // Handle soft and hard states
if (strtolower($this->params->shift('stateType', 'soft')) === 'hard') { if (strtolower($this->params->shift('stateType', 'soft')) === 'hard') {
$stateColumn = 'service_hard_state'; $stateColumn = 'service_hard_state';
@ -221,7 +195,7 @@ class Monitoring_ListController extends Controller
'host_display_name' => $this->translate('Hostname'), 'host_display_name' => $this->translate('Hostname'),
'host_address' => $this->translate('Host Address'), 'host_address' => $this->translate('Host Address'),
'host_last_check' => $this->translate('Last Host Check') 'host_last_check' => $this->translate('Last Host Check')
)); ), $query);
$this->view->stats = $this->backend->select()->from('statusSummary', array( $this->view->stats = $this->backend->select()->from('statusSummary', array(
'services_total', 'services_total',
@ -247,10 +221,6 @@ class Monitoring_ListController extends Controller
*/ */
public function downtimesAction() public function downtimesAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab('downtimes', $this->translate('Downtimes'), $this->translate('List downtimes')); $this->addTitleTab('downtimes', $this->translate('Downtimes'), $this->translate('List downtimes'));
$this->setAutorefreshInterval(12); $this->setAutorefreshInterval(12);
@ -291,7 +261,7 @@ class Monitoring_ListController extends Controller
'downtime_scheduled_start' => $this->translate('Scheduled Start'), 'downtime_scheduled_start' => $this->translate('Scheduled Start'),
'downtime_scheduled_end' => $this->translate('Scheduled End'), 'downtime_scheduled_end' => $this->translate('Scheduled End'),
'downtime_duration' => $this->translate('Duration') 'downtime_duration' => $this->translate('Duration')
)); ), $query);
if ($this->Auth()->hasPermission('monitoring/command/downtime/delete')) { if ($this->Auth()->hasPermission('monitoring/command/downtime/delete')) {
$this->view->delDowntimeForm = new DeleteDowntimeCommandForm(); $this->view->delDowntimeForm = new DeleteDowntimeCommandForm();
@ -304,10 +274,6 @@ class Monitoring_ListController extends Controller
*/ */
public function notificationsAction() public function notificationsAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab( $this->addTitleTab(
'notifications', 'notifications',
$this->translate('Notifications'), $this->translate('Notifications'),
@ -332,15 +298,11 @@ class Monitoring_ListController extends Controller
$this->setupPaginationControl($this->view->notifications); $this->setupPaginationControl($this->view->notifications);
$this->setupSortControl(array( $this->setupSortControl(array(
'notification_start_time' => $this->translate('Notification Start') 'notification_start_time' => $this->translate('Notification Start')
)); ), $query);
} }
public function contactsAction() public function contactsAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab('contacts', $this->translate('Contacts'), $this->translate('List contacts')); $this->addTitleTab('contacts', $this->translate('Contacts'), $this->translate('List contacts'));
$query = $this->backend->select()->from('contact', array( $query = $this->backend->select()->from('contact', array(
@ -375,14 +337,11 @@ class Monitoring_ListController extends Controller
'contact_pager' => $this->translate('Pager Address / Number'), 'contact_pager' => $this->translate('Pager Address / Number'),
'contact_notify_service_timeperiod' => $this->translate('Service Notification Timeperiod'), 'contact_notify_service_timeperiod' => $this->translate('Service Notification Timeperiod'),
'contact_notify_host_timeperiod' => $this->translate('Host Notification Timeperiod') 'contact_notify_host_timeperiod' => $this->translate('Host Notification Timeperiod')
)); ), $query);
} }
public function eventgridAction() public function eventgridAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab('eventgrid', $this->translate('Event Grid'), $this->translate('Show the Event Grid')); $this->addTitleTab('eventgrid', $this->translate('Event Grid'), $this->translate('Show the Event Grid'));
$form = new StatehistoryForm(); $form = new StatehistoryForm();
@ -422,10 +381,6 @@ class Monitoring_ListController extends Controller
public function contactgroupsAction() public function contactgroupsAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab( $this->addTitleTab(
'contactgroups', 'contactgroups',
$this->translate('Contact Groups'), $this->translate('Contact Groups'),
@ -460,15 +415,11 @@ class Monitoring_ListController extends Controller
$this->setupSortControl(array( $this->setupSortControl(array(
'contactgroup_name' => $this->translate('Contactgroup Name'), 'contactgroup_name' => $this->translate('Contactgroup Name'),
'contactgroup_alias' => $this->translate('Contactgroup Alias') 'contactgroup_alias' => $this->translate('Contactgroup Alias')
)); ), $query);
} }
public function commentsAction() public function commentsAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab('comments', $this->translate('Comments'), $this->translate('List comments')); $this->addTitleTab('comments', $this->translate('Comments'), $this->translate('List comments'));
$this->setAutorefreshInterval(12); $this->setAutorefreshInterval(12);
@ -498,7 +449,8 @@ class Monitoring_ListController extends Controller
'service_display_name' => $this->translate('Service'), 'service_display_name' => $this->translate('Service'),
'comment_type' => $this->translate('Comment Type'), 'comment_type' => $this->translate('Comment Type'),
'comment_expiration' => $this->translate('Expiration') 'comment_expiration' => $this->translate('Expiration')
) ),
$query
); );
if ($this->Auth()->hasPermission('monitoring/command/comment/delete')) { if ($this->Auth()->hasPermission('monitoring/command/comment/delete')) {
@ -509,10 +461,6 @@ class Monitoring_ListController extends Controller
public function servicegroupsAction() public function servicegroupsAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab( $this->addTitleTab(
'servicegroups', 'servicegroups',
$this->translate('Service Groups'), $this->translate('Service Groups'),
@ -563,15 +511,11 @@ class Monitoring_ListController extends Controller
'services_critical' => $this->translate('Services CRITICAL'), 'services_critical' => $this->translate('Services CRITICAL'),
'services_warning' => $this->translate('Services WARNING'), 'services_warning' => $this->translate('Services WARNING'),
'services_pending' => $this->translate('Services PENDING') 'services_pending' => $this->translate('Services PENDING')
)); ), $query);
} }
public function hostgroupsAction() public function hostgroupsAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab('hostgroups', $this->translate('Host Groups'), $this->translate('List host groups')); $this->addTitleTab('hostgroups', $this->translate('Host Groups'), $this->translate('List host groups'));
$this->setAutorefreshInterval(12); $this->setAutorefreshInterval(12);
@ -618,15 +562,11 @@ class Monitoring_ListController extends Controller
'services_critical' => $this->translate('Services CRITICAL'), 'services_critical' => $this->translate('Services CRITICAL'),
'services_warning' => $this->translate('Services WARNING'), 'services_warning' => $this->translate('Services WARNING'),
'services_pending' => $this->translate('Services PENDING') 'services_pending' => $this->translate('Services PENDING')
)); ), $query);
} }
public function eventhistoryAction() public function eventhistoryAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab( $this->addTitleTab(
'eventhistory', 'eventhistory',
$this->translate('Event Overview'), $this->translate('Event Overview'),
@ -654,14 +594,11 @@ class Monitoring_ListController extends Controller
$this->setupPaginationControl($this->view->history); $this->setupPaginationControl($this->view->history);
$this->setupSortControl(array( $this->setupSortControl(array(
'timestamp' => $this->translate('Occurence') 'timestamp' => $this->translate('Occurence')
)); ), $query);
} }
public function servicegridAction() public function servicegridAction()
{ {
if ($url = $this->hasBetterUrl()) {
return $this->redirectNow($url);
}
$this->addTitleTab('servicegrid', $this->translate('Service Grid'), $this->translate('Show the Service Grid')); $this->addTitleTab('servicegrid', $this->translate('Service Grid'), $this->translate('Show the Service Grid'));
$this->setAutorefreshInterval(15); $this->setAutorefreshInterval(15);
$query = $this->backend->select()->from('serviceStatus', array( $query = $this->backend->select()->from('serviceStatus', array(
@ -675,7 +612,7 @@ class Monitoring_ListController extends Controller
$this->setupSortControl(array( $this->setupSortControl(array(
'host_name' => $this->translate('Hostname'), 'host_name' => $this->translate('Hostname'),
'service_description' => $this->translate('Service description') 'service_description' => $this->translate('Service description')
)); ), $query);
$pivot = $query->pivot('service_description', 'host_name'); $pivot = $query->pivot('service_description', 'host_name');
$this->view->pivot = $pivot; $this->view->pivot = $pivot;
$this->view->horizontalPaginator = $pivot->paginateXAxis(); $this->view->horizontalPaginator = $pivot->paginateXAxis();
@ -697,9 +634,6 @@ class Monitoring_ListController extends Controller
$this->setupFilterControl($editor); $this->setupFilterControl($editor);
$this->view->filter = $editor->getFilter(); $this->view->filter = $editor->getFilter();
if ($sort = $this->params->get('sort')) {
$query->order($sort, $this->params->get('dir'));
}
$this->handleFormatRequest($query); $this->handleFormatRequest($query);
return $query; return $query;
} }