Controller: Apply the user chosen sort rules when in compact view as well

This commit is contained in:
Johannes Meyer 2015-05-15 12:33:41 +02:00
parent 3b7f27f614
commit e04655e8cf

View File

@ -22,44 +22,56 @@ class Controller extends ModuleActionController
public function init() public function init()
{ {
parent::init(); parent::init();
$this->handleSortControlSubmit();
}
/**
* Check whether the sort control has been submitted and redirect using GET parameters
*/
protected function handleSortControlSubmit()
{
$request = $this->getRequest(); $request = $this->getRequest();
$url = Url::fromRequest(); if (! $request->isPost()) {
return;
}
if ($request->isPost() && ($sort = $request->getPost('sort'))) { if (($sort = $request->getPost('sort'))) {
$url = Url::fromRequest();
$url->setParam('sort', $sort); $url->setParam('sort', $sort);
if ($dir = $request->getPost('dir')) { if (($dir = $request->getPost('dir'))) {
$url->setParam('dir', $dir); $url->setParam('dir', $dir);
} else { } else {
$url->removeParam('dir'); $url->removeParam('dir');
} }
$this->redirectNow($url); $this->redirectNow($url);
} }
} }
/** /**
* Create a SortBox widget at the `sortBox' view property * Create a SortBox widget and apply its sort rules on the given query
* *
* In case the current view has been requested as compact this method does nothing. * The widget is set on the `sortBox' view property only if the current view has not been requested as compact
* *
* @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 * @param Sortable $query Query to apply the user chosen sort rules on
* *
* @return $this * @return $this
*/ */
protected function setupSortControl(array $columns, Sortable $query = null) protected function setupSortControl(array $columns, Sortable $query = null)
{ {
if (! $this->view->compact) { $request = $this->getRequest();
$req = $this->getRequest(); $sortBox = SortBox::create('sortbox-' . $request->getActionName(), $columns);
$this->view->sortBox = $sortBox = SortBox::create( $sortBox->setRequest($request);
'sortbox-' . $req->getActionName(),
$columns if ($query) {
)->setRequest($req);
if ($query !== null) {
$sortBox->setQuery($query); $sortBox->setQuery($query);
$sortBox->handleRequest($request);
} }
$sortBox->handleRequest();
if (! $this->view->compact) {
$this->view->sortBox = $sortBox;
} }
return $this; return $this;