chg: [component:CRUD] Added support of metafield in quickfilter feature

pull/93/head
Sami Mokaddem 2021-11-10 15:28:09 +01:00
parent d6d592ff8c
commit cc0b1ad3b4
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 44 additions and 22 deletions

View File

@ -37,6 +37,8 @@ class CRUDComponent extends Component
$options['filters'] = [];
}
$options['filters'][] = 'quickFilter';
} else {
$options['quickFilters'] = [];
}
$options['filters'][] = 'filteringLabel';
if ($this->taggingSupported()) {
@ -53,7 +55,7 @@ class CRUDComponent extends Component
$query = $options['filterFunction']($query);
}
$query = $this->setFilters($params, $query, $options);
$query = $this->setQuickFilters($params, $query, empty($options['quickFilters']) ? [] : $options['quickFilters']);
$query = $this->setQuickFilters($params, $query, $options);
if (!empty($options['contain'])) {
$query->contain($options['contain']);
}
@ -896,13 +898,27 @@ class CRUDComponent extends Component
return $massagedFilters;
}
public function setQuickFilters(array $params, \Cake\ORM\Query $query, array $quickFilterFields): \Cake\ORM\Query
public function setQuickFilters(array $params, \Cake\ORM\Query $query, array $options): \Cake\ORM\Query
{
$quickFilterFields = $options['quickFilters'];
$queryConditions = [];
$this->Controller->set('quickFilter', empty($quickFilterFields) ? [] : $quickFilterFields);
if ($this->metaFieldsSupported() && !empty($options['quickFilterForMetaField']['enabled'])) {
$this->Controller->set('quickFilterForMetaField', [
'enabled' => $options['quickFilterForMetaField']['enabled'] ?? false,
'wildcard_search' => $options['quickFilterForMetaField']['enabled'] ?? false,
]);
}
if (!empty($params['quickFilter']) && !empty($quickFilterFields)) {
$this->Controller->set('quickFilterValue', $params['quickFilter']);
$queryConditions = $this->genQuickFilterConditions($params, $quickFilterFields);
if ($this->metaFieldsSupported() && !empty($options['quickFilterForMetaField']['enabled'])) {
$searchValue = !empty($options['quickFilterForMetaField']['wildcard_search']) ? "%{$params['quickFilter']}%" : $params['quickFilter'];
$metaFieldConditions = $this->Table->buildMetaFieldQuerySnippetForMatchingParent(['value' => $searchValue]);
$queryConditions[] = $metaFieldConditions;
}
$query->where(['OR' => $queryConditions]);
} else {
$this->Controller->set('quickFilterValue', '');
@ -998,10 +1014,10 @@ class CRUDComponent extends Component
protected function setMetaFieldFilters($query, $metaFieldFilters)
{
$modelAlias = $this->Table->getAlias();
$subQuery = $this->Table->find('metaFieldValue', $metaFieldFilters)
->select($modelAlias . '.id');
return $query->where([$modelAlias . '.id IN' => $subQuery]);
$metaFieldConditions = $this->Table->buildMetaFieldQuerySnippetForMatchingParent($metaFieldFilters);
$query->where($metaFieldConditions);
return $query;
}
protected function setTagFilters($query, $tags)

View File

@ -22,11 +22,7 @@ class IndividualsController extends AppController
$this->CRUD->index([
'filters' => $this->filterFields,
'quickFilters' => $this->quickFilterFields,
'contextFilters' => [
'fields' => [
'Alignments.type'
]
],
'quickFilterForMetaField' => ['enabled' => true, 'wildcard_search' => true],
'contain' => $this->containFields
]);
$responsePayload = $this->CRUD->getResponsePayload();
@ -34,7 +30,6 @@ class IndividualsController extends AppController
return $responsePayload;
}
$this->set('alignmentScope', 'individuals');
$this->set('metaGroup', 'ContactDB');
}
public function filtering()
@ -49,7 +44,6 @@ class IndividualsController extends AppController
if (!empty($responsePayload)) {
return $responsePayload;
}
$this->set('metaGroup', 'ContactDB');
}
public function view($id)
@ -59,7 +53,6 @@ class IndividualsController extends AppController
if (!empty($responsePayload)) {
return $responsePayload;
}
$this->set('metaGroup', 'ContactDB');
}
public function edit($id)
@ -69,7 +62,6 @@ class IndividualsController extends AppController
if (!empty($responsePayload)) {
return $responsePayload;
}
$this->set('metaGroup', 'ContactDB');
$this->render('add');
}
@ -80,7 +72,6 @@ class IndividualsController extends AppController
if (!empty($responsePayload)) {
return $responsePayload;
}
$this->set('metaGroup', 'ContactDB');
}
public function tag($id)

View File

@ -36,6 +36,7 @@ class MetaFieldsBehavior extends Behavior
],
'implementedMethods' => [
'normalizeMetafields' => 'normalizeMetafields',
'buildMetaFieldQuerySnippetForMatchingParent' => 'buildQuerySnippetForMatchingParent',
],
'implementedFinders' => [
'metafieldValue' => 'findMetafieldValue',
@ -134,14 +135,23 @@ class MetaFieldsBehavior extends Behavior
* ])
*/
public function findMetafieldValue(Query $query, array $filters)
{
$conditions = $this->buildQuerySnippetForMatchingParent($filters);
$query->where($conditions);
return $query;
}
public function buildQuerySnippetForMatchingParent(array $filters): array
{
if (empty($filters)) {
return $query;
return [];
}
if (count(array_filter(array_keys($filters), 'is_string'))) {
$filters = [$filters];
}
$conjugatedFilters = $this->buildConjugatedFilters($filters);
$conditions = $this->buildConjugatedQuerySnippet($conjugatedFilters);
$query->where($conditions);
return $query;
return $conditions;
}
protected function buildConjugatedFilters(array $filters): array
@ -176,7 +186,7 @@ class MetaFieldsBehavior extends Behavior
return $conditions;
}
public function buildComposedQuerySnippet(array $filters, string $operator='AND'): array
protected function buildComposedQuerySnippet(array $filters, string $operator='AND'): array
{
$conditions = [];
foreach ($filters as $filterOperator => $filter) {
@ -188,7 +198,7 @@ class MetaFieldsBehavior extends Behavior
}
protected function getQueryExpressionForField(QueryExpression $exp, string $field, string $value)
protected function getQueryExpressionForField(QueryExpression $exp, string $field, string $value): QueryExpression
{
if (substr($value, 0, 1) == '!') {
$value = substr($value, 1);
@ -201,7 +211,7 @@ class MetaFieldsBehavior extends Behavior
return $exp;
}
protected function buildQuerySnippet(array $filter)
protected function buildQuerySnippet(array $filter): Query
{
$whereClosure = function (QueryExpression $exp) use ($filter) {
foreach ($filter as $column => $value) {

View File

@ -15,6 +15,11 @@
if (!empty($data['quickFilter'])) {
$quickFilter = $data['quickFilter'];
}
if (!empty($quickFilterForMetaField['enabled'])) {
$quickFilter[] = [
'MetaField\'s value' => !empty($quickFilterForMetaField['wildcard_search'])
];
}
$filterEffective = !empty($quickFilter); // No filters will be picked up, thus rendering the filtering useless
$filteringButton = '';
if (!empty($data['allowFilering'])) {