chg: [clusterRelations:view_relations] Quick submit and few fixes

pull/6120/head
mokaddem 2020-05-07 16:17:44 +02:00
parent 1390ed6a91
commit d01b142582
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
6 changed files with 140 additions and 69 deletions

View File

@ -100,46 +100,28 @@ class GalaxyClusterRelationsController extends AppController
$relation['GalaxyClusterRelation']['sharing_group_id'] = null; $relation['GalaxyClusterRelation']['sharing_group_id'] = null;
} }
// Fetch cluster source and adapt IDs $adpatedRelation = $this->adaptIds($relation);
$conditions = array(); $relation = $adpatedRelation['relation'];
if (!is_numeric($relation['GalaxyClusterRelation']['source_id'])) { $clusterSource = $adpatedRelation['clusterSource'];
$conditions['GalaxyCluster.uuid'] = $relation['GalaxyClusterRelation']['source_id']; $clusterTarget = $adpatedRelation['clusterTarget'];
} else {
$conditions['GalaxyCluster.id'] = $relation['GalaxyClusterRelation']['source_id'];
}
$clusterSource = $this->GalaxyClusterRelation->GalaxyCluster->fetchGalaxyClusters($this->Auth->user(), array('conditions' => $conditions), false);
if (empty($clusterSource)) {
throw new NotFoundException('Source cluster not found.');
}
$clusterSource = $clusterSource[0];
$relation['GalaxyClusterRelation']['galaxy_cluster_id'] = $clusterSource['GalaxyCluster']['id'];
unset($relation['GalaxyClusterRelation']['source_id']);
// Fetch cluster target and adapt IDs $errors = $this->GalaxyClusterRelation->saveRelation($this->Auth->user(), $relation);
$conditions = array(); $message = empty($errors) ? __('Relationship added.') : __('Relationship could not be added.');
if (!is_numeric($relation['GalaxyClusterRelation']['target_id'])) {
$conditions['GalaxyCluster.uuid'] = $relation['GalaxyClusterRelation']['target_id'];
} else {
$conditions['GalaxyCluster.id'] = $relation['GalaxyClusterRelation']['target_id'];
}
$clusterTarget = $this->GalaxyClusterRelation->GalaxyCluster->fetchGalaxyClusters($this->Auth->user(), array('conditions' => $conditions), false);
if (empty($clusterSource)) {
throw new NotFoundException('Target cluster not found.');
}
$clusterTarget = $clusterTarget[0];
$relation['GalaxyClusterRelation']['referenced_galaxy_cluster_id'] = $clusterTarget['GalaxyCluster']['id'];
unset($relation['GalaxyClusterRelation']['target_id']);
$saveSuccess = $this->GalaxyClusterRelation->saveRelation($this->Auth->user(), $relation);
$message = $saveSuccess ? __('Relationship added.') : __('Relationship could not be added.');
if ($this->_isRest()) { if ($this->_isRest()) {
if ($result) { if (empty($errors)) {
return $this->RestResponse->saveSuccessResponse('GalaxyClusterRelation', 'add', $this->response->type(), $message); return $this->RestResponse->saveSuccessResponse('GalaxyClusterRelation', 'add', $this->response->type(), $message);
} else { } else {
return $this->RestResponse->saveFailResponse('GalaxyClusterRelation', 'add', $message, $this->response->type()); return $this->RestResponse->saveFailResponse('GalaxyClusterRelation', 'add', $message, $this->response->type());
} }
} elseif ($this->request->is('ajax')) {
$this->autoRender = false;
if (empty($errors)) {
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'success' => '')),'status' => 200, 'type' => 'json'));
} else {
return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => 'Could not save relation, reason: ' . json_encode(array_merge($errors, $this->GalaxyClusterRelation->validationErrors)))),'status' => 200, 'type' => 'json'));
}
} else { } else {
if ($saveSuccess) { if (empty($errors)) {
$this->Flash->success($message); $this->Flash->success($message);
$this->redirect(array('action' => 'index')); $this->redirect(array('action' => 'index'));
} else { } else {
@ -181,6 +163,12 @@ class GalaxyClusterRelationsController extends AppController
if ($this->request->is('post') || $this->request->is('put')) { if ($this->request->is('post') || $this->request->is('put')) {
$relation = $this->request->data; $relation = $this->request->data;
$relation['GalaxyClusterRelation']['id'] = $id; $relation['GalaxyClusterRelation']['id'] = $id;
if ($relation['GalaxyClusterRelation']['distribution'] != 4) {
$relation['GalaxyClusterRelation']['sharing_group_id'] = null;
}
$adpatedRelation = $this->adaptIds($relation);
$relation = $adpatedRelation['relation'];
$errors = $this->GalaxyClusterRelation->editRelation($this->Auth->user(), $relation); $errors = $this->GalaxyClusterRelation->editRelation($this->Auth->user(), $relation);
$message = empty($errors) ? __('Relationship saved.') : __('Relationship could not be edited.'); $message = empty($errors) ? __('Relationship saved.') : __('Relationship could not be edited.');
if ($this->_isRest()) { if ($this->_isRest()) {
@ -236,4 +224,42 @@ class GalaxyClusterRelationsController extends AppController
} }
} }
} }
public function adaptIds($relation)
{
// Fetch cluster source and adapt IDs
$conditions = array();
if (!is_numeric($relation['GalaxyClusterRelation']['source_id'])) {
$conditions['GalaxyCluster.uuid'] = $relation['GalaxyClusterRelation']['source_id'];
} else {
$conditions['GalaxyCluster.id'] = $relation['GalaxyClusterRelation']['source_id'];
}
$clusterSource = $this->GalaxyClusterRelation->GalaxyCluster->fetchGalaxyClusters($this->Auth->user(), array('conditions' => $conditions), false);
if (empty($clusterSource)) {
throw new NotFoundException('Source cluster not found.');
}
$clusterSource = $clusterSource[0];
$relation['GalaxyClusterRelation']['galaxy_cluster_id'] = $clusterSource['GalaxyCluster']['id'];
unset($relation['GalaxyClusterRelation']['source_id']);
// Fetch cluster target and adapt IDs
$conditions = array();
if (!is_numeric($relation['GalaxyClusterRelation']['target_id'])) {
$conditions['GalaxyCluster.uuid'] = $relation['GalaxyClusterRelation']['target_id'];
} else {
$conditions['GalaxyCluster.id'] = $relation['GalaxyClusterRelation']['target_id'];
}
$clusterTarget = $this->GalaxyClusterRelation->GalaxyCluster->fetchGalaxyClusters($this->Auth->user(), array('conditions' => $conditions), false);
if (empty($clusterSource)) {
throw new NotFoundException('Target cluster not found.');
}
$clusterTarget = $clusterTarget[0];
$relation['GalaxyClusterRelation']['referenced_galaxy_cluster_id'] = $clusterTarget['GalaxyCluster']['id'];
unset($relation['GalaxyClusterRelation']['target_id']);
return array(
'clusterSource' => $clusterSource,
'clusterTarget' => $clusterTarget,
'relation' => $relation,
);
}
} }

