chg: [object:fromAttributes] Shows selected types and started

implementaion of the actual object creation - WiP
pull/4672/head
mokaddem 2019-05-20 14:30:20 +02:00
parent f751c822b5
commit 44d71a327a
5 changed files with 64 additions and 11 deletions

View File

@ -249,7 +249,7 @@ class ACLComponent extends Component
'edit' => array('perm_add'),
'get_row' => array('perm_add'),
'orphanedObjectDiagnostics' => array(),
'mergeObjectsFromAttributes' => array(),
'groupAttributesIntoObject' => array(),
'revise_object' => array('perm_add'),
'view' => array('*'),
),

View File

@ -996,7 +996,9 @@ class ObjectsController extends AppController
function proposeObjectsFromAttributes($event_id, $selected_attributes='[]')
{
$selected_attributes = json_decode($selected_attributes, true);
$potential_templates = $this->MispObject->validObjectsFromAttributeTypes($this->Auth->user(), $event_id, $selected_attributes);
$res = $this->MispObject->validObjectsFromAttributeTypes($this->Auth->user(), $event_id, $selected_attributes);
$potential_templates = $res['templates'];
$attribute_types = $res['types'];
usort($potential_templates, function($a, $b) {
if ($a['ObjectTemplate']['id'] == $b['ObjectTemplate']['id']) {
return 0;
@ -1011,10 +1013,11 @@ class ObjectsController extends AppController
}
});
$this->set('potential_templates', $potential_templates);
$this->set('selected_types', $attribute_types);
$this->set('event_id', $event_id);
}
function mergeObjectsFromAttributes($event_id, $selected_template, $selected_attribute_ids='[]')
function groupAttributesIntoObject($event_id, $selected_template, $selected_attribute_ids='[]')
{
if ($this->request->is('post')) {
$event = $this->MispObject->Event->find('first', array(
@ -1032,6 +1035,48 @@ class ObjectsController extends AppController
if (empty($template)) {
throw new NotFoundException(__('Invalid template.'));
}
$distribution = $this->request->data['Object']['distribution'];
$sharing_group_id = $this->request->data['Object']['sharing_group_id'];
$comment = $this->request->data['Object']['comment'];
$selected_attribute_ids = $this->request->data['Object']['selectedAttributeIds'];
$selected_object_relation_mapping = $this->request->data['Object']['selectedObjectRelationMapping'];
if ($distribution == 4) {
$sg = $this->MispObject->SharingGroup->find('first', array(
'conditions' => array('SharingGroup.id' => $sharing_group_id),
'recursive' => -1,
'fields' => array('SharingGroup.id', 'SharingGroup.name'),
'order' => false
));
if (empty($sg)) {
throw new NotFoundException(__('Invalid sharing group.'));
}
}
$object = array('Object' => array(
'distribution' => $distribution,
'sharing_group_id' => $sharing_group_id,
'comment' => $comment
));
$attributes = $this->MispObject->Attribute->fetchAttributes($this->Auth->user(), array('conditions' => array(
'Attribute.id' => $selected_attribute_ids,
'Attribute.event_id' => $event_id,
'Attribute.object_id' => 0
)));
foreach ($attributes as $i => $attribute) {
if (isset($selected_object_relation_mapping[$attribute['Attribute']['id']])) {
$object_relation = $selected_object_relation_mapping[$attribute['Attribute']['id']];
$attributes['Attribute'][$i]['object_id'] = $created_id;
$attributes['Attribute'][$i]['object_relation'] = $object_relation;
}
}
$object['Attribute'] = $attributes;
debug($object);
// $result = $this->MispObject->saveObject($object, $event_id, $template, $this->Auth->user());
if ($result) {
return $this->RestResponse->saveSuccessResponse('Objects', 'Created from Attributes', '1', $this->response->type());
} else {
$error = __('Failed to create an Object from Attributes.');
return $this->RestResponse->saveFailResponse('Objects', 'add', false, $error, $this->response->type());
}
} else {
$selected_attribute_ids = json_decode($selected_attribute_ids, true);
$selected_attributes = $this->MispObject->Attribute->fetchAttributes($this->Auth->user(), array('conditions' => array(

View File

@ -816,6 +816,6 @@ class MispObject extends AppModel
$templates[$i]['ObjectTemplate']['invalidTypes'] = $res['invalidTypes'];
$templates[$i]['ObjectTemplate']['invalidTypesMultiple'] = $res['invalidTypesMultiple'];
}
return $templates;
return array('templates' => $templates, 'types' => $attribute_types);
}
}

View File

@ -1,6 +1,6 @@
<button class="btn btn-inverse" onclick="showObjectProposition()"><i class="fas fa-chevron-left"></i></button>
<?php
echo $this->Form->create('Object');
echo $this->Form->create('Object', array('url' => $baseurl . '/objects/groupAttributesIntoObject/' . $event_id . '/' . $selectedTemplateTd));
?>
<dl style="margin-top: 10px;">
<dt><?php echo __('Object Template');?></dt>
@ -38,8 +38,10 @@ echo $this->Form->create('Object');
)); ?>
<div class="hidden">
<?php
echo $this->Form->input('selectedTemplateId', array('type' => 'hidden', 'value' => $selectedTemplateTd));
echo $this->Form->input('selectedAttributeIds', array('type' => 'hidden', 'value' => json_encode($selectedAttributeIds)));
// echo $this->Form->input('selectedTemplateId', array('type' => 'hidden', 'value' => $selectedTemplateTd));
// echo $this->Form->input('selectedAttributeIds', array('type' => 'hidden', 'value' => json_encode($selectedAttributeIds)));
echo $this->Form->input('selectedTemplateId', array('type' => 'text', 'value' => $selectedTemplateTd));
echo $this->Form->input('selectedAttributeIds', array('type' => 'text', 'value' => json_encode($selectedAttributeIds)));
echo $this->Form->input('selectedObjectRelationMapping', array('value' => ''));
echo $this->Form->end();
?>
@ -98,7 +100,7 @@ $(".Object_distribution_select").change(function() {
function submitMergeAttributeIntoObjectForm(btn) {
var $btn = $(btn);
var $form = $('#ObjectMergeObjectsFromAttributesForm');
var $form = $('#ObjectGroupAttributesIntoObjectForm');
var attribute_mapping = {};
$('#attributeMappingBody').find('tr').each(function() {
var $tr = $(this);
@ -113,7 +115,7 @@ function submitMergeAttributeIntoObjectForm(btn) {
$btn.html('<it class="fa fa-spinner fa-spin"></it>');
},
success:function (data, textStatus) {
location.reload();
// location.reload();
},
error:function() {
showMessage('fail', 'Could not merge Attributes into an Object.');
@ -122,7 +124,7 @@ function submitMergeAttributeIntoObjectForm(btn) {
// close all
},
type:"post",
url: "<?php echo $baseurl . '/objects/mergeObjectsFromAttributes/' . $event_id . '/' . $selectedTemplateTd; ?>"
url: $form.attr('action')
});
}
</script>

View File

@ -1,4 +1,10 @@
<div style="max-width: 1000px; max-height: 800px; overflow-y: auto;">
<div>
<?php echo !empty($selected_types) ? '<strong>' . __('Selected types: ') . '</strong>' : ''; ?>
<?php foreach ($selected_types as $type): ?>
<span class="label label-info"><?php echo h($type) ?></span>
<?php endforeach; ?>
</div>
<?php if (empty($potential_templates)): ?>
<?php echo __('No matching Object.'); ?>
<?php else: ?>
@ -63,7 +69,7 @@
$parentDIV.css({height: bb.height, width: bb.width});
$('#tableGroupAttributeIntoObject').toggle('slide');
$('#resultPreview').show().html('<div style="align-items: center; justify-content: center; display: flex; height: 100%; width: 100%"><i class="fas fa-spinner fa-spin" style="font-size: xx-large;"></i></div>');
$.get('<?php echo $baseurl . '/objects/mergeObjectsFromAttributes/' . h($event_id) . '/' ?>' + object_template_id + '/' + getSelected(), function(data) {
$.get('<?php echo $baseurl . '/objects/groupAttributesIntoObject/' . h($event_id) . '/' ?>' + object_template_id + '/' + getSelected(), function(data) {
$('#resultPreview').html(data);
});
}