new: [object:add] UI to propose to merge into similar objects - WiP

pull/4530/head
mokaddem 2019-04-18 16:58:49 +02:00
parent 8ff29cc2e4
commit 67b3f29dd0
2 changed files with 176 additions and 5 deletions

View File

@ -83,12 +83,51 @@ class ObjectsController extends AppController
}
$this->set('sg', $sg);
}
// try to fetch similar objects
$cur_attrs = Hash::extract($this->request->data, 'Attribute.{n}.value');
$options = array(
'conditions' => array(
'event_id' => $event_id,
'NOT' => array(
'AND' => array(
'object_id' => $object_id,
'object_id' => 0
)
),
'value' => $cur_attrs
),
);
$conditions = array(
'AND' => array(
$this->MispObject->buildConditions($this->Auth->user()),
'event_id' => $event_id,
'NOT' => array(
'AND' => array(
'object_id' => $object_id,
'object_id' => 0
)
),
'value' => $cur_attrs
)
);
$similar_object_ids = $this->MispObject->Attribute->find('list', array(
'conditions' => $conditions,
'recursive' => -1,
'fields' => 'object_id',
'group' => 'object_id'
));
$this->set('distributionLevels', $this->MispObject->Attribute->distributionLevels);
$this->set('action', $action);
$this->set('template', $template);
$this->set('object_id', $object_id);
$this->set('event', $event);
$this->set('data', $this->request->data);
if (count($similar_object_ids) < 5) {
$similar_objects = $this->MispObject->fetchObjects($this->Auth->user(), array('conditions' => array('Object.id' => $similar_object_ids, 'Object.template_uuid' => $template['ObjectTemplate']['uuid'])));
$this->set('similar_objects', $similar_objects);
}
}
@ -176,6 +215,14 @@ class ObjectsController extends AppController
if (isset($this->request->data['request'])) {
$this->request->data = $this->request->data['request'];
}
// if (isset($this->request->data['Object']['mergeIntoObject'])) {
// if ($this->request->data['Object']['mergeIntoObject'] != '0') {
// $merge_into_object_id = $this->request->data['Object']['mergeIntoObject'];
// return $this->revise_object('edit', $eventId, $templateId, $merge_into_object_id);
// }
// unset($this->request->data['Object']['mergeIntoObject']);
// }
if (isset($this->request->data['Object']['data'])) {
$this->request->data = json_decode($this->request->data['Object']['data'], true);
}
@ -354,6 +401,12 @@ class ObjectsController extends AppController
$this->Flash->error('Object cannot be edited, no valid template found.');
$this->redirect(array('controller' => 'events', 'action' => 'view', $object['Object']['event_id']));
}
if (isset($this->params['named']['attributeToInject'])) {
$attributeToInject = json_decode(base64_decode($this->params['named']['attributeToInject']), true);
debug($object);
debug($attributeToInject);
}
$template = $this->MispObject->prepareTemplate($template, $object);
$enabledRows = false;

View File

