new: [genericElements:topBar] Added contextual filtering

pull/37/head
mokaddem 2020-12-07 16:06:01 +01:00
parent 1f32072d69
commit 80a8062ba8
5 changed files with 84 additions and 21 deletions

View File

@ -42,7 +42,14 @@ class CRUDComponent extends Component
} else {
$this->Controller->loadComponent('Paginator');
$data = $this->Controller->Paginator->paginate($query);
if (!empty($options['context'])) {
$this->setCurrentContext($options, $params);
}
$this->Controller->set('data', $data);
if (!empty($options['context'])) {
$contexts = array_merge(['_all'], $this->getAllContexts($options['context']));
$this->Controller->set('contexts', $contexts);
}
}
}
@ -349,6 +356,16 @@ class CRUDComponent extends Component
return $query;
}
protected function setCurrentContext($options, $params)
{
foreach ($params as $filter => $filterValue) {
if ($options['context'] == $filter) {
$this->Controller->set('currentContext', $filterValue);
break;
}
}
}
public function toggle(int $id, string $fieldName = 'enabled', array $params = []): void
{
if (empty($id)) {
@ -425,4 +442,9 @@ class CRUDComponent extends Component
}
}
}
private function getAllContexts($context)
{
return $this->Table->find()->distinct([$context])->all()->extract($context)->toList();
}
}

View File

@ -35,6 +35,7 @@ class MetaTemplatesController extends AppController
$this->CRUD->index([
'filters' => ['name', 'uuid', 'scope'],
'quickFilters' => ['name', 'uuid', 'scope'],
'context' => 'scope',
'contain' => ['MetaTemplateFields']
]);
if ($this->ParamHandler->isRest()) {

View File

@ -3,8 +3,10 @@ echo $this->element('genericElements/IndexTable/index_table', [
'data' => [
'data' => $data,
'top_bar' => [
'pull' => 'right',
'children' => [
[
'type' => 'context_filters',
],
[
'type' => 'search',
'button' => __('Filter'),

View File

@ -1,32 +1,42 @@
<?php
if (!isset($data['requirement']) || $data['requirement']) {
$onClickActive = false;
if (!empty($data['popover_url'])) {
$onClick = sprintf(
'onClick="populateAndLoadModal(%s)"',
sprintf("'%s'", h($data['popover_url']))
);
$onClickActive = true;
}
if (empty($onClick) && (!empty($data['onClick']) || empty($data['url']))) {
$onClickParams = array();
if (!empty($data['onClickParams'])) {
foreach ($data['onClickParams'] as $param) {
if ($param === 'this') {
$onClickParams[] = h($param);
} else {
$onClickParams[] = '\'' . h($param) . '\'';
if (empty($onClick)) {
if (!empty($data['onClick']) || empty($data['url'])) {
$onClickParams = array();
if (!empty($data['onClickParams'])) {
foreach ($data['onClickParams'] as $param) {
if ($param === 'this') {
$onClickParams[] = h($param);
} else {
$onClickParams[] = '\'' . h($param) . '\'';
}
}
}
$onClickParams = implode(',', $onClickParams);
$onClick = sprintf(
'onClick = "%s%s"',
(empty($data['url'])) ? 'event.preventDefault();' : '',
(!empty($data['onClick']) ? sprintf(
'%s(%s)',
h($data['onClick']),
$onClickParams
) : '')
);
$onClickActive = true;
} else if(!empty($data['url'])) {
$onClick = sprintf(
'onClick = "%s"',
sprintf('window.location=\'%s\'', $data['url'])
);
}
$onClickParams = implode(',', $onClickParams);
$onClick = sprintf(
'onClick = "%s%s"',
(empty($data['url'])) ? 'event.preventDefault();' : '',
(!empty($data['onClick']) ? sprintf(
'%s(%s)',
h($data['onClick']),
$onClickParams
) : '')
);
}
$dataFields = array();
if (!empty($data['data'])) {
@ -40,9 +50,9 @@
}
$dataFields = implode(' ', $dataFields);
echo sprintf(
'<button class="btn btn-small %s %s" %s href="%s" %s %s %s %s %s>%s%s%s</button>',
'<button class="btn btn-sm %s %s" %s href="%s" %s %s %s %s %s>%s%s%s</button>',
empty($data['class']) ? '' : h($data['class']),
empty($data['active']) ? 'btn-inverse' : 'btn-primary', // Change the default class for highlighted/active toggles here
$onClickActive ? 'btn-primary' : (empty($data['active']) ? 'btn-light' : 'btn-secondary'), // Change the default class for highlighted/active toggles here
empty($data['id']) ? '' : 'id="' . h($data['id']) . '"',
empty($data['url']) ? '#' : h($data['url']), // prevent default is passed if the url is not set
empty($onClick) ? '' : $onClick, // pass $data['onClick'] for the function name to call and $data['onClickParams'] for the parameter list

View File

@ -0,0 +1,28 @@
<?php
$contextArray = [];
$currentContext = !empty($currentContext) ? $currentContext : '_all';
foreach ($contexts as $context) {
$urlParams = [
'controller' => $this->request->getParam('controller'),
'action' => 'index',
];
if ($context != '_all') {
$urlParams['?'] = ['scope' => $context];
}
$contextArray[] = [
'active' => $context === $currentContext,
'url' => $this->Url->build($urlParams),
'text' => Cake\Utility\Inflector::humanize($context),
];
}
$dataGroup = [
'type' => 'simple',
'children' => $contextArray,
];
if (isset($data['requirement'])) {
$dataGroup['requirement'] = $data['requirement'];
}
echo $this->element('/genericElements/ListTopBar/group_simple', [
'data' => $dataGroup
]);