Merge branch 'ent-8042-agrupacion-eventos-no-funcionan-bien-comentarios' into 'develop'

Added feature for read comments in grouped events

Closes pandora_enterprise#8042

See merge request artica/pandorafms!4467
This commit is contained in:
Daniel Rodriguez 2021-12-07 16:51:07 +00:00
commit c3f9ed1bf2
3 changed files with 123 additions and 56 deletions

View File

@ -83,10 +83,10 @@ $in_process_event = get_parameter('in_process_event', 0);
$validate_event = get_parameter('validate_event', 0);
$delete_event = get_parameter('delete_event', 0);
$get_event_filters = get_parameter('get_event_filters', 0);
$get_comments = get_parameter('get_comments', 0);
$get_comments = (bool) get_parameter('get_comments', false);
$get_events_fired = (bool) get_parameter('get_events_fired');
$get_id_source_event = get_parameter('get_id_source_event');
if ($get_comments) {
if ($get_comments === true) {
$event = get_parameter('event', false);
$filter = get_parameter('filter', false);
@ -94,6 +94,8 @@ if ($get_comments) {
return __('Failed to retrieve comments');
}
$eventsGrouped = [];
if ($filter['group_rep'] == 1) {
$events = events_get_all(
['te.*'],
@ -119,23 +121,52 @@ if ($get_comments) {
// True for show comments of validated events.
true
);
if ($events !== false) {
$event = $events[0];
}
} else {
$events = events_get_event(
$event['id_evento'],
false,
$meta,
$history
);
// Consider if the event is grouped.
if (isset($event['event_rep']) === true && $event['event_rep'] > 0) {
// Evaluate if we are in metaconsole or not.
$eventTable = (is_metaconsole() === true) ? 'tmetaconsole_event' : 'tevento';
// Default grouped message filtering (evento and estado).
$whereGrouped = sprintf(
'`evento` = "%s" AND `estado` = "%s"',
io_safe_output($event['evento']),
$event['estado']
);
// If id_agente is reported, filter the messages by them as well.
if ((int) $event['id_agente'] > 0) {
$whereGrouped .= sprintf(' AND `id_agente` = "%s"', $event['id_agente']);
}
if ($events !== false) {
$event = $events;
// Get grouped comments.
$eventsGrouped = db_get_all_rows_sql(
sprintf(
'SELECT `user_comment`
FROM `%s`
WHERE %s',
$eventTable,
$whereGrouped
)
);
} else {
$events = events_get_event(
$event['id_evento'],
false,
is_metaconsole(),
$history
);
if ($events !== false) {
$event = $events;
}
}
}
echo events_page_comments($event, true);
// End of get_comments.
echo events_page_comments($event, true, $eventsGrouped);
return;
}
@ -1207,26 +1238,19 @@ if ($dialogue_event_response) {
}
}
if ($add_comment) {
$aviability_comment = true;
$comment = get_parameter('comment');
if ($add_comment === true) {
$comment = (string) get_parameter('comment');
$eventId = (int) get_parameter('event_id');
// Safe comments for hacks.
if (preg_match('/script/i', io_safe_output($comment))) {
$aviability_comment = false;
$return = false;
}
$event_id = get_parameter('event_id');
if ($aviability_comment !== false) {
$return = events_comment($event_id, $comment, 'Added comment', $meta, $history);
}
if ($return) {
echo 'comment_ok';
} else {
echo 'comment_error';
$return = events_comment($eventId, $comment, 'Added comment', $meta, $history);
}
echo ($return === true) ? 'comment_ok' : 'comment_error';
return;
}

View File

@ -25,6 +25,8 @@
* GNU General Public License for more details.
* ============================================================================
*/
// Begin.
global $config;
require_once $config['homedir'].'/include/functions_ui.php';
@ -35,7 +37,7 @@ enterprise_include_once('include/functions_metaconsole.php');
enterprise_include_once('meta/include/functions_events_meta.php');
enterprise_include_once('meta/include/functions_agents_meta.php');
enterprise_include_once('meta/include/functions_modules_meta.php');
if (is_metaconsole()) {
if (is_metaconsole() === true) {
$id_source_event = get_parameter('id_source_event');
}
@ -1619,14 +1621,14 @@ function events_get_events($filter=false, $fields=false)
*/
function events_get_event($id, $fields=false, $meta=false, $history=false)
{
if (empty($id)) {
if (empty($id) === true) {
return false;
}
global $config;
if (is_array($fields)) {
if (! in_array('id_grupo', $fields)) {
if (is_array($fields) === true) {
if (in_array('id_grupo', $fields) === false) {
$fields[] = 'id_grupo';
}
}
@ -1634,7 +1636,7 @@ function events_get_event($id, $fields=false, $meta=false, $history=false)
$table = events_get_events_table($meta, $history);
$event = db_get_row($table, 'id_evento', $id, $fields);
if (! check_acl($config['id_user'], $event['id_grupo'], 'ER')) {
if ((bool) check_acl($config['id_user'], $event['id_grupo'], 'ER') === false) {
return false;
}
@ -2255,7 +2257,7 @@ function events_comment(
// If comments are not stored in json, the format is old.
$event_comments_array = json_decode($event_comments[0]['user_comment']);
if (empty($event_comments_array)) {
if (empty($event_comments_array) === true) {
$comments_format = 'old';
} else {
$comments_format = 'new';
@ -2268,6 +2270,7 @@ function events_comment(
$comment_for_json['action'] = $action;
$comment_for_json['id_user'] = $config['id_user'];
$comment_for_json['utimestamp'] = time();
$comment_for_json['event_id'] = $first_event;
$event_comments_array[] = $comment_for_json;
@ -2282,11 +2285,11 @@ function events_comment(
break;
case 'old':
// Give old ugly format to comment. TODO: Change this method for
// aux table or json.
// Give old ugly format to comment.
// Change this method for aux table or json.
$comment = str_replace(["\r\n", "\r", "\n"], '<br>', $comment);
if ($comment != '') {
if ($comment !== '') {
$commentbox = '<div class="comment_box">'.io_safe_input($comment).'</div>';
} else {
$commentbox = '';
@ -5102,11 +5105,13 @@ function events_page_general($event)
/**
* Generate 'comments' page for event viewer.
*
* @param array $event Event.
* @param array $event Event.
* @param boolean $ajax If the query come from AJAX.
* @param boolean $grouped If the event must shown comments grouped.
*
* @return string HTML.
*/
function events_page_comments($event, $ajax=false)
function events_page_comments($event, $ajax=false, $groupedComments=[])
{
// Comments.
global $config;
@ -5117,23 +5122,53 @@ function events_page_comments($event, $ajax=false)
$table_comments->head = [];
$table_comments->class = 'table_modal_alternate';
$comments = ($event['user_comment'] ?? '');
$comments = (empty($groupedComments) === true) ? $event['user_comment'] : $groupedComments;
if (empty($comments)) {
if (empty($comments) === true) {
$table_comments->style[0] = 'text-align:center;';
$table_comments->colspan[0][0] = 2;
$data = [];
$data[0] = __('There are no comments');
$table_comments->data[] = $data;
} else {
if (is_array($comments)) {
if (is_array($comments) === true) {
$comments_array = [];
foreach ($comments as $comm) {
if (empty($comm)) {
if (empty($comm) === true) {
continue;
}
// If exists user_comments, come from grouped events and must be handled like this.
if (isset($comm['user_comment']) === true) {
$comm = $comm['user_comment'];
}
$comments_array[] = io_safe_output(json_decode($comm, true));
}
// Plain comments. Can be improved.
$sortedCommentsArray = [];
foreach ($comments_array as $comm) {
foreach ($comm as $subComm) {
$sortedCommentsArray[] = $subComm;
}
}
// Sorting the comments by utimestamp (newer is first).
usort(
$sortedCommentsArray,
function ($a, $b) {
if ($a['utimestamp'] == $b['utimestamp']) {
return 0;
}
return ($a['utimestamp'] > $b['utimestamp']) ? -1 : 1;
}
);
// Clean the unsorted comments and return it to the original array.
$comments_array = [];
$comments_array[] = $sortedCommentsArray;
} else {
$comments = str_replace(["\n", '&#x0a;'], '<br>', $comments);
// If comments are not stored in json, the format is old.
@ -5141,23 +5176,28 @@ function events_page_comments($event, $ajax=false)
}
foreach ($comments_array as $comm) {
// Show the comments more recent first.
if (is_array($comm)) {
$comm = array_reverse($comm);
}
if (empty($comm)) {
$comments_format = 'old';
} else {
$comments_format = 'new';
}
$comments_format = (empty($comm) === true) ? 'old' : 'new';
switch ($comments_format) {
case 'new':
foreach ($comm as $c) {
$data[0] = '<b>'.$c['action'].' by '.$c['id_user'].'</b>';
$data[0] .= '<br><br><i>'.date($config['date_format'], $c['utimestamp']).'</i>';
$eventIdExplanation = (empty($groupedComments) === false) ? sprintf(' (#%d)', $c['event_id']) : '';
$data[0] = sprintf(
'<b>%s %s %s%s</b>',
$c['action'],
__('by'),
$c['id_user'],
$eventIdExplanation
);
$data[0] .= sprintf(
'<br><br><i>%s</i>',
date($config['date_format'], $c['utimestamp'])
);
$data[1] = '<p class="break_word">'.stripslashes(str_replace(['\n', '\r'], '<br/>', $c['comment'])).'</p>';
$table_comments->data[] = $data;
}
break;
@ -5247,7 +5287,7 @@ function events_page_comments($event, $ajax=false)
);
}
if ($ajax) {
if ($ajax === true) {
return $comments_form.html_print_table($table_comments, true);
}

View File

@ -576,7 +576,6 @@ function event_comment(current_event) {
return;
}
var event_id = event.id_evento;
var comment = $("#textarea_comment").val();
var meta = 0;
if ($("#hidden-meta").val() != undefined) {
@ -596,7 +595,11 @@ function event_comment(current_event) {
var params = [];
params.push("page=include/ajax/events");
params.push("add_comment=1");
params.push("event_id=" + event_id);
if (event.event_rep > 0) {
params.push("event_id=" + event.max_id_evento);
} else {
params.push("event_id=" + event.id_evento);
}
params.push("comment=" + comment);
params.push("meta=" + meta);
params.push("history=" + history);