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); $validate_event = get_parameter('validate_event', 0);
$delete_event = get_parameter('delete_event', 0); $delete_event = get_parameter('delete_event', 0);
$get_event_filters = get_parameter('get_event_filters', 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_events_fired = (bool) get_parameter('get_events_fired');
$get_id_source_event = get_parameter('get_id_source_event'); $get_id_source_event = get_parameter('get_id_source_event');
if ($get_comments) { if ($get_comments === true) {
$event = get_parameter('event', false); $event = get_parameter('event', false);
$filter = get_parameter('filter', false); $filter = get_parameter('filter', false);
@ -94,6 +94,8 @@ if ($get_comments) {
return __('Failed to retrieve comments'); return __('Failed to retrieve comments');
} }
$eventsGrouped = [];
if ($filter['group_rep'] == 1) { if ($filter['group_rep'] == 1) {
$events = events_get_all( $events = events_get_all(
['te.*'], ['te.*'],
@ -119,23 +121,52 @@ if ($get_comments) {
// True for show comments of validated events. // True for show comments of validated events.
true true
); );
if ($events !== false) { if ($events !== false) {
$event = $events[0]; $event = $events[0];
} }
} else { } else {
$events = events_get_event( // Consider if the event is grouped.
$event['id_evento'], if (isset($event['event_rep']) === true && $event['event_rep'] > 0) {
false, // Evaluate if we are in metaconsole or not.
$meta, $eventTable = (is_metaconsole() === true) ? 'tmetaconsole_event' : 'tevento';
$history // 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) { // Get grouped comments.
$event = $events; $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; return;
} }
@ -1207,26 +1238,19 @@ if ($dialogue_event_response) {
} }
} }
if ($add_comment) { if ($add_comment === true) {
$aviability_comment = true; $comment = (string) get_parameter('comment');
$comment = get_parameter('comment'); $eventId = (int) get_parameter('event_id');
// Safe comments for hacks.
if (preg_match('/script/i', io_safe_output($comment))) { if (preg_match('/script/i', io_safe_output($comment))) {
$aviability_comment = false;
$return = 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 { } else {
echo 'comment_error'; $return = events_comment($eventId, $comment, 'Added comment', $meta, $history);
} }
echo ($return === true) ? 'comment_ok' : 'comment_error';
return; return;
} }

View File

@ -25,6 +25,8 @@
* GNU General Public License for more details. * GNU General Public License for more details.
* ============================================================================ * ============================================================================
*/ */
// Begin.
global $config; global $config;
require_once $config['homedir'].'/include/functions_ui.php'; 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_events_meta.php');
enterprise_include_once('meta/include/functions_agents_meta.php'); enterprise_include_once('meta/include/functions_agents_meta.php');
enterprise_include_once('meta/include/functions_modules_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'); $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) function events_get_event($id, $fields=false, $meta=false, $history=false)
{ {
if (empty($id)) { if (empty($id) === true) {
return false; return false;
} }
global $config; global $config;
if (is_array($fields)) { if (is_array($fields) === true) {
if (! in_array('id_grupo', $fields)) { if (in_array('id_grupo', $fields) === false) {
$fields[] = 'id_grupo'; $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); $table = events_get_events_table($meta, $history);
$event = db_get_row($table, 'id_evento', $id, $fields); $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; return false;
} }
@ -2255,7 +2257,7 @@ function events_comment(
// If comments are not stored in json, the format is old. // If comments are not stored in json, the format is old.
$event_comments_array = json_decode($event_comments[0]['user_comment']); $event_comments_array = json_decode($event_comments[0]['user_comment']);
if (empty($event_comments_array)) { if (empty($event_comments_array) === true) {
$comments_format = 'old'; $comments_format = 'old';
} else { } else {
$comments_format = 'new'; $comments_format = 'new';
@ -2268,6 +2270,7 @@ function events_comment(
$comment_for_json['action'] = $action; $comment_for_json['action'] = $action;
$comment_for_json['id_user'] = $config['id_user']; $comment_for_json['id_user'] = $config['id_user'];
$comment_for_json['utimestamp'] = time(); $comment_for_json['utimestamp'] = time();
$comment_for_json['event_id'] = $first_event;
$event_comments_array[] = $comment_for_json; $event_comments_array[] = $comment_for_json;
@ -2282,11 +2285,11 @@ function events_comment(
break; break;
case 'old': case 'old':
// Give old ugly format to comment. TODO: Change this method for // Give old ugly format to comment.
// aux table or json. // Change this method for aux table or json.
$comment = str_replace(["\r\n", "\r", "\n"], '<br>', $comment); $comment = str_replace(["\r\n", "\r", "\n"], '<br>', $comment);
if ($comment != '') { if ($comment !== '') {
$commentbox = '<div class="comment_box">'.io_safe_input($comment).'</div>'; $commentbox = '<div class="comment_box">'.io_safe_input($comment).'</div>';
} else { } else {
$commentbox = ''; $commentbox = '';
@ -5102,11 +5105,13 @@ function events_page_general($event)
/** /**
* Generate 'comments' page for event viewer. * 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. * @return string HTML.
*/ */
function events_page_comments($event, $ajax=false) function events_page_comments($event, $ajax=false, $groupedComments=[])
{ {
// Comments. // Comments.
global $config; global $config;
@ -5117,23 +5122,53 @@ function events_page_comments($event, $ajax=false)
$table_comments->head = []; $table_comments->head = [];
$table_comments->class = 'table_modal_alternate'; $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->style[0] = 'text-align:center;';
$table_comments->colspan[0][0] = 2; $table_comments->colspan[0][0] = 2;
$data = []; $data = [];
$data[0] = __('There are no comments'); $data[0] = __('There are no comments');
$table_comments->data[] = $data; $table_comments->data[] = $data;
} else { } else {
if (is_array($comments)) { if (is_array($comments) === true) {
$comments_array = [];
foreach ($comments as $comm) { foreach ($comments as $comm) {
if (empty($comm)) { if (empty($comm) === true) {
continue; 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)); $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 { } else {
$comments = str_replace(["\n", '&#x0a;'], '<br>', $comments); $comments = str_replace(["\n", '&#x0a;'], '<br>', $comments);
// If comments are not stored in json, the format is old. // 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) { foreach ($comments_array as $comm) {
// Show the comments more recent first. $comments_format = (empty($comm) === true) ? 'old' : 'new';
if (is_array($comm)) {
$comm = array_reverse($comm);
}
if (empty($comm)) {
$comments_format = 'old';
} else {
$comments_format = 'new';
}
switch ($comments_format) { switch ($comments_format) {
case 'new': case 'new':
foreach ($comm as $c) { foreach ($comm as $c) {
$data[0] = '<b>'.$c['action'].' by '.$c['id_user'].'</b>'; $eventIdExplanation = (empty($groupedComments) === false) ? sprintf(' (#%d)', $c['event_id']) : '';
$data[0] .= '<br><br><i>'.date($config['date_format'], $c['utimestamp']).'</i>';
$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>'; $data[1] = '<p class="break_word">'.stripslashes(str_replace(['\n', '\r'], '<br/>', $c['comment'])).'</p>';
$table_comments->data[] = $data; $table_comments->data[] = $data;
} }
break; 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); return $comments_form.html_print_table($table_comments, true);
} }

View File

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