completed integria setup and incident creation and update

This commit is contained in:
alejandro-campos 2019-09-17 13:38:17 +02:00
parent 11cb53582c
commit 87f8badc18
6 changed files with 360 additions and 141 deletions

View File

@ -131,18 +131,23 @@ if (get_parameter('update_config', 0) == 1) {
}
// Get parameters from Integria IMS API.
$group_values = [];
$integria_group_values = [];
$integria_criticity_values = [];
$integria_users_values = [];
$integria_types_values = [];
$integria_status_values = [];
$integria_groups_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_groups', []);
get_array_from_csv_data($integria_groups_csv, $group_values);
get_array_from_csv_data_pair($integria_groups_csv, $integria_group_values);
$integria_status_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incidents_status', []);
get_array_from_csv_data_pair($integria_status_csv, $integria_status_values);
$integria_criticity_levels_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incident_priorities', []);
get_array_from_csv_data($integria_criticity_levels_csv, $integria_criticity_values);
get_array_from_csv_data_pair($integria_criticity_levels_csv, $integria_criticity_values);
$integria_users_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_users', []);
@ -156,7 +161,7 @@ foreach ($csv_array as $csv_line) {
$integria_types_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_types', []);
get_array_from_csv_data($integria_types_csv, $integria_types_values);
get_array_from_csv_data_pair($integria_types_csv, $integria_types_values);
// Enable table.
$table_enable = new StdClass();
@ -226,11 +231,41 @@ $table_cr_settings->class = 'databox filters';
$table_cr_settings->size['name'] = '30%';
$table_cr_settings->style['name'] = 'font-weight: bold';
// Custom response incident title.
$row = [];
$row['name'] = __('Title');
$row['control'] = html_print_input_text(
'incident_title',
$config['incident_title'],
__('Name'),
50,
100,
true,
false,
false
).ui_print_help_icon('response_macros', true);
$table_cr_settings->data['custom_response_incident_title'] = $row;
// Custom response incident description.
$row = [];
$row['name'] = __('Description');
$row['control'] = html_print_input_text(
'incident_content',
$config['incident_content'],
'',
50,
100,
true,
false,
false
).ui_print_help_icon('response_macros', true);
$table_cr_settings->data['custom_response_incident_content'] = $row;
// Custom response default group.
$row = [];
$row['name'] = __('Default group');
$row['name'] = __('Group');
$row['control'] = html_print_select(
$group_values,
$integria_group_values,
'default_group',
$config['default_group'],
'',
@ -246,7 +281,7 @@ $table_cr_settings->data['custom_response_def_group'] = $row;
// Custom response default criticity.
$row = [];
$row['name'] = __('Default criticity');
$row['name'] = __('Criticity');
$row['control'] = html_print_select(
$integria_criticity_values,
'default_criticity',
@ -262,27 +297,31 @@ $row['control'] = html_print_select(
);
$table_cr_settings->data['custom_response_def_criticity'] = $row;
// Custom response default creator.
$row = [];
$row['name'] = __('Creator');
$row['control'] = html_print_autocomplete_users_from_integria(
'default_creator',
$config['default_creator'],
true
);
$table_cr_settings->data['custom_response_def_creator'] = $row;
// Custom response default owner.
$row = [];
$row['name'] = __('Default owner');
$row['control'] = html_print_select(
$integria_users_values,
$row['name'] = __('Owner');
$row['control'] = html_print_autocomplete_users_from_integria(
'default_owner',
$config['default_owner'],
'',
__('Select'),
0,
true,
false,
true,
'',
false
true
);
$table_cr_settings->data['custom_response_def_owner'] = $row;
// Custom response default incident type.
$row = [];
$row['name'] = __('Incident type');
$row['name'] = __('Type');
$row['control'] = html_print_select(
$integria_types_values,
'incident_type',
@ -298,35 +337,23 @@ $row['control'] = html_print_select(
);
$table_cr_settings->data['custom_response_incident_type'] = $row;
// Custom response incident title.
// Custom response default incident status.
$row = [];
$row['name'] = __('Incident title');
$row['control'] = html_print_input_text(
'incident_title',
$config['incident_title'],
__('Name'),
50,
100,
true,
false,
false
).ui_print_help_icon('response_macros', true);
$table_cr_settings->data['custom_response_incident_title'] = $row;
// Custom response incident content.
$row = [];
$row['name'] = __('Incident content');
$row['control'] = html_print_input_text(
'incident_content',
$config['incident_content'],
$row['name'] = __('Status');
$row['control'] = html_print_select(
$integria_status_values,
'incident_status',
$config['incident_status'],
'',
50,
100,
__('Select'),
0,
true,
false,
true,
'',
false
).ui_print_help_icon('response_macros', true);
$table_cr_settings->data['custom_response_incident_content'] = $row;
);
$table_cr_settings->data['custom_response_incident_status'] = $row;
// Test.
$row = [];
@ -374,7 +401,7 @@ if ($has_connection != false) {
// Form custom response settings.
echo '<div id="form_custom_response_settings">';
echo '<fieldset>';
echo '<legend>'.__('Custom response settings').'</legend>';
echo '<legend>'.__('Incident default values').'</legend>';
html_print_table($table_cr_settings);
@ -503,6 +530,7 @@ echo '</form>';
type: "POST",
url: "ajax.php",
dataType: "json",
timeout: timeout ? timeout * 1000 : 0,
data: data
})
.done(function(data, textStatus, xhr) {

View File

@ -5451,7 +5451,7 @@ if (!function_exists('getallheaders')) {
*
* @return boolean True if API request succeeded, false if API request failed.
*/
function integria_api_call($api_hostname, $user, $user_pass, $api_pass, $operation, $params_array, $show_credentials_error_msg=false)
function integria_api_call($api_hostname, $user, $user_pass, $api_pass, $operation, $params_array=[], $show_credentials_error_msg=false)
{
$params_string = implode(',', $params_array);
@ -5466,8 +5466,8 @@ function integria_api_call($api_hostname, $user, $user_pass, $api_pass, $operati
// Build URL for API request.
$url = $api_hostname.'/integria/include/api.php';
ob_start();
$out = fopen('php://output', 'w');
// ob_start();
// $out = fopen('php://output', 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
@ -5478,9 +5478,8 @@ function integria_api_call($api_hostname, $user, $user_pass, $api_pass, $operati
curl_setopt($ch, CURLOPT_STDERR, $out);
$result = curl_exec($ch);
fclose($out);
$debug = ob_get_clean();
// fclose($out);
// $debug = ob_get_clean();
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = false;
@ -5504,7 +5503,7 @@ function integria_api_call($api_hostname, $user, $user_pass, $api_pass, $operati
// Parse CSV consisting of one or more lines of the form key-value pair into an array.
function get_array_from_csv_data($csv_data, &$array_values)
function get_array_from_csv_data_pair($csv_data, &$array_values)
{
$csv_array = explode("\n", $csv_data);

View File

@ -1467,6 +1467,10 @@ function config_update_config()
$error_update[] = __('Integria default criticity');
}
if (!config_update_value('default_creator', (string) get_parameter('default_creator', $config['default_creator']))) {
$error_update[] = __('Integria default creator');
}
if (!config_update_value('default_owner', (string) get_parameter('default_owner', $config['default_owner']))) {
$error_update[] = __('Integria default owner');
}
@ -1475,6 +1479,10 @@ function config_update_config()
$error_update[] = __('Integria default incident type');
}
if (!config_update_value('incident_status', (int) get_parameter('incident_status', $config['incident_status']))) {
$error_update[] = __('Integria default incident status');
}
if (!config_update_value('incident_title', (string) get_parameter('incident_title', $config['incident_title']))) {
$error_update[] = __('Integria default incident title');
}

View File

@ -3356,3 +3356,108 @@ function html_print_input($data, $wrapper='div', $input_only=false)
return $output;
}
/**
* Print an autocomplete input filled out with Integria IMS users.
*
* @param string $name The name of ajax control, by default is "users".
* @param string $default The default value to show in the ajax control.
* @param boolean $return If it is true return a string with the output instead to echo the output.
* @param string $size Size.
*
* @return mixed If the $return is true, return the output as string.
*/
function html_print_autocomplete_users_from_integria(
$name='users',
$default='',
$return=false,
$size='30'
) {
global $config;
ob_start();
html_print_input_text_extended(
$name,
$default,
'text-'.$name,
'',
$size,
100,
false,
'',
['style' => 'background: url(images/user_green.png) no-repeat right;']
);
html_print_input_hidden($name.'_hidden', $id_agent_module);
ui_print_help_tip(__('Type at least two characters to search the user.'), false);
$javascript_ajax_page = ui_get_full_url('ajax.php', false, false, false, false);
?>
<script type="text/javascript">
function escapeHTML (str)
{
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
}
$(document).ready (function () {
$("#text-<?php echo $name; ?>").autocomplete({
minLength: 2,
source: function( request, response ) {
var term = request.term; //Word to search
data_params = {
page: "include/ajax/integria_incidents.ajax",
search_term: term,
get_users: 1,
};
jQuery.ajax ({
data: data_params,
async: false,
type: "POST",
url: action="<?php echo $javascript_ajax_page; ?>",
timeout: 10000,
dataType: "json",
success: function (data) {
console.log(data);
temp = [];
$.each(data, function (id, module) {
temp.push({
'value' : id,
'label' : module});
});
response(temp);
}
});
},
change: function( event, ui ) {
if (!ui.item)
$("input[name='<?php echo $name; ?>_hidden']")
.val(0);
return false;
},
select: function( event, ui ) {
$("input[name='<?php echo $name; ?>_hidden']")
.val(ui.item.value);
$("#text-<?php echo $name; ?>").val( ui.item.label );
return false;
}
}
);
});
</script>
<?php
$output = ob_get_clean();
if ($return) {
return $output;
} else {
echo $output;
}
}

View File

@ -5342,6 +5342,25 @@ table.info_table.policy_sub_table {
color: #848484;
}
/* Inputs type text shown as a black line */
.integria_incidents_options input[type="text"] {
background-color: transparent;
border: none;
border-radius: 0;
border-bottom: 1px solid #ccc;
font-family: "lato-bolder", "Open Sans", sans-serif;
font-size: 10pt;
padding: 2px 5px;
box-sizing: border-box;
background-repeat: no-repeat;
background-position: left bottom 2px;
margin-bottom: 4px;
}
.integria_incident_options input[readonly] {
color: #848484;
}
/*
* ---------------------------------------------------------------------
* - CLASSES FOR THE NEW TOGGLES -

View File

@ -39,19 +39,24 @@ if ($has_connection === false) {
return;
}
// If everything OK, get parameters from Integria IMS API.
$group_values = [];
// If everything OK, get parameters from Integria IMS API in order to populate combos.
$integria_group_values = [];
$integria_criticity_values = [];
$integria_users_values = [];
$integria_types_values = [];
$integria_status_values = [];
$integria_groups_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_groups', []);
get_array_from_csv_data($integria_groups_csv, $group_values);
get_array_from_csv_data_pair($integria_groups_csv, $integria_group_values);
$integria_status_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incidents_status', []);
get_array_from_csv_data_pair($integria_status_csv, $integria_status_values);
$integria_criticity_levels_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incident_priorities', []);
get_array_from_csv_data($integria_criticity_levels_csv, $integria_criticity_values);
get_array_from_csv_data_pair($integria_criticity_levels_csv, $integria_criticity_values);
$integria_users_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_users', []);
@ -65,129 +70,184 @@ foreach ($csv_array as $csv_line) {
$integria_types_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_types', []);
get_array_from_csv_data($integria_types_csv, $integria_types_values);
get_array_from_csv_data_pair($integria_types_csv, $integria_types_values);
$event_id = (int) get_parameter('from_event');
$create_incident = (int) get_parameter('create_incident', 0);
$incident_group_id = (int) get_parameter('default_group');
$incident_default_criticity_id = (int) get_parameter('default_criticity');
$incident_default_owner = (int) get_parameter('default_owner');
$incident_type = (int) get_parameter('incident_type');
$incident_id_edit = (int) get_parameter('incident_id');
$create_incident = (bool) get_parameter('create_incident', 0);
$update_incident = (bool) get_parameter('update_incident', 0);
$incident_group_id = (int) get_parameter('group');
$incident_criticity_id = (int) get_parameter('criticity');
$incident_owner = get_parameter('owner');
$incident_type = (int) get_parameter('type');
$incident_creator = get_parameter('creator');
$incident_status = (int) get_parameter('status');
$incident_title = events_get_field_value_by_event_id($event_id, get_parameter('incident_title'));
$incident_content = events_get_field_value_by_event_id($event_id, get_parameter('incident_content'));
if ($create_incident === 1) {
// Call Integria IMS API method to create an incident.
$result_api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'create_incident', [$incident_title, $incident_group_id, $incident_default_criticity_id, $incident_content, '', '0', '', 'admin', '0', '1']);
$update = (isset($_GET['incident_id']) === true);
// If incident id is specified, retrieve incident values from api to populate combos with such values.
if ($update) {
// Call Integria IMS API method to get details of an incident given its id.
$result_api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incident_details', [$incident_id_edit]);
// API call does not return indexes, therefore future modifications of API function in Integria IMS may lead to inconsistencies when accessing resulting array in this file.
$incident_details = explode(',', $result_api_call);
}
// Perform action.
if ($create_incident === true) {
// Call Integria IMS API method to create an incident.
$result_api_call = integria_api_call($config['integria_hostname'], $incident_creator, $config['integria_pass'], $config['integria_api_pass'], 'create_incident', [$incident_title, $incident_group_id, $incident_criticity_id, $incident_content, '', '0', '', $incident_owner, '0', $incident_status]);
// Necessary to explicitly set true if not false because it returns api call result in case of success instead of true value.
$incident_created_ok = ($result_api_call != false) ? true : false;
ui_print_result_message(
$incident_created_ok,
__('Successfully created'),
__('Could not be created')
__('Successfully created in Integria IMS'),
__('Could not be created in Integria IMS')
);
} else if ($update_incident === true) {
// Call Integria IMS API method to update an incident.
$result_api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'update_incident', [$incident_id_edit, $incident_title, $incident_content, '', $incident_group_id, $incident_criticity_id, 0, $incident_status, $incident_owner]);
// Necessary to explicitly set true if not false because it returns api call result in case of success instead of true value.
$incident_updated_ok = ($result_api_call != false) ? true : false;
ui_print_result_message(
$incident_updated_ok,
__('Successfully updated in Integria IMS'),
__('Could not be updated in Integria IMS')
);
}
$table = new stdClass();
$table->width = '100%';
$table->id = 'add_alert_table';
$table->class = 'databox filters';
$table->class = 'databox filters integria_incidents_options';
$table->head = [];
$table->data = [];
$table->size = [];
$table->size = [];
$table->size[0] = '15%';
$table->size[1] = '90%';
$table->style[0] = 'width: 33%; padding-right: 50px; padding-left: 100px;';
$table->style[1] = 'width: 33%; padding-right: 50px; padding-left: 50px;';
$table->style[2] = 'width: 33%; padding-right: 100px; padding-left: 50px;';
$table->colspan[0][0] = 2;
$table->colspan[3][0] = 3;
$table->data[0][0] = __('Group');
$table->data[0][1] = html_print_select(
$group_values,
'default_group',
$config['default_group'],
'',
__('Select'),
0,
true,
false,
true,
'',
false
);
$table->data[1][0] = __('Default Criticity');
$table->data[1][1] = html_print_select(
$integria_criticity_values,
'default_criticity',
$config['default_criticity'],
'',
__('Select'),
0,
true,
false,
true,
'',
false
);
$table->data[2][0] = __('Default Owner');
$table->data[2][1] = html_print_select(
$integria_users_values,
'default_owner',
$config['default_owner'],
'',
__('Select'),
0,
true,
false,
true,
'',
false
);
$table->data[0][2] = __('Incident Type');
$table->data[0][3] = html_print_select(
$integria_types_values,
'incident_type',
$config['incident_type'],
'',
__('Select'),
0,
true,
false,
true,
'',
false
);
$table->data[1][2] = __('Incident title').ui_print_help_icon('response_macros', true);
$table->data[1][3] = html_print_input_text(
$table->data[0][0] = '<div class="label_select"><p class="input_label">'.__('Title').':&nbsp'.ui_print_help_icon('response_macros', true).'</p>';
$table->data[0][0] .= '<div class="label_select_parent">'.html_print_input_text(
'incident_title',
$config['incident_title'],
$update ? $incident_details[3] : $config['incident_title'],
__('Name'),
50,
100,
true,
false,
true
);
).'</div>';
$table->data[2][2] = __('Incident content').ui_print_help_icon('response_macros', true);
$table->data[2][3] = html_print_input_text(
'incident_content',
$config['incident_content'],
$table->data[1][0] = '<div class="label_select"><p class="input_label">'.__('Type').': </p>';
$table->data[1][0] .= '<div class="label_select_parent">'.html_print_select(
$integria_types_values,
'type',
$update ? $incident_details[17] : $config['incident_type'],
'',
50,
100,
__('Select'),
0,
true,
false,
true,
'',
false,
'width: 100%;'
).'</div>';
$table->data[2][0] = '<div class="label_select"><p class="input_label">'.__('Status').': </p>';
$table->data[2][0] .= '<div class="label_select_parent">'.html_print_select(
$integria_status_values,
'status',
$update ? $incident_details[6] : $config['incident_status'],
'',
__('Select'),
0,
true,
false,
true,
'',
false,
'width: 100%;'
).'</div>';
$table->data[1][1] = '<div class="label_select"><p class="input_label">'.__('Group').': </p>';
$table->data[1][1] .= '<div class="label_select_parent">'.html_print_select(
$integria_group_values,
'group',
$update ? $incident_details[8] : $config['default_group'],
'',
__('Select'),
0,
true,
false,
true,
'',
false,
'width: 100%;'
).'</div>';
$table->data[2][1] = '<div class="label_select"><p class="input_label">'.__('Creator').': </p>';
$table->data[2][1] .= '<div class="label_select_parent">'.html_print_autocomplete_users_from_integria(
'creator',
$update ? $incident_details[10] : $config['default_creator'],
true
);
).'</div>';
$table->data[1][2] = '<div class="label_select"><p class="input_label">'.__('Criticity').': </p>';
$table->data[1][2] .= '<div class="label_select_parent">'.html_print_select(
$integria_criticity_values,
'criticity',
$update ? $incident_details[7] : $config['default_criticity'],
'',
__('Select'),
0,
true,
false,
true,
'',
false,
'width: 100%;'
).'</div>';
$table->data[2][2] = '<div class="label_select"><p class="input_label">'.__('Owner').': </p>';
$table->data[2][2] .= '<div class="label_select_parent">'.html_print_autocomplete_users_from_integria(
'owner',
$update ? $incident_details[10] : $config['default_owner'],
true
).'</div>';
$table->data[3][0] = '<div class="label_select"><p class="input_label">'.__('Description').':&nbsp'.ui_print_help_icon('response_macros', true).'</p>';
$table->data[3][0] .= '<div class="label_select_parent">'.html_print_textarea(
'incident_content',
3,
20,
$update ? $incident_details[4] : $config['incident_content'],
'',
true
).'</div>';
echo '<form name="create_integria_incident_form" method="POST">';
html_print_table($table);
html_print_input_hidden('create_incident', 1);
if (!$update) {
html_print_input_hidden('create_incident', 1);
} else {
html_print_input_hidden('update_incident', 1);
}
echo '<div style="width: 100%; text-align:right;">';
html_print_submit_button(__('Create'), 'accion', false, 'class="sub wand"');
echo '</div>';