chg: [galaxyElement] Added individual deletion and JSON flattening/expanding

pull/6686/head
mokaddem 2020-12-02 11:55:15 +01:00
parent 3f18da6422
commit f85dcc4fff
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
6 changed files with 159 additions and 37 deletions

View File

@ -16,6 +16,7 @@ class GalaxyElementsController extends AppController
public function index($clusterId)
{
$filters = $this->IndexFilter->harvestParameters(array('context', 'searchall'));
$aclConditions = $this->GalaxyElement->buildClusterConditions($this->Auth->user(), $clusterId);
if (empty($filters['context'])) {
$filters['context'] = 'all';
@ -39,25 +40,78 @@ class GalaxyElementsController extends AppController
$this->set('elements', $elements);
$this->set('clusterId', $clusterId);
$this->set('context', $filters['context']);
$this->set('passedArgs', json_encode(array('context' => $filters['context'], 'searchall' => isset($filters['searchall']) ? $filters['searchall'] : '')));
$this->set('passedArgs', json_encode([
'context' => $filters['context'],
'searchall' => isset($filters['searchall']) ? $filters['searchall'] : ''
]));
$cluster = $this->GalaxyElement->GalaxyCluster->fetchIfAuthorized($this->Auth->user(), $clusterId, array('edit', 'delete'), false, false);
$canModify = !empty($cluster['authorized']);
$canModify = true;
$this->set('canModify', $canModify);
if ($filters['context'] == 'JSONView') {
$expanded = $this->GalaxyElement->getExpandedJSONFromElements($elements);
$this->set('JSONElements', $expanded);
}
if ($this->request->is('ajax')) {
$this->layout = 'ajax';
$this->render('ajax/index');
}
}
public function indexTree($clusterId)
public function delete($elementId)
{
$elements = $this->GalaxyElement->fetchElements($this->Auth->user(), $clusterId);
$keyedValue = [];
foreach ($elements as $i => $element) {
$keyedValue[$element['key']][] = $element['value'];
$element = $this->GalaxyElement->find('first', array('conditions' => array('GalaxyElement.id' => $elementId)));
if (empty($element)) {
throw new Exception(__('Element not found'));
}
$this->set('element', $element);
$clusterId = $element['GalaxyElement']['galaxy_cluster_id'];
$cluster = $this->GalaxyElement->GalaxyCluster->fetchIfAuthorized($this->Auth->user(), $clusterId, array('edit'), true, false);
if ($this->request->is('post')) {
$deleteResult = $this->GalaxyElement->delete($elementId);
if ($deleteResult) {
$this->GalaxyElement->GalaxyCluster->editCluster($this->Auth->user(), $cluster, [], false);
$message = __('Galaxy element %s deleted', $elementId);
$this->Flash->success($message);
} else {
$message = __('Could not delete galaxy element');
$this->Flash->error($message);
}
$this->redirect($this->referer());
} else {
if (!$this->request->is('ajax')) {
throw new MethodNotAllowedException(__('This function can only be reached via AJAX.'));
} else {
$this->layout = 'ajax';
$this->set('elementId', $elementId);
$this->render('ajax/delete');
}
}
}
public function flattenJson($clusterId)
{
$cluster = $this->GalaxyElement->GalaxyCluster->fetchIfAuthorized($this->Auth->user(), $clusterId, array('edit'), true, false);
if ($this->request->is('post') || $this->request->is('put')) {
$json = $this->GalaxyElement->jsonDecode($this->request->data['GalaxyElement']['jsonData']);
$flattened = Hash::flatten($json);
$newElements = [];
foreach ($flattened as $k => $v) {
$newElements[] = ['key' => $k, 'value' => $v];
}
$cluster['GalaxyCluster']['GalaxyElement'] = $newElements;
$errors = $this->GalaxyElement->GalaxyCluster->editCluster($this->Auth->user(), $cluster, [], false);
if (empty($errors)) {
return $this->RestResponse->saveSuccessResponse('GalaxyElement', 'flattenJson', $clusterId, false);
} else {
$message = implode(', ', $errors);
return $this->RestResponse->saveFailResponse('GalaxyElement', 'flattenJson', $clusterId, $message, false);
}
}
$this->set('clusterId', $clusterId);
if ($this->request->is('ajax')) {
$this->layout = 'ajax';
$this->render('ajax/flattenJson');
}
$expanded = Hash::expand($keyedValue);
return $this->RestResponse->viewData($expanded);
}
}

View File

@ -128,4 +128,14 @@ class GalaxyElement extends AppModel
}
return $elements;
}
public function getExpandedJSONFromElements($elements)
{
$keyedValue = [];
foreach ($elements as $i => $element) {
$keyedValue[$element['GalaxyElement']['key']][] = $element['GalaxyElement']['value'];
}
$expanded = Hash::expand($keyedValue);
return $expanded;
}
}

