new: [Cerebrates] previewSharingGroups, downloadSg

pull/9461/head
Christophe Vandeplas 2024-01-13 16:43:30 +00:00
parent 28d760c143
commit 5a278ecd98
5 changed files with 283 additions and 221 deletions

View File

@ -248,7 +248,6 @@ class CerebratesController extends AppController
if ($this->request->is('post')) {
/** @var \App\Model\Entity\Cerebrate $cerebrate */
$cerebrate = $this->Cerebrates->findById($cerebrate_id)->first();
if (empty($cerebrate)) {
throw new NotFoundException(__('Invalid Cerebrate instance ID provided.'));
}
@ -284,76 +283,70 @@ class CerebratesController extends AppController
public function previewSharingGroups($id)
{
throw new CakeException('Not implemented');
// $this->set('menuData', ['menuList' => 'sync', 'menuItem' => 'previewCerebrateSGs']);
// $cerebrate = $this->Cerebrate->find('first', [
// 'recursive' => -1,
// 'conditions' => ['Cerebrate.id' => $id]
// ]);
// if (empty($cerebrate)) {
// throw new NotFoundException(__('Invalid Cerebrate instance ID provided.'));
// }
// $result = $this->Cerebrate->queryInstance([
// 'cerebrate' => $cerebrate,
// 'path' => '/sharingGroups/index',
// 'params' => $this->IndexFilter->harvestParameters([
// 'name',
// 'uuid',
// 'quickFilter'
// ]),
// 'type' => 'GET'
// ]);
// $result = $this->Cerebrate->checkRemoteSharingGroups($result);
// if ($this->_isRest()) {
// return $this->RestResponse->viewData($result, $this->response->type());
// } else {
// App::uses('CustomPaginationTool', 'Tools');
// $customPagination = new CustomPaginationTool();
// $customPagination->truncateAndPaginate($result, $this->params, false, true);
// $this->set('data', $result);
// $this->set('cerebrate', $cerebrate);
// }
/** @var \App\Model\Entity\Cerebrate $cerebrate */
$cerebrate = $this->Cerebrates->findById($id)->first();
if (empty($cerebrate)) {
throw new NotFoundException(__('Invalid Cerebrate instance ID provided.'));
}
$sgs = $cerebrate->queryInstance([
'path' => '/sharingGroups/index',
'params' => $this->harvestParameters([
'name',
'uuid',
'quickFilter'
]),
'type' => 'GET'
]);
if (!empty($sgs))
$result = $cerebrate->checkRemoteSharingGroups($sgs);
else $result = [];
if ($this->ParamHandler->isRest()) {
return $this->RestResponse->viewData($result, $this->response->getType());
} else {
$customPagination = new CustomPaginationTool();
$passedParams = $this->request->getQueryParams();
$customPagination->truncateAndPaginate($result, $passedParams, 'SharingGroups', true);
$this->set('passedParams', $passedParams);
$this->set('data', $result);
$this->set('cerebrate', $cerebrate);
}
}
public function downloadSg($cerebrate_id, $sg_id)
{
throw new CakeException('Not implemented');
// if ($this->request->is('post')) {
// $cerebrate = $this->Cerebrate->find('first', [
// 'recursive' => -1,
// 'conditions' => ['Cerebrate.id' => $cerebrate_id]
// ]);
// if (empty($cerebrate)) {
// throw new NotFoundException(__('Invalid Cerebrate instance ID provided.'));
// }
// $result = $this->Cerebrate->queryInstance([
// 'cerebrate' => $cerebrate,
// 'path' => '/sharingGroups/view/' . $sg_id,
// 'type' => 'GET'
// ]);
// $saveResult = $this->Cerebrate->captureSg($result, $this->Auth->user());
// if ($this->_isRest()) {
// if (is_array($saveResult)) {
// return $this->RestResponse->viewData($saveResult, $this->response->type());
// } else {
// return $this->RestResponse->saveFailResponse('Cerebrates', 'download_sg', $cerebrate_id . '/' . $sg_id, $saveResult);
// }
// } else {
// if (is_array($saveResult)) {
// $this->Flash->success(__('Sharing Group downloaded.'));
// } else {
// $this->Flash->error($saveResult);
// }
// $this->redirect($this->referer());
// }
// } else {
// $this->set('id', $cerebrate_id);
// $this->set('title', __('Download sharing group information'));
// $this->set('question', __('Are you sure you want to download and add / update the remote sharing group?'));
// $this->set('actionName', __('Download'));
// $this->layout = false;
// $this->render('/genericTemplates/confirm');
// }
if ($this->request->is('post')) {
/** @var \App\Model\Entity\Cerebrate $cerebrate */
$cerebrate = $this->Cerebrates->findById($cerebrate_id)->first();
if (empty($cerebrate)) {
throw new NotFoundException(__('Invalid Cerebrate instance ID provided.'));
}
$result = $cerebrate->queryInstance([
'path' => '/sharingGroups/view/' . $sg_id,
'type' => 'GET'
]);
$saveResult = $cerebrate->captureSg($result, $this->ACL->getUser());
if ($this->ParamHandler->isRest()) {
if (is_array($saveResult)) {
return $this->RestResponse->viewData($saveResult, $this->response->getType());
} else {
return $this->RestResponse->saveFailResponse('Cerebrates', 'download_sg', $cerebrate_id . '/' . $sg_id, $saveResult);
}
} else {
if (is_array($saveResult)) {
$this->Flash->success(__('Sharing Group downloaded.'));
} else {
$this->Flash->error($saveResult);
}
$this->redirect($this->referer());
}
} else {
$this->set('id', $cerebrate_id);
$this->set('title', __('Download sharing group information'));
$this->set('question', __('Are you sure you want to download and add / update the remote sharing group?'));
$this->set('actionName', __('Download'));
$this->layout = false;
$this->render('/genericTemplates/confirm');
}
}
}

