Merge branch 'ent-10976-busqueda-rapida-agentes-pasar-a-datatables' into 'develop'

Ent 10976 busqueda rapida agentes pasar a datatables

See merge request artica/pandorafms!5834
This commit is contained in:
Rafael Ameijeiras 2023-07-12 06:04:56 +00:00
commit a8d8676422
5 changed files with 233 additions and 339 deletions

View File

@ -4343,7 +4343,7 @@ function ui_print_datatable(array $parameters)
$(".action_buttons_right_content").html("<div class=\"pagination-child-div\"></div>");
$(".action_buttons_right_content").html("<div class=\"pagination-child-div\"></div>");
$(".pagination-child-div").append($("#'.$table_id.'_wrapper > .dataTables_paginate.paging_simple_numbers"));
$(".pagination-child-div").append($("#'.$table_id.'_wrapper > .dataTables_paginate.paging_simple_numbers").attr("style","margin-right: 10px;"));
$(".pagination-child-div").append($("#'.$table_id.'_wrapper > .dataTables_length"));
$(".pagination-child-div").append($("#'.$table_id.'_wrapper > .dt-buttons"));
$(".pagination-child-div").append($("#'.$table_id.'_wrapper > .dataTables_filter"));

View File

@ -16,163 +16,52 @@ global $config;
enterprise_include_once('include/functions_policies.php');
require_once $config['homedir'].'/include/functions_users.php';
$searchAgents = check_acl($config['id_user'], 0, 'AR');
$selectNameUp = '';
$selectNameDown = '';
$selectDescriptionUp = '';
$selectDescriptionDown = '';
$selectOsUp = '';
$selectOsDown = '';
$selectIntervalUp = '';
$selectIntervalDown = '';
$selectGroupUp = '';
$selectGroupDown = '';
$selectLastContactUp = '';
$selectLastContactDown = '';
switch ($sortField) {
case 'name':
switch ($sort) {
case 'up':
$selectNameUp = $selected;
$order = [
'field' => 'nombre',
'order' => 'ASC',
];
break;
case 'down':
$selectNameDown = $selected;
$order = [
'field' => 'nombre',
'order' => 'DESC',
];
break;
}
break;
case 'comentarios':
switch ($sort) {
case 'up':
$selectDescriptionUp = $selected;
$order = [
'field' => 'comentarios',
'order' => 'ASC',
];
break;
case 'down':
$selectDescriptionDown = $selected;
$order = [
'field' => 'comentarios',
'order' => 'DESC',
];
break;
}
break;
case 'os':
switch ($sort) {
case 'up':
$selectOsUp = $selected;
$order = [
'field' => 'id_os',
'order' => 'ASC',
];
break;
case 'down':
$selectOsDown = $selected;
$order = [
'field' => 'id_os',
'order' => 'DESC',
];
break;
}
break;
case 'interval':
switch ($sort) {
case 'up':
$selectIntervalUp = $selected;
$order = [
'field' => 'intervalo',
'order' => 'ASC',
];
break;
case 'down':
$selectIntervalDown = $selected;
$order = [
'field' => 'intervalo',
'order' => 'DESC',
];
break;
}
break;
case 'group':
switch ($sort) {
case 'up':
$selectGroupUp = $selected;
$order = [
'field' => 'id_grupo',
'order' => 'ASC',
];
break;
case 'down':
$selectGroupDown = $selected;
$order = [
'field' => 'id_grupo',
'order' => 'DESC',
];
break;
}
break;
case 'last_contact':
switch ($sort) {
case 'up':
$selectLastContactUp = $selected;
$order = [
'field' => 'ultimo_contacto',
'order' => 'ASC',
];
break;
case 'down':
$selectLastContactDown = $selected;
$order = [
'field' => 'ultimo_contacto',
'order' => 'DESC',
];
break;
}
break;
default:
$selectNameUp = $selected;
$selectNameDown = '';
$selectDescriptionUp = '';
$selectDescriptionDown = '';
$selectOsUp = '';
$selectOsDown = '';
$selectIntervalUp = '';
$selectIntervalDown = '';
$selectGroupUp = '';
$selectGroupDown = '';
$selectLastContactUp = '';
$selectLastContactDown = '';
$order = [
'field' => 'nombre',
'order' => 'ASC',
];
break;
$searchAgents = get_parameter('search_agents', 0);
$stringSearchSQL = get_parameter('stringSearchSQL');
$order = get_datatable_order(true);
if (empty($order)) {
$order = [];
}
$totalAgents = 0;
switch ($order['field']) {
case 'comentarios':
$order['field'] = 't1.comentarios';
break;
case 'os':
$order['field'] = 't1.id_os';
break;
case 'interval':
$order['field'] = 't1.intervalo';
break;
case 'group_icon':
$order['field'] = 't1.id_grupo';
break;
case 'module':
$order['field'] = 'id_agente';
break;
case 'status':
$order['field'] = 'id_agente';
break;
case 'alert':
$order['field'] = 'id_agente';
break;
case 'last_contact':
$order['field'] = 't1.ultimo_contacto';
break;
case 'agent':
default:
$order['field'] = 't1.alias';
break;
}
$agents = false;
if ($searchAgents) {
@ -181,19 +70,20 @@ if ($searchAgents) {
$has_secondary = enterprise_hook('agents_is_using_secondary_groups');
$stringSearchSQL = str_replace('&amp;', '&', $stringSearchSQL);
$sql = "SELECT DISTINCT taddress_agent.id_agent FROM taddress
INNER JOIN taddress_agent ON
taddress.id_a = taddress_agent.id_a
WHERE taddress.ip LIKE '%$stringSearchSQL%'";
WHERE taddress.ip LIKE '$stringSearchSQL'";
$id = db_get_all_rows_sql($sql);
if ($id != '') {
$aux = $id[0]['id_agent'];
$search_sql = " t1.nombre LIKE '%%cd ".$stringSearchSQL."%%' OR
t2.nombre LIKE '%%".$stringSearchSQL."%%' OR
t1.alias LIKE '%%".$stringSearchSQL."%%' OR
t1.comentarios LIKE '%%".$stringSearchSQL."%%' OR
t1.id_agente = $aux";
$search_sql = " t1.nombre LIKE '".$stringSearchSQL."' OR
t2.nombre LIKE '".$stringSearchSQL."' OR
t1.alias LIKE '".$stringSearchSQL."' OR
t1.comentarios LIKE '".$stringSearchSQL."' OR
t1.id_agente =".$aux;
$idCount = count($id);
@ -204,16 +94,16 @@ if ($searchAgents) {
}
}
} else {
$search_sql = " t1.nombre LIKE '%%".$stringSearchSQL."%%' OR
t2.nombre LIKE '%%".$stringSearchSQL."%%' OR
t1.direccion LIKE '%%".$stringSearchSQL."%%' OR
t1.comentarios LIKE '%%".$stringSearchSQL."%%' OR
t1.alias LIKE '%%".$stringSearchSQL."%%'";
$search_sql = " t1.nombre LIKE '".$stringSearchSQL."' OR
t2.nombre LIKE '".$stringSearchSQL."' OR
t1.direccion LIKE '".$stringSearchSQL."' OR
t1.comentarios LIKE '".$stringSearchSQL."' OR
t1.alias LIKE '".$stringSearchSQL."'";
}
if ($has_secondary === true) {
$search_sql .= " OR (tasg.id_group IS NOT NULL AND
tasg.id_group IN (SELECT id_grupo FROM tgrupo WHERE nombre LIKE '%%".$stringSearchSQL."%%'))";
tasg.id_group IN (SELECT id_grupo FROM tgrupo WHERE nombre LIKE '".$stringSearchSQL."'))";
}
$sql = "
@ -247,10 +137,14 @@ if ($searchAgents) {
';
$select = 'SELECT DISTINCT(t1.id_agente), t1.ultimo_contacto, t1.nombre, t1.comentarios, t1.id_os, t1.intervalo, t1.id_grupo, t1.disabled, t1.alias, t1.quiet';
if ($only_count) {
$limit = ' ORDER BY '.$order['field'].' '.$order['order'].' LIMIT '.$config['block_size'].' OFFSET 0';
} else {
$limit = ' ORDER BY '.$order['field'].' '.$order['order'].' LIMIT '.$config['block_size'].' OFFSET '.get_parameter('offset', 0);
if (is_array($order)) {
// Datatables offset, limit.
$start = get_parameter('start', 0);
$length = get_parameter(
'length',
$config['block_size']
);
$limit = ' ORDER BY '.$order['field'].' '.$order['direction'].' LIMIT '.$length.' OFFSET '.$start;
}
$query = $select.$sql;
@ -272,4 +166,45 @@ if ($searchAgents) {
'SELECT COUNT(DISTINCT id_agente) AS agent_count '.$sql
);
}
foreach ($agents as $key => $agent) {
if ($agent['disabled']) {
$agents[$key]['agent'] = '<em><a style href=index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].'
title="'.$agent['id_agente'].'"><b><span style>'.ucfirst(strtolower($agent['alias'])).'</span></b></a>'.ui_print_help_tip(__('Disabled'), true).'</em>';
} else {
$agents[$key]['agent'] = '<a style href=index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].'
title='.$agent['nombre'].'><b><span style>'.ucfirst(strtolower($agent['alias'])).'</span></b></a>';
}
$agents[$key]['os'] = ui_print_os_icon($agent['id_os'], false, true);
$agents[$key]['interval'] = human_time_description_raw($agent['intervalo'], false, 'tiny');
$agents[$key]['group_icon'] = ui_print_group_icon($agent['id_grupo'], true);
$agent_info = reporting_get_agent_module_info($agent['id_agente']);
$modulesCell = reporting_tiny_stats($agent_info, true);
$agents[$key]['module'] = $modulesCell;
$agents[$key]['status'] = $agent_info['status_img'];
$agents[$key]['alert'] = $agent_info['alert_img'];
$last_time = time_w_fixed_tz($agent['ultimo_contacto']);
$now = get_system_time();
$diferencia = ($now - $last_time);
$time = ui_print_timestamp($last_time, true);
$time_style = $time;
if ($diferencia > ($agent['intervalo'] * 2)) {
$time_style = '<b><span class="color_ff0">'.$time.'</span></b>';
}
$agents[$key]['last_contact'] = $time_style;
}
// RecordsTotal && recordsfiltered resultados totales.
echo json_encode(
[
'data' => ($agents ?? []),
'recordsTotal' => $totalAgents,
'recordsFiltered' => $totalAgents,
]
);
}