View File

@ -88,8 +88,8 @@
<div class="row-fuild">
<div id="relations_container"></div>
</div>
<div class="row-fluid">
<div id="elements_div" class="span8"></div>
<div class="">
<div id="elements_div"></div>
</div>
</div>
<script type="text/javascript">

View File

@ -0,0 +1,10 @@
<?php
echo $this->element('/genericElements/Form/hardSoftDeleteForm', [
'title' => __('Delete Galaxy Cluster Element'),
'modelName' => __('galaxy element'),
'value' => $element['GalaxyElement']['key'] . ' :: ' . $element['GalaxyElement']['value'],
'id' => $element['GalaxyElement']['id'],
'hardDeleteURL' => sprintf('%s/galaxy_elements/delete/%s/1', $baseurl, $element['GalaxyElement']['id']),
'doNotShowHelp' => true
]);
?>

View File

@ -0,0 +1,25 @@
<?php
$modelForForm = 'GalaxyElement';
echo $this->element('genericElements/Form/genericForm', array(
'form' => $this->Form,
'data' => array(
'title' => __('Convert JSON into galaxy cluster\'s elements'),
'model' => $modelForForm,
'fields' => array(
array(
'field' => 'jsonData',
'label' => __('JSON'),
'type' => 'textarea',
'class' => 'input span6',
'div' => 'input clear'
),
),
'submit' => array(
'action' => $this->request->params['action'],
'ajaxSubmit' => sprintf('submitPopoverForm(\'%s\', \'flattenJson\', 0, 1)', h($clusterId))
),
),
));
?>
<?php echo $this->Js->writeBuffer(); // Write cached scripts

View File

@ -1,5 +1,5 @@
<?php
echo $this->element('/genericElements/IndexTable/index_table', array(
$indexOptions = array(
'data' => array(
'paginatorOptions' => array(
'update' => '#elements_div',
@ -7,6 +7,30 @@ echo $this->element('/genericElements/IndexTable/index_table', array(
'data' => $elements,
'top_bar' => array(
'children' => array(
array(
'children' => array(
array(
'active' => $context === 'all',
'text' => __('Tabular view'),
'onClick' => 'runIndexQuickFilter',
'onClickParams' => [
h($clusterId) . '/context:all',
$baseurl . '/galaxy_elements/index',
'#elements_div'
],
),
array(
'active' => $context === 'JSONView',
'text' => __('JSON view'),
'onClick' => 'runIndexQuickFilter',
'onClickParams' => [
h($clusterId) . '/context:JSONView',
$baseurl . '/galaxy_elements/index',
'#elements_div'
],
),
)
),
array(
'type' => 'simple',
'children' => array(
@ -14,35 +38,13 @@ echo $this->element('/genericElements/IndexTable/index_table', array(
'onClick' => 'openGenericModal',
'onClickParams' => [$baseurl . '/galaxy_elements/flattenJson/' . h($clusterId)],
'active' => true,
'text' => __('Add JSON'),
'text' => __('Add JSON as cluster\'s elements'),
'title' => __('The provided JSON will be converted into Galaxy Cluster Elements'),
'fa-icon' => 'plus',
'requirement' => $canModify,
'requirement' => $canModify && ($context === 'JSONView'),
),
)
),
array(
'type' => 'simple',
'children' => array(
array(
'active' => $context === 'all',
'url' => sprintf('%s/galaxy_elements/index/%s/context:all', $baseurl, $clusterId),
'text' => __('Tabular view'),
),
array(
'active' => $context === 'treeView',
'url' => sprintf('%s/galaxy_elements/index/%s/context:treeView', $baseurl, $clusterId),
'text' => __('Tree view'),
),
)
),
array(
'type' => 'search',
'button' => __('Filter'),
'placeholder' => __('Enter value to search'),
'data' => '',
'searchKey' => 'value'
)
)
),
'primary_id_path' => 'GalaxyElement.id',
@ -72,5 +74,26 @@ echo $this->element('/genericElements/IndexTable/index_table', array(
)
)
)
));
echo $this->Js->writeBuffer();
);
if ($context == 'JSONView') {
$indexOptions['data']['fields'] = [];
$indexOptions['data']['data'] = [];
$indexOptions['data']['skip_pagination'] = true;
$indexOptions['data']['actions'] = [];
}
echo $this->element('/genericElements/IndexTable/index_table', $indexOptions);
if ($context == 'JSONView') {
echo sprintf('<div id="elementJSONDiv" class="well well-small">%s</div>', json_encode($JSONElements));
}
echo $this->Js->writeBuffer();
?>
<script>
var $jsondiv = $('#elementJSONDiv');
if ($jsondiv.length > 0) {
$jsondiv.html(syntaxHighlightJson($jsondiv.text(), 8));
}
</script>