2021-01-13 14:30:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Controller\AppController;
|
|
|
|
use Cake\Utility\Hash;
|
|
|
|
use Cake\Utility\Text;
|
|
|
|
use \Cake\Database\Expression\QueryExpression;
|
2021-03-18 15:17:39 +01:00
|
|
|
use Cake\ORM\TableRegistry;
|
2021-01-13 14:30:44 +01:00
|
|
|
|
|
|
|
class BroodsController extends AppController
|
|
|
|
{
|
2021-09-10 11:55:54 +02:00
|
|
|
public $filterFields = ['Broods.name', 'Broods.uuid', 'Broods.url', 'Broods.description', 'Organisations.id', 'Broods.trusted', 'pull', 'authkey'];
|
|
|
|
public $quickFilterFields = [['Broods.name' => true], 'Broods.uuid', ['Broods.description' => true]];
|
|
|
|
public $containFields = ['Organisations'];
|
|
|
|
|
2021-01-13 14:30:44 +01:00
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$this->CRUD->index([
|
2021-09-10 11:55:54 +02:00
|
|
|
'filters' => $this->filterFields,
|
|
|
|
'quickFilters' => $this->quickFilterFields,
|
2021-01-14 12:42:05 +01:00
|
|
|
'contextFilters' => [
|
|
|
|
'fields' => [
|
|
|
|
'pull',
|
|
|
|
]
|
|
|
|
],
|
2021-09-10 11:55:54 +02:00
|
|
|
'contain' => $this->containFields
|
2021-01-13 14:30:44 +01:00
|
|
|
]);
|
2021-01-14 12:42:05 +01:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
|
|
|
$this->set('metaGroup', 'Sync');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
$this->CRUD->add();
|
2021-01-14 12:42:05 +01:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
|
|
|
$this->set('metaGroup', 'Sync');
|
|
|
|
$this->loadModel('Organisations');
|
|
|
|
$dropdownData = [
|
|
|
|
'organisation' => $this->Organisations->find('list', [
|
|
|
|
'sort' => ['name' => 'asc']
|
2023-01-11 09:56:39 +01:00
|
|
|
])->toArray()
|
2021-01-13 14:30:44 +01:00
|
|
|
];
|
|
|
|
$this->set(compact('dropdownData'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function view($id)
|
|
|
|
{
|
|
|
|
$this->CRUD->view($id, ['contain' => ['Organisations']]);
|
2021-01-14 12:42:05 +01:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
|
|
|
$this->set('metaGroup', 'Sync');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
$this->CRUD->edit($id);
|
2021-01-14 12:42:05 +01:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
|
|
|
$this->set('metaGroup', 'Sync');
|
2021-01-14 15:33:01 +01:00
|
|
|
$this->loadModel('Organisations');
|
|
|
|
$dropdownData = [
|
|
|
|
'organisation' => $this->Organisations->find('list', [
|
|
|
|
'sort' => ['name' => 'asc']
|
2023-01-11 09:56:39 +01:00
|
|
|
])->toArray()
|
2021-01-14 15:33:01 +01:00
|
|
|
];
|
|
|
|
$this->set(compact('dropdownData'));
|
2021-01-13 14:30:44 +01:00
|
|
|
$this->render('add');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
$this->CRUD->delete($id);
|
2021-01-14 12:42:05 +01:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
|
|
|
$this->set('metaGroup', 'Sync');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConnection($id)
|
|
|
|
{
|
2023-02-13 15:08:57 +01:00
|
|
|
$this->request->getSession()->close(); // close session to allow concurrent requests
|
2021-01-13 14:30:44 +01:00
|
|
|
$status = $this->Broods->queryStatus($id);
|
|
|
|
return $this->RestResponse->viewData($status, 'json');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function previewIndex($id, $scope)
|
|
|
|
{
|
2022-12-07 14:54:28 +01:00
|
|
|
$validScopes = array_keys($this->Broods->previewScopes);
|
2022-11-29 11:51:03 +01:00
|
|
|
if (!in_array($scope, $validScopes)) {
|
|
|
|
throw new MethodNotAllowedException(__('Invalid scope. Valid options are: {0}', implode(', ', $validScopes)));
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
2023-02-13 15:39:03 +01:00
|
|
|
$filtering = [
|
|
|
|
'page' => $this->request->getQuery('page', 1),
|
|
|
|
'limit' => $this->request->getQuery('limit', 20),
|
|
|
|
];
|
|
|
|
if (!empty($this->request->getQuery('quickFilter'))) {
|
|
|
|
$filtering['quickFilter'] = $this->request->getQuery('quickFilter');
|
|
|
|
}
|
|
|
|
$data = $this->Broods->queryIndex($id, $scope, $filtering, true);
|
2021-01-13 14:30:44 +01:00
|
|
|
if (!is_array($data)) {
|
|
|
|
$data = [];
|
|
|
|
}
|
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
return $this->RestResponse->viewData($data, 'json');
|
|
|
|
} else {
|
2022-12-07 14:54:28 +01:00
|
|
|
$data = $this->Broods->attachAllSyncStatus($data, $scope);
|
2021-01-13 14:30:44 +01:00
|
|
|
$data = $this->CustomPagination->paginate($data);
|
2022-11-29 11:51:03 +01:00
|
|
|
$optionFilters = ['quickFilter'];
|
|
|
|
$CRUDParams = $this->ParamHandler->harvestParams($optionFilters);
|
|
|
|
$CRUDOptions = [
|
2022-12-07 14:54:28 +01:00
|
|
|
'quickFilters' => $this->Broods->previewScopes[$scope]['quickFilterFields'],
|
2022-11-29 11:51:03 +01:00
|
|
|
];
|
|
|
|
$this->CRUD->setQuickFilterForView($CRUDParams, $CRUDOptions);
|
2021-01-13 14:30:44 +01:00
|
|
|
$this->set('data', $data);
|
|
|
|
$this->set('brood_id', $id);
|
|
|
|
if ($this->request->is('ajax')) {
|
|
|
|
$this->viewBuilder()->disableAutoLayout();
|
|
|
|
}
|
|
|
|
$this->set('metaGroup', 'Sync');
|
|
|
|
$this->render('preview_' . $scope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function downloadOrg($brood_id, $org_id)
|
|
|
|
{
|
2022-12-07 14:54:28 +01:00
|
|
|
if ($this->request->is('post')) {
|
|
|
|
$result = $this->Broods->downloadOrg($brood_id, $org_id);
|
|
|
|
$success = __('Organisation fetched from remote.');
|
|
|
|
$fail = __('Could not save the remote organisation');
|
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
if ($result) {
|
|
|
|
return $this->RestResponse->saveSuccessResponse('Brood', 'downloadOrg', $brood_id, 'json', $success);
|
|
|
|
} else {
|
|
|
|
return $this->RestResponse->saveFailResponse('Brood', 'downloadOrg', $brood_id, $fail, 'json');
|
|
|
|
}
|
2021-01-13 14:30:44 +01:00
|
|
|
} else {
|
2022-12-07 14:54:28 +01:00
|
|
|
if ($result) {
|
|
|
|
$this->Flash->success($success);
|
|
|
|
} else {
|
|
|
|
$this->Flash->error($fail);
|
|
|
|
}
|
|
|
|
$this->redirect($this->referer());
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
2022-12-07 14:54:28 +01:00
|
|
|
}
|
|
|
|
if ($org_id === 'all') {
|
|
|
|
$question = __('All organisations from brood `{0}` will be downloaded. Continue?', h($brood_id));
|
|
|
|
$title = __('Download all organisations from this brood');
|
|
|
|
$actionName = __('Download all');
|
2021-01-13 14:30:44 +01:00
|
|
|
} else {
|
2022-12-07 14:54:28 +01:00
|
|
|
$question = __('The organisations `{0}` from brood `{1}` will be downloaded. Continue?', h($org_id), h($brood_id));
|
|
|
|
$title = __('Download organisation from this brood');
|
|
|
|
$actionName = __('Download organisation');
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
2022-12-07 14:54:28 +01:00
|
|
|
$this->set('title', $title);
|
|
|
|
$this->set('question', $question);
|
|
|
|
$this->set('modalOptions', [
|
|
|
|
'confirmButton' => [
|
|
|
|
'variant' => $org_id === 'all' ? 'warning' : 'primary',
|
|
|
|
'text' => $actionName,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
$this->render('/genericTemplates/confirm');
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|
2021-01-18 14:08:12 +01:00
|
|
|
|
|
|
|
public function downloadIndividual($brood_id, $individual_id)
|
|
|
|
{
|
|
|
|
$result = $this->Broods->downloadIndividual($brood_id, $individual_id);
|
|
|
|
$success = __('Individual fetched from remote.');
|
|
|
|
$fail = __('Could not save the remote individual');
|
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
if ($result) {
|
|
|
|
return $this->RestResponse->saveSuccessResponse('Brood', 'downloadIndividual', $brood_id, 'json', $success);
|
|
|
|
} else {
|
|
|
|
return $this->RestResponse->saveFailResponse('Brood', 'downloadIndividual', $brood_id, $fail, 'json');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($result) {
|
|
|
|
$this->Flash->success($success);
|
|
|
|
} else {
|
|
|
|
$this->Flash->error($fail);
|
|
|
|
}
|
|
|
|
$this->redirect($this->referer());
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 15:17:39 +01:00
|
|
|
|
2021-06-28 14:49:38 +02:00
|
|
|
public function downloadSharingGroup($brood_id, $sg_id)
|
|
|
|
{
|
|
|
|
$result = $this->Broods->downloadSharingGroup($brood_id, $sg_id, $this->ACL->getUser()['id']);
|
|
|
|
$success = __('Sharing group fetched from remote.');
|
|
|
|
$fail = __('Could not save the remote sharing group');
|
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
if ($result) {
|
|
|
|
return $this->RestResponse->saveSuccessResponse('Brood', 'downloadSharingGroup', $brood_id, 'json', $success);
|
|
|
|
} else {
|
|
|
|
return $this->RestResponse->saveFailResponse('Brood', 'downloadSharingGroup', $brood_id, $fail, 'json');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($result) {
|
|
|
|
$this->Flash->success($success);
|
|
|
|
} else {
|
|
|
|
$this->Flash->error($fail);
|
|
|
|
}
|
|
|
|
$this->redirect($this->referer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 15:17:39 +01:00
|
|
|
public function interconnectTools()
|
|
|
|
{
|
2021-06-18 11:09:19 +02:00
|
|
|
$this->InboxProcessors = TableRegistry::getTableLocator()->get('InboxProcessors');
|
|
|
|
$processor = $this->InboxProcessors->getProcessor('Brood', 'ToolInterconnection');
|
2021-03-18 15:17:39 +01:00
|
|
|
$data = [
|
|
|
|
'origin' => '127.0.0.1',
|
|
|
|
'comment' => 'Test comment',
|
2021-03-19 11:15:43 +01:00
|
|
|
'user_id' => $this->ACL->getUser()->id,
|
2021-03-18 15:17:39 +01:00
|
|
|
'data' => [
|
|
|
|
'foo' => 'foo',
|
|
|
|
'bar' => 'bar',
|
|
|
|
'baz' => 'baz',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$processorResult = $processor->create($data);
|
2021-03-19 11:54:43 +01:00
|
|
|
return $processor->genHTTPReply($this, $processorResult, ['controller' => 'Broods', 'action' => 'index']);
|
2021-03-18 15:17:39 +01:00
|
|
|
}
|
2021-01-13 14:30:44 +01:00
|
|
|
}
|