diff --git a/library/Icinga/Web/Controller.php b/library/Icinga/Web/Controller.php index 42bd94b5e..4f3fb5794 100644 --- a/library/Icinga/Web/Controller.php +++ b/library/Icinga/Web/Controller.php @@ -15,24 +15,50 @@ use Icinga\Web\Widget\Limiter; */ 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 * * 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 + * @param Sortable $query Query to set on the newly created SortBox * * @return $this */ - protected function setupSortControl(array $columns) + protected function setupSortControl(array $columns, Sortable $query = null) { if (! $this->view->compact) { $req = $this->getRequest(); - $this->view->sortBox = SortBox::create( + $this->view->sortBox = $sortBox = SortBox::create( 'sortbox-' . $req->getActionName(), $columns - )->applyRequest($req); + )->setRequest($req); + if ($query !== null) { + $sortBox->setQuery($query); + } + $sortBox->handleRequest(); } return $this; diff --git a/library/Icinga/Web/Widget/SortBox.php b/library/Icinga/Web/Widget/SortBox.php index fc2025e5b..022efdf32 100644 --- a/library/Icinga/Web/Widget/SortBox.php +++ b/library/Icinga/Web/Widget/SortBox.php @@ -5,6 +5,8 @@ namespace Icinga\Web\Widget; use Icinga\Web\Form; use Icinga\Web\Request; +use Icinga\Data\Sortable; +use Icinga\Application\Icinga; /** * SortBox widget @@ -14,7 +16,7 @@ use Icinga\Web\Request; * 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 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. * * Example: @@ -23,7 +25,7 @@ use Icinga\Web\Request; * $this->getRequest()->getActionName(), * $columns * ); - * $this->view->sortControl->applyRequest($this->getRequest()); + * $this->view->sortControl->setRequest($this->getRequest()); * */ class SortBox extends AbstractWidget @@ -49,6 +51,13 @@ class SortBox extends AbstractWidget */ protected $request; + /** + * What to apply sort parameters on + * + * @var Sortable + */ + protected $query = null; + /** * Create a SortBox with the entries from $sortFields * @@ -81,12 +90,36 @@ class SortBox extends AbstractWidget * * @return $this */ - public function applyRequest($request) + public function setRequest($request) { $this->request = $request; 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 * diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 518bd6528..ece98d28c 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -20,6 +20,7 @@ class Monitoring_ListController extends Controller */ public function init() { + parent::init(); $this->createTabs(); } @@ -41,25 +42,6 @@ class Monitoring_ListController extends Controller 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) * @@ -75,10 +57,6 @@ class Monitoring_ListController extends Controller */ public function hostsAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - // Handle soft and hard states if (strtolower($this->params->shift('stateType', 'soft')) === 'hard') { $stateColumn = 'host_hard_state'; @@ -141,7 +119,7 @@ class Monitoring_ListController extends Controller 'host_display_name' => $this->translate('Hostname'), 'host_address' => $this->translate('Address'), 'host_last_check' => $this->translate('Last Check') - )); + ), $query); } /** @@ -149,10 +127,6 @@ class Monitoring_ListController extends Controller */ public function servicesAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - // Handle soft and hard states if (strtolower($this->params->shift('stateType', 'soft')) === 'hard') { $stateColumn = 'service_hard_state'; @@ -221,7 +195,7 @@ class Monitoring_ListController extends Controller 'host_display_name' => $this->translate('Hostname'), 'host_address' => $this->translate('Host Address'), 'host_last_check' => $this->translate('Last Host Check') - )); + ), $query); $this->view->stats = $this->backend->select()->from('statusSummary', array( 'services_total', @@ -247,10 +221,6 @@ class Monitoring_ListController extends Controller */ public function downtimesAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - $this->addTitleTab('downtimes', $this->translate('Downtimes'), $this->translate('List downtimes')); $this->setAutorefreshInterval(12); @@ -291,7 +261,7 @@ class Monitoring_ListController extends Controller 'downtime_scheduled_start' => $this->translate('Scheduled Start'), 'downtime_scheduled_end' => $this->translate('Scheduled End'), 'downtime_duration' => $this->translate('Duration') - )); + ), $query); if ($this->Auth()->hasPermission('monitoring/command/downtime/delete')) { $this->view->delDowntimeForm = new DeleteDowntimeCommandForm(); @@ -304,10 +274,6 @@ class Monitoring_ListController extends Controller */ public function notificationsAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - $this->addTitleTab( 'notifications', $this->translate('Notifications'), @@ -332,15 +298,11 @@ class Monitoring_ListController extends Controller $this->setupPaginationControl($this->view->notifications); $this->setupSortControl(array( 'notification_start_time' => $this->translate('Notification Start') - )); + ), $query); } public function contactsAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - $this->addTitleTab('contacts', $this->translate('Contacts'), $this->translate('List contacts')); $query = $this->backend->select()->from('contact', array( @@ -375,14 +337,11 @@ class Monitoring_ListController extends Controller 'contact_pager' => $this->translate('Pager Address / Number'), 'contact_notify_service_timeperiod' => $this->translate('Service Notification Timeperiod'), 'contact_notify_host_timeperiod' => $this->translate('Host Notification Timeperiod') - )); + ), $query); } public function eventgridAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } $this->addTitleTab('eventgrid', $this->translate('Event Grid'), $this->translate('Show the Event Grid')); $form = new StatehistoryForm(); @@ -422,10 +381,6 @@ class Monitoring_ListController extends Controller public function contactgroupsAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - $this->addTitleTab( 'contactgroups', $this->translate('Contact Groups'), @@ -460,15 +415,11 @@ class Monitoring_ListController extends Controller $this->setupSortControl(array( 'contactgroup_name' => $this->translate('Contactgroup Name'), 'contactgroup_alias' => $this->translate('Contactgroup Alias') - )); + ), $query); } public function commentsAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - $this->addTitleTab('comments', $this->translate('Comments'), $this->translate('List comments')); $this->setAutorefreshInterval(12); @@ -498,7 +449,8 @@ class Monitoring_ListController extends Controller 'service_display_name' => $this->translate('Service'), 'comment_type' => $this->translate('Comment Type'), 'comment_expiration' => $this->translate('Expiration') - ) + ), + $query ); if ($this->Auth()->hasPermission('monitoring/command/comment/delete')) { @@ -509,10 +461,6 @@ class Monitoring_ListController extends Controller public function servicegroupsAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - $this->addTitleTab( 'servicegroups', $this->translate('Service Groups'), @@ -563,15 +511,11 @@ class Monitoring_ListController extends Controller 'services_critical' => $this->translate('Services CRITICAL'), 'services_warning' => $this->translate('Services WARNING'), 'services_pending' => $this->translate('Services PENDING') - )); + ), $query); } public function hostgroupsAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - $this->addTitleTab('hostgroups', $this->translate('Host Groups'), $this->translate('List host groups')); $this->setAutorefreshInterval(12); @@ -618,15 +562,11 @@ class Monitoring_ListController extends Controller 'services_critical' => $this->translate('Services CRITICAL'), 'services_warning' => $this->translate('Services WARNING'), 'services_pending' => $this->translate('Services PENDING') - )); + ), $query); } public function eventhistoryAction() { - if ($url = $this->hasBetterUrl()) { - return $this->redirectNow($url); - } - $this->addTitleTab( 'eventhistory', $this->translate('Event Overview'), @@ -654,14 +594,11 @@ class Monitoring_ListController extends Controller $this->setupPaginationControl($this->view->history); $this->setupSortControl(array( 'timestamp' => $this->translate('Occurence') - )); + ), $query); } 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->setAutorefreshInterval(15); $query = $this->backend->select()->from('serviceStatus', array( @@ -675,7 +612,7 @@ class Monitoring_ListController extends Controller $this->setupSortControl(array( 'host_name' => $this->translate('Hostname'), 'service_description' => $this->translate('Service description') - )); + ), $query); $pivot = $query->pivot('service_description', 'host_name'); $this->view->pivot = $pivot; $this->view->horizontalPaginator = $pivot->paginateXAxis(); @@ -697,9 +634,6 @@ class Monitoring_ListController extends Controller $this->setupFilterControl($editor); $this->view->filter = $editor->getFilter(); - if ($sort = $this->params->get('sort')) { - $query->order($sort, $this->params->get('dir')); - } $this->handleFormatRequest($query); return $query; }