View File

@ -274,9 +274,7 @@ class Cerebrate extends AppModel
$uuids = Hash::extract($orgs, '{n}.uuid');
/** @var \App\Model\Table\OrganisationsTable $organisationTable */
$organisationTable = TableRegistry::getTableLocator()->get('Organisations');
$existingOrgs = $organisationTable->find('all', [
'recursive' => -1,
])
$existingOrgs = $organisationTable->find('all')
->where(['uuid'])
->where(function (QueryExpression $exp, Query $q) use ($uuids) {
return $exp->in('uuid', array_values($uuids));
@ -324,24 +322,22 @@ class Cerebrate extends AppModel
private function __compareMembers($existingMembers, $remoteMembers)
{
throw new CakeException('Not implemented');
// $memberFound = [];
// $memberNotFound = [];
// foreach ($remoteMembers as $remoteMember) {
// $found = false;
// foreach ($existingMembers as $existingMember) {
// if ($existingMember['uuid'] == $remoteMember['uuid']) {
// $found = true;
// $memberFound[] = $remoteMember['uuid'];
// break;
// }
// }
// if (!$found) {
// $memberNotFound[] = $remoteMember['uuid'];
// }
// }
// return empty($memberNotFound);
$memberFound = [];
$memberNotFound = [];
foreach ($remoteMembers as $remoteMember) {
$found = false;
foreach ($existingMembers as $existingMember) {
if ($existingMember['uuid'] == $remoteMember['uuid']) {
$found = true;
$memberFound[] = $remoteMember['uuid'];
break;
}
}
if (!$found) {
$memberNotFound[] = $remoteMember['uuid'];
}
}
return empty($memberNotFound);
}
/*
@ -351,146 +347,132 @@ class Cerebrate extends AppModel
*/
public function checkRemoteSharingGroups($sgs)
{
throw new CakeException('Not implemented');
// $this->SharingGroup = ClassRegistry::init('SharingGroup');
// $uuids = Hash::extract($sgs, '{n}.uuid');
// $existingSgs = $this->SharingGroup->find('all', [
// 'recursive' => -1,
// 'contain' => [
// 'SharingGroupOrg' => ['Organisation'],
// 'Organisation',
// ],
// 'conditions' => [
// 'SharingGroup.uuid' => $uuids
// ],
// ]);
// $rearranged = [];
// foreach ($existingSgs as $existingSg) {
// $existingSg['SharingGroup']['SharingGroupOrg'] = $existingSg['SharingGroupOrg'];
// $existingSg['SharingGroup']['Organisation'] = $existingSg['Organisation'];
// $rearranged[$existingSg['SharingGroup']['uuid']] = $existingSg['SharingGroup'];
// }
// unset($existingSgs);
// $fieldsToCheck = ['name', 'releasability', 'description'];
// foreach ($sgs as $k => $sg) {
// $sgs[$k]['exists_locally'] = false;
// if (isset($rearranged[$sg['uuid']])) {
// $sgs[$k]['exists_locally'] = true;
// $sgs[$k]['differences'] = $this->compareSgs($rearranged[$sg['uuid']], $sgs[$k]);
// }
// }
// return $sgs;
/** @var \App\Model\Table\SharingGroupsTable $sharingGroupTable */
$sharingGroupTable = TableRegistry::getTableLocator()->get('SharingGroups');
$uuids = Hash::extract($sgs, '{n}.uuid');
$existingSgs = $sharingGroupTable->find('all')
->contain(['SharingGroupOrgs'=> 'Organisations', 'Organisations'])
->where(['SharingGroups.uuid'])
->where(function (QueryExpression $exp, Query $q) use ($uuids) {
return $exp->in('SharingGroups.uuid', array_values($uuids));
});
$rearranged = [];
foreach ($existingSgs as $existingSg) {
$existingSg = $existingSg->toArray();
$rearranged[$existingSg['uuid']] = $existingSg;
}
unset($existingSgs);
$fieldsToCheck = ['name', 'releasability', 'description'];
foreach ($sgs as $k => $sg) {
$sgs[$k]['exists_locally'] = false;
if (isset($rearranged[$sg['uuid']])) {
$sgs[$k]['exists_locally'] = true;
$sgs[$k]['differences'] = $this->compareSgs($rearranged[$sg['uuid']], $sgs[$k]);
}
}
return $sgs;
}
private function compareSgs($existingSg, $remoteSg)
{
throw new CakeException('Not implemented');
$differences = [];
$fieldsToCheck = ['name', 'releasability', 'description'];
// $differences = [];
// $fieldsToCheck = ['name', 'releasability', 'description'];
// foreach ($fieldsToCheck as $fieldToCheck) {
// if (
// !(empty($remoteSg[$fieldToCheck]) && empty($existingSg[$fieldToCheck])) &&
// $remoteSg[$fieldToCheck] !== $existingSg[$fieldToCheck]
// ) {
// if ($fieldToCheck === 'name') {
// if ($this->__compareNames($existingSg[$fieldToCheck], $remoteSg[$fieldToCheck])) {
// continue;
// }
// }
// $differences[] = $fieldToCheck;
// }
// }
// if (!$this->__compareMembers(Hash::extract($existingSg['SharingGroupOrg'], '{n}.Organisation'), $remoteSg['sharing_group_orgs'])) {
// $differences[] = 'members';
// }
// return $differences;
foreach ($fieldsToCheck as $fieldToCheck) {
if (
!(empty($remoteSg[$fieldToCheck]) && empty($existingSg[$fieldToCheck])) &&
$remoteSg[$fieldToCheck] !== $existingSg[$fieldToCheck]
) {
if ($fieldToCheck === 'name') {
if ($this->__compareNames($existingSg[$fieldToCheck], $remoteSg[$fieldToCheck])) {
continue;
}
}
$differences[] = $fieldToCheck;
}
}
if (!$this->__compareMembers(Hash::extract($existingSg['SharingGroupOrg'], '{n}.Organisation'), $remoteSg['sharing_group_orgs'])) {
$differences[] = 'members';
}
return $differences;
}
private function convertSg($sg_data)
{
throw new CakeException('Not implemented');
// $mapping = [
// 'name' => [
// 'field' => 'name',
// 'required' => 1
// ],
// 'uuid' => [
// 'field' => 'uuid',
// 'required' => 1
// ],
// 'releasability' => [
// 'field' => 'releasability'
// ],
// 'description' => [
// 'field' => 'description'
// ],
// ];
// $sg = [];
// foreach ($mapping as $cerebrate_field => $field_data) {
// if (empty($sg_data[$cerebrate_field])) {
// if (!empty($field_data['required'])) {
// return false;
// } else {
// continue;
// }
// }
// $sg[$field_data['field']] = $sg_data[$cerebrate_field];
// }
// $sg['SharingGroupOrg'] = [];
// if (!empty($sg_data['sharing_group_orgs'])) {
// $sg['SharingGroupOrg'] = $sg_data['sharing_group_orgs'];
// foreach ($sg['SharingGroupOrg'] as $k => $org) {
// if (isset($org['_joinData'])) {
// unset($sg['SharingGroupOrg'][$k]['_joinData']);
// }
// if (!isset($org['extend'])) {
// $sg['SharingGroupOrg'][$k]['extend'] = false;
// }
// }
// }
// return $sg;
$mapping = [
'name' => [
'field' => 'name',
'required' => 1
],
'uuid' => [
'field' => 'uuid',
'required' => 1
],
'releasability' => [
'field' => 'releasability'
],
'description' => [
'field' => 'description'
],
];
$sg = [];
foreach ($mapping as $cerebrate_field => $field_data) {
if (empty($sg_data[$cerebrate_field])) {
if (!empty($field_data['required'])) {
return false;
} else {
continue;
}
}
$sg[$field_data['field']] = $sg_data[$cerebrate_field];
}
$sg['SharingGroupOrg'] = [];
if (!empty($sg_data['sharing_group_orgs'])) {
$sg['SharingGroupOrg'] = $sg_data['sharing_group_orgs'];
foreach ($sg['SharingGroupOrg'] as $k => $org) {
if (isset($org['_joinData'])) {
unset($sg['SharingGroupOrg'][$k]['_joinData']);
}
if (!isset($org['extend'])) {
$sg['SharingGroupOrg'][$k]['extend'] = false;
}
foreach ($org as $ok => $ov) {
if ($ov === null) unset($sg['SharingGroupOrg'][$k][$ok]);
}
}
}
return $sg;
}
/**
* captureSg
*
* @param array $sg_data
* @param App\Model\Entity\User $user
* @param bool $edit
* @param bool $noChange
* @return mixed
*/
public function captureSg($sg_data, $user, &$edit=false, &$noChange=false)
{
throw new CakeException('Not implemented');
// $this->SharingGroup = ClassRegistry::init('SharingGroup');
// $sg = $this->convertSg($sg_data);
// if ($sg) {
// $existingSg = $this->SharingGroup->find('first', [
// 'recursive' => -1,
// 'contain' => [
// 'SharingGroupOrg' => ['Organisation'],
// 'Organisation',
// ],
// 'conditions' => [
// 'SharingGroup.uuid' => $sg_data['uuid']
// ],
// ]);
// if (!empty($existingSg)) {
// $edit = true;
// }
// $captureResult = $this->SharingGroup->captureSG($sg, $user, false);
// if (!empty($captureResult)) {
// $savedSg = $this->SharingGroup->find('first', [
// 'recursive' => -1,
// 'contain' => [
// 'SharingGroupOrg' => ['Organisation'],
// 'Organisation',
// ],
// 'conditions' => [
// 'SharingGroup.id' => $captureResult
// ],
// ]);
// return $savedSg;
// }
// return __('The organisation could not be saved.');
// }
// return __('The retrieved data isn\'t a valid sharing group.');
/** @var \App\Model\Table\SharingGroupsTable $sharingGroupTable */
$sharingGroupTable = TableRegistry::getTableLocator()->get('SharingGroups');
$sg = $this->convertSg($sg_data);
if ($sg) {
$existingSg = $sharingGroupTable->findByUuid($sg_data['uuid'])->first();
if (!empty($existingSg)) {
$edit = true;
}
$captureResult = $sharingGroupTable->captureSG($sg, $user->toArray(), false);
if (!empty($captureResult)) {
$savedSg = $sharingGroupTable->findById($captureResult)
->contain(['SharingGroupOrgs'=> 'Organisations', 'Organisations'])
->first();
return $savedSg;
}
return __('The organisation could not be saved.');
}
return __('The retrieved data isn\'t a valid sharing group.');
}
}

View File

@ -58,7 +58,7 @@
'actions' => [
[
'onclick' => sprintf(
'openGenericModal(\'%s/cerebrates/download_org/%s/[onclick_params_data_path]\');',
'openGenericModal(\'{0}/cerebrates/download_org/{1}/[onclick_params_data_path]\');',
$baseurl,
h($cerebrate['id'])
),
@ -68,9 +68,7 @@
]
],
'paginatorOptions' => array_merge(
[
'url' => [$cerebrate['id']]
],
['url' => [$cerebrate['id']]],
$passedParams
),
'persistUrlParams' => [0, 'quickFilter']

View File

@ -0,0 +1,89 @@
<?php
$fields = [
[
'name' => __('Id'),
'sort' => 'id',
'data_path' => 'id'
],
[
'name' => __('Status'),
'sort' => 'exists_locally',
'element' => 'remote_status',
'data_path' => ''
],
[
'name' => __('UUID'),
'sort' => 'uuid',
'data_path' => 'uuid'
],
[
'name' => __('Name'),
'sort' => 'name',
'data_path' => 'name'
],
[
'name' => __('Releasability'),
'sort' => 'releasability',
'data_path' => 'releasability'
],
[
'name' => __('Description'),
'sort' => 'description',
'data_path' => 'description'
],
[
'name' => __('# Member'),
'element' => 'custom',
'function' => function($row) {
return count($row['sharing_group_orgs']);
}
],
];
echo $this->element('genericElements/IndexTable/index_table', [
'data' => [
'data' => $data,
'top_bar' => [
'pull' => 'right',
'children' => [
[
'type' => 'search',
'button' => __('Filter'),
'placeholder' => __('Enter value to search'),
'data' => '',
'preserve_url_params' => [$cerebrate['id']],
'searchKey' => 'quickFilter'
]
]
],
'fields' => $fields,
'title' => empty($ajax) ? __(
'Sharing group list via Cerebrate {0} ({1})',
h($cerebrate['id']),
h($cerebrate['name'])
) : false,
'description' => empty($ajax) ? __('Preview of the sharing group known to the remote Cerebrate instance.') : false,
'actions' => [
[
'onclick' => sprintf(
'openGenericModal(\'{0}/cerebrates/download_sg/{1}/[onclick_params_data_path]\');',
$baseurl,
h($cerebrate['id'])
),
'onclick_params_data_path' => 'id',
'icon' => 'download',
'title' => __('Fetch sharing group object')
]
],
'paginatorOptions' => array_merge(
['url' => [$cerebrate['id']]],
$passedParams
),
'persistUrlParams' => [0, 'quickFilter']
],
'containerId' => 'preview_sgs_container'
]);
?>
<script type="text/javascript">
var passedArgsArray = <?= json_encode([h($cerebrate['id'])]) ?>;
</script>

View File

@ -17,7 +17,7 @@
} else {
$icon = 'sync';
$colour = 'organge';
$text = __('Object exists locally, but the following fields contain different information on the remote: %s', implode(', ', $differences));
$text = __('Object exists locally, but the following fields contain different information on the remote: {0}', implode(', ', $differences));
}
}
echo sprintf(