fix: [clusterRelations:catpure] More flexible tag capture

pull/6120/head
mokaddem 2020-06-08 12:14:56 +02:00
parent 48aa7df49e
commit 2a807d30cc
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 34 additions and 30 deletions

View File

@ -147,30 +147,33 @@ class GalaxiesController extends AppController
public function import()
{
if ($this->request->is('post') || $this->request->is('put')) {
$data = $this->request->data['Galaxy'];
if ($data['submittedjson']['name'] != '' && $data['json'] != '') {
throw new MethodNotAllowedException(__('Only one import field can be used at a time'));
}
if ($data['submittedjson']['size'] > 0) {
$filename = basename($data['submittedjson']['name']);
$file_content = file_get_contents($data['submittedjson']['tmp_name']);
if ((isset($data['submittedjson']['error']) && $data['submittedjson']['error'] == 0) ||
(!empty($data['submittedjson']['tmp_name']) && $data['submittedjson']['tmp_name'] != '')
) {
if (!$file_content) {
throw new InternalErrorException(__('PHP says file was not uploaded. Are you attacking me?'));
}
}
$text = $file_content;
if ($this->_isRest()) {
$clusters = $this->request->data;
} else {
$text = $data['json'];
$data = $this->request->data['Galaxy'];
if ($data['submittedjson']['name'] != '' && $data['json'] != '') {
throw new MethodNotAllowedException(__('Only one import field can be used at a time'));
}
if ($data['submittedjson']['size'] > 0) {
$filename = basename($data['submittedjson']['name']);
$file_content = file_get_contents($data['submittedjson']['tmp_name']);
if ((isset($data['submittedjson']['error']) && $data['submittedjson']['error'] == 0) ||
(!empty($data['submittedjson']['tmp_name']) && $data['submittedjson']['tmp_name'] != '')
) {
if (!$file_content) {
throw new InternalErrorException(__('PHP says file was not uploaded. Are you attacking me?'));
}
}
$text = $file_content;
} else {
$text = $data['json'];
}
$clusters = json_decode($text, true);
if ($clusters === null) {
throw new MethodNotAllowedException(__('Error while decoding JSON'));
}
}
$clusters = json_decode($text, true);
if ($clusters === null) {
throw new MethodNotAllowedException(__('Error while decoding JSON'));
}
$forceUpdate = $this->request->data['Galaxy']['force_update'];
$saveResult = $this->Galaxy->importGalaxyAndClusters($this->Auth->user(), $clusters, $forceUpdate=$forceUpdate);
$saveResult = $this->Galaxy->importGalaxyAndClusters($this->Auth->user(), $clusters);
if ($saveResult['success']) {
$message = sprintf(__('Galaxy clusters imported. %s imported, %s ignored, %s failed. %s'), $saveResult['imported'], $saveResult['ignored'], $saveResult['failed'], !empty($saveResult['errors']) ? implode(', ', $saveResult['errors']) : '');
if ($this->_isRest()) {

View File

@ -257,7 +257,7 @@ class Galaxy extends AppModel
* @param bool $forceUpdate update clusters regardless of their version
* @return array
*/
public function importGalaxyAndClusters($user, $clusters, $forceUpdate=false)
public function importGalaxyAndClusters($user, $clusters)
{
$results = array('success' => false, 'imported' => 0, 'ignored' => 0, 'failed' => 0, 'errors' => array());
foreach ($clusters as $k => $cluster) {

View File

@ -383,8 +383,14 @@ class GalaxyClusterRelation extends AppModel
$saveSuccess = $this->save($relation);
if ($saveSuccess) {
$results['imported']++;
if (!empty($relation['GalaxyClusterRelationTag'])) {
$tagNames = Hash::extract($relation['GalaxyClusterRelationTag'], '{n}.name');
$modelKey = false;
if (!empty($relation['GalaxyClusterRelation']['GalaxyClusterRelationTag'])) {
$modelKey = 'GalaxyClusterRelationTag';
} elseif (!empty($relation['GalaxyClusterRelation']['Tag'])) {
$modelKey = 'Tag';
}
if ($modelKey !== false) {
$tagNames = Hash::extract($relation['GalaxyClusterRelation'][$modelKey], '{n}.name');
// Here we only attach tags. If they were removed at some point it's not taken into account. Since we don't have tag soft-deletion, tags added by users will be kept.
$this->GalaxyClusterRelationTag->attachTags($user, $this->id, $tagNames, $capture=true);
}

View File

@ -9,11 +9,6 @@ echo $this->element('genericElements/Form/genericForm', array(
'title' => __('Import galaxy clusters'),
'description' => __('Paste a JSON of cluster to import or provide a JSON file below.'),
'fields' => array(
array(
'field' => 'force_update',
'label' => __('Force update'),
'type' => 'checkbox',
),
array(
'field' => 'json',
'type' => 'text',