Controller: Create the filter editor in setupFilterControl() ...

...instead of demanding a concrete controller to do so.
We still have to decide how to handle parameter preservation
properly.

refs #9029
This commit is contained in:
Johannes Meyer 2015-08-13 17:05:13 +02:00
parent 63fb8dcafa
commit 1e6c394693
9 changed files with 168 additions and 53 deletions

View File

@ -54,19 +54,12 @@ class GroupController extends AuthBackendController
} }
$query = $backend->select(array('group_name')); $query = $backend->select(array('group_name'));
$filterEditor = Widget::create('filterEditor')
->setQuery($query)
->setSearchColumns(array('group', 'user'))
->preserveParams('limit', 'sort', 'dir', 'view', 'backend')
->ignoreParams('page')
->handleRequest($this->getRequest());
$query->applyFilter($filterEditor->getFilter());
$this->setupFilterControl($filterEditor);
$this->view->groups = $query; $this->view->groups = $query;
$this->view->backend = $backend; $this->view->backend = $backend;
$this->setupPaginationControl($query); $this->setupPaginationControl($query);
$this->setupFilterControl($query);
$this->setupLimitControl(); $this->setupLimitControl();
$this->setupSortControl( $this->setupSortControl(
array( array(
@ -101,15 +94,7 @@ class GroupController extends AuthBackendController
->from('group_membership', array('user_name')) ->from('group_membership', array('user_name'))
->where('group_name', $groupName); ->where('group_name', $groupName);
$filterEditor = Widget::create('filterEditor') $this->setupFilterControl($members, null, array('user'));
->setQuery($members)
->setSearchColumns(array('user'))
->preserveParams('limit', 'sort', 'dir', 'view', 'backend', 'group')
->ignoreParams('page')
->handleRequest($this->getRequest());
$members->applyFilter($filterEditor->getFilter());
$this->setupFilterControl($filterEditor);
$this->setupPaginationControl($members); $this->setupPaginationControl($members);
$this->setupLimitControl(); $this->setupLimitControl();
$this->setupSortControl( $this->setupSortControl(

View File

@ -54,19 +54,12 @@ class UserController extends AuthBackendController
} }
$query = $backend->select(array('user_name')); $query = $backend->select(array('user_name'));
$filterEditor = Widget::create('filterEditor')
->setQuery($query)
->setSearchColumns(array('user'))
->preserveParams('limit', 'sort', 'dir', 'view', 'backend')
->ignoreParams('page')
->handleRequest($this->getRequest());
$query->applyFilter($filterEditor->getFilter());
$this->setupFilterControl($filterEditor);
$this->view->users = $query; $this->view->users = $query;
$this->view->backend = $backend; $this->view->backend = $backend;
$this->setupPaginationControl($query); $this->setupPaginationControl($query);
$this->setupFilterControl($query);
$this->setupLimitControl(); $this->setupLimitControl();
$this->setupSortControl( $this->setupSortControl(
array( array(
@ -100,15 +93,11 @@ class UserController extends AuthBackendController
$memberships = $this->loadMemberships(new User($userName))->select(); $memberships = $this->loadMemberships(new User($userName))->select();
$filterEditor = Widget::create('filterEditor') $this->setupFilterControl(
->setQuery($memberships) $memberships,
->setSearchColumns(array('group_name')) array('group_name' => t('User Group')),
->preserveParams('limit', 'sort', 'dir', 'view', 'backend', 'user') array('group_name')
->ignoreParams('page') );
->handleRequest($this->getRequest());
$memberships->applyFilter($filterEditor->getFilter());
$this->setupFilterControl($filterEditor);
$this->setupPaginationControl($memberships); $this->setupPaginationControl($memberships);
$this->setupLimitControl(); $this->setupLimitControl();
$this->setupSortControl( $this->setupSortControl(

View File

@ -63,6 +63,13 @@ class DbUserBackend extends DbRepository implements UserBackendInterface, Inspec
*/ */
protected $blacklistedQueryColumns = array('user'); protected $blacklistedQueryColumns = array('user');
/**
* The search columns being provided
*
* @var array
*/
protected $searchColumns = array('user');
/** /**
* The default sort rules to be applied on a query * The default sort rules to be applied on a query
* *
@ -98,6 +105,23 @@ class DbUserBackend extends DbRepository implements UserBackendInterface, Inspec
} }
} }
/**
* Initialize this repository's filter columns
*
* @return array
*/
protected function initializeFilterColumns()
{
$userLabel = t('Username') . ' ' . t('(Case insensitive)');
return array(
$userLabel => 'user',
t('Username') => 'user_name',
t('Active') => 'is_active',
t('Created At') => 'created_at',
t('Last Modified') => 'last_modified'
);
}
/** /**
* Insert a table row with the given data * Insert a table row with the given data
* *

View File

@ -52,6 +52,13 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface, In
*/ */
protected $blacklistedQueryColumns = array('user'); protected $blacklistedQueryColumns = array('user');
/**
* The search columns being provided
*
* @var array
*/
protected $searchColumns = array('user');
/** /**
* The default sort rules to be applied on a query * The default sort rules to be applied on a query
* *
@ -243,6 +250,21 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface, In
); );
} }
/**
* Initialize this repository's filter columns
*
* @return array
*/
protected function initializeFilterColumns()
{
return array(
t('Username') => 'user_name',
t('Active') => 'is_active',
t('Created At') => 'created_at',
t('Last Modified') => 'last_modified'
);
}
/** /**
* Initialize this repository's conversion rules * Initialize this repository's conversion rules
* *

View File

@ -73,6 +73,13 @@ class DbUserGroupBackend extends DbRepository implements UserGroupBackendInterfa
*/ */
protected $blacklistedQueryColumns = array('group', 'user'); protected $blacklistedQueryColumns = array('group', 'user');
/**
* The search columns being provided
*
* @var array
*/
protected $searchColumns = array('group', 'user');
/** /**
* The value conversion rules to apply on a query or statement * The value conversion rules to apply on a query or statement
* *
@ -97,6 +104,26 @@ class DbUserGroupBackend extends DbRepository implements UserGroupBackendInterfa
} }
} }
/**
* Initialize this repository's filter columns
*
* @return array
*/
protected function initializeFilterColumns()
{
$userLabel = t('Username') . ' ' . t('(Case insensitive)');
$groupLabel = t('User Group') . ' ' . t('(Case insensitive)');
return array(
$userLabel => 'user',
t('Username') => 'user_name',
$groupLabel => 'group',
t('User Group') => 'group_name',
t('Parent') => 'parent',
t('Created At') => 'created_at',
t('Last Modified') => 'last_modified'
);
}
/** /**
* Insert a table row with the given data * Insert a table row with the given data
* *

View File

@ -34,6 +34,13 @@ class IniUserGroupBackend extends IniRepository implements UserGroupBackendInter
*/ */
protected $blacklistedQueryColumns = array('group'); protected $blacklistedQueryColumns = array('group');
/**
* The search columns being provided
*
* @var array
*/
protected $searchColumns = array('group');
/** /**
* The value conversion rules to apply on a query or statement * The value conversion rules to apply on a query or statement
* *
@ -55,6 +62,21 @@ class IniUserGroupBackend extends IniRepository implements UserGroupBackendInter
$this->ds->getConfigObject()->setKeyColumn('name'); $this->ds->getConfigObject()->setKeyColumn('name');
} }
/**
* Initialize this repository's filter columns
*
* @return array
*/
protected function initializeFilterColumns()
{
return array(
t('User Group') => 'group',
t('Parent') => 'parent',
t('Created At') => 'created_at',
t('Last Modified') => 'last_modified'
);
}
/** /**
* Add a new group to this backend * Add a new group to this backend
* *

View File

@ -83,7 +83,14 @@ class LdapUserGroupBackend /*extends LdapRepository*/ implements UserGroupBacken
* *
* @var array * @var array
*/ */
protected $filterColumns = array('group', 'user'); protected $blacklistedQueryColumns = array('group', 'user');
/**
* The search columns being provided
*
* @var array
*/
protected $searchColumns = array('group', 'user');
/** /**
* The default sort rules to be applied on a query * The default sort rules to be applied on a query
@ -457,6 +464,21 @@ class LdapUserGroupBackend /*extends LdapRepository*/ implements UserGroupBacken
return array('group' => $columns, 'group_membership' => $columns); return array('group' => $columns, 'group_membership' => $columns);
} }
/**
* Initialize this repository's filter columns
*
* @return array
*/
protected function initializeFilterColumns()
{
return array(
t('Username') => 'user',
t('User Group') => 'group_name',
t('Created At') => 'created_at',
t('Last Modified') => 'last_modified'
);
}
/** /**
* Initialize this repository's conversion rules * Initialize this repository's conversion rules
* *

View File

@ -3,6 +3,7 @@
namespace Icinga\Web; namespace Icinga\Web;
use Icinga\Data\Filterable;
use Icinga\Data\Sortable; use Icinga\Data\Sortable;
use Icinga\Data\QueryInterface; use Icinga\Data\QueryInterface;
use Icinga\Exception\Http\HttpNotFoundException; use Icinga\Exception\Http\HttpNotFoundException;
@ -140,16 +141,51 @@ class Controller extends ModuleActionController
} }
/** /**
* Set the view property `filterEditor' to the given FilterEditor * Create a FilterEditor widget and apply the user's chosen filter options on the given filterable
* *
* In case the current view has been requested as compact this method does nothing. * The widget is set on the `filterEditor' view property only if the current view has not been requested as compact.
* The optional $filterColumns parameter should be an array of key-value pairs where the key is the name of the
* column and the value the label to show to the user. The optional $searchColumns parameter should be an array
* of column names to be used to handle quick searches.
* *
* @param Form $editor The FilterEditor * If the given filterable is an instance of Icinga\Data\FilterColumns, $filterable->getFilterColumns() and
* $filterable->getSearchColumns() is called to provide the respective columns if $filterColumns or $searchColumns
* is not given.
*
* @param Filterable $filterable The filterable to create a filter editor for
* @param array $filterColumns The filter columns to offer to the user
* @param array $searchColumns The search columns to utilize for quick searches
* *
* @return $this * @return $this
*
* @todo Preserving and ignoring parameters should be configurable (another two method params? property magic?)
*/ */
protected function setupFilterControl($editor) protected function setupFilterControl(
{ Filterable $filterable,
array $filterColumns = null,
array $searchColumns = null
) {
$editor = Widget::create('filterEditor')
->setQuery($filterable)
->preserveParams(
'limit',
'sort',
'dir',
'format',
'view',
'user',
'group',
'backend',
'stateType',
'addColumns',
'problems',
'_dev'
)
->ignoreParams('page')
->setColumns($filterColumns)
->setSearchColumns($searchColumns)
->handleRequest($this->getRequest());
if (! $this->view->compact) { if (! $this->view->compact) {
$this->view->filterEditor = $editor; $this->view->filterEditor = $editor;
} }

View File

@ -10,7 +10,6 @@ use Icinga\Web\Widget\Tabextension\DashboardAction;
use Icinga\Web\Widget\Tabextension\OutputFormat; use Icinga\Web\Widget\Tabextension\OutputFormat;
use Icinga\Web\Widget\Tabs; use Icinga\Web\Widget\Tabs;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Web\Widget;
use Icinga\Module\Monitoring\Forms\StatehistoryForm; use Icinga\Module\Monitoring\Forms\StatehistoryForm;
use Icinga\Module\Monitoring\DataView\DataView; use Icinga\Module\Monitoring\DataView\DataView;
@ -616,18 +615,7 @@ class Monitoring_ListController extends Controller
*/ */
protected function filterQuery(DataView $dataView) protected function filterQuery(DataView $dataView)
{ {
$editor = Widget::create('filterEditor') $this->setupFilterControl($dataView);
->setQuery($dataView)
->preserveParams(
'limit', 'sort', 'dir', 'format', 'view', 'backend',
'stateType', 'addColumns', '_dev', 'problems'
)
->ignoreParams('page')
->setSearchColumns($dataView->getSearchColumns())
->handleRequest($this->getRequest());
$this->setupFilterControl($editor);
$this->handleFormatRequest($dataView); $this->handleFormatRequest($dataView);
return $dataView; return $dataView;
} }