chg: [Controllers] Alignments, Individuals, Organisations updated for the CRUD component

remotes/origin/main
iglocska 2020-06-19 00:37:56 +02:00
parent ba009426c5
commit e7cc383cbf
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
3 changed files with 50 additions and 228 deletions

View File

@ -19,7 +19,7 @@ class AlignmentsController extends AppController
if (!empty($organisation_id)) { if (!empty($organisation_id)) {
$query->where(['organisation_id' => $organisation_id]); $query->where(['organisation_id' => $organisation_id]);
} }
if ($this->_isRest()) { if ($this->ParamHandler->isRest()) {
$alignments = $query->all(); $alignments = $query->all();
return $this->RestResponse->viewData($alignments, 'json'); return $this->RestResponse->viewData($alignments, 'json');
} else { } else {
@ -36,7 +36,7 @@ class AlignmentsController extends AppController
throw new NotFoundException(__('Invalid alignment.')); throw new NotFoundException(__('Invalid alignment.'));
} }
$individual = $this->Alignments->get($id); $individual = $this->Alignments->get($id);
if ($this->_isRest()) { if ($this->ParamHandler->isRest()) {
return $this->RestResponse->viewData($individual, 'json'); return $this->RestResponse->viewData($individual, 'json');
} else { } else {
@ -54,7 +54,7 @@ class AlignmentsController extends AppController
if ($this->request->is('post') || $this->request->is('delete')) { if ($this->request->is('post') || $this->request->is('delete')) {
if ($this->Alignments->delete($individual)) { if ($this->Alignments->delete($individual)) {
$message = __('Individual deleted.'); $message = __('Individual deleted.');
if ($this->_isRest()) { if ($this->ParamHandler->isRest()) {
$individual = $this->Alignments->get($id); $individual = $this->Alignments->get($id);
return $this->RestResponse->saveSuccessResponse('Alignments', 'delete', $id, 'json', $message); return $this->RestResponse->saveSuccessResponse('Alignments', 'delete', $id, 'json', $message);
} else { } else {
@ -88,7 +88,7 @@ class AlignmentsController extends AppController
} }
if ($this->Alignments->save($alignment)) { if ($this->Alignments->save($alignment)) {
$message = __('Alignment added.'); $message = __('Alignment added.');
if ($this->_isRest()) { if ($this->ParamHandler->isRest()) {
$alignment = $this->Alignments->get($this->Alignments->id); $alignment = $this->Alignments->get($this->Alignments->id);
return $this->RestResponse->viewData($alignment, 'json'); return $this->RestResponse->viewData($alignment, 'json');
} else { } else {
@ -97,7 +97,7 @@ class AlignmentsController extends AppController
} }
} else { } else {
$message = __('Alignment could not be added.'); $message = __('Alignment could not be added.');
if ($this->_isRest()) { if ($this->ParamHandler->isRest()) {
return $this->RestResponse->saveFailResponse('Individuals', 'addAlignment', false, $message); return $this->RestResponse->saveFailResponse('Individuals', 'addAlignment', false, $message);
} else { } else {
$this->Flash->error($message); $this->Flash->error($message);

View File

@ -14,142 +14,52 @@ class IndividualsController extends AppController
{ {
public function index() public function index()
{ {
$query = $this->Individuals->find(); $this->CRUD->index([
$filterFields = ['uuid', 'email', 'first_name', 'last_name', 'position']; 'filters' => ['uuid', 'email', 'first_name', 'last_name', 'position', 'Organisations.id'],
if (!empty($this->request->getQuery('quickFilter'))) { 'quickFilters' => ['uuid', 'email', 'first_name', 'last_name', 'position'],
$quickFilter = $this->request->getQuery('quickFilter'); 'contain' => ['Alignments' => 'Organisations']
$conditions = []; ]);
foreach ($filterFields as $field) { if ($this->ParamHandler->isRest()) {
$conditions[] = [$field . ' LIKE' => '%' . $quickFilter . '%']; return $this->restResponsePayload;
}
}
$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->set('alignmentScope', 'individuals');
$this->set('metaGroup', 'ContactDB');
} }
public function add() public function add()
{ {
$individual = $this->Individuals->newEmptyEntity(); $this->CRUD->add();
if ($this->request->is('post')) { if ($this->ParamHandler->isRest()) {
$individual = $this->Individuals->patchEntity($individual, $this->request->getData()); return $this->restResponsePayload;
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->set('metaGroup', 'ContactDB'); $this->set('metaGroup', 'ContactDB');
$this->set('individual', $individual);
} }
public function view($id) public function view($id)
{ {
if (empty($id)) { $this->CRUD->view($id, ['contain' => ['Alignments' => 'Organisations']]);
throw new NotFoundException(__('Invalid organisation.')); if ($this->ParamHandler->isRest()) {
} return $this->restResponsePayload;
$individual = $this->Individuals->get($id, [
'contain' => ['Alignments' => 'Organisations']
]);
if ($this->_isRest()) {
return $this->RestResponse->viewData($individual, 'json');
} else {
} }
$this->set('metaGroup', 'ContactDB'); $this->set('metaGroup', 'ContactDB');
$this->set('individual', $individual);
} }
public function edit($id) public function edit($id)
{ {
if (empty($id)) { $this->CRUD->edit($id);
throw new NotFoundException(__('Invalid organisation.')); if ($this->ParamHandler->isRest()) {
} return $this->restResponsePayload;
$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->set('metaGroup', 'ContactDB'); $this->set('metaGroup', 'ContactDB');
$this->set('individual', $individual);
$this->render('add'); $this->render('add');
} }
public function delete($id) public function delete($id)
{ {
if (empty($id)) { $this->CRUD->delete($id);
throw new NotFoundException(__('Invalid organisation.')); if ($this->ParamHandler->isRest()) {
} return $this->restResponsePayload;
$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->set('metaGroup', 'ContactDB'); $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');
} }
} }

View File

@ -6,148 +6,60 @@ use App\Controller\AppController;
use Cake\Utility\Hash; use Cake\Utility\Hash;
use Cake\Utility\Text; use Cake\Utility\Text;
use \Cake\Database\Expression\QueryExpression; use \Cake\Database\Expression\QueryExpression;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\Exception\MethodNotAllowedException;
use Cake\Http\Exception\ForbiddenException;
class OrganisationsController extends AppController class OrganisationsController extends AppController
{ {
public function index() public function index()
{ {
$filterFields = ['name', 'uuid', 'nationality', 'sector', 'type', 'url']; $this->CRUD->index([
if (!empty($this->request->getQuery('quickFilter'))) { 'filters' => ['name', 'uuid', 'nationality', 'sector', 'type', 'url', 'Alignments.id'],
$quickFilter = $this->request->getQuery('quickFilter'); 'quickFilters' => ['name', 'uuid', 'nationality', 'sector', 'type', 'url'],
$conditions = []; 'contain' => ['Alignments' => 'Individuals']
foreach ($filterFields as $field) { ]);
$conditions[] = [$field . ' LIKE' => '%' . $quickFilter . '%']; if ($this->ParamHandler->isRest()) {
} return $this->restResponsePayload;
}
$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->set('alignmentScope', 'individuals');
$this->set('metaGroup', 'ContactDB');
} }
public function add() public function add()
{ {
$organisation = $this->Organisations->newEmptyEntity(); $this->CRUD->add();
if ($this->request->is('post')) { if ($this->ParamHandler->isRest()) {
$organisation = $this->Organisations->patchEntity($organisation, $this->request->getData()); return $this->restResponsePayload;
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->set('metaGroup', 'ContactDB'); $this->set('metaGroup', 'ContactDB');
$this->set('organisation', $organisation);
} }
public function view($id) public function view($id)
{ {
if (empty($id)) { $this->CRUD->view($id, ['contain' => ['Alignments' => 'Individuals']]);
throw new NotFoundException(__('Invalid organisation.')); if ($this->ParamHandler->isRest()) {
} return $this->restResponsePayload;
$organisation = $this->Organisations->get($id, [
'contain' => ['Alignments' => 'Individuals']
]);
if ($this->_isRest()) {
return $this->RestResponse->viewData($organisation, 'json');
} else {
} }
$this->set('metaGroup', 'ContactDB'); $this->set('metaGroup', 'ContactDB');
$this->set('organisation', $organisation);
} }
public function edit($id) public function edit($id)
{ {
if (empty($id)) { $this->CRUD->edit($id);
throw new NotFoundException(__('Invalid organisation.')); if ($this->ParamHandler->isRest()) {
} return $this->restResponsePayload;
$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->set('metaGroup', 'ContactDB'); $this->set('metaGroup', 'ContactDB');
$this->set('organisation', $organisation);
$this->render('add'); $this->render('add');
} }
public function delete($id) public function delete($id)
{ {
if (empty($id)) { $this->CRUD->delete($id);
throw new NotFoundException(__('Invalid organisation.')); if ($this->ParamHandler->isRest()) {
} return $this->restResponsePayload;
$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->set('metaGroup', 'ContactDB'); $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');
} }
} }