View File

@ -13,174 +13,63 @@
// GNU General Public License for more details.
global $config;
enterprise_include_once('include/functions_policies.php');
require_once $config['homedir'].'/include/functions_users.php';
/*
enterprise_include_once('include/functions_policies.php');
require_once $config['homedir'].'/include/functions_users.php';*/
if ($only_count) {
ob_start();
}
// Datatables list.
try {
$columns = [
'agent',
'comentarios',
'os',
'interval',
'group_icon',
'module',
'status',
'alert',
'last_contact',
];
// TODO: CLEAN extra_sql
$extra_sql = '';
$column_names = [
__('Agent'),
__('Description'),
__('OS'),
__('Interval'),
__('Group'),
__('Modules'),
__('Status'),
__('Alerts'),
__('Last contact'),
];
$searchAgents = check_acl($config['id_user'], 0, 'AR');
$tableId = 'agents_search';
$stringSearchSQL = $_SESSION['stringSearchSQL'];
if (!$agents || !$searchAgents) {
if (!$only_count) {
echo "<br><div class='nf'>".__('Zero results found')."</div>\n";
}
} else {
$table = new stdClass();
$table->cellpadding = 4;
$table->cellspacing = 4;
$table->width = '98%';
$table->class = 'info_table';
$table->head = [];
if ($only_count) {
$table->head[0] = __('Agent');
$table->head[1] = __('Description');
$table->head[2] = __('OS');
$table->head[3] = __('Interval');
$table->head[4] = __('Group');
} else {
$table->head[0] = __('Agent').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=name&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectNameUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=name&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectNameDown]).'</a>';
$table->head[1] = __('Description').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=comentarios&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectDescriptionUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=comentarios&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectDescriptionDown]).'</a>';
$table->head[2] = __('OS').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=os&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectOsUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=os&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectOsDown]).'</a>';
$table->head[3] = __('Interval').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=interval&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectIntervalUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=interval&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectIntervalDown]).'</a>';
$table->head[4] = __('Group').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=group&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectGroupUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=group&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectGroupDown]).'</a>';
}
$table->head[5] = __('Modules');
$table->head[6] = __('Status');
$table->head[7] = __('Alerts');
$table->head[8] = __('Last contact').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=last_contact&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectLastContactUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=last_contact&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectLastContactDown]).'</a>';
$table->head[9] = '';
$table->headstyle = [];
$table->headstyle[0] = 'text-align: left';
$table->headstyle[1] = 'text-align: left';
$table->headstyle[2] = 'text-align: left';
$table->headstyle[3] = 'text-align: left';
$table->headstyle[4] = 'text-align: left';
$table->headstyle[5] = 'text-align: left';
$table->headstyle[6] = 'text-align: left';
$table->headstyle[7] = 'text-align: left';
$table->headstyle[8] = 'text-align: left';
$table->headstyle[9] = 'text-align: center';
$table->align = [];
$table->align[0] = 'left';
$table->align[1] = 'left';
$table->align[2] = 'left';
$table->align[3] = 'left';
$table->align[4] = 'left';
$table->align[5] = 'left';
$table->align[6] = 'left';
$table->align[7] = 'left';
$table->align[8] = 'left';
$table->align[9] = 'center';
$table->data = [];
foreach ($agents as $agent) {
$agent_info = reporting_get_agent_module_info($agent['id_agente']);
$modulesCell = reporting_tiny_stats($agent_info, true);
if ($agent['disabled']) {
$cellName = '<em>'.'<a style href=index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].' title='.$agent['nombre'].'><b>'.'<span style>'.$agent['alias'].'</span></b></a>'.ui_print_help_tip(__('Disabled'), true).'</em>';
} else {
$cellName = '<a style href=index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].' title='.$agent['nombre'].'><b>'.'<span style>'.$agent['alias'].'</span></b></a>';
}
if ($agent['quiet']) {
$cellName .= '&nbsp;';
$cellName .= html_print_image('images/dot_blue.png', true, ['border' => '0', 'title' => __('Quiet'), 'alt' => '']);
}
$in_planned_downtime = db_get_sql(
'SELECT executed FROM tplanned_downtime
INNER JOIN tplanned_downtime_agents
ON tplanned_downtime.id = tplanned_downtime_agents.id_downtime
WHERE tplanned_downtime_agents.id_agent = '.$agent['id_agente'].' AND tplanned_downtime.executed = 1'
);
if ($in_planned_downtime) {
$cellName .= '<em>'.ui_print_help_tip(__('Agent in scheduled downtime'), true, 'images/minireloj-16.png');
$cellName .= '</em>';
}
$last_time = time_w_fixed_tz($agent['ultimo_contacto']);
$now = get_system_time();
$diferencia = ($now - $last_time);
$time = ui_print_timestamp($last_time, true);
$time_style = $time;
if ($diferencia > ($agent['intervalo'] * 2)) {
$time_style = '<b><span class="color_ff0">'.$time.'</span></b>';
}
$manage_agent = '';
if (check_acl($config['id_user'], $agent['id_grupo'], 'AW')) {
$url_manage = 'index.php?sec=estado&sec2=godmode/agentes/configurar_agente&id_agente='.$agent['id_agente'];
$manage_agent = '<a href="'.$url_manage.'">'.html_print_image(
'images/cog.png',
true,
[
'title' => __('Manage'),
'alt' => __('Manage'),
'class' => 'invert_filter',
]
).'</a>';
}
$table->cellclass[][9] = 'table_action_buttons';
array_push(
$table->data,
[
$cellName,
ui_print_truncate_text($agent['comentarios'], 'comentarios', false, true, true, '[&hellip;]'),
ui_print_os_icon($agent['id_os'], false, true),
human_time_description_raw($agent['intervalo'], false, 'tiny'),
ui_print_group_icon($agent['id_grupo'], true),
$modulesCell,
$agent_info['status_img'],
$agent_info['alert_img'],
$time_style,
$manage_agent,
]
);
}
echo '<br />';
html_print_table($table);
unset($table);
if (!$only_count) {
$tablePagination = ui_pagination(
$totalAgents,
false,
0,
0,
true,
'offset',
false
);
}
html_print_action_buttons(
'',
unset($_SESSION['stringSearchSQL']);
// Load datatables user interface.
ui_print_datatable(
[
'type' => 'data_table',
'class' => 'fixed_action_buttons',
'right_content' => $tablePagination,
'id' => $tableId,
'class' => 'info_table',
'style' => 'width: 99%',
'columns' => $columns,
'column_names' => $column_names,
'ajax_url' => 'operation/search_agents.getdata',
'ajax_data' => [
'search_agents' => 1,
'stringSearchSQL' => $stringSearchSQL,
],
'order' => [
'field' => 'alias',
'direction' => 'asc',
],
'search_button_class' => 'sub filter float-right',
'filter_main_class' => 'box-flat white_table_graph fixed_filter_bar',
]
);
}
if ($only_count) {
$list_agents = ob_get_clean();
html_print_action_buttons('');
} catch (Exception $e) {
echo $e->getMessage();
}

