MISP/app/View/Attributes/add.ctp

290 lines
9.7 KiB
Plaintext
Raw Normal View History

<div class="attributes <? if (!$ajax) echo 'form';?>">
<?php echo $this->Form->create('Attribute', array('id'));?>
2014-03-25 13:56:00 +01:00
<legend><?php echo __('Add Attribute'); ?></legend>
<fieldset>
2014-03-25 13:56:00 +01:00
<div id="formWarning" class="message ajaxMessage"></div>
<div class="add_attribute_fields">
<?php
echo $this->Form->hidden('event_id');
echo $this->Form->input('category', array(
'empty' => '(choose one)'
));
echo $this->Form->input('type', array(
'empty' => '(first choose category)'
));
if ('true' == Configure::read('MISP.sync')) {
$initialDistribution = 3;
if (Configure::read('MISP.default_attribute_distribution') != null) {
if (Configure::read('MISP.default_attribute_distribution') === 'event') {
$initialDistribution = $currentDist;
} else {
$initialDistribution = Configure::read('MISP.default_attribute_distribution');
}
}
echo $this->Form->input('distribution', array(
'options' => array($distributionLevels),
'label' => 'Distribution',
'selected' => $initialDistribution,
));
}
echo $this->Form->input('value', array(
'type' => 'textarea',
'error' => array('escape' => false),
'div' => 'input clear',
'class' => 'input-xxlarge'
2013-06-03 10:48:38 +02:00
));
echo $this->Form->input('comment', array(
'type' => 'text',
'label' => 'Contextual Comment',
'error' => array('escape' => false),
'div' => 'input clear',
'class' => 'input-xxlarge'
));
?>
<div class="input clear"></div>
<?php
echo $this->Form->input('to_ids', array(
'checked' => false,
'data-content' => isset($attrDescriptions['signature']['formdesc']) ? $attrDescriptions['signature']['formdesc'] : $attrDescriptions['signature']['desc'],
'label' => 'for Intrusion Detection System',
));
echo $this->Form->input('batch_import', array(
'type' => 'checkbox',
'data-content' => 'Create multiple attributes one per line',
));
// link an onchange event to the form elements
$this->Js->get('#AttributeCategory')->event('change', 'formCategoryChanged("#AttributeCategory")');
2013-06-03 10:48:38 +02:00
?>
</div>
</fieldset>
<p style="color:red;font-weight:bold;display:none;" id="warning-message">Warning: You are about to share data that is of a classified nature (Attribution / targeting data). Make sure that you are authorised to share this.</p>
<?php if ($ajax): ?>
<div class="overlay_spacing">
<table>
<tr>
<td style="vertical-align:top">
2014-03-25 13:56:00 +01:00
<span id="submitButton" class="btn btn-primary" onClick="submitForm()">Submit</span>
</td>
<td style="width:540px;">
<p style="color:red;font-weight:bold;display:none;text-align:center" id="warning-message">Warning: You are about to share data that is of a classified nature (Attribution / targeting data). Make sure that you are authorised to share this.</p>
</td>
<td style="vertical-align:top;">
<span class="btn btn-inverse" id="cancel_attribute_add">Cancel</span>
</td>
</tr>
</table>
</div>
<?php
else:
echo $this->Form->button('Submit', array('class' => 'btn btn-primary'));
endif;
echo $this->Form->end();
?>
</div>
<?php
if(!$ajax) {
$event['Event']['id'] = $this->request->data['Attribute']['event_id'];
$event['Event']['published'] = $published;
echo $this->element('side_menu', array('menuList' => 'event', 'menuItem' => 'addAttribute', 'event' => $event));
}
?>
2013-06-03 15:59:22 +02:00
<script type="text/javascript">
//
//Generate Category / Type filtering array
//
var category_type_mapping = new Array();
<?php
foreach ($categoryDefinitions as $category => $def) {
echo "category_type_mapping['" . addslashes($category) . "'] = {";
$first = true;
foreach ($def['types'] as $type) {
if ($first) $first = false;
else echo ', ';
echo "'" . addslashes($type) . "' : '" . addslashes($type) . "'";
}
echo "}; \n";
}
?>
function formCategoryChanged(id) {
// fill in the types
var options = $('#AttributeType').prop('options');
$('option', $('#AttributeType')).remove();
$.each(category_type_mapping[$('#AttributeCategory').val()], function(val, text) {
options[options.length] = new Option(text, val);
});
// enable the form element
$('#AttributeType').prop('disabled', false);
}
//
// Generate tooltip information
//
var formInfoValues = new Array();
var fieldsArrayAttribute = new Array('AttributeCategory', 'AttributeType', 'AttributeDistribution', 'AttributeValue', 'AttributeComment', 'AttributeToIds', 'AttributeBatchImport');
<?php
foreach ($typeDefinitions as $type => $def) {
$info = isset($def['formdesc']) ? $def['formdesc'] : $def['desc'];
echo "formInfoValues['" . addslashes($type) . "'] = \"" . addslashes($info) . "\";\n"; // as we output JS code we need to add slashes
}
foreach ($categoryDefinitions as $category => $def) {
$info = isset($def['formdesc']) ? $def['formdesc'] : $def['desc'];
echo "formInfoValues['" . addslashes($category) . "'] = \"" . addslashes($info) . "\";\n"; // as we output JS code we need to add slashes
}
foreach ($distributionDescriptions as $type => $def) {
$info = isset($def['formdesc']) ? $def['formdesc'] : $def['desc'];
echo "formInfoValues['" . addslashes($type) . "'] = \"" . addslashes($info) . "\";\n"; // as we output JS code we need to add slashes
}
?>
2013-06-06 16:36:28 +02:00
$(document).ready(function() {
2013-06-06 16:36:28 +02:00
$("#AttributeType, #AttributeCategory, #Attribute, #AttributeDistribution").on('mouseover', function(e) {
var $e = $(e.target);
if ($e.is('option')) {
$('#'+e.currentTarget.id).popover('destroy');
$('#'+e.currentTarget.id).popover({
trigger: 'focus',
2013-06-06 16:36:28 +02:00
placement: 'right',
container: 'body',
2013-06-06 16:36:28 +02:00
content: formInfoValues[$e.val()],
}).popover('show');
}
});
$("input, label").on('mouseleave', function(e) {
$('#'+e.currentTarget.id).popover('destroy');
});
$("input, label").on('mouseover', function(e) {
var $e = $(e.target);
$('#'+e.currentTarget.id).popover('destroy');
$('#'+e.currentTarget.id).popover({
trigger: 'focus',
2013-06-06 16:36:28 +02:00
placement: 'right',
container: 'body',
2013-06-06 16:36:28 +02:00
}).popover('show');
// $('#'+e.currentTarget.id).on('mouseleave', $('#'+e.currentTarget.id).popover('destroy');
//$('#'+e.currentTarget.id).on('mouseout', $('#'+e.currentTarget.id).popover('destroy'));
2013-06-06 16:36:28 +02:00
});
// workaround for browsers like IE and Chrome that do now have an onmouseover on the 'options' of a select.
// disadvangate is that user needs to click on the item to see the tooltip.
// no solutions exist, except to generate the select completely using html.
$("#AttributeType, #AttributeCategory, #Attribute, #AttributeDistribution").on('change', function(e) {
if (this.id === "AttributeCategory") {
var select = document.getElementById("AttributeCategory");
if (select.value === 'Attribution' || select.value === 'Targeting data') {
$("#warning-message").show();
} else {
$("#warning-message").hide();
}
}
var $e = $(e.target);
$('#'+e.currentTarget.id).popover('destroy');
$('#'+e.currentTarget.id).popover({
trigger: 'focus',
placement: 'right',
container: 'body',
content: formInfoValues[$e.val()],
}).popover('show');
});
<?php if ($ajax): ?>
$('#cancel_attribute_add').click(function() {
$('#gray_out').hide();
$('#attribute_add_form').hide();
});
<?php endif; ?>
2013-06-06 16:36:28 +02:00
});
// Submit button should post the form results to the add action and check the response
function submitForm() {
$.ajax({
data: $("#submitButton").closest("form").serialize(),
success:function (data, textStatus) {
handleAjaxResponse(data);
},
type:"post",
url:"/attributes/add/<?php echo $event_id; ?>"
});
};
function handleAjaxResponse(response) {
if (response === "\"saved\"") {
$("#gray_out").hide();
$("#attribute_add_form").hide();
updateAttributeIndexOnSuccess();
} else {
var savedArray = saveValuesForPersistance();
$.ajax({
async:true,
dataType:"html",
success:function (data, textStatus) {
$("#attribute_add_form").html(data);
responseArray = JSON.parse(response);
handleValidationErrors(responseArray);
2014-03-25 13:56:00 +01:00
if (!isEmpty(responseArray)) {
$("#formWarning").show();
$("#formWarning").html('The attribute could not be saved. Please, try again.');
}
recoverValuesFromPersistance(savedArray);
},
url:"/attributes/add/<?php echo $event_id; ?>"
});
}
}
2014-03-25 13:56:00 +01:00
function isEmpty(obj) {
var name;
for (name in obj) {
return false;
}
return true;
}
function updateAttributeIndexOnSuccess() {
$.ajax({
beforeSend: function (XMLHttpRequest) {
$(".loading").show();
},
dataType:"html",
success:function (data, textStatus) {
$(".loading").hide();
$("#attributes_div").html(data);
},
url:"/events/view/<?php echo $event_id; ?>/attributesPage:1"
});
}
// before we update the form (in case the action failed), we want to retrieve the data from every field, so that we can set the fields in the new form that we fetch
function saveValuesForPersistance() {
var formPersistanceArray = new Array();
for (i = 0; i < fieldsArrayAttribute.length; i++) {
formPersistanceArray[fieldsArrayAttribute[i]] = document.getElementById(fieldsArrayAttribute[i]).value;
}
return formPersistanceArray;
}
function recoverValuesFromPersistance(formPersistanceArray) {
for (i = 0; i < fieldsArrayAttribute.length; i++) {
document.getElementById(fieldsArrayAttribute[i]).value = formPersistanceArray[fieldsArrayAttribute[i]];
}
}
function handleValidationErrors(responseArray) {
for (var k in responseArray) {
var elementName = k.charAt(0).toUpperCase() + k.slice(1);
$("#Attribute" + elementName).parent().addClass("error");
$("#Attribute" + elementName).parent().append("<div class=\"error-message\">" + responseArray[k] + "</div>");
}
}
</script>
<?php echo $this->Js->writeBuffer(); // Write cached scripts