chg: [component:CRUD] Added support of metafield in quickfilter feature
parent
d6d592ff8c
commit
cc0b1ad3b4
|
@ -37,6 +37,8 @@ class CRUDComponent extends Component
|
||||||
$options['filters'] = [];
|
$options['filters'] = [];
|
||||||
}
|
}
|
||||||
$options['filters'][] = 'quickFilter';
|
$options['filters'][] = 'quickFilter';
|
||||||
|
} else {
|
||||||
|
$options['quickFilters'] = [];
|
||||||
}
|
}
|
||||||
$options['filters'][] = 'filteringLabel';
|
$options['filters'][] = 'filteringLabel';
|
||||||
if ($this->taggingSupported()) {
|
if ($this->taggingSupported()) {
|
||||||
|
@ -53,7 +55,7 @@ class CRUDComponent extends Component
|
||||||
$query = $options['filterFunction']($query);
|
$query = $options['filterFunction']($query);
|
||||||
}
|
}
|
||||||
$query = $this->setFilters($params, $query, $options);
|
$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'])) {
|
if (!empty($options['contain'])) {
|
||||||
$query->contain($options['contain']);
|
$query->contain($options['contain']);
|
||||||
}
|
}
|
||||||
|
@ -896,13 +898,27 @@ class CRUDComponent extends Component
|
||||||
return $massagedFilters;
|
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 = [];
|
$queryConditions = [];
|
||||||
$this->Controller->set('quickFilter', empty($quickFilterFields) ? [] : $quickFilterFields);
|
$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)) {
|
if (!empty($params['quickFilter']) && !empty($quickFilterFields)) {
|
||||||
$this->Controller->set('quickFilterValue', $params['quickFilter']);
|
$this->Controller->set('quickFilterValue', $params['quickFilter']);
|
||||||
$queryConditions = $this->genQuickFilterConditions($params, $quickFilterFields);
|
$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]);
|
$query->where(['OR' => $queryConditions]);
|
||||||
} else {
|
} else {
|
||||||
$this->Controller->set('quickFilterValue', '');
|
$this->Controller->set('quickFilterValue', '');
|
||||||
|
@ -998,10 +1014,10 @@ class CRUDComponent extends Component
|
||||||
|
|
||||||
protected function setMetaFieldFilters($query, $metaFieldFilters)
|
protected function setMetaFieldFilters($query, $metaFieldFilters)
|
||||||
{
|
{
|
||||||
$modelAlias = $this->Table->getAlias();
|
$metaFieldConditions = $this->Table->buildMetaFieldQuerySnippetForMatchingParent($metaFieldFilters);
|
||||||
$subQuery = $this->Table->find('metaFieldValue', $metaFieldFilters)
|
$query->where($metaFieldConditions);
|
||||||
->select($modelAlias . '.id');
|
|
||||||
return $query->where([$modelAlias . '.id IN' => $subQuery]);
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setTagFilters($query, $tags)
|
protected function setTagFilters($query, $tags)
|
||||||
|
|
|
@ -22,11 +22,7 @@ class IndividualsController extends AppController
|
||||||
$this->CRUD->index([
|
$this->CRUD->index([
|
||||||
'filters' => $this->filterFields,
|
'filters' => $this->filterFields,
|
||||||
'quickFilters' => $this->quickFilterFields,
|
'quickFilters' => $this->quickFilterFields,
|
||||||
'contextFilters' => [
|
'quickFilterForMetaField' => ['enabled' => true, 'wildcard_search' => true],
|
||||||
'fields' => [
|
|
||||||
'Alignments.type'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
'contain' => $this->containFields
|
'contain' => $this->containFields
|
||||||
]);
|
]);
|
||||||
$responsePayload = $this->CRUD->getResponsePayload();
|
$responsePayload = $this->CRUD->getResponsePayload();
|
||||||
|
@ -34,7 +30,6 @@ class IndividualsController extends AppController
|
||||||
return $responsePayload;
|
return $responsePayload;
|
||||||
}
|
}
|
||||||
$this->set('alignmentScope', 'individuals');
|
$this->set('alignmentScope', 'individuals');
|
||||||
$this->set('metaGroup', 'ContactDB');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filtering()
|
public function filtering()
|
||||||
|
@ -49,7 +44,6 @@ class IndividualsController extends AppController
|
||||||
if (!empty($responsePayload)) {
|
if (!empty($responsePayload)) {
|
||||||
return $responsePayload;
|
return $responsePayload;
|
||||||
}
|
}
|
||||||
$this->set('metaGroup', 'ContactDB');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function view($id)
|
public function view($id)
|
||||||
|
@ -59,7 +53,6 @@ class IndividualsController extends AppController
|
||||||
if (!empty($responsePayload)) {
|
if (!empty($responsePayload)) {
|
||||||
return $responsePayload;
|
return $responsePayload;
|
||||||
}
|
}
|
||||||
$this->set('metaGroup', 'ContactDB');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function edit($id)
|
public function edit($id)
|
||||||
|
@ -69,7 +62,6 @@ class IndividualsController extends AppController
|
||||||
if (!empty($responsePayload)) {
|
if (!empty($responsePayload)) {
|
||||||
return $responsePayload;
|
return $responsePayload;
|
||||||
}
|
}
|
||||||
$this->set('metaGroup', 'ContactDB');
|
|
||||||
$this->render('add');
|
$this->render('add');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +72,6 @@ class IndividualsController extends AppController
|
||||||
if (!empty($responsePayload)) {
|
if (!empty($responsePayload)) {
|
||||||
return $responsePayload;
|
return $responsePayload;
|
||||||
}
|
}
|
||||||
$this->set('metaGroup', 'ContactDB');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tag($id)
|
public function tag($id)
|
||||||
|
|
|
@ -36,6 +36,7 @@ class MetaFieldsBehavior extends Behavior
|
||||||
],
|
],
|
||||||
'implementedMethods' => [
|
'implementedMethods' => [
|
||||||
'normalizeMetafields' => 'normalizeMetafields',
|
'normalizeMetafields' => 'normalizeMetafields',
|
||||||
|
'buildMetaFieldQuerySnippetForMatchingParent' => 'buildQuerySnippetForMatchingParent',
|
||||||
],
|
],
|
||||||
'implementedFinders' => [
|
'implementedFinders' => [
|
||||||
'metafieldValue' => 'findMetafieldValue',
|
'metafieldValue' => 'findMetafieldValue',
|
||||||
|
@ -134,14 +135,23 @@ class MetaFieldsBehavior extends Behavior
|
||||||
* ])
|
* ])
|
||||||
*/
|
*/
|
||||||
public function findMetafieldValue(Query $query, array $filters)
|
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)) {
|
if (empty($filters)) {
|
||||||
return $query;
|
return [];
|
||||||
|
}
|
||||||
|
if (count(array_filter(array_keys($filters), 'is_string'))) {
|
||||||
|
$filters = [$filters];
|
||||||
}
|
}
|
||||||
$conjugatedFilters = $this->buildConjugatedFilters($filters);
|
$conjugatedFilters = $this->buildConjugatedFilters($filters);
|
||||||
$conditions = $this->buildConjugatedQuerySnippet($conjugatedFilters);
|
$conditions = $this->buildConjugatedQuerySnippet($conjugatedFilters);
|
||||||
$query->where($conditions);
|
return $conditions;
|
||||||
return $query;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function buildConjugatedFilters(array $filters): array
|
protected function buildConjugatedFilters(array $filters): array
|
||||||
|
@ -176,7 +186,7 @@ class MetaFieldsBehavior extends Behavior
|
||||||
return $conditions;
|
return $conditions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildComposedQuerySnippet(array $filters, string $operator='AND'): array
|
protected function buildComposedQuerySnippet(array $filters, string $operator='AND'): array
|
||||||
{
|
{
|
||||||
$conditions = [];
|
$conditions = [];
|
||||||
foreach ($filters as $filterOperator => $filter) {
|
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) == '!') {
|
if (substr($value, 0, 1) == '!') {
|
||||||
$value = substr($value, 1);
|
$value = substr($value, 1);
|
||||||
|
@ -201,7 +211,7 @@ class MetaFieldsBehavior extends Behavior
|
||||||
return $exp;
|
return $exp;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function buildQuerySnippet(array $filter)
|
protected function buildQuerySnippet(array $filter): Query
|
||||||
{
|
{
|
||||||
$whereClosure = function (QueryExpression $exp) use ($filter) {
|
$whereClosure = function (QueryExpression $exp) use ($filter) {
|
||||||
foreach ($filter as $column => $value) {
|
foreach ($filter as $column => $value) {
|
||||||
|
|
|
@ -15,6 +15,11 @@
|
||||||
if (!empty($data['quickFilter'])) {
|
if (!empty($data['quickFilter'])) {
|
||||||
$quickFilter = $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
|
$filterEffective = !empty($quickFilter); // No filters will be picked up, thus rendering the filtering useless
|
||||||
$filteringButton = '';
|
$filteringButton = '';
|
||||||
if (!empty($data['allowFilering'])) {
|
if (!empty($data['allowFilering'])) {
|
||||||
|
|
Loading…
Reference in New Issue