View File

@ -849,5 +849,10 @@ class GalaxyClustersController extends AppController
$this->set('existingRelations', $existingRelations); $this->set('existingRelations', $existingRelations);
$this->set('cluster', $cluster); $this->set('cluster', $cluster);
$this->set('tree', $tree); $this->set('tree', $tree);
$this->loadModel('Attribute');
$distributionLevels = $this->Attribute->distributionLevels;
unset($distributionLevels[4]);
unset($distributionLevels[5]);
$this->set('distributionLevels', $distributionLevels);
} }
} }

View File

@ -192,8 +192,10 @@ class GalaxyClusterRelation extends AppModel
public function saveRelation($user, $relation) public function saveRelation($user, $relation)
{ {
$errors = array();
if (!$user['Role']['perm_galaxy_editor'] && !$user['Role']['perm_site_admin']) { if (!$user['Role']['perm_galaxy_editor'] && !$user['Role']['perm_site_admin']) {
return false; $errors[] = __('Incorrect permission');
return $errors;
} }
$relation['GalaxyClusterRelation']['org_id'] = $user['org_id']; $relation['GalaxyClusterRelation']['org_id'] = $user['org_id'];
if (!isset($relation['GalaxyClusterRelation']['orgc_id'])) { if (!isset($relation['GalaxyClusterRelation']['orgc_id'])) {
@ -211,18 +213,21 @@ class GalaxyClusterRelation extends AppModel
'GalaxyClusterRelation.org_id' => $relation['GalaxyClusterRelation']['org_id'], 'GalaxyClusterRelation.org_id' => $relation['GalaxyClusterRelation']['org_id'],
))); )));
if (!empty($existingRelation)) { if (!empty($existingRelation)) {
return false; $errors[] = __('Relation already exists');
return $errors;
} }
$this->create(); if (empty($errors)) {
$saveSuccess = $this->save($relation); $this->create();
if ($saveSuccess) { $saveSuccess = $this->save($relation);
$savedRelation = $this->find('first', array( if ($saveSuccess) {
'conditions' => array('id' => $this->id), $savedRelation = $this->find('first', array(
'recursive' => -1 'conditions' => array('id' => $this->id),
)); 'recursive' => -1
// TODO: save tags as well ));
// TODO: save tags as well
}
} }
return $saveSuccess; return $errors;
} }
public function editRelation($user, $relation, $fieldList=array()) public function editRelation($user, $relation, $fieldList=array())