@ -12,6 +12,14 @@
);
echo $this->Form->input('data', $formSettings);
?>
<div class='hidden'>
<?php
echo $this->Form->input('mergeIntoObject', array(
'value' => 0,
'div' => false
));
?>
</div>
<div style="margin-bottom:20px;">
<table class="table table-condensed table-striped">
<tbody>
@ -38,7 +46,7 @@
<td><?php echo h($data['Object']['comment']); ?></td>
</tr>
<tr>
<table class="table table-condensed table-striped">
<table id="attribute_table" class="table table-condensed table-striped">
<thead>
<th><?php echo __('Attribute');?></th>
<th><?php echo __('Category');?></th>
@ -51,10 +59,16 @@
</thead>
<tbody>
<?php
$simple_flattened_object = array();
$simple_flattened_object_noval = array();
$attributeFields = array('category', 'type', 'value', 'to_ids' , 'comment', 'uuid', 'distribution');
if (!empty($data['Attribute'])):
foreach ($data['Attribute'] as $attribute):
echo '<tr>';
$cur_flat = h($attribute['object_relation']) . '.' . h($attribute['type']) . '.' .h($attribute['value']);
$cur_flat_noval = h($attribute['object_relation']) . '.' . h($attribute['type']);
$simple_flattened_attribute[$cur_flat] = 1;
$simple_flattened_attribute_noval[$cur_flat_noval] = 1;
echo sprintf('<tr data-curflat="%s" data-curflatnoval="%s">', h($cur_flat), h($cur_flat_noval));
echo '<td>' . h($attribute['object_relation']) . '</td>';
foreach ($attributeFields as $field):
if ($field == 'distribution') {
@ -81,15 +95,119 @@
</tbody>
</table>
</div>
<?php
echo $this->Form->button(__('Submit'), array('class' => 'btn btn-primary'));
?>
<?php echo $this->Form->button(__('Submit'), array('class' => 'btn btn-primary')); ?>
<a href="<?php echo $baseurl . '/events/view/' . h($event['Event']['id']); ?>" style="margin-left:10px;" class="btn btn-inverse"><?php echo __('Cancel');?></a>
<?php if (!empty($similar_objects) && $action !== 'edit'): ?>
<?php
// debug($data);
?>
<?php echo '<h3>' . __('The event have similar object.') . '</h3>'; ?>
<?php echo '<h5>' . __('Would you like to merge your new object with one of the following?') . '</h5>'; ?>
<div class="row" style="margin-bottom: 20px;">
<?php foreach ($similar_objects as $i => $object): ?>
<div style="border: 1px solid #3465a4 ; border-radius: 5px;" class="span5 similarObjectPanel">
<div class="blueElement" style="padding: 4px 5px;">
<div style="text-align: center;">
<span class="btn btn-success useCursorPointer" onclick="setMergeObject(<?php echo h($object['Object']['id']) ?>)"><?php echo __('Merge'); ?></span>
</div>
<div>
<span class="bold"><?php echo __('ID') . ':'; ?></span>
<a href="<?php echo $baseurl . '/objects/edit/' . h($object['Object']['id']); ?>" style="color: white;"><?php echo h($object['Object']['id']); ?></a>
</div>
<div>
<span class="bold"><?php echo __('Name') . ':'; ?></span>
<span><?php echo h($object['Object']['name']); ?></span>
</div>
<div>
<span class="bold"><?php echo __('Description') . ':'; ?></span>
<span><?php echo h($object['Object']['description']); ?></span><br>
</div>
<div>
<span class="bold"><?php echo __('Distribution') . ':'; ?></span>
<span><?php echo h($object['Object']['distribution']); ?></span>
</div>
</div>
<table class="table table-striped table-condensed" style="margin-bottom: 3px;">
<tbody>
<?php foreach ($object['Attribute'] as $attribute): ?>
<?php
$simple_flattened_similar_attribute = h($attribute['object_relation']) . '.' . h($attribute['type']) . '.' .h($attribute['value']);
$simple_flattened_similar_attribute_noval = h($attribute['object_relation']) . '.' . h($attribute['type']);
$classname = '';
$to_highlight = '';
if (
isset($simple_flattened_attribute_noval[$simple_flattened_similar_attribute_noval])
&& !isset($simple_flattened_attribute[$simple_flattened_similar_attribute])
) {
$classname = 'warning';
$to_highlight = $simple_flattened_similar_attribute_noval;
} else if (!isset($simple_flattened_attribute[$simple_flattened_similar_attribute])) {
$classname = 'success';
}
?>
<tr class="<?php echo $classname ?>" data-tohighlight="<?php echo h($to_highlight); ?>">
<td><?php echo h($attribute['object_relation']); ?></td>
<td><?php echo h($attribute['category']); ?></td>
<td><?php echo h($attribute['type']); ?></td>
<td><?php echo h($attribute['value']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php
echo $this->Form->end();
?>
</div>
<script>
function setMergeObject(object_id) {
var cur_object = $('input[name="data[Object][data]"]').val();
window.location = "<?php echo $baseurl . '/objects/edit/'; ?>" + object_id + "/attributeToInject:" + btoa(cur_object);
}
function highlight_rows($panel, state) {
var rows = $panel.find('tr.warning');
var to_highlight = [];
rows.each(function() {
to_highlight.push($(this).data().tohighlight);
});
to_highlight.forEach(function(curflat) {
var $row_to_highlight = $('#attribute_table').find('tr[data-curflatnoval="' + curflat + '"]');
if (state === undefined) {
$row_to_highlight.addClass('error');
} else if (state) {
$row_to_highlight.addClass('error');
} else {
$row_to_highlight.removeClass('error');
}
});
}
function inject_merge_result($panel, state) {
}
$(document).ready(function() {
$('.similarObjectPanel').hover(
function() {
var $panel = $(this);
highlight_rows($panel);
inject_merge_result($panel);
},
function() {
var $panel = $(this);
highlight_rows($panel, false);
inject_merge_result($panel, false);
}
);
});
</script>
<?php
echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'event', 'menuItem' => 'addObject', 'event' => $event));
?>