chg: [clusterRelations:CRUD] Added support of tags

pull/6120/head
mokaddem 2020-05-29 08:56:29 +02:00
parent 12412c88dd
commit 1bb2839dd3
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
5 changed files with 35 additions and 12 deletions

View File

@ -145,7 +145,7 @@ class GalaxyClusterRelationsController extends AppController
public function edit($id)
{
$conditions = array('conditions' => array('GalaxyClusterRelation.id' => $id));
$conditions = array('conditions' => array('GalaxyClusterRelation.id' => $id), 'contain' => array('GalaxyClusterRelationTag' => 'Tag'));
$existingRelation = $this->GalaxyClusterRelation->fetchRelations($this->Auth->user(), $conditions);
if (empty($existingRelation)) {
throw new NotFoundException(__('Invalid cluster relation'));
@ -156,6 +156,9 @@ class GalaxyClusterRelationsController extends AppController
throw new MethodNotAllowedException(__('Default cluster relation cannot be edited'));
}
$existingRelation['GalaxyClusterRelation']['tags'] = Hash::extract($existingRelation['GalaxyClusterRelationTag'], '{n}.Tag.name');
$existingRelation['GalaxyClusterRelation']['tags'] = implode(', ', $existingRelation['GalaxyClusterRelation']['tags']);
$this->loadModel('Attribute');
$distributionLevels = $this->Attribute->distributionLevels;
unset($distributionLevels[5]);
@ -182,6 +185,12 @@ class GalaxyClusterRelationsController extends AppController
$errors = array(__('Invalid permssions'));
}
if (!empty($relation['GalaxyClusterRelation']['tags'])) {
$tags = explode(',', $relation['GalaxyClusterRelation']['tags']);
$tags = array_map('trim', $tags);
$relation['GalaxyClusterRelation' ]['tags'] = $tags;
}
if ($this->Auth->user()['Role']['perm_site_admin'] || $clusterSource['GalaxyCluster']['org_id'] != $this->Auth->user()['org_id']) {
$errors = $this->GalaxyClusterRelation->editRelation($this->Auth->user(), $relation);
} else {

View File

@ -214,7 +214,7 @@ class Galaxy extends AppModel
$db->insertMulti('galaxy_elements', $fields, $elements);
}
$tempUser = array('Role' => array('perm_tag_editor' => 1, 'perm_site_admin' => 1)); // only site-admin are authorized to update galaxies
$this->GalaxyCluster->GalaxyClusterRelation->addRelations($tempUser, $relations);
$this->GalaxyCluster->GalaxyClusterRelation->addRelations($tempUser, $relations, $capture=true);
}
return true;
}

View File

@ -129,7 +129,7 @@ class GalaxyClusterRelation extends AppModel
$this->deleteAll($conditions, false, false);
}
public function addRelations($user, $relations)
public function addRelations($user, $relations, $capture=false)
{
$fieldList = array(
'galaxy_cluster_uuid',
@ -155,7 +155,7 @@ class GalaxyClusterRelation extends AppModel
$saveResult = $this->save($relation, array('fieldList' => $fieldList));
if ($saveResult) {
$savedId = $this->id;
$this->GalaxyClusterRelationTag->attachTags($user, $savedId, $relation['tags']);
$this->GalaxyClusterRelationTag->attachTags($user, $savedId, $relation['tags'], $capture=$capture);
}
} else {
throw new NotFoundException(__('Invalid referenced galaxy cluster'));
@ -210,7 +210,7 @@ class GalaxyClusterRelation extends AppModel
return $errors;
}
public function editRelation($user, $relation, $fieldList=array())
public function editRelation($user, $relation, $fieldList=array(), $capture=false)
{
$this->SharingGroup = ClassRegistry::init('SharingGroup');
$errors = array();
@ -263,6 +263,9 @@ class GalaxyClusterRelation extends AppModel
foreach($this->validationErrors as $validationError) {
$errors[] = $validationError[0];
}
} else {
$this->GalaxyClusterRelationTag->deleteAll(array('GalaxyClusterRelationTag.galaxy_cluster_relation_id' => $relation['GalaxyClusterRelation']['id']));
$this->GalaxyClusterRelationTag->attachTags($user, $relation['GalaxyClusterRelation']['id'], $relation['GalaxyClusterRelation']['tags'], $capture=$capture);
}
}
}
@ -331,7 +334,7 @@ class GalaxyClusterRelation extends AppModel
$results['imported']++;
if (!empty($relation['GalaxyClusterRelationTag'])) {
$tagNames = Hash::extract($relation['GalaxyClusterRelationTag'], '{n}.name');
$this->GalaxyClusterRelationTag->attachTags($user, $this->id, $tagNames);
$this->GalaxyClusterRelationTag->attachTags($user, $this->id, $tagNames, $capture=true);
}
} else {
$results['failed']++;

View File

@ -43,11 +43,16 @@ class GalaxyClusterRelationTag extends AppModel
$this->delete($id);
}
public function attachTags($user, $galaxyClusterRelationId, $tags)
public function attachTags($user, $galaxyClusterRelationId, $tags, $capture=false)
{
$allSaved = true;
$saveResult = false;
foreach ($tags as $tagName) {
$tagId = $this->Tag->captureTag(array('name' => $tagName), $user);
if ($capture) {
$tagId = $this->Tag->captureTag(array('name' => $tagName), $user);
} else {
$tagId = $this->Tag->lookupTagIdFromName($tagName);
}
$existingAssociation = $this->find('first', array(
'recursive' => -1,
'conditions' => array(
@ -55,7 +60,7 @@ class GalaxyClusterRelationTag extends AppModel
'galaxy_cluster_relation_id' => $galaxyClusterRelationId
)
));
if (empty($existingAssociation)) {
if (empty($existingAssociation) && $tagId != -1) {
$this->create();
$saveResult = $this->save(array('galaxy_cluster_relation_id' => $galaxyClusterRelationId, 'tag_id' => $tagId));
$allSaved = $allSaved && $saveResult;
@ -64,6 +69,11 @@ class GalaxyClusterRelationTag extends AppModel
}
}
}
return $saveResult;
return $allSaved;
}
public function detachTag($user, $relationTagId)
{
$this->delete(array('GalaxyClusterRelationTag.relationTagId' => $relationTagId));
}
}

View File

@ -34,8 +34,9 @@
),
array(
'field' => 'tags',
'label' => __('Tags'),
'type' => 'text',
'label' => __('Tag list'),
'type' => 'textarea',
'placeholder' => __('estimative-language:likelihood-probability="very-likely", false-positive:risk="low"')
),
),
'submit' => array(