__('Delete') ]; // set default action set_default_action(); if (grv('action') == 'save') { if (isrv('id') && gfrv('id') == 0 && isrv('graph_template_id_prev') && gfrv('graph_template_id_prev') == 0) { srv('action', 'edit'); } } switch (grv('action')) { case 'save': aggregate_form_save(); break; case 'actions': aggregate_form_actions(); break; case 'edit': top_header(); aggregate_template_edit(); bottom_footer(); break; default: top_header(); aggregate_template(); bottom_footer(); break; } /** * aggregate_form_save the save function */ function aggregate_form_save() : void { // make sure we are saving aggregate template if (!isrv('save_component_template')) { header('Location: aggregate_templates.php?action=edit&id=' . gnrv('id')); exit(); } $save1 = []; // updating existing template or creating a new one? if (isrv('id') && grv('id') > 0) { $is_new = false; $save1['id'] = gnrv('id'); } else { $is_new = true; $save1['id'] = 0; } // set some defaults for possibly disabled values if (!isrv('total')) { srv('total', 0); } if (!isrv('total_type')) { srv('total_type', 0); } if (!isrv('order_type')) { srv('order_type', 0); } if (!isrv('total_prefix')) { srv('total_prefix', ''); } // populate aggregate template save array and validate posted values $save1['name'] = form_input_validate(gnrv('name'), 'name', '', false, 3); $save1['graph_template_id'] = gfrv('graph_template_id_prev'); $save1['gprint_prefix'] = form_input_validate(gnrv('gprint_prefix'), 'gprint_prefix', '', true, 3); $save1['gprint_format'] = isrv('gprint_format') ? 'on' : ''; $save1['graph_type'] = form_input_validate(gnrv('graph_type'), 'graph_type', '', false, 3); $save1['total'] = form_input_validate(gnrv('total'), 'total', '', false, 3); $save1['total_type'] = form_input_validate(gnrv('total_type'), 'total_type', '', false, 3); $save1['total_prefix'] = form_input_validate(gnrv('total_prefix'), 'total_prefix', '', true, 3); $save1['order_type'] = form_input_validate(gnrv('order_type'), 'order_type', '', false, 3); $save1['user_id'] = $_SESSION[SESS_USER_ID]; // form validation failed if (is_error_message()) { header('Location: aggregate_templates.php?action=edit&id=' . gnrv('id')); exit; } cacti_log('AGGREGATE GRAPH TEMPLATE Saved ID: ' . $save1['id'] . ' Name: ' . $save1['name'], false, 'AGGREGATE', POLLER_VERBOSITY_DEBUG); // do a quick comparison to see if anything changed if ($is_new == false) { $old = db_fetch_row_prepared('SELECT * FROM aggregate_graph_templates WHERE id = ?', [$save1['id']] ); $save_me = 0; $save_me += ($old['name'] != $save1['name']); $save_me += ($old['gprint_prefix'] != $save1['gprint_prefix']); $save_me += ($old['gprint_format'] != $save1['gprint_format']); $save_me += ($old['graph_type'] != $save1['graph_type']); $save_me += ($old['total'] != $save1['total']); $save_me += ($old['total_type'] != $save1['total_type']); $save_me += ($old['total_prefix'] != $save1['total_prefix']); $save_me += ($old['order_type'] != $save1['order_type']); } else { $save_me = 1; } if ($save_me) { $id = sql_save($save1, 'aggregate_graph_templates', 'id'); // update children of the template db_execute_prepared("UPDATE aggregate_graphs SET gprint_prefix = ?, gprint_format = ?, graph_type = ?, total = ?, total_prefix = ?, order_type = ? WHERE aggregate_template_id = ? AND template_propogation='on'", [ $save1['gprint_prefix'], $save1['gprint_format'], $save1['graph_type'], $save1['total'], $save1['total_prefix'], $save1['order_type'], $id ] ); cacti_log('AGGREGATE GRAPH TEMPLATE Saved ID: ' . $id, false, 'AGGREGATE', POLLER_VERBOSITY_DEBUG); } else { $id = $save1['id']; } if (!$id) { raise_message(2); header('Location: aggregate_templates.php?action=edit&id=' . gnrv('id')); exit; } // save extra graph parameters // validate posted graph params $params_new = aggregate_validate_graph_params($_POST, true); $params_new['aggregate_template_id'] = $id; /* compare new graph param values with existing in DB. * We need to know if there were changes so we only * rebuild existing graphs if needed. */ $params_changed = false; $params_old = db_fetch_row_prepared('SELECT * FROM aggregate_graph_templates_graph WHERE aggregate_template_id = ?', [$id] ); if (!empty($params_old)) { foreach ($params_old as $field => $value_old) { if (isset($params_new[$field]) && $params_new[$field] != $value_old) { $params_changed = true; break; } } } else { $params_changed = true; } // save only if all posted form fields passed validation if ($params_changed && is_error_message() == false) { sql_save($params_new, 'aggregate_graph_templates_graph', 'aggregate_template_id', false); } // save the template items now // get existing item ids and sequences from graph template $graph_templates_items = array_rekey( db_fetch_assoc_prepared('SELECT id, sequence FROM graph_templates_item WHERE local_graph_id = 0 AND graph_template_id = ?', [$save1['graph_template_id']] ), 'id', ['sequence'] ); // get existing aggregate template items $aggregate_template_items_old = array_rekey( db_fetch_assoc_prepared('SELECT * FROM aggregate_graph_templates_item WHERE aggregate_template_id = ?', [$id]), 'graph_templates_item_id', ['sequence', 'color_template', 't_graph_type_id', 'graph_type_id', 't_cdef_id', 'cdef_id', 'item_skip', 'item_total'] ); // update graph template item values with posted values aggregate_validate_graph_items($_POST, $graph_templates_items); $items_changed = false; $items_to_save = []; foreach ($graph_templates_items as $item_id => $data) { $item_new = []; $item_new['aggregate_template_id'] = $id; $item_new['graph_templates_item_id'] = $item_id; $item_new['color_template'] = isset($data['color_template']) ? $data['color_template'] : 0; $item_new['item_skip'] = isset($data['item_skip']) ? 'on' : ''; $item_new['item_total'] = isset($data['item_total']) ? 'on' : ''; $item_new['sequence'] = isset($data['sequence']) ? $data['sequence'] : 0; // compare with old item to see if we need to push out. if (!isset($aggregate_template_items_old[$item_id])) { // this item does not yet exist $items_changed = true; } else { // fill in missing fields with db values $item_new = array_merge($aggregate_template_items_old[$item_id], $item_new); // compare data from user to data from DB foreach ($data as $field => $new_value) { if ($aggregate_template_items_old[$item_id][$field] != $new_value) { $items_changed = true; } } } $items_to_save[] = $item_new; } if ($items_changed) { aggregate_graph_items_save($items_to_save, 'aggregate_graph_templates_item'); } if ($save_me || $params_changed || $items_changed) { if (!push_out_aggregates($id)) { raise_message(2); } } // Save helpers mutate the global message registry; PHPStan cannot observe that side effect. /** @phpstan-ignore booleanNot.alwaysTrue */ if (!is_error_message()) { raise_message(1); } header('Location: aggregate_templates.php?action=edit&id=' . $id); } /** * aggregate_form_actions - the action function */ function aggregate_form_actions() : void { global $actions; // ================= input validation ================= gfrv('drp_action'); // ==================================================== // if we are to save this form, instead of display it if (isrv('selected_items')) { $selected_items = sanitize_unserialize_selected_items(gnrv('selected_items')); if ($selected_items != false) { if (gnrv('drp_action') == '1') { // delete db_execute('DELETE FROM aggregate_graph_templates WHERE ' . array_to_sql_or($selected_items, 'id')); db_execute('DELETE FROM aggregate_graph_templates_item WHERE ' . array_to_sql_or($selected_items, 'aggregate_template_id')); db_execute('DELETE FROM aggregate_graph_templates_graph WHERE ' . array_to_sql_or($selected_items, 'aggregate_template_id')); db_execute("UPDATE aggregate_graphs SET aggregate_template_id=0, template_propogation='' WHERE " . array_to_sql_or($selected_items, 'aggregate_template_id')); } } header('Location: aggregate_templates.php'); exit; } else { $ilist = ''; $iarray = []; // loop through each of the color templates selected on the previous page and get more info about them foreach ($_POST as $var => $val) { if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) { // ================= input validation ================= input_validate_input_number($matches[1], 'chk[1]'); // ==================================================== $ilist .= '