new: [internal] CRUD controller added

- centralise all CRUD operations
  - flexible library, with the goal of keeping controllers as lean as possible
  - can be used as a passthrough for options
remotes/origin/main
iglocska 2020-06-19 00:34:51 +02:00
parent b172a4d538
commit 91dc59e3a6
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 35 additions and 25 deletions

View File

@ -18,12 +18,21 @@ class CRUDComponent extends Component
$this->ObjectAlias = \Cake\Utility\Inflector::singularize($this->TableAlias); $this->ObjectAlias = \Cake\Utility\Inflector::singularize($this->TableAlias);
} }
public function index(array $filters = [], array $quickFilterFields = []) public function index(array $options): void
{ {
$params = $this->Controller->ParamHandler->harvestParams($filters); if (!empty($options['quickFilters'])) {
if (empty($options['filters'])) {
$options['filters'] = [];
}
$options['filters'][] = 'quickFilter';
}
$params = $this->Controller->ParamHandler->harvestParams(empty($options['filters']) ? [] : $options['filters']);
$query = $this->Table->find(); $query = $this->Table->find();
$query = $this->setFilters($params, $query); $query = $this->setFilters($params, $query);
$query = $this->setQuickFilters($params, $query, $quickFilterFields); $query = $this->setQuickFilters($params, $query, empty($options['quickFilters']) ? [] : $options['quickFilters']);
if (!empty($options['contain'])) {
$query->contain($options['contain']);
}
if (!empty($conditions)) { if (!empty($conditions)) {
$query->where([ $query->where([
'OR' => $conditions 'OR' => $conditions
@ -45,8 +54,8 @@ class CRUDComponent extends Component
if ($this->request->is('post')) { if ($this->request->is('post')) {
$data = $this->Table->patchEntity($data, $this->request->getData()); $data = $this->Table->patchEntity($data, $this->request->getData());
if ($this->Table->save($data)) { if ($this->Table->save($data)) {
$message = __('%s added.', $this->ObjectAlias); $message = __('{0} added.', $this->ObjectAlias);
if ($this->ParamHandler->isRest()) { if ($this->Controller->ParamHandler->isRest()) {
$data = $this->Table->get($id); $data = $this->Table->get($id);
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
} else { } else {
@ -54,27 +63,28 @@ class CRUDComponent extends Component
$this->Controller->redirect(['action' => 'index']); $this->Controller->redirect(['action' => 'index']);
} }
} else { } else {
$message = __('%s could not be added.', $this->ObjectAlias); $message = __('{0} could not be added.', $this->ObjectAlias);
if ($this->Controller->_isRest()) { if ($this->Controller->ParamHandler->isRest()) {
} else { } else {
$this->Controller->Flash->error($message); $this->Controller->Flash->error($message);
} }
} }
} }
$this->Controller->set('entity', $data);
} }
public function edit($id): void public function edit(int $id): void
{ {
if (empty($id)) { if (empty($id)) {
throw new NotFoundException(__('Invalid %s.', $this->ObjectAlias)); throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
} }
$data = $this->Table->get($id); $data = $this->Table->get($id);
if ($this->request->is(['post', 'put'])) { if ($this->request->is(['post', 'put'])) {
$this->Table->patchEntity($data, $this->request->getData()); $this->Table->patchEntity($data, $this->request->getData());
if ($this->Table->save($data)) { if ($this->Table->save($data)) {
$message = __('%s updated.', $this->ObjectAlias); $message = __('{0} updated.', $this->ObjectAlias);
if ($this->ParamHandler->isRest()) { if ($this->Controller->ParamHandler->isRest()) {
$data = $this->Table->get($id); $data = $this->Table->get($id);
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
} else { } else {
@ -82,42 +92,42 @@ class CRUDComponent extends Component
$this->Controller->redirect(['action' => 'index']); $this->Controller->redirect(['action' => 'index']);
} }
} else { } else {
if ($this->ParamHandler->isRest()) { if ($this->Controller->ParamHandler->isRest()) {
} }
} }
} }
$this->Controller->set('data', $data); $this->Controller->set('entity', $data);
} }
public function view($id, $params): void public function view(int $id, array $params = []): void
{ {
if (empty($id)) { if (empty($id)) {
throw new NotFoundException(__('Invalid %s.', $this->ObjectAlias)); throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
} }
$data = $this->Table->get($id, $params); $data = $this->Table->get($id, $params);
if ($this->Controller->ParamHandler->isRest()) { if ($this->Controller->ParamHandler->isRest()) {
$this->Controller->restResponsePayload = $this->Controller->RestResponse->viewData($data, 'json'); $this->Controller->restResponsePayload = $this->Controller->RestResponse->viewData($data, 'json');
} }
$this->Controller->set('data', $data); $this->Controller->set('entity', $data);
} }
public function delete($id): void public function delete(int $id): void
{ {
if (empty($id)) { if (empty($id)) {
throw new NotFoundException(__('Invalid %s.', $this->ObjectAlias)); throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
} }
$data = $this->Table->get($id); $data = $this->Table->get($id);
if ($this->request->is('post') || $this->request->is('delete')) { if ($this->request->is('post') || $this->request->is('delete')) {
if ($this->Table->delete($data)) { if ($this->Table->delete($data)) {
$message = __('%s deleted.', $this->ObjectAlias); $message = __('{0} deleted.', $this->ObjectAlias);
if ($this->Controller->ParamHandler->isRest()) { if ($this->Controller->ParamHandler->isRest()) {
$data = $this->Table->get($id); $data = $this->Table->get($id);
$this->Controller->restResponsePayload = $this->RestResponse->saveSuccessResponse($this->TableAlias, 'delete', $id, 'json', $message); $this->Controller->restResponsePayload = $this->RestResponse->saveSuccessResponse($this->TableAlias, 'delete', $id, 'json', $message);
} else { } else {
$this->Controller->Flash->success($message); $this->Controller->Flash->success($message);
$this->redirect($this->referer()); $this->Controller->redirect($this->Controller->referer());
} }
} }
} }
@ -137,12 +147,12 @@ class CRUDComponent extends Component
]; ];
if (!empty($params)) { if (!empty($params)) {
foreach ($params as $param => $paramValue) { foreach ($params as $param => $paramValue) {
if (strpos($params, '.') !== null) { if (strpos($param, '.') !== null) {
$params = explode('.', $params); $param = explode('.', $param);
if ($param[0] === $this->Table->alias) { if ($param[0] === $this->Table->getAlias()) {
$massagedFilters['simpleFilters'][implode('.', $param)] = $paramValue; $massagedFilters['simpleFilters'][implode('.', $param)] = $paramValue;
} else { } else {
$massagedFilters['relatedFilters'][implode('.', $param)] = $paramsValue; $massagedFilters['relatedFilters'][implode('.', $param)] = $paramValue;
} }
} else { } else {
$massagedFilters['simpleFilters'][] = $params; $massagedFilters['simpleFilters'][] = $params;
@ -180,7 +190,7 @@ class CRUDComponent extends Component
if (!empty($params['relatedFilters'])) { if (!empty($params['relatedFilters'])) {
foreach ($params['relatedFilters'] as $filter => $filterValue) { foreach ($params['relatedFilters'] as $filter => $filterValue) {
$filterParts = explode('.', $filter); $filterParts = explode('.', $filter);
$query->matching($filterParts[0], function(\Cake\ORM\Query $q){ $query->matching($filterParts[0], function(\Cake\ORM\Query $q) use ($filterValue, $filter) {
if (strlen(trim($filterValue, '%')) === strlen($filterValue)) { if (strlen(trim($filterValue, '%')) === strlen($filterValue)) {
return $q->where([$filter => $filterValue]); return $q->where([$filter => $filterValue]);
} else { } else {