From 8df792f11b401ba84d851b73d9b8b03b4d1b3ae2 Mon Sep 17 00:00:00 2001 From: Daniel Cebrian Date: Fri, 15 Dec 2023 10:41:21 +0100 Subject: [PATCH] #12571 custom graphs refactored --- .../reporting/graph_builder.graph_editor.php | 246 +++++++----------- .../godmode/reporting/graph_builder.php | 2 +- pandora_console/include/ajax/graph.ajax.php | 103 ++++++++ pandora_console/include/functions_html.php | 25 +- .../include/styles/custom_graph.css | 16 ++ 5 files changed, 235 insertions(+), 157 deletions(-) create mode 100644 pandora_console/include/styles/custom_graph.css diff --git a/pandora_console/godmode/reporting/graph_builder.graph_editor.php b/pandora_console/godmode/reporting/graph_builder.graph_editor.php index 0256c28f92..c4da379ebd 100644 --- a/pandora_console/godmode/reporting/graph_builder.graph_editor.php +++ b/pandora_console/godmode/reporting/graph_builder.graph_editor.php @@ -30,7 +30,7 @@ if (!$report_w && !$report_m) { require_once $config['homedir'].'/include/functions_agents.php'; require_once $config['homedir'].'/include/functions_modules.php'; require_once $config['homedir'].'/include/functions_groups.php'; - +ui_require_css_file('custom_graph'); $editGraph = (bool) get_parameter('edit_graph', 0); $action = get_parameter('action', ''); @@ -41,113 +41,6 @@ if (isset($_GET['get_agent'])) { } } -switch ($action) { - case 'sort_items': - $resultOperationDB = null; - $position_to_sort = (int) get_parameter('position_to_sort', 1); - $ids_serialize = (string) get_parameter('ids_items_to_sort', ''); - $move_to = (string) get_parameter('move_to', 'after'); - - $countItems = db_get_sql( - ' - SELECT COUNT(id_gs) - FROM tgraph_source - WHERE id_graph = '.$id_graph - ); - - if (($countItems < $position_to_sort) || ($position_to_sort < 1)) { - $resultOperationDB = false; - } else if (!empty($ids_serialize)) { - $ids = explode('|', $ids_serialize); - $items = db_get_all_rows_sql( - 'SELECT id_gs, `field_order` - FROM tgraph_source - WHERE id_graph = '.$id_graph.' - ORDER BY `field_order`' - ); - - if ($items === false) { - $items = []; - } - - - // Clean the repeated order values. - $order_temp = 1; - foreach ($items as $item) { - db_process_sql_update( - 'tgraph_source', - ['`field_order`' => $order_temp], - ['id_gs' => $item['id_rc']] - ); - - $order_temp++; - } - - $items = db_get_all_rows_sql( - 'SELECT id_gs, `field_order` - FROM tgraph_source - WHERE id_graph = '.$id_graph.' - ORDER BY `field_order`' - ); - - if ($items === false) { - $items = []; - } - - - - $temp = []; - - $temp = []; - foreach ($items as $item) { - // Remove the contents from the block to sort. - if (array_search($item['id_gs'], $ids) === false) { - $temp[$item['field_order']] = $item['id_gs']; - } - } - - $items = $temp; - - - - $sorted_items = []; - foreach ($items as $pos => $id_unsort) { - if ($pos == $position_to_sort) { - if ($move_to == 'after') { - $sorted_items[] = $id_unsort; - } - - foreach ($ids as $id) { - $sorted_items[] = $id; - } - - if ($move_to != 'after') { - $sorted_items[] = $id_unsort; - } - } else { - $sorted_items[] = $id_unsort; - } - } - - $items = $sorted_items; - - - - foreach ($items as $order => $id) { - db_process_sql_update( - 'tgraph_source', - ['`field_order`' => ($order + 1)], - ['id_gs' => $id] - ); - } - - $resultOperationDB = true; - } else { - $resultOperationDB = false; - } - break; -} - if ($editGraph) { $graphRows = db_get_all_rows_sql( 'SELECT t1.*, @@ -257,41 +150,6 @@ $table->data[0][1] = html_print_label_input_block( ); -$SortItems = "
"; -$SortItems .= html_print_table($table, true); -$SortItems .= html_print_input_hidden('action', 'sort_items', true); -$SortItems .= html_print_div( - [ - 'class' => 'action-buttons', - 'content' => html_print_submit_button( - __('Sort'), - 'srcbutton', - false, - [ - 'class' => 'mini', - 'icon' => 'search', - 'mode' => 'secondary', - ], - true - ), - ], - true -); -$SortItems .= '
'; - -ui_toggle( - $SortItems, - ''.__('Sort items').'', - '', - '', - false, - false, - '', - 'white-box-content no_border', - 'filter-datatable-main box-flat white_table_graph max_floating_element_size' -); - - // Configuration form. echo ''; echo "
"; @@ -300,10 +158,11 @@ echo "'; echo ''; echo ''; @@ -340,6 +199,7 @@ echo ''; // Modules table. if ($count_module_array > 0) { echo "
'.html_print_input( [ - 'type' => 'select_multiple_modules_filtered', - 'uniqId' => 'modules', - 'class' => 'flex flex-row', - 'searchBar' => true, + 'type' => 'select_multiple_modules_filtered', + 'uniqId' => 'modules', + 'class' => 'flex flex-row', + 'searchBar' => false, + 'placeholderAgents' => __('Search agent name'), ] ).'
"; + echo ''; echo ' @@ -348,6 +208,8 @@ if ($count_module_array > 0) { '; + echo ''; + echo ''; $color = 0; for ($a = 0; $a < $count_module_array; $a++) { // Calculate table line color. @@ -359,7 +221,7 @@ if ($count_module_array > 0) { $color = 1; } - echo ""; + echo ""; echo "'; echo "'; @@ -410,9 +272,30 @@ if ($count_module_array > 0) { echo ''; - echo ''; @@ -420,6 +303,8 @@ if ($count_module_array > 0) { echo ''; } + echo ''; + echo '
'.__('P.').' '.__('Agent').''.__('Weight').' '.__('Delete').' '.__('Sort').'
$position_array[$a]
$position_array[$a]".$agent_array[$a].'"; echo modules_get_agentmodule_name($module_array[$a]).''; + echo ''; - echo html_print_checkbox_extended('sorted_items[]', $idgs_array[$a], false, false, '', 'class="selected_check"', true); + echo html_print_input_image( + 'up', + 'images/arrow-up-white.png', + 'up', + ($config['style'] !== 'pandora_black') ? 'filter: invert(100%)' : '', + true, + [ + 'class' => 'invert_filter main_menu_icon', + 'onclick' => 'reorder(\'up\', \''.$idgs_array[$a].'\', this)', + ], + ); + echo html_print_input_image( + 'down', + 'images/arrow-down-white.png', + 'down', + ($config['style'] !== 'pandora_black') ? 'filter: invert(100%)' : '', + true, + [ + 'class' => 'invert_filter main_menu_icon', + 'onclick' => 'reorder(\'down\', \''.$idgs_array[$a].'\', this)', + ] + ); echo '
'; } @@ -482,4 +367,65 @@ function added_ids_sorted_items_to_hidden_input() { return true; } } + + +function reorder(action, idElement, element) { + var tr = $(element).parent().parent(); + switch (action) { + case "up": + changePosition(action, idElement) + .then((data) => { + if(data.success) { + $(tr).find('.position').html(parseInt($(tr).find('.position').html()) - 1); + $($(tr).prev()).find('.position').html(parseInt($($(tr).prev()).find('.position').html()) + 1); + $(tr).prev().insertAfter(tr); + } + }) + .catch((err) => { + console.log(err); + }) + break; + + case "down": + changePosition(action, idElement) + .then((data) => { + if(data.success) { + $(tr).find('.position').html(parseInt($(tr).find('.position').html()) + 1); + $($(tr).next()).find('.position').html(parseInt(($(tr).next()).find('.position').html()) - 1); + $(tr).next().insertBefore(tr); + } + }) + .catch((err) => { + console.log(err); + }) + break; + + default: + break; + } +} + +function changePosition(order, idElement) { + return new Promise(function(resolve, reject) { + $.ajax({ + method: "POST", + url: "", + dataType: "json", + data: { + page: "include/ajax/graph.ajax", + sort_items: 1, + order, + id_graph: , + id: idElement + }, + success: function(data) { + resolve(data); + }, + error: function(error) { + reject(error); + } + }); + }); +} + diff --git a/pandora_console/godmode/reporting/graph_builder.php b/pandora_console/godmode/reporting/graph_builder.php index 2e7b1f782e..e6aa119e84 100644 --- a/pandora_console/godmode/reporting/graph_builder.php +++ b/pandora_console/godmode/reporting/graph_builder.php @@ -237,7 +237,7 @@ if ($add_module === true) { ); $id_agent_modules = db_get_all_rows_sql( - 'SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente IN ('.implode(',', $id_agents).") AND nombre IN ('".implode("','", $id_modules)."')" + 'SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente IN ('.implode(',', $id_agents).") AND id_agente_modulo IN ('".implode("','", $id_modules)."')" ); if (count($id_agent_modules) > 0 && $id_agent_modules != '') { diff --git a/pandora_console/include/ajax/graph.ajax.php b/pandora_console/include/ajax/graph.ajax.php index 6ea76f373b..2db3a72912 100644 --- a/pandora_console/include/ajax/graph.ajax.php +++ b/pandora_console/include/ajax/graph.ajax.php @@ -17,6 +17,8 @@ $save_custom_graph = (bool) get_parameter('save_custom_graph'); $print_custom_graph = (bool) get_parameter('print_custom_graph', false); $print_sparse_graph = (bool) get_parameter('print_sparse_graph'); $get_graphs = (bool) get_parameter('get_graphs_container'); +$sort_items = (bool) get_parameter('sort_items'); + $width = get_parameter('width', 0); $height = get_parameter('height', 0); @@ -337,3 +339,104 @@ if ($get_graphs) { return; } } + + +if ($sort_items === true) { + $order = (string) get_parameter('order'); + $id = (string) get_parameter('id', ''); + $idGraph = (string) get_parameter('id_graph', ''); + $total = db_get_num_rows('SELECT * FROM tgraph_source WHERE id_graph = '.$idGraph.''); + $item = db_get_row_sql( + 'SELECT id_gs, field_order + FROM tgraph_source + WHERE id_gs = '.$id.' + ORDER BY field_order' + ); + + switch ($order) { + case 'up': + if (($item['field_order'] - 1) < 1) { + echo json_encode(['success' => false]); + return; + } + + $prevItem = db_get_row_sql( + 'SELECT id_gs, field_order + FROM tgraph_source + WHERE id_graph = '.$idGraph.' + AND field_order = '.($item['field_order'] - 1).' + ORDER BY field_order' + ); + + db_process_sql_begin(); + + $resultItem = db_process_sql_update( + 'tgraph_source', + ['field_order' => ($item['field_order'] - 1)], + ['id_gs' => $item['id_gs']], + false + ); + $resultPrevItem = db_process_sql_update( + 'tgraph_source', + ['field_order' => ($prevItem['field_order'] + 1)], + ['id_gs' => $prevItem['id_gs']], + false + ); + + if ($resultItem !== false && $resultPrevItem !== false) { + db_process_sql_commit(); + echo json_encode(['success' => true]); + return; + } else { + db_process_sql_rollback(); + echo json_encode(['success' => false]); + return; + } + break; + + case 'down': + if (($item['field_order'] + 1) > $total) { + echo json_encode(['success' => false]); + return; + } + + $nextItem = db_get_row_sql( + 'SELECT id_gs, field_order + FROM tgraph_source + WHERE id_graph = '.$idGraph.' + AND field_order = '.($item['field_order'] + 1).' + ORDER BY field_order' + ); + + + db_process_sql_begin(); + + $resultItem = db_process_sql_update( + 'tgraph_source', + ['field_order' => ($item['field_order'] + 1)], + ['id_gs' => $item['id_gs']], + false + ); + $resultNextItem = db_process_sql_update( + 'tgraph_source', + ['field_order' => ($nextItem['field_order'] - 1)], + ['id_gs' => $nextItem['id_gs']], + false + ); + + if ($resultItem !== false && $resultNextItem !== false) { + db_process_sql_commit(); + echo json_encode(['success' => true]); + return; + } else { + db_process_sql_rollback(); + echo json_encode(['success' => false]); + return; + } + break; + + default: + echo json_encode(['success' => false]); + break; + } +} diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 5b37dc51d2..eada8c4d90 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -774,7 +774,8 @@ function html_print_select( $select2_multiple_enable_all=false, $form='', $order=false, - $custom_id=null + $custom_id=null, + $placeholder='', ) { $output = "\n"; @@ -1013,7 +1014,8 @@ function html_print_select( $output .= '