wrap csv fields with string delimiter on export

This commit is contained in:
alejandro.campos@artica.es 2023-05-08 14:47:22 +02:00
parent 5983333965
commit e0090aee95
2 changed files with 30 additions and 11 deletions

View File

@ -6458,3 +6458,22 @@ function nms_check_api()
return true;
}
}
/**
* Simply return a string enclosed in quote delimiter to be used in csv exports.
*
* @param string $str String to be formatted.
*
* @return string Formatted string.
*/
function csv_format_delimiter(?string $str)
{
if ($str === null) {
return $str;
}
// Due to the ticket requirements, double quote is used as fixed string delimiter.
// TODO: a setup option that enables user to choose a delimiter character would probably be desirable in the future.
return '"'.$str.'"';
}

View File

@ -166,11 +166,11 @@ try {
// Dump headers.
foreach ($names as $n) {
echo io_safe_output($n).$config['csv_divider'];
echo csv_format_delimiter(io_safe_output($n)).$config['csv_divider'];
}
if (is_metaconsole() === true) {
echo 'server_id'.$config['csv_divider'];
echo csv_format_delimiter('server_id').$config['csv_divider'];
}
echo chr(13);
@ -203,21 +203,21 @@ try {
switch ($key) {
case 'module_status':
echo events_translate_module_status(
echo csv_format_delimiter(events_translate_module_status(
$row[$key]
);
));
break;
case 'event_type':
echo events_translate_event_type(
echo csv_format_delimiter(events_translate_event_type(
$row[$key]
);
));
break;
case 'criticity':
echo events_translate_event_criticity(
echo csv_format_delimiter(events_translate_event_criticity(
$row[$key]
);
));
break;
case 'custom_data':
@ -244,11 +244,11 @@ try {
$custom_data = implode($separator, $custom_data_array);
}
echo io_safe_output($custom_data);
echo csv_format_delimiter(io_safe_output($custom_data));
break;
default:
echo io_safe_output($row[$key]);
echo csv_format_delimiter(io_safe_output($row[$key]));
break;
}
@ -256,7 +256,7 @@ try {
}
if (is_metaconsole() === true) {
echo $row['server_id'].$config['csv_divider'];
echo csv_format_delimiter($row['server_id']).$config['csv_divider'];
}
echo chr(13);