diff --git a/src/Controller/BroodsController.php b/src/Controller/BroodsController.php index a857d43..b120f8c 100644 --- a/src/Controller/BroodsController.php +++ b/src/Controller/BroodsController.php @@ -14,6 +14,18 @@ class BroodsController extends AppController public $quickFilterFields = [['Broods.name' => true], 'Broods.uuid', ['Broods.description' => true]]; public $containFields = ['Organisations']; + protected $previewScopes = [ + 'organisations' => [ + 'quickFilterFields' => ['uuid', ['name' => true], ], + ], + 'individuals' => [ + 'quickFilterFields' => ['uuid', ['email' => true], ['first_name' => true], ['last_name' => true], ], + ], + 'sharingGroups' => [ + 'quickFilterFields' => ['uuid', ['name' => true], ], + ], + ]; + public function index() { $this->CRUD->index([ @@ -96,8 +108,9 @@ class BroodsController extends AppController public function previewIndex($id, $scope) { - if (!in_array($scope, ['organisations', 'individuals', 'sharingGroups'])) { - throw new MethodNotAllowedException(__('Invalid scope. Valid options are: organisations, individuals, sharing_groups')); + $validScopes = array_keys($this->previewScopes); + if (!in_array($scope, $validScopes)) { + throw new MethodNotAllowedException(__('Invalid scope. Valid options are: {0}', implode(', ', $validScopes))); } $filter = $this->request->getQuery('quickFilter'); $data = $this->Broods->queryIndex($id, $scope, $filter); @@ -108,6 +121,12 @@ class BroodsController extends AppController return $this->RestResponse->viewData($data, 'json'); } else { $data = $this->CustomPagination->paginate($data); + $optionFilters = ['quickFilter']; + $CRUDParams = $this->ParamHandler->harvestParams($optionFilters); + $CRUDOptions = [ + 'quickFilters' => $this->previewScopes[$scope]['quickFilterFields'], + ]; + $this->CRUD->setQuickFilterForView($CRUDParams, $CRUDOptions); $this->set('data', $data); $this->set('brood_id', $id); if ($this->request->is('ajax')) { diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index f932e5a..b2a7ab5 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -1119,17 +1119,9 @@ class CRUDComponent extends Component public function setQuickFilters(array $params, \Cake\ORM\Query $query, array $options): \Cake\ORM\Query { + $this->setQuickFilterForView($params, $options); $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'])) { @@ -1139,10 +1131,25 @@ class CRUDComponent extends Component } $query->where(['OR' => $queryConditions]); + } + return $query; + } + + public function setQuickFilterForView(array $params, array $options): void + { + $quickFilterFields = $options['quickFilters']; + $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']); } else { $this->Controller->set('quickFilterValue', ''); } - return $query; } public function genQuickFilterConditions(array $params, array $quickFilterFields): array