示例#1
0
function upgrade_to_0_8_3()
{
    include "../lib/template.php";
    db_install_execute("0.8.3", "ALTER TABLE `user_auth` CHANGE `graph_policy` `policy_graphs` TINYINT( 1 ) UNSIGNED DEFAULT '1' NOT NULL;");
    db_install_execute("0.8.3", "ALTER TABLE `user_auth` ADD `policy_trees` TINYINT( 1 ) UNSIGNED DEFAULT '1' NOT NULL, ADD `policy_hosts` TINYINT( 1 ) UNSIGNED DEFAULT '1' NOT NULL, ADD `policy_graph_templates` TINYINT( 1 ) UNSIGNED DEFAULT '1' NOT NULL;");
    db_install_execute("0.8.3", "ALTER TABLE `graph_tree_items` ADD `host_id` MEDIUMINT( 8 ) UNSIGNED NOT NULL AFTER `title`;");
    db_install_execute("0.8.3", "ALTER TABLE `rra` ADD `timespan` INT( 12 ) UNSIGNED NOT NULL;");
    db_install_execute("0.8.3", "UPDATE rra set timespan=(rows*steps*144);");
    db_install_execute("0.8.3", "CREATE TABLE `user_auth_perms` (\n\t\t`user_id` mediumint(8) unsigned NOT NULL default '0',\n\t\t`item_id` mediumint(8) unsigned NOT NULL default '0',\n\t\t`type` tinyint(2) unsigned NOT NULL default '0',\n\t\tPRIMARY KEY  (`user_id`,`item_id`,`type`),\n\t\tKEY `user_id` (`user_id`,`type`)\n\t\t) TYPE=MyISAM;");
    $auth_graph = db_fetch_assoc("select user_id,local_graph_id from user_auth_graph");
    /* update to new 'user_auth_perms' table */
    if (sizeof($auth_graph) > 0) {
        foreach ($auth_graph as $item) {
            db_install_execute("0.8.3", "replace into user_auth_perms (user_id,item_id,type) values (" . $item["user_id"] . "," . $item["local_graph_id"] . ",1);");
        }
    }
    $auth_tree = db_fetch_assoc("select user_id,tree_id from user_auth_tree");
    /* update to new 'user_auth_perms' table */
    if (sizeof($auth_tree) > 0) {
        foreach ($auth_tree as $item) {
            db_install_execute("0.8.3", "replace into user_auth_perms (user_id,item_id,type) values (" . $item["user_id"] . "," . $item["tree_id"] . ",2);");
        }
    }
    $users = db_fetch_assoc("select id from user_auth");
    /* default all current users to tree view mode 1 (single pane) */
    if (sizeof($users) > 0) {
        foreach ($users as $item) {
            db_install_execute("0.8.3", "replace into settings_graphs (user_id,name,value) values (" . $item["id"] . ",'default_tree_view_mode',1);");
        }
    }
    /* drop unused tables */
    db_install_execute("0.8.3", "DROP TABLE `user_auth_graph`;");
    db_install_execute("0.8.3", "DROP TABLE `user_auth_tree`;");
    db_install_execute("0.8.3", "DROP TABLE `user_auth_hosts`;");
    /* bug#72 */
    db_install_execute("0.8.3", "UPDATE graph_templates_item set cdef_id=15 where id=25;");
    db_install_execute("0.8.3", "UPDATE graph_templates_item set cdef_id=15 where id=26;");
    db_install_execute("0.8.3", "UPDATE graph_templates_item set cdef_id=15 where id=27;");
    db_install_execute("0.8.3", "UPDATE graph_templates_item set cdef_id=15 where id=28;");
    push_out_graph_item(25);
    push_out_graph_item(26);
    push_out_graph_item(27);
    push_out_graph_item(28);
    /* too many people had problems with the poller cache in 0.8.2a... */
    db_install_execute("0.8.3", "DROP TABLE `data_input_data_cache`");
    db_install_execute("0.8.3", "CREATE TABLE `data_input_data_cache` (\n\t\t`local_data_id` mediumint(8) unsigned NOT NULL default '0',\n\t\t`host_id` mediumint(8) NOT NULL default '0',\n\t\t`data_input_id` mediumint(8) unsigned NOT NULL default '0',\n\t\t`action` tinyint(2) NOT NULL default '1',\n\t\t`command` varchar(255) NOT NULL default '',\n\t\t`management_ip` varchar(15) NOT NULL default '',\n\t\t`snmp_community` varchar(100) NOT NULL default '',\n\t\t`snmp_version` tinyint(1) NOT NULL default '0',\n\t\t`snmp_username` varchar(50) NOT NULL default '',\n\t\t`snmp_password` varchar(50) NOT NULL default '',\n\t\t`rrd_name` varchar(19) NOT NULL default '',\n\t\t`rrd_path` varchar(255) NOT NULL default '',\n\t\t`rrd_num` tinyint(2) unsigned NOT NULL default '0',\n\t\t`arg1` varchar(255) default NULL,\n\t\t`arg2` varchar(255) default NULL,\n\t\t`arg3` varchar(255) default NULL,\n\t\tPRIMARY KEY  (`local_data_id`,`rrd_name`),\n\t\tKEY `local_data_id` (`local_data_id`)\n\t\t) TYPE=MyISAM;");
}
示例#2
0
function form_save() {
	require(CACTI_BASE_PATH . "/include/graph/graph_arrays.php");

	if (isset($_POST["save_component_item"])) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("graph_template_id"));
		input_validate_input_number(get_request_var_post("task_item_id"));
		/* ==================================================== */

		$items[0] = array();

		if (get_request_var_post("graph_type_id") == GRAPH_ITEM_TYPE_LEGEND) {
			/* this can be a major time saver when creating lots of graphs with the typical
			GPRINT LAST/AVERAGE/MAX legends */
			$items = array(
				0 => array(
					"color_id" => "0",
					"graph_type_id" => GRAPH_ITEM_TYPE_GPRINT_LAST,
					"consolidation_function_id" => RRA_CF_TYPE_AVERAGE,
					"text_format" => __("Current:"),
					"hard_return" => ""
					),
				1 => array(
					"color_id" => "0",
					"graph_type_id" => GRAPH_ITEM_TYPE_GPRINT_AVERAGE,
					"consolidation_function_id" => RRA_CF_TYPE_AVERAGE,
					"text_format" => __("Average:"),
					"hard_return" => ""
					),
				2 => array(
					"color_id" => "0",
					"graph_type_id" => GRAPH_ITEM_TYPE_GPRINT_MAX,
					"consolidation_function_id" => RRA_CF_TYPE_AVERAGE,
					"text_format" => __("Maximum:"),
					"hard_return" => CHECKED
					));
		}

		if ($_POST["graph_type_id"] == GRAPH_ITEM_TYPE_CUSTOM_LEGEND) {
			/* this can be a major time saver when creating lots of graphs with the typical VDEFs */
			$items = array(
				0 => array(
					"color_id" => "0",
					"graph_type_id" => read_config_option("cl1_gt_id"),
					"consolidation_function_id" => read_config_option("cl1_cf_id"),
					"vdef_id" => read_config_option("cl1_vdef_id"),
					"text_format" => read_config_option("cl1_text_format"),
					"hard_return" => read_config_option("cl1_hard_return")
					),
				1 => array(
					"color_id" => "0",
					"graph_type_id" => read_config_option("cl2_gt_id"),
					"consolidation_function_id" => read_config_option("cl2_cf_id"),
					"vdef_id" => read_config_option("cl2_vdef_id"),
					"text_format" => read_config_option("cl2_text_format"),
					"hard_return" => read_config_option("cl2_hard_return")
					),
				2 => array(
					"color_id" => "0",
					"graph_type_id" => read_config_option("cl3_gt_id"),
					"consolidation_function_id" => read_config_option("cl3_cf_id"),
					"vdef_id" => read_config_option("cl3_vdef_id"),
					"text_format" => read_config_option("cl3_text_format"),
					"hard_return" => read_config_option("cl3_hard_return")
					),
				3 => array(
					"color_id" => "0",
					"graph_type_id" => read_config_option("cl4_gt_id"),
					"consolidation_function_id" => read_config_option("cl4_cf_id"),
					"vdef_id" => read_config_option("cl4_vdef_id"),
					"text_format" => read_config_option("cl4_text_format"),
					"hard_return" => read_config_option("cl4_hard_return")
					),
				);
			foreach ($items as $key => $item) { #drop "empty" custom legend items
				if (empty($item["text_format"])) unset($items[$key]);
			}
		}

		foreach ($items as $item) {
			$save["id"] 				= form_input_validate($_POST["graph_template_item_id"], "graph_template_item_id", "^[0-9]+$", false, 3);
			$save["hash"] 				= get_hash_graph_template($_POST["graph_template_item_id"], "graph_template_item");
			$save["graph_template_id"] 	= form_input_validate($_POST["graph_template_id"], "graph_template_id", "^[0-9]+$", false, 3);
			$save["local_graph_id"] 	= 0;
			$save["task_item_id"] 		= form_input_validate(((isset($item["task_item_id"]) ? $item["task_item_id"] : (isset($_POST["task_item_id"]) ? $_POST["task_item_id"] : 0))), "task_item_id", "^[0-9]+$", true, 3);
			$save["color_id"] 			= form_input_validate(((isset($item["color_id"]) ? $item["color_id"] : (isset($_POST["color_id"]) ? $_POST["color_id"] : 0))), "color_id", "^[0-9]+$", true, 3);
			$save["alpha"] 				= form_input_validate(((isset($item["alpha"]) ? $item["alpha"] : (isset($_POST["alpha"]) ? $_POST["alpha"] : "FF"))), "alpha", "^[a-fA-F0-9]+$", true, 3);
			$save["graph_type_id"]		= form_input_validate(((isset($item["graph_type_id"]) ? $item["graph_type_id"] : (isset($_POST["graph_type_id"]) ? $_POST["graph_type_id"] : 0))), "graph_type_id", "^[0-9]+$", true, 3);
			if (isset($_POST["line_width"]) || isset($item["line_width"])) {
				$save["line_width"] 	= form_input_validate((isset($item["line_width"]) ? $item["line_width"] : $_POST["line_width"]), "line_width", "^[0-9]+[\.,]+[0-9]+$", true, 3);
			}else { # make sure to transfer old LINEx style into line_width on save
				switch ($save["graph_type_id"]) {
					case GRAPH_ITEM_TYPE_LINE1:
						$save["line_width"] = 1;
						break;
					case GRAPH_ITEM_TYPE_LINE2:
						$save["line_width"] = 2;
						break;
					case GRAPH_ITEM_TYPE_LINE3:
						$save["line_width"] = 3;
						break;
					default:
						$save["line_width"] = 0;
				}
			}
			$save["dashes"] 			= form_input_validate((isset($_POST["dashes"]) ? $_POST["dashes"] : ""), "dashes", "^[0-9]+[,0-9]*$", true, 3);
			$save["dash_offset"] 		= form_input_validate((isset($_POST["dash_offset"]) ? $_POST["dash_offset"] : ""), "dash_offset", "^[0-9]+$", true, 3);
			$save["cdef_id"] 			= form_input_validate(((isset($item["cdef_id"]) ? $item["cdef_id"] : (isset($_POST["cdef_id"]) ? $_POST["cdef_id"] : 0))), "cdef_id", "^[0-9]+$", true, 3);
			$save["vdef_id"] 			= form_input_validate(((isset($item["vdef_id"]) ? $item["vdef_id"] : (isset($_POST["vdef_id"]) ? $_POST["vdef_id"] : 0))), "vdef_id", "^[0-9]+$", true, 3);
			$save["shift"] 				= form_input_validate((isset($_POST["shift"]) ? $_POST["shift"] : ""), "shift", "^((on)|)$", true, 3);
			$save["consolidation_function_id"] = form_input_validate(((isset($item["consolidation_function_id"]) ? $item["consolidation_function_id"] : (isset($_POST["consolidation_function_id"]) ? $_POST["consolidation_function_id"] : 0))), "consolidation_function_id", "^[0-9]+$", true, 3);
			$save["textalign"] 			= form_input_validate((isset($_POST["textalign"]) ? $_POST["textalign"] : ""), "textalign", "^[a-z]+$", true, 3);
			$save["text_format"] 		= form_input_validate(((isset($item["text_format"]) ? $item["text_format"] : (isset($_POST["text_format"]) ? $_POST["text_format"] : ""))), "text_format", "", true, 3);
			$save["value"] 				= form_input_validate((isset($_POST["value"]) ? $_POST["value"] : ""), "value", "", true, 3);
			$save["hard_return"] 		= form_input_validate(((isset($item["hard_return"]) ? $item["hard_return"] : (isset($_POST["hard_return"]) ? $_POST["hard_return"] : ""))), "hard_return", "", true, 3);
			$save["gprint_id"] 			= form_input_validate(((isset($item["gprint_id"]) ? $item["gprint_id"] : (isset($_POST["gprint_id"]) ? $_POST["gprint_id"] : 0))), "gprint_id", "^[0-9]+$", true, 3);
			/* generate a new sequence if needed */
			if (empty($_POST["sequence"])) {
				$_POST["sequence"] 		= get_sequence($_POST["sequence"], "sequence", "graph_templates_item", "graph_template_id=" . $_POST["graph_template_id"] . " and local_graph_id=0");
			}
			$save["sequence"] 			= form_input_validate($_POST["sequence"], "sequence", "^[0-9]+$", false, 3);

			if (!is_error_message()) {
				/* Before we save the item, let's get a look at task_item_id <-> input associations */
				$orig_data_source_graph_inputs = db_fetch_assoc("select
					graph_template_input.id,
					graph_template_input.name,
					graph_templates_item.task_item_id
					from (graph_template_input,graph_template_input_defs,graph_templates_item)
					where graph_template_input.id=graph_template_input_defs.graph_template_input_id
					and graph_template_input_defs.graph_template_item_id=graph_templates_item.id
					and graph_template_input.graph_template_id=" . $save["graph_template_id"] . "
					and graph_template_input.column_name='task_item_id'
					group by graph_templates_item.task_item_id");

				$orig_data_source_to_input = array_rekey($orig_data_source_graph_inputs, "task_item_id", "id");

				$graph_template_item_id = sql_save($save, "graph_templates_item");

				if ($graph_template_item_id) {
					raise_message(1);

					if (!empty($save["task_item_id"])) {
						/* old item clean-up.  Don't delete anything if the item <-> task_item_id association remains the same. */
						if ($_POST["hidden_task_item_id"] != $_POST["task_item_id"]) {
							/* It changed.  Delete any old associations */
							db_execute("delete from graph_template_input_defs where graph_template_item_id=$graph_template_item_id");

							/* Input for current data source exists and has changed.  Update the association */
							if (isset($orig_data_source_to_input{$save["task_item_id"]})) {
								db_execute("REPLACE INTO graph_template_input_defs " .
											"(graph_template_input_id, graph_template_item_id) " .
											"VALUES (" .
											$orig_data_source_to_input{$save["task_item_id"]} . "," .
											$graph_template_item_id .
											")");
							}
						}

						/* an input for the current data source does NOT currently exist, let's create one */
						if (!isset($orig_data_source_to_input{$save["task_item_id"]})) {
							$ds_name = db_fetch_cell("select data_source_name from data_template_rrd where id=" . $_POST["task_item_id"]);

							db_execute("REPLACE INTO graph_template_input " .
										"(hash,graph_template_id,name,column_name) " .
										"VALUES ('" .
										get_hash_graph_template(0, "graph_template_input") . "'," .
										$save["graph_template_id"] . "," .
										"'Data Source [" . $ds_name . "]'," .
										'task_item_id' .
										")");

							$graph_template_input_id = db_fetch_insert_id();

							$graph_items = db_fetch_assoc("select id from graph_templates_item where graph_template_id=" . $save["graph_template_id"] . " and task_item_id=" . $_POST["task_item_id"]);

							if (sizeof($graph_items) > 0) {
								foreach ($graph_items as $graph_item) {
									db_execute("REPLACE INTO graph_template_input_defs " .
												"(graph_template_input_id,graph_template_item_id) " .
												"VALUES (" .
												$graph_template_input_id . "," .
												$graph_item["id"] .
												")");
								}
							}
						}
					}

					push_out_graph_item($graph_template_item_id);


					if (isset($_POST["task_item_id"]) && isset($orig_data_source_to_input{$_POST["task_item_id"]})) {
						/* make sure all current graphs using this graph input are aware of this change */
						push_out_graph_input($orig_data_source_to_input{$_POST["task_item_id"]}, $graph_template_item_id, array($graph_template_item_id => $graph_template_item_id));
					}
				}else{
					raise_message(2);
				}
			}

			$_POST["sequence"] = 0;
		}

		if (is_error_message()) {
			header("Location: graph_templates_items.php?action=item_edit&graph_template_item_id=" . (empty($graph_template_item_id) ? $_POST["graph_template_item_id"] : $graph_template_item_id) . "&id=" . $_POST["graph_template_id"]);
		}else{
			header("Location: graph_templates.php?action=template_edit&id=" . $_POST["graph_template_id"] . "&template_id=" . $_POST["graph_template_id"] . "&tab=items");
		}
		exit;
	}
}
示例#3
0
function form_save() {
	if (isset($_POST["save_component_item"])) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("graph_template_id"));
		input_validate_input_number(get_request_var_post("task_item_id"));
		/* ==================================================== */

		global $graph_item_types;

		$items[0] = array();

		if ($graph_item_types{$_POST["graph_type_id"]} == "LEGEND") {
			/* this can be a major time saver when creating lots of graphs with the typical
			GPRINT LAST/AVERAGE/MAX legends */
			$items = array(
				0 => array(
					"color_id" => "0",
					"graph_type_id" => "9",
					"consolidation_function_id" => "4",
					"text_format" => "Current:",
					"hard_return" => ""
					),
				1 => array(
					"color_id" => "0",
					"graph_type_id" => "9",
					"consolidation_function_id" => "1",
					"text_format" => "Average:",
					"hard_return" => ""
					),
				2 => array(
					"color_id" => "0",
					"graph_type_id" => "9",
					"consolidation_function_id" => "3",
					"text_format" => "Maximum:",
					"hard_return" => "on"
					));
		}

		foreach ($items as $item) {
			/* generate a new sequence if needed */
			if (empty($_POST["sequence"])) {
				$_POST["sequence"] = get_sequence($_POST["sequence"], "sequence", "graph_templates_item", "graph_template_id=" . $_POST["graph_template_id"] . " and local_graph_id=0");
			}

			$save["id"] = $_POST["graph_template_item_id"];
			$save["hash"] = get_hash_graph_template($_POST["graph_template_item_id"], "graph_template_item");
			$save["graph_template_id"] = $_POST["graph_template_id"];
			$save["local_graph_id"] = 0;
			$save["task_item_id"] = form_input_validate($_POST["task_item_id"], "task_item_id", "", true, 3);
			$save["color_id"] = form_input_validate((isset($item["color_id"]) ? $item["color_id"] : $_POST["color_id"]), "color_id", "", true, 3);
			/* if alpha is disabled, use invisible_alpha instead */
			if (!isset($_POST["alpha"])) {$_POST["alpha"] = $_POST["invisible_alpha"];}
			$save["alpha"] = form_input_validate((isset($item["alpha"]) ? $item["alpha"] : $_POST["alpha"]), "alpha", "", true, 3);
			$save["graph_type_id"] = form_input_validate((isset($item["graph_type_id"]) ? $item["graph_type_id"] : $_POST["graph_type_id"]), "graph_type_id", "", true, 3);
			$save["cdef_id"] = form_input_validate($_POST["cdef_id"], "cdef_id", "", true, 3);
			$save["consolidation_function_id"] = form_input_validate((isset($item["consolidation_function_id"]) ? $item["consolidation_function_id"] : $_POST["consolidation_function_id"]), "consolidation_function_id", "", true, 3);
			$save["text_format"] = form_input_validate((isset($item["text_format"]) ? $item["text_format"] : $_POST["text_format"]), "text_format", "", true, 3);
			$save["value"] = form_input_validate($_POST["value"], "value", "", true, 3);
			$save["hard_return"] = form_input_validate(((isset($item["hard_return"]) ? $item["hard_return"] : (isset($_POST["hard_return"]) ? $_POST["hard_return"] : ""))), "hard_return", "", true, 3);
			$save["gprint_id"] = form_input_validate($_POST["gprint_id"], "gprint_id", "", true, 3);
			$save["sequence"] = $_POST["sequence"];

			if (!is_error_message()) {
				/* Before we save the item, let's get a look at task_item_id <-> input associations */
				$orig_data_source_graph_inputs = db_fetch_assoc("select
					graph_template_input.id,
					graph_template_input.name,
					graph_templates_item.task_item_id
					from (graph_template_input,graph_template_input_defs,graph_templates_item)
					where graph_template_input.id=graph_template_input_defs.graph_template_input_id
					and graph_template_input_defs.graph_template_item_id=graph_templates_item.id
					and graph_template_input.graph_template_id=" . $save["graph_template_id"] . "
					and graph_template_input.column_name='task_item_id'
					group by graph_templates_item.task_item_id");

				$orig_data_source_to_input = array_rekey($orig_data_source_graph_inputs, "task_item_id", "id");

				$graph_template_item_id = sql_save($save, "graph_templates_item");

				if ($graph_template_item_id) {
					raise_message(1);

					if (!empty($save["task_item_id"])) {
						/* old item clean-up.  Don't delete anything if the item <-> task_item_id association remains the same. */
						if ($_POST["_task_item_id"] != $_POST["task_item_id"]) {
							/* It changed.  Delete any old associations */
							db_execute("delete from graph_template_input_defs where graph_template_item_id=$graph_template_item_id");

							/* Input for current data source exists and has changed.  Update the association */
							if (isset($orig_data_source_to_input{$save["task_item_id"]})) {
								db_execute("replace into graph_template_input_defs (graph_template_input_id,
								graph_template_item_id) values (" . $orig_data_source_to_input{$save["task_item_id"]}
								. ",$graph_template_item_id)");
							}
						}

						/* an input for the current data source does NOT currently exist, let's create one */
						if (!isset($orig_data_source_to_input{$save["task_item_id"]})) {
							$ds_name = db_fetch_cell("select data_source_name from data_template_rrd where id=" . $_POST["task_item_id"]);

							db_execute("replace into graph_template_input (hash,graph_template_id,name,column_name) values (
								'" . get_hash_graph_template(0, "graph_template_input") . "'," . $save["graph_template_id"] . ",
								'Data Source [$ds_name]','task_item_id')");

							$graph_template_input_id = db_fetch_insert_id();

							$graph_items = db_fetch_assoc("select id from graph_templates_item where graph_template_id=" . $save["graph_template_id"] . " and task_item_id=" . $_POST["task_item_id"]);

							if (sizeof($graph_items) > 0) {
							foreach ($graph_items as $graph_item) {
								db_execute("replace into graph_template_input_defs (graph_template_input_id,graph_template_item_id) values ($graph_template_input_id," . $graph_item["id"] . ")");
							}
							}
						}
					}

					push_out_graph_item($graph_template_item_id);

					if (isset($orig_data_source_to_input{$_POST["task_item_id"]})) {
						/* make sure all current graphs using this graph input are aware of this change */
						push_out_graph_input($orig_data_source_to_input{$_POST["task_item_id"]}, $graph_template_item_id, array($graph_template_item_id => $graph_template_item_id));
					}
				}else{
					raise_message(2);
				}
			}

			$_POST["sequence"] = 0;
		}

		if (is_error_message()) {
			header("Location: graph_templates_items.php?action=item_edit&graph_template_item_id=" . (empty($graph_template_item_id) ? $_POST["graph_template_item_id"] : $graph_template_item_id) . "&id=" . $_POST["graph_template_id"]);
			exit;
		}else{
			header("Location: graph_templates.php?action=template_edit&id=" . $_POST["graph_template_id"]);
			exit;
		}
	}
}
示例#4
0
function form_save()
{
    if (isset($_POST['save_component_item'])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post('graph_template_id'));
        input_validate_input_number(get_request_var_post('graph_template_item_id'));
        input_validate_input_number(get_request_var_post('task_item_id'));
        /* ==================================================== */
        global $graph_item_types;
        $items[0] = array();
        if ($graph_item_types[$_POST['graph_type_id']] == 'LEGEND') {
            /* this can be a major time saver when creating lots of graphs with the typical
            			GPRINT LAST/AVERAGE/MAX legends */
            $items = array(0 => array('color_id' => '0', 'graph_type_id' => '9', 'consolidation_function_id' => '4', 'text_format' => 'Current:', 'hard_return' => ''), 1 => array('color_id' => '0', 'graph_type_id' => '9', 'consolidation_function_id' => '1', 'text_format' => 'Average:', 'hard_return' => ''), 2 => array('color_id' => '0', 'graph_type_id' => '9', 'consolidation_function_id' => '3', 'text_format' => 'Maximum:', 'hard_return' => 'on'));
        }
        foreach ($items as $item) {
            /* generate a new sequence if needed */
            if (empty($_POST['sequence'])) {
                $_POST['sequence'] = get_sequence($_POST['sequence'], 'sequence', 'graph_templates_item', 'graph_template_id=' . $_POST['graph_template_id'] . ' AND local_graph_id=0');
            }
            $save['id'] = $_POST['graph_template_item_id'];
            $save['hash'] = get_hash_graph_template($_POST['graph_template_item_id'], 'graph_template_item');
            $save['graph_template_id'] = $_POST['graph_template_id'];
            $save['local_graph_id'] = 0;
            $save['task_item_id'] = form_input_validate($_POST['task_item_id'], 'task_item_id', '', true, 3);
            $save['color_id'] = form_input_validate(isset($item['color_id']) ? $item['color_id'] : $_POST['color_id'], 'color_id', '', true, 3);
            /* if alpha is disabled, use invisible_alpha instead */
            if (!isset($_POST['alpha'])) {
                $_POST['alpha'] = $_POST['invisible_alpha'];
            }
            $save['alpha'] = form_input_validate(isset($item['alpha']) ? $item['alpha'] : $_POST['alpha'], 'alpha', '', true, 3);
            $save['graph_type_id'] = form_input_validate(isset($item['graph_type_id']) ? $item['graph_type_id'] : $_POST['graph_type_id'], 'graph_type_id', '', true, 3);
            $save['cdef_id'] = form_input_validate($_POST['cdef_id'], 'cdef_id', '', true, 3);
            $save['consolidation_function_id'] = form_input_validate(isset($item['consolidation_function_id']) ? $item['consolidation_function_id'] : $_POST['consolidation_function_id'], 'consolidation_function_id', '', true, 3);
            $save['text_format'] = form_input_validate(isset($item['text_format']) ? $item['text_format'] : $_POST['text_format'], 'text_format', '', true, 3);
            $save['value'] = form_input_validate($_POST['value'], 'value', '', true, 3);
            $save['hard_return'] = form_input_validate(isset($item['hard_return']) ? $item['hard_return'] : (isset($_POST['hard_return']) ? $_POST['hard_return'] : ''), 'hard_return', '', true, 3);
            $save['gprint_id'] = form_input_validate($_POST['gprint_id'], 'gprint_id', '', true, 3);
            $save['sequence'] = $_POST['sequence'];
            if (!is_error_message()) {
                /* Before we save the item, let's get a look at task_item_id <-> input associations */
                $orig_data_source_graph_inputs = db_fetch_assoc_prepared("SELECT\n\t\t\t\t\tgraph_template_input.id,\n\t\t\t\t\tgraph_template_input.name,\n\t\t\t\t\tgraph_templates_item.task_item_id\n\t\t\t\t\tFROM (graph_template_input, graph_template_input_defs, graph_templates_item)\n\t\t\t\t\tWHERE graph_template_input.id = graph_template_input_defs.graph_template_input_id\n\t\t\t\t\tAND graph_template_input_defs.graph_template_item_id = graph_templates_item.id\n\t\t\t\t\tAND graph_template_input.graph_template_id = ?\n\t\t\t\t\tAND graph_template_input.column_name = 'task_item_id'\n\t\t\t\t\tGROUP BY graph_templates_item.task_item_id", array($save['graph_template_id']));
                $orig_data_source_to_input = array_rekey($orig_data_source_graph_inputs, 'task_item_id', 'id');
                $graph_template_item_id = sql_save($save, 'graph_templates_item');
                if ($graph_template_item_id) {
                    raise_message(1);
                    if (!empty($save['task_item_id'])) {
                        /* old item clean-up.  Don't delete anything if the item <-> task_item_id association remains the same. */
                        if ($_POST['_task_item_id'] != $_POST['task_item_id']) {
                            /* It changed.  Delete any old associations */
                            db_execute_prepared('DELETE FROM graph_template_input_defs WHERE graph_template_item_id = ?', array($graph_template_item_id));
                            /* Input for current data source exists and has changed.  Update the association */
                            if (isset($orig_data_source_to_input[$save['task_item_id']])) {
                                db_execute_prepared('REPLACE INTO graph_template_input_defs (graph_template_input_id, graph_template_item_id) values (?, ?)', array($orig_data_source_to_input[$save['task_item_id']], $graph_template_item_id));
                            }
                        }
                        /* an input for the current data source does NOT currently exist, let's create one */
                        if (!isset($orig_data_source_to_input[$save['task_item_id']])) {
                            $ds_name = db_fetch_cell_prepared('SELECT data_source_name FROM data_template_rrd WHERE id = ?', array($_POST['task_item_id']));
                            db_execute_prepared("REPLACE INTO graph_template_input (hash, graph_template_id, name, column_name) VALUES (?, ?, ?, 'task_item_id')", array(get_hash_graph_template(0, 'graph_template_input'), $save['graph_template_id'], 'Data Source [$ds_name]'));
                            $graph_template_input_id = db_fetch_insert_id();
                            $graph_items = db_fetch_assoc_prepared('SELECT id FROM graph_templates_item WHERE graph_template_id = ? AND task_item_id = ?', array($save['graph_template_id'], $_POST['task_item_id']));
                            if (sizeof($graph_items) > 0) {
                                foreach ($graph_items as $graph_item) {
                                    db_execute_prepared('REPLACE INTO graph_template_input_defs (graph_template_input_id, graph_template_item_id) VALUES (?, ?)', array($graph_template_input_id, $graph_item['id']));
                                }
                            }
                        }
                    }
                    push_out_graph_item($graph_template_item_id);
                    if (isset($orig_data_source_to_input[$_POST['task_item_id']])) {
                        /* make sure all current graphs using this graph input are aware of this change */
                        push_out_graph_input($orig_data_source_to_input[$_POST['task_item_id']], $graph_template_item_id, array($graph_template_item_id => $graph_template_item_id));
                    }
                } else {
                    raise_message(2);
                }
            }
            $_POST['sequence'] = 0;
        }
        if (is_error_message()) {
            header('Location: graph_templates_items.php?action=item_edit&graph_template_item_id=' . (empty($graph_template_item_id) ? $_POST['graph_template_item_id'] : $graph_template_item_id) . '&id=' . $_POST['graph_template_id']);
            exit;
        } else {
            header('Location: graph_templates.php?action=template_edit&id=' . $_POST['graph_template_id']);
            exit;
        }
    }
}