View File

@ -48,7 +48,79 @@ $table->style[13] = 'font-weight: bold; text-align: left;';
$table->style[14] = 'font-weight: bold; text-align: left;';
$table->style[15] = 'font-weight: bold; text-align: left;';
// Get total agents.
$userGroups = users_get_groups($config['id_user'], 'AR', false);
$id_userGroups = array_keys($userGroups);
$has_secondary = enterprise_hook('agents_is_using_secondary_groups');
$stringSearchSQL = str_replace('&amp;', '&', $stringSearchSQL);
$sql = "SELECT DISTINCT taddress_agent.id_agent FROM taddress
INNER JOIN taddress_agent ON
taddress.id_a = taddress_agent.id_a
WHERE taddress.ip LIKE '$stringSearchSQL'";
$id = db_get_all_rows_sql($sql);
if ($id != '') {
$aux = $id[0]['id_agent'];
$search_sql = " t1.nombre LIKE '".$stringSearchSQL."' OR
t2.nombre LIKE '".$stringSearchSQL."' OR
t1.alias LIKE '".$stringSearchSQL."' OR
t1.comentarios LIKE '".$stringSearchSQL."' OR
t1.id_agente =".$aux;
$idCount = count($id);
if ($idCount >= 2) {
for ($i = 1; $i < $idCount; $i++) {
$aux = $id[$i]['id_agent'];
$search_sql .= " OR t1.id_agente = $aux";
}
}
} else {
$search_sql = " t1.nombre LIKE '".$stringSearchSQL."' OR
t2.nombre LIKE '".$stringSearchSQL."' OR
t1.direccion LIKE '".$stringSearchSQL."' OR
t1.comentarios LIKE '".$stringSearchSQL."' OR
t1.alias LIKE '".$stringSearchSQL."'";
}
if ($has_secondary === true) {
$search_sql .= " OR (tasg.id_group IS NOT NULL AND
tasg.id_group IN (SELECT id_grupo FROM tgrupo WHERE nombre LIKE '".$stringSearchSQL."'))";
}
$sql = "
FROM tagente t1 LEFT JOIN tagent_secondary_group tasg
ON t1.id_agente = tasg.id_agent
INNER JOIN tgrupo t2
ON t2.id_grupo = t1.id_grupo
WHERE (
1 = (
SELECT is_admin
FROM tusuario
WHERE id_user = '".$config['id_user']."'
)
OR (
t1.id_grupo IN (".implode(',', $id_userGroups).')
OR tasg.id_group IN ('.implode(',', $id_userGroups).")
)
OR 0 IN (
SELECT id_grupo
FROM tusuario_perfil
WHERE id_usuario = '".$config['id_user']."'
AND id_perfil IN (
SELECT id_perfil
FROM tperfil WHERE agent_view = 1
)
)
)
AND (
".$search_sql.'
)
';
$totalAgents = db_get_value_sql(
'SELECT COUNT(DISTINCT id_agente) AS agent_count '.$sql
);
$table->data[0][0] = html_print_image('images/agent.png', true, ['title' => __('Agents found'), 'class' => 'invert_filter']);
@ -76,14 +148,10 @@ if (enterprise_installed()) {
html_print_table($table);
if ($searchAgents && $totalAgents > 0) {
if ($searchAgents) {
echo $list_agents;
echo "<a href='index.php?search_category=agents&keywords=".$config['search_keywords']."&head_search_keywords=Search'>".sprintf(
__('Show %s of %s. View all matches'),
$count_agents_main,
$totalAgents
).'</a>';
echo "<a href='index.php?search_category=agents&keywords=".$config['search_keywords']."&head_search_keywords=Search'>".__('View all matches').'</a>';
}

View File

@ -36,6 +36,7 @@ foreach ($arrayKeywords as $keyword) {
$stringSearchSQL = implode('&#x20;', $temp);
$stringSearchSQL = str_replace('_', '\_', $stringSearchSQL);
$_SESSION['stringSearchSQL'] = $stringSearchSQL;
if ($config['search_category'] == 'all') {
$searchTab = 'main';
} else {
@ -205,6 +206,7 @@ ui_print_standard_header(
);
$only_count = false;
switch ($searchTab) {
case 'main':
$only_count = true;