diff --git a/pandora_console/include/functions_inventory.php b/pandora_console/include/functions_inventory.php index 2ac322a4fe..0f47ba6e12 100644 --- a/pandora_console/include/functions_inventory.php +++ b/pandora_console/include/functions_inventory.php @@ -687,6 +687,142 @@ function inventory_get_data( } +function inventory_get_datatable( + $agents_ids, + $inventory_module_name, + $utimestamp, + $inventory_search_string='', + $export_csv=false, + $return_mode=false, + $order_by_agent=false +) { + global $config; + + $offset = (int) get_parameter('offset'); + + $where = []; + + array_push( + $where, + 'tmodule_inventory.id_module_inventory = tagent_module_inventory.id_module_inventory' + ); + + // Discart empty first position. + if (isset($agents_ids[0]) === true && empty($agents_ids[0]) === true) { + unset($agents_ids[0]); + } + + // If there are no agents selected. + if (empty($agents_ids) === true) { + return ERR_NODATA; + } + + if (array_search(-1, $agents_ids) === false) { + array_push($where, 'id_agente IN ('.implode(',', $agents_ids).')'); + } + + if ($inventory_module_name[0] !== '0' + && $inventory_module_name !== '' + && $inventory_module_name !== 'all' + ) { + array_push($where, "tmodule_inventory.name IN ('".implode("','", (array) $inventory_module_name)."')"); + } + + if ($inventory_search_string != '') { + array_push($where, "tagent_module_inventory.data LIKE '%".$inventory_search_string."%'"); + } + + $sql = 'SELECT * + FROM tmodule_inventory, tagent_module_inventory + WHERE + '.implode(' AND ', $where).' + ORDER BY tmodule_inventory.id_module_inventory LIMIT '.$offset.', '.$config['block_size']; + + $sql_count = 'SELECT COUNT(*) + FROM tmodule_inventory, tagent_module_inventory + WHERE '.implode(' AND ', $where); + + $rows = db_get_all_rows_sql($sql); + $count = db_get_sql($sql_count); + + if ($order_by_agent === false) { + $modules = []; + $module_rows = []; + + foreach ($rows as $row) { + array_push($modules, $row['name']); + } + + foreach ($modules as $module) { + $rows_tmp = []; + foreach ($rows as $row) { + if ($row['name'] === $module) { + $agent_name = db_get_value_sql( + 'SELECT alias + FROM tagente + WHERE id_agente = '.$row['id_agente'] + ); + + $row['name_agent'] = $agent_name; + array_push($rows_tmp, $row); + $module_rows[$module] = $rows_tmp; + } + } + } + + // hd($module_rows); + // echo ''; + return $module_rows; + } else { + $agents_rows = []; + $agent_data = []; + $rows_tmp = []; + foreach ($rows as $row) { + $agent_name = db_get_value_sql( + 'SELECT alias + FROM tagente + WHERE id_agente = '.$row['id_agente'] + ); + $row['name_agent'] = $agent_name; + $agent_data[$row['id_agente']][] = $row; + } + + foreach ($agent_data as $id_agent => $rows) { + $agent_name = db_get_value_sql( + 'SELECT alias + FROM tagente + WHERE id_agente = '.$id_agent + ); + + $rows_tmp['agent'] = $agent_name; + + foreach ($rows as $row) { + if ($utimestamp > 0) { + $data_row = db_get_row_sql( + "SELECT data, timestamp + FROM tagente_datos_inventory + WHERE utimestamp <= '".$utimestamp."' + AND id_agent_module_inventory = ".$row['id_agent_module_inventory'].' ORDER BY utimestamp DESC' + ); + + if ($data_row !== false) { + $row['data'] = $data_row['data']; + $row['timestamp'] = $data_row['timestamp']; + } else { + continue; + } + } + } + + $rows_tmp['row'] = $rows; + array_push($agents_rows, $rows_tmp); + } + + return $agents_rows; + } +} + + function inventory_get_dates($module_inventory_name, $inventory_agent, $inventory_id_group) { $sql = 'SELECT tagente_datos_inventory.utimestamp, diff --git a/pandora_console/include/functions_ui.php b/pandora_console/include/functions_ui.php index df491679c8..eb408d2be4 100755 --- a/pandora_console/include/functions_ui.php +++ b/pandora_console/include/functions_ui.php @@ -3256,8 +3256,8 @@ function ui_print_datatable(array $parameters) throw new Exception('[ui_print_datatable]: Columns and columns names must have same length'); } - if (!isset($parameters['ajax_url'])) { - throw new Exception('[ui_print_datatable]: Parameter ajax_url is required'); + if (!isset($parameters['ajax_url']) && !isset($parameters['data_element'])) { + throw new Exception('[ui_print_datatable]: Parameter ajax_url or data_element is required'); } if (!isset($parameters['default_pagination'])) { @@ -3490,18 +3490,6 @@ function ui_print_datatable(array $parameters) $pagination_class = $parameters['pagination_class']; } - // Javascript controller. - $js = ' $value) { + $row_tmp[$columns[$key]] = $value; + } + + $row_tmp['Timestamp'] = $row['timestamp']; + array_push($data, (object) $row_tmp); + } + } + + $id_table = 'id_'.$row['id_module_inventory']; + + $table = ui_print_datatable( + [ + 'id' => $id_table, + 'class' => 'info_table w100p', + 'style' => 'width: 100%', + 'columns' => $columns, + 'column_names' => $columns, + 'no_sortable_columns' => [], + 'data_element' => $data, + 'searching' => true, + 'dom_elements' => 'lftipB', + 'order' => [ + 'field' => $columns[0], + 'direction' => 'asc', + ], + 'zeroRecords' => __('No inventory found'), + 'emptyTable' => __('No inventory found'), + 'return' => true, + 'default_pagination' => 10, + 'no_sortable_columns' => [-1], + ] + ); + + $modules .= ui_toggle( + $table, + ''.$row['name'].'', + '', + '', + true, + true, + '', + 'white-box-content w100p', + 'box-shadow white_table_graph w100p', + 'images/arrow_down_green.png', + 'images/arrow_right_green.png', + false, + false, + false, + '', + '', + null, + null, + $id_table + ); + } + + ui_toggle( + $modules, + $agent_rows['agent'], + '', + '', + false, + false + ); + } + } else { + foreach ($rows as $module_rows) { + $agent = ''; + foreach ($module_rows as $row) { + $columns = explode(';', io_safe_output($row['data_format'])); + array_push($columns, 'Timestamp'); + $data = []; + + $data_explode = explode(PHP_EOL, $row['data']); + foreach ($data_explode as $values) { + // Exclude results don't match filter. + if ($inventory_search_string && preg_match('/'.io_safe_output($inventory_search_string).'/', ($values)) == 0) { + continue; + } + + $data_tmp = []; + if ($values !== '') { + $values_explode = explode(';', io_safe_output($values)); + + foreach ($values_explode as $key => $value) { + $data_tmp[$columns[$key]] = $value; + } + + $data_tmp['Timestamp'] = $row['timestamp']; + array_push($data, $data_tmp); + } + } + + + $id_table = 'id_'.$row['id_module_inventory']; + + $table = ui_print_datatable( + [ + 'id' => $id_table, + 'class' => 'info_table w100p', + 'style' => 'width: 100%', + 'columns' => $columns, + 'column_names' => $columns, + 'no_sortable_columns' => [], + 'data_element' => $data, + 'searching' => true, + 'dom_elements' => 'lftipB', + 'order' => [ + 'field' => $columns[0], + 'direction' => 'asc', + ], + 'zeroRecords' => __('No inventory found'), + 'emptyTable' => __('No inventory found'), + 'return' => true, + 'default_pagination' => 10, + 'no_sortable_columns' => [-1], + ] + ); + + $agent .= ui_toggle( + $table, + ''.$row['name_agent'].'', + '', + '', + true, + true, + '', + 'white-box-content w100p', + 'box-shadow white_table_graph w100p', + 'images/arrow_down_green.png', + 'images/arrow_right_green.png', + false, + false, + false, + '', + '', + null, + null, + $id_table + ); + } + + ui_toggle( + $agent, + $module_rows[0]['name'], + '', + '', + false, + false + ); + } + } + } else { + $result = []; + + $sql = 'SELECT alias, direccion, nombre + FROM tagente + WHERE id_agente = '.$inventory_id_agent; + $results = db_get_all_rows_sql($sql); + + foreach ($results as $result) { + hd($result); + } + + ui_print_datatable( + [ + 'id' => 'basic_info', + 'class' => 'info_table w100p', + 'style' => 'width: 100%', + 'columns' => $columns, + 'column_names' => $columns, + 'no_sortable_columns' => [], + 'data_element' => $data, + 'searching' => true, + 'dom_elements' => 'lftipB', + 'order' => [ + 'field' => $columns[0], + 'direction' => 'asc', + ], + 'zeroRecords' => __('No inventory found'), + 'emptyTable' => __('No inventory found'), + 'default_pagination' => 10, + 'no_sortable_columns' => [-1], + ] + ); + } } else { if (empty($inventory_data) === true) { ui_print_info_message(['no_close' => true, 'message' => __('No data found.') ]); @@ -546,6 +750,10 @@ ui_require_jquery_file('bgiframe'); $("#id_group").blur (function () { $(this).css ("width", "180px"); }); + + // Reduce margins between table and pagination. + $('.dataTables_paginate.paging_simple_numbers').css('margin-top', 10); + $('.dataTables_paginate.paging_simple_numbers').css('margin-bottom', 10); }); /* ]]> */