View File

@ -6,14 +6,14 @@ echo $this->element('genericElements/assetLoader', array(
<div> <div>
<div style="padding: 5px; background-color: #f6f6f6; border-bottom: 1px solid #ccc; "> <div style="padding: 5px; background-color: #f6f6f6; border-bottom: 1px solid #ccc; ">
<div id="relationsQuickAddForm"> <form id="relationsQuickAddForm">
<div class="input"> <div class="input">
<label for="RelationshipSource"><?= __('Source UUID') ?></label> <label for="RelationshipSource"><?= __('Source UUID') ?></label>
<input id="RelationshipSource" type="text" value="<?= h($cluster['GalaxyCluster']['uuid']) ?>" disabled></input> <input id="RelationshipSource" name="source_id" type="text" value="<?= h($cluster['GalaxyCluster']['uuid']) ?>" disabled></input>
</div> </div>
<div class="input"> <div class="input">
<label for="RelationshipType"><?= __('Relationship type') ?></label> <label for="RelationshipType"><?= __('Relationship type') ?></label>
<select id="RelationshipType"> <select id="RelationshipType" name="referenced_galaxy_cluster_type">
<?php foreach ($existingRelations as $relation): ?> <?php foreach ($existingRelations as $relation): ?>
<option value="<?= h($relation) ?>"><?= h($relation) ?></option> <option value="<?= h($relation) ?>"><?= h($relation) ?></option>
<?php endforeach; ?> <?php endforeach; ?>
@ -23,18 +23,26 @@ echo $this->element('genericElements/assetLoader', array(
</div> </div>
<div class="input"> <div class="input">
<label for="RelationshipTarget"><?= __('Target UUID') ?></label> <label for="RelationshipTarget"><?= __('Target UUID') ?></label>
<input id="RelationshipTarget" type="text"></input> <input id="RelationshipTarget" name="target_id" type="text"></input>
</div>
<div class="input">
<label for="RelationshipDistribution"><?= __('Distribution') ?></label>
<select id="RelationshipDistribution" name="distribution">
<?php foreach ($distributionLevels as $k => $distribution): ?>
<option value="<?= h($k) ?>"><?= h($distribution) ?></option>
<?php endforeach; ?>
</select>
</div> </div>
<div class="input"> <div class="input">
<label for="RelationshipTags"><?= __('Tags') ?></label> <label for="RelationshipTags"><?= __('Tags') ?></label>
<input id="RelationshipTags" type="text"></input> <input id="RelationshipTags" name="tags" type="text"></input>
</div> </div>
<div class="clear"></div> <div class="clear"></div>
<button id="buttonAddRelationship" type="button" class="btn btn-primary" style=""> <button id="buttonAddRelationship" type="button" class="btn btn-primary" style="">
<i class="fas fa-plus"></i> <i class="fas fa-plus"></i>
Add relationship Add relationship
</button> </button>
</div> </form>
</div> </div>
</div> </div>
@ -63,26 +71,47 @@ echo $this->element('genericElements/assetLoader', array(
}) })
function submitRelationshipForm() { function submitRelationshipForm() {
var url = "<?= $baseurl ?>/galaxy_clusters/addRelations/"; var url = "<?= $baseurl ?>/galaxy_cluster_relations/add/";
$.ajax({ var data = {
beforeSend: function (XMLHttpRequest) { source_id: $('#RelationshipSource').val(),
toggleLoadingButton(true); target_id: $('#RelationshipTarget').val(),
type: $('#RelationshipType').val(),
tags: $('#RelationshipTags').val(),
distribution: $('#RelationshipDistribution').val(),
tags: $('#RelationshipTags').val(),
};
if (data.referenced_galaxy_cluster_type === 'custom') {
data['referenced_galaxy_cluster_type'] = freeTextVal;
}
toggleLoadingButton(true);
fetchFormDataAjax(url,
function(formData) {
$('body').append($('<div id="temp"/>').html(formData));
$('#temp #GalaxyClusterRelationSourceId').val(data.source_id);
$('#temp #GalaxyClusterRelationTargetId').val(data.target_id);
$('#temp #GalaxyClusterRelationReferencedGalaxyClusterType').val(data.type);
$('#temp #GalaxyClusterRelationDistribution').val(data.distribution);
$('#temp #GalaxyClusterRelationTags').val(data.tags);
$.ajax({
data: $('#GalaxyClusterRelationAddForm').serialize(),
success:function (data) {
console.log(data);
},
error:function(jqXHR, textStatus, errorThrown) {
showMessage('fail', textStatus + ": " + errorThrown);
},
complete:function() {
toggleLoadingButton(false);
$('#temp').remove();
},
type:"post",
url: $('#GalaxyClusterRelationAddForm').attr('action')
});
}, },
data: $('#relationsQuickAddForm').serialize(), function() {
success: function (data, textStatus) {
$('#top').html(data);
showMessage("success", "Relation added");
},
error: function (jqXHR, textStatus, errorThrown) {
showMessage('fail', textStatus + ": " + errorThrown);
},
complete: function() {
toggleLoadingButton(false); toggleLoadingButton(false);
}, }
type:"post", )
cache: false,
url: url,
});
} }
function toggleLoadingButton(loading) { function toggleLoadingButton(loading) {

View File

@ -37,6 +37,9 @@
'label' => __('Tags'), 'label' => __('Tags'),
'type' => 'text', 'type' => 'text',
), ),
),
'submit' => array(
'ajaxSubmit' => ''
) )
) )
)); ));

View File

@ -4815,7 +4815,7 @@ function changeTaxonomyRequiredState(checkbox) {
}); });
} }
function fetchFormDataAjax(url, callback) { function fetchFormDataAjax(url, callback, errorCallback) {
var formData = false; var formData = false;
$.ajax({ $.ajax({
data: '[]', data: '[]',
@ -4824,6 +4824,9 @@ function fetchFormDataAjax(url, callback) {
}, },
error:function() { error:function() {
handleGenericAjaxResponse({'saved':false, 'errors':['Request failed due to an unexpected error.']}); handleGenericAjaxResponse({'saved':false, 'errors':['Request failed due to an unexpected error.']});
if (errorCallback !== undefined) {
errorCallback();
}
}, },
async: false, async: false,
type:"get", type:"get",