From e7cc383cbf8ba0c1a92614ed16f8729ee5ef5f75 Mon Sep 17 00:00:00 2001 From: iglocska Date: Fri, 19 Jun 2020 00:37:56 +0200 Subject: [PATCH] chg: [Controllers] Alignments, Individuals, Organisations updated for the CRUD component --- app/src/Controller/AlignmentsController.php | 10 +- app/src/Controller/IndividualsController.php | 132 +++-------------- .../Controller/OrganisationsController.php | 136 ++++-------------- 3 files changed, 50 insertions(+), 228 deletions(-) diff --git a/app/src/Controller/AlignmentsController.php b/app/src/Controller/AlignmentsController.php index d025cf0..b91db4c 100644 --- a/app/src/Controller/AlignmentsController.php +++ b/app/src/Controller/AlignmentsController.php @@ -19,7 +19,7 @@ class AlignmentsController extends AppController if (!empty($organisation_id)) { $query->where(['organisation_id' => $organisation_id]); } - if ($this->_isRest()) { + if ($this->ParamHandler->isRest()) { $alignments = $query->all(); return $this->RestResponse->viewData($alignments, 'json'); } else { @@ -36,7 +36,7 @@ class AlignmentsController extends AppController throw new NotFoundException(__('Invalid alignment.')); } $individual = $this->Alignments->get($id); - if ($this->_isRest()) { + if ($this->ParamHandler->isRest()) { return $this->RestResponse->viewData($individual, 'json'); } else { @@ -54,7 +54,7 @@ class AlignmentsController extends AppController if ($this->request->is('post') || $this->request->is('delete')) { if ($this->Alignments->delete($individual)) { $message = __('Individual deleted.'); - if ($this->_isRest()) { + if ($this->ParamHandler->isRest()) { $individual = $this->Alignments->get($id); return $this->RestResponse->saveSuccessResponse('Alignments', 'delete', $id, 'json', $message); } else { @@ -88,7 +88,7 @@ class AlignmentsController extends AppController } if ($this->Alignments->save($alignment)) { $message = __('Alignment added.'); - if ($this->_isRest()) { + if ($this->ParamHandler->isRest()) { $alignment = $this->Alignments->get($this->Alignments->id); return $this->RestResponse->viewData($alignment, 'json'); } else { @@ -97,7 +97,7 @@ class AlignmentsController extends AppController } } else { $message = __('Alignment could not be added.'); - if ($this->_isRest()) { + if ($this->ParamHandler->isRest()) { return $this->RestResponse->saveFailResponse('Individuals', 'addAlignment', false, $message); } else { $this->Flash->error($message); diff --git a/app/src/Controller/IndividualsController.php b/app/src/Controller/IndividualsController.php index ed87702..2e7ba75 100644 --- a/app/src/Controller/IndividualsController.php +++ b/app/src/Controller/IndividualsController.php @@ -14,142 +14,52 @@ class IndividualsController extends AppController { public function index() { - $query = $this->Individuals->find(); - $filterFields = ['uuid', 'email', 'first_name', 'last_name', 'position']; - if (!empty($this->request->getQuery('quickFilter'))) { - $quickFilter = $this->request->getQuery('quickFilter'); - $conditions = []; - foreach ($filterFields as $field) { - $conditions[] = [$field . ' LIKE' => '%' . $quickFilter . '%']; - } - } - $quickFilter = $this->request->getQuery('quickFilter'); - foreach ($filterFields as $filterField) { - $tempFilter = $this->request->getQuery($filterField); - if (!empty($tempFilter)) { - if (strpos($tempFilter, '%') !== false) { - $conditions[] = [$filterField . ' LIKE' => $tempFilter]; - } else { - $conditions[] = [$filterField => $tempFilter]; - } - } - } - $query->contain(['Alignments' => 'Organisations']); - if (!empty($this->request->getQuery('organisation_id'))) { - $query->matching('Alignments', function($q) { - return $q->where(['Alignments.organisation_id' => $this->request->getQuery('organisation_id')]); - }); - } - if (!empty($conditions)) { - $query->where($conditions); - } - if ($this->_isRest()) { - $individuals = $query->all(); - return $this->RestResponse->viewData($individuals, 'json'); - } else { - $this->loadComponent('Paginator'); - $individuals = $this->Paginator->paginate($query); - $this->set('data', $individuals); - $this->set('alignmentScope', 'individuals'); - $this->set('metaGroup', 'ContactDB'); + $this->CRUD->index([ + 'filters' => ['uuid', 'email', 'first_name', 'last_name', 'position', 'Organisations.id'], + 'quickFilters' => ['uuid', 'email', 'first_name', 'last_name', 'position'], + 'contain' => ['Alignments' => 'Organisations'] + ]); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } + $this->set('alignmentScope', 'individuals'); + $this->set('metaGroup', 'ContactDB'); } public function add() { - $individual = $this->Individuals->newEmptyEntity(); - if ($this->request->is('post')) { - $individual = $this->Individuals->patchEntity($individual, $this->request->getData()); - if ($this->Individuals->save($individual)) { - $message = __('Individual added.'); - if ($this->_isRest()) { - $individual = $this->Individuals->get($id); - return $this->RestResponse->viewData($individual, 'json'); - } else { - $this->Flash->success($message); - $this->redirect(['action' => 'index']); - } - } else { - $message = __('Individual could not be added.'); - if ($this->_isRest()) { - - } else { - $this->Flash->error($message); - } - } + $this->CRUD->add(); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('individual', $individual); } public function view($id) { - if (empty($id)) { - throw new NotFoundException(__('Invalid organisation.')); - } - $individual = $this->Individuals->get($id, [ - 'contain' => ['Alignments' => 'Organisations'] - ]); - if ($this->_isRest()) { - return $this->RestResponse->viewData($individual, 'json'); - } else { - + $this->CRUD->view($id, ['contain' => ['Alignments' => 'Organisations']]); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('individual', $individual); } public function edit($id) { - if (empty($id)) { - throw new NotFoundException(__('Invalid organisation.')); - } - $individual = $this->Individuals->get($id); - if ($this->request->is(['post', 'put'])) { - $this->Individuals->patchEntity($individual, $this->request->getData()); - if ($this->Individuals->save($individual)) { - $message = __('Individual updated.'); - if ($this->_isRest()) { - $individual = $this->Individuals->get($id); - return $this->RestResponse->viewData($individual, 'json'); - } else { - $this->Flash->success($message); - return $this->redirect(['action' => 'index']); - } - } else { - if ($this->_isRest()) { - - } - } + $this->CRUD->edit($id); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('individual', $individual); $this->render('add'); } public function delete($id) { - if (empty($id)) { - throw new NotFoundException(__('Invalid organisation.')); - } - $individual = $this->Individuals->get($id); - if ($this->request->is('post') || $this->request->is('delete')) { - if ($this->Individuals->delete($individual)) { - $message = __('Individual deleted.'); - if ($this->_isRest()) { - $individual = $this->Individuals->get($id); - return $this->RestResponse->saveSuccessResponse('Individuals', 'delete', $id, 'json', $message); - } else { - $this->Flash->success($message); - return $this->redirect($this->referer()); - } - } + $this->CRUD->delete($id); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('scope', 'individuals'); - $this->set('id', $individual['id']); - $this->set('individual', $individual); - $this->viewBuilder()->setLayout('ajax'); - $this->render('/genericTemplates/delete'); } } diff --git a/app/src/Controller/OrganisationsController.php b/app/src/Controller/OrganisationsController.php index f7704ca..303f5a6 100644 --- a/app/src/Controller/OrganisationsController.php +++ b/app/src/Controller/OrganisationsController.php @@ -6,148 +6,60 @@ use App\Controller\AppController; use Cake\Utility\Hash; use Cake\Utility\Text; use \Cake\Database\Expression\QueryExpression; +use Cake\Http\Exception\NotFoundException; +use Cake\Http\Exception\MethodNotAllowedException; +use Cake\Http\Exception\ForbiddenException; class OrganisationsController extends AppController { public function index() { - $filterFields = ['name', 'uuid', 'nationality', 'sector', 'type', 'url']; - if (!empty($this->request->getQuery('quickFilter'))) { - $quickFilter = $this->request->getQuery('quickFilter'); - $conditions = []; - foreach ($filterFields as $field) { - $conditions[] = [$field . ' LIKE' => '%' . $quickFilter . '%']; - } - } - $quickFilter = $this->request->getQuery('quickFilter'); - foreach ($filterFields as $filterField) { - $tempFilter = $this->request->getQuery($filterField); - if (!empty($tempFilter)) { - if (strpos($tempFilter, '%') !== false) { - $conditions[] = [$filterField . ' LIKE' => $tempFilter]; - } else { - $conditions[] = [$filterField => $tempFilter]; - } - } - } - $query = $this->Organisations->find(); - $query->contain(['Alignments' => 'Individuals']); - if (!empty($this->request->getQuery('individual_id'))) { - $query->matching('Alignments', function($q) { - return $q->where(['Alignments.individual_id' => $this->request->getQuery('individual_id')]); - }); - } - if (!empty($conditions)) { - $query->where([ - 'OR' => $conditions - ]); - } - if ($this->_isRest()) { - $organisations = $query->all(); - return $this->RestResponse->viewData($organisations, 'json'); - } else { - $this->loadComponent('Paginator'); - $organisations = $this->Paginator->paginate($query); - $this->set('data', $organisations); - $this->set('metaGroup', 'ContactDB'); + $this->CRUD->index([ + 'filters' => ['name', 'uuid', 'nationality', 'sector', 'type', 'url', 'Alignments.id'], + 'quickFilters' => ['name', 'uuid', 'nationality', 'sector', 'type', 'url'], + 'contain' => ['Alignments' => 'Individuals'] + ]); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } + $this->set('alignmentScope', 'individuals'); + $this->set('metaGroup', 'ContactDB'); } public function add() { - $organisation = $this->Organisations->newEmptyEntity(); - if ($this->request->is('post')) { - $organisation = $this->Organisations->patchEntity($organisation, $this->request->getData()); - if ($this->Organisations->save($organisation)) { - $message = __('Organisation added.'); - if ($this->_isRest()) { - $organisation = $this->Organisations->get($id); - return $this->RestResponse->viewData($organisation, 'json'); - } else { - $this->Flash->success($message); - $this->redirect(['action' => 'index']); - } - } else { - $message = __('Organisation could not be added.'); - if ($this->_isRest()) { - - } else { - $this->Flash->error($message); - } - } + $this->CRUD->add(); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('organisation', $organisation); } public function view($id) { - if (empty($id)) { - throw new NotFoundException(__('Invalid organisation.')); - } - $organisation = $this->Organisations->get($id, [ - 'contain' => ['Alignments' => 'Individuals'] - ]); - if ($this->_isRest()) { - return $this->RestResponse->viewData($organisation, 'json'); - } else { - + $this->CRUD->view($id, ['contain' => ['Alignments' => 'Individuals']]); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('organisation', $organisation); } public function edit($id) { - if (empty($id)) { - throw new NotFoundException(__('Invalid organisation.')); - } - $organisation = $this->Organisations->get($id); - if ($this->request->is(['post', 'put'])) { - $this->Organisations->patchEntity($organisation, $this->request->getData()); - if ($this->Organisations->save($organisation)) { - $message = __('Organisation updated.'); - if ($this->_isRest()) { - $organisation = $this->Organisations->get($id); - return $this->RestResponse->viewData($organisation, 'json'); - } else { - $this->Flash->success($message); - return $this->redirect(['action' => 'index']); - } - } else { - if ($this->_isRest()) { - - } - } + $this->CRUD->edit($id); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('organisation', $organisation); $this->render('add'); } public function delete($id) { - if (empty($id)) { - throw new NotFoundException(__('Invalid organisation.')); - } - $organisation = $this->Organisations->get($id); - if ($this->request->is('post') || $this->request->is('delete')) { - if ($this->Organisations->delete($organisation)) { - $message = __('Organisation deleted.'); - if ($this->_isRest()) { - $organisation = $this->Organisations->get($id); - return $this->RestResponse->saveSuccessResponse('Organisations', 'delete', $id, 'json', $message); - } else { - $this->Flash->success($message); - return $this->redirect($this->referer()); - } - } + $this->CRUD->delete($id); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('scope', 'organisations'); - $this->set('id', $organisation['id']); - $this->set('organisation', $organisation); - $this->viewBuilder()->setLayout('ajax'); - $this->render('/genericTemplates/delete'); } }