chg: [CRUDComponent] Support of bulk delete operations

pull/59/head
mokaddem 2021-06-23 11:12:14 +02:00
parent 4cb627b444
commit 7ec0dabd0b
1 changed files with 103 additions and 17 deletions

View File

@ -7,6 +7,8 @@ use Cake\Error\Debugger;
use Cake\Utility\Hash; use Cake\Utility\Hash;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;
use Cake\View\ViewBuilder; use Cake\View\ViewBuilder;
use Cake\Http\Exception\MethodNotAllowedException;
use Cake\Http\Exception\NotFoundException;
class CRUDComponent extends Component class CRUDComponent extends Component
{ {
@ -340,33 +342,117 @@ class CRUDComponent extends Component
$this->Controller->set('entity', $data); $this->Controller->set('entity', $data);
} }
public function delete(int $id): void public function delete($id=false): void
{ {
if (empty($id)) { if ($this->request->is('get')) {
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias)); if(!empty($id)) {
} $data = $this->Table->get($id);
$data = $this->Table->get($id); $this->Controller->set('id', $data['id']);
if ($this->request->is('post') || $this->request->is('delete')) { $this->Controller->set('data', $data);
if ($this->Table->delete($data)) { $this->Controller->set('bulkEnabled', false);
$message = __('{0} deleted.', $this->ObjectAlias); } else {
if ($this->Controller->ParamHandler->isRest()) { $this->Controller->set('bulkEnabled', true);
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); }
} else if ($this->Controller->ParamHandler->isAjax()) { } else if ($this->request->is('post') || $this->request->is('delete')) {
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'delete', $data, $message); $ids = $this->getIdsOrFail($id);
} else { $isBulk = count($ids) > 1;
$this->Controller->Flash->success($message); $bulkSuccesses = 0;
$this->Controller->redirect($this->Controller->referer()); foreach ($ids as $id) {
$data = $this->Table->get($id);
$success = $this->Table->delete($data);
$success = true;
if ($success) {
$bulkSuccesses++;
} }
} }
$message = $this->getMessageBasedOnResult(
$bulkSuccesses == count($ids),
$isBulk,
__('{0} deleted.', $this->ObjectAlias),
__('All {0} have been deleted.', Inflector::pluralize($this->ObjectAlias)),
__('Could not delete {0}.', $this->ObjectAlias),
__('{0} / {1} {2} have been deleted.',
$bulkSuccesses,
count($ids),
Inflector::pluralize($this->ObjectAlias)
)
);
$this->setResponseForController('delete', $bulkSuccesses, $message, $data);
} }
$this->Controller->set('metaGroup', 'ContactDB'); $this->Controller->set('metaGroup', 'ContactDB');
$this->Controller->set('scope', 'users'); $this->Controller->set('scope', 'users');
$this->Controller->set('id', $data['id']);
$this->Controller->set('data', $data);
$this->Controller->viewBuilder()->setLayout('ajax'); $this->Controller->viewBuilder()->setLayout('ajax');
$this->Controller->render('/genericTemplates/delete'); $this->Controller->render('/genericTemplates/delete');
} }
public function setResponseForController($action, $success, $message, $data=[], $errors=null)
{
if ($success) {
if ($this->Controller->ParamHandler->isRest()) {
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
} elseif ($this->Controller->ParamHandler->isAjax()) {
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxSuccessResponse($this->ObjectAlias, $action, $data, $message);
} else {
$this->Controller->Flash->success($message);
$this->Controller->redirect($this->Controller->referer());
}
} else {
if ($this->Controller->ParamHandler->isRest()) {
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
} elseif ($this->Controller->ParamHandler->isAjax()) {
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, $action, $data, $message, !is_null($errors) ? $errors : $data->getErrors());
} else {
$this->Controller->Flash->error($message);
$this->Controller->redirect($this->Controller->referer());
}
}
}
private function getMessageBasedOnResult($isSuccess, $isBulk, $messageSingleSuccess, $messageBulkSuccess, $messageSingleFailure, $messageBulkFailure)
{
if ($isSuccess) {
$message = $isBulk ? $messageBulkSuccess : $messageSingleSuccess;
} else {
$message = $isBulk ? $messageBulkFailure : $messageSingleFailure;
}
return $message;
}
/**
* getIdsOrFail
*
* @param mixed $id
* @return Array The ID converted to a list or the list of provided IDs from the request
* @throws NotFoundException when no ID could be found
*/
public function getIdsOrFail($id=false): Array
{
$params = $this->Controller->ParamHandler->harvestParams(['ids']);
if (!empty($params['ids'])) {
$params['ids'] = json_decode($params['ids']);
}
$ids = [];
if (empty($id)) {
if (empty($params['ids'])) {
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
}
$ids = $params['ids'];
} else {
$id = $this->getInteger($id);
if (!is_null($id)) {
$ids = [$id];
} else {
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
}
}
return $ids;
}
private function getInteger($value)
{
return is_numeric($value) ? intval($value) : null;
}
protected function massageFilters(array $params): array protected function massageFilters(array $params): array
{ {
$massagedFilters = [ $massagedFilters = [