function api_graph_template_save($graph_template_id, $_fields_graph) { require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_info.php"); require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_push.php"); /* sanity checks */ validate_id_die($graph_template_id, "graph_template_id", true); /* make sure that there is at least one field to save */ if (sizeof($_fields_graph) == 0) { return false; } /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $graph_template_id); /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_graph, api_graph_template_form_list()); $_fields += sql_get_database_field_array($_fields_graph, api_graph_form_list()); if (db_replace("graph_template", $_fields, array("id"))) { if (empty($graph_template_id)) { $graph_template_id = db_fetch_insert_id(); }else{ /* push out graph template fields */ api_graph_template_propagate($graph_template_id); } return $graph_template_id; }else{ return false; } }
function api_graph_save($graph_id, &$_fields_graph, $skip_cache_update = false) { require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); /* sanity check for $graph_id */ if (!is_numeric($graph_id)) { return false; } /* field: id */ $_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $graph_id); /* field: graph_template_id */ if (isset($_fields_graph["graph_template_id"])) { $_fields["graph_template_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph["graph_template_id"]); } /* field: host_id */ if (isset($_fields_graph["host_id"])) { $_fields["host_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph["host_id"]); } /* convert the input array into something that is compatible with db_replace() */ $_fields += sql_get_database_field_array($_fields_graph, api_graph_form_list()); /* check for an empty field list */ if (sizeof($_fields) == 1) { return true; } if (db_replace("graph", $_fields, array("id"))) { $graph_id = db_fetch_insert_id(); if ($skip_cache_update == false) { api_graph_title_cache_update($graph_id); } return true; }else{ return false; } }
function api_graph_fields_validate(&$_fields_graph, &$_fields_suggested_values, $graph_field_name_format = "|field|", $suggested_values_field_name_format = "") { require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); if (sizeof($_fields_graph) == 0) { return array(); } /* array containing errored fields */ $error_fields = array(); /* get a complete field list */ $fields_graph = api_graph_form_list(); /* base fields */ while (list($_field_name, $_field_array) = each($fields_graph)) { if ((isset($_fields_graph[$_field_name])) && (isset($_field_array["validate_regexp"])) && (isset($_field_array["validate_empty"]))) { $form_field_name = str_replace("|field|", $_field_name, $graph_field_name_format); if (!form_input_validate($_fields_graph[$_field_name], $form_field_name, $_field_array["validate_regexp"], $_field_array["validate_empty"])) { $error_fields[] = $form_field_name; } } } /* suggested values */ while (list($_field_name, $_sv_array) = each($_fields_suggested_values)) { if ((isset($fields_graph[$_field_name])) && (isset($fields_graph[$_field_name]["validate_regexp"])) && (isset($fields_graph[$_field_name]["validate_empty"]))) { while (list($_sv_seq, $_sv_arr) = each($_sv_array)) { $form_field_name = str_replace("|field|", $_field_name, str_replace("|id|", $_sv_arr["id"], $suggested_values_field_name_format)); if (!form_input_validate($_sv_arr["value"], $form_field_name, $fields_graph[$_field_name]["validate_regexp"], $fields_graph[$_field_name]["validate_empty"])) { $error_fields[] = $form_field_name; } } } } return $error_fields; }
function api_graph_total_get($filter_array = "") { require_once(CACTI_BASE_PATH . "/lib/graph/graph_form.php"); $sql_where = ""; /* validation and setup for the WHERE clause */ if ((is_array($filter_array)) && (sizeof($filter_array) > 0)) { /* validate each field against the known master field list */ $_sv_arr = array(); $field_errors = api_graph_fields_validate(sql_filter_array_to_field_array($filter_array), $_sv_arr); /* if a field input error has occured, register the error in the session and return */ if (sizeof($field_errors) > 0) { field_register_error($field_errors); return false; /* otherwise, form an SQL WHERE string using the filter fields */ }else{ $sql_where = sql_filter_array_to_where_string($filter_array, api_graph_form_list(), true); } } return db_fetch_cell("select count(*) from graph $sql_where"); }
function api_graph_template_propagate($graph_template_id) { require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_info.php"); if ((empty($graph_template_id)) || (!is_numeric($graph_template_id))) { return false; } /* get information about this graph template */ $graph_template = api_graph_template_get($graph_template_id); /* must be a valid graph template */ if ($graph_template === false) { return false; } /* retrieve a list of graph fields */ $graph_fields = api_graph_form_list(); $g_fields = array(); /* loop through each graph column name (from the above array) */ foreach ($graph_fields as $field_name => $field_array) { /* are we allowed to push out the column? */ if ((isset($graph_template["t_$field_name"])) && (isset($graph_template[$field_name])) && ($graph_template["t_$field_name"] == "0")) { $g_fields[$field_name] = array("type" => $field_array["data_type"], "value" => $graph_template[$field_name]); } } if (sizeof($g_fields) > 0) { $g_fields["graph_template_id"] = array("type" => DB_TYPE_INTEGER, "value" => $graph_template_id); return db_update("graph", $g_fields, array("graph_template_id")); } return true; }
function form_save() { if ($_POST["action_post"] == "graph_edit") { /* fetch some cache variables */ if (empty($_POST["id"])) { $_graph_template_id = 0; } else { $_graph_template_id = db_fetch_cell("select graph_template_id from graph where id=" . $_POST["id"]); } /* cache all post field values */ init_post_field_cache(); $form_graph_fields = array(); $form_graph_item_fields = array(); /* parse out form values that we care about */ reset($_POST); while (list($name, $value) = each($_POST)) { if (substr($name, 0, 2) == "g|") { $matches = explode("|", $name); $form_graph_fields[$matches[1]] = $value; } else { if (substr($name, 0, 4) == "gip|") { $matches = explode("|", $name); $form_graph_item_fields[$matches[2]] = $value; } } } /* make a list of fields to save */ while (list($_field_name, $_field_value) = each($form_graph_fields)) { /* make sure that we know about this field */ if (isset($fields_graph[$_field_name])) { $save_graph[$_field_name] = $_field_value; } } /* add any unchecked checkbox fields */ $form_graph_fields += field_register_html_checkboxes(api_graph_form_list(), "g||field|"); $form_graph_fields["host_id"] = $_POST["host_id"]; $form_graph_fields["graph_template_id"] = $_POST["graph_template_id"]; /* step #2: field validation */ $suggested_value_fields = array(); /* placeholder */ field_register_error(api_graph_fields_validate($form_graph_fields, $suggested_value_fields, "g||field|", "")); /* step #3: field save */ if (is_error_message()) { log_save("User input validation error for graph [ID#" . $_POST["id"] . "]", SEV_DEBUG); } else { /* save graph data */ if (!api_graph_save($_POST["id"], $form_graph_fields)) { log_save("Save error for graph [ID#" . $_POST["id"] . "]", SEV_ERROR); } /* save graph item data for templated graphs */ if (!empty($_graph_template_id)) { if (sizeof($form_graph_item_fields) > 0) { foreach ($form_graph_item_fields as $graph_template_item_input_id => $value) { if (!api_graph_template_item_input_propagate($graph_template_item_input_id, $value)) { log_save("Save error when propagating graph item input [ID#{$graph_template_item_input_id}] to graph [ID#" . $_POST["id"] . "]", SEV_ERROR); } } } } } if (is_error_message() || $_POST["graph_template_id"] != $_graph_template_id) { header("Location: graphs.php?action=edit&id=" . $_POST["id"] . (!isset($_POST["host_id"]) ? "" : "&host_id=" . $_POST["host_id"]) . (!isset($_POST["graph_template_id"]) ? "" : "&graph_template_id=" . $_POST["graph_template_id"])); } else { header("Location: graphs.php"); } /* submit button on the actions area page */ } else { if ($_POST["action_post"] == "box-1") { $selected_rows = explode(":", $_POST["box-1-action-area-selected-rows"]); if ($_POST["box-1-action-area-type"] == "search") { $get_string = ""; if ($_POST["box-1-search_device"] != "-1") { $get_string .= ($get_string == "" ? "?" : "&") . "search_device=" . urlencode($_POST["box-1-search_device"]); } if (trim($_POST["box-1-search_filter"]) != "") { $get_string .= ($get_string == "" ? "?" : "&") . "search_filter=" . urlencode($_POST["box-1-search_filter"]); } header("Location: graphs.php{$get_string}"); } else { if ($_POST["box-1-action-area-type"] == "remove") { foreach ($selected_rows as $graph_id) { api_graph_remove($graph_id); } } else { if ($_POST["box-1-action-area-type"] == "duplicate") { // not yet coded } else { if ($_POST["box-1-action-area-type"] == "change_graph_template") { // note yet coded } else { if ($_POST["box-1-action-area-type"] == "change_host") { foreach ($selected_rows as $graph_id) { api_graph_host_update($graph_id, $_POST["box-1-change_device"]); } } else { if ($_POST["box-1-action-area-type"] == "convert_graph_template") { // note yet coded } else { if ($_POST["box-1-action-area-type"] == "place_tree") { // note yet coded } } } } } } } header("Location: graphs.php"); /* 'filter' area at the bottom of the box */ } else { if ($_POST["action_post"] == "graph_list") { $get_string = ""; /* the 'clear' button wasn't pressed, so we should filter */ if (!isset($_POST["box-1-action-clear-button"])) { if (trim($_POST["box-1-search_filter"]) != "") { $get_string = ($get_string == "" ? "?" : "&") . "search_filter=" . urlencode($_POST["box-1-search_filter"]); } } header("Location: graphs.php{$get_string}"); } } } }
function package_graph_template_import(&$xml_array, $package_id, $object_hash) { require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_info.php"); require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_update.php"); $save_fields = array(); /* tag the graph template as a package member */ $save_fields["package_id"] = $package_id; /* * XML Tag: <template> */ /* obtain a list of all graph template specific fields */ $graph_template_fields = api_graph_template_form_list(); if (isset($xml_array["template"])) { /* get the base fields from the xml */ foreach (array_keys($graph_template_fields) as $field_name) { if (isset($xml_array["template"][$field_name])) { $save_fields[$field_name] = xml_character_decode($xml_array["template"][$field_name]); } } } /* * XML Tag: <graph> */ /* obtain a list of all graph specific fields */ $graph_fields = api_graph_form_list(); if (isset($xml_array["graph"])) { /* get the base fields from the xml */ foreach (array_keys($graph_fields) as $field_name) { if (isset($xml_array["graph"][$field_name])) { $save_fields[$field_name] = xml_character_decode($xml_array["graph"][$field_name]); } } } /* make sure we got the required information before trying to save */ if ((isset($xml_array["template"])) && (isset($xml_array["graph"]))) { /* save the graph template field to the database and register its new id */ $graph_template_id = package_hash_update($object_hash, api_graph_template_save(0, $save_fields)); } /* make sure the save completed successfully */ if ($graph_template_id === false) { return; } /* * XML Tag: <items> */ /* obtain a list of all graph template item specific fields */ $graph_template_item_fields = api_graph_template_item_form_list(); if ((isset($xml_array["items"])) && (is_array($xml_array["items"]))) { $save_fields = array(); /* get the base fields from the xml */ foreach ($xml_array["items"] as $graph_template_item_hash => $graph_template_item) { $save_fields = array(); /* make sure that each field is associated with the new graph template */ $save_fields["graph_template_id"] = $graph_template_id; /* get the base fields from the xml */ foreach (array_keys($graph_template_item_fields) as $field_name) { if (isset($graph_template_item[$field_name])) { if ($field_name == "data_template_item_id") { $save_fields[$field_name] = package_hash_resolve($graph_template_item[$field_name]); }else{ $save_fields[$field_name] = xml_character_decode($graph_template_item[$field_name]); } } } /* save the data query field to the database and register its new id */ package_hash_update($graph_template_item_hash, api_graph_template_item_save(0, $save_fields)); } } /* * XML Tag: <suggested_values> */ if (isset($xml_array["suggested_values"])) { $save_fields = array(); /* get the base fields from the xml */ foreach ($xml_array["suggested_values"] as $field_array) { if ((isset($field_array["field_name"])) && (isset($field_array["sequence"])) && (isset($field_array["value"]))) { /* build an array containing each data input field */ $save_fields{$field_array["field_name"]}[] = array("id" => "0", "value" => xml_character_decode($field_array["value"])); } } /* save the suggested values to the database */ api_graph_template_suggested_values_save($graph_template_id, $save_fields); } }
function &package_graph_template_export($graph_template_id, $indent = 3) { require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_info.php"); require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); $xml = ""; /* * XML Tag: <template> */ /* obtain a list of all graph template specific fields */ $graph_template_fields = api_graph_template_form_list(); /* obtain a copy of this specfic graph template */ $graph_template = api_graph_template_get($graph_template_id); $_xml = ""; foreach (array_keys($graph_template_fields) as $field_name) { /* create an XML key for each graph template field */ $_xml .= package_xml_tag_get($field_name, xml_character_encode($graph_template[$field_name]), $indent + 2); } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("template", $_xml, $indent + 1, true); /* * XML Tag: <graph> */ /* obtain a list of all graph specific fields */ $graph_fields = api_graph_form_list(); $_xml = ""; foreach (array_keys($graph_fields) as $field_name) { /* check because the 'title' column does not exist */ if (isset($graph_template[$field_name])) { /* create an XML key for each graph field */ $_xml .= package_xml_tag_get($field_name, xml_character_encode($graph_template[$field_name]), $indent + 2); } /* create an XML key for each "template" graph field */ $_xml .= package_xml_tag_get("t_" . $field_name, xml_character_encode($graph_template{"t_" . $field_name}), $indent + 2); } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("graph", $_xml, $indent + 1, true); /* * XML Tag: <items> */ /* obtain a list of all graph template item specific fields */ $graph_template_items_fields = api_graph_template_item_form_list(); /* obtain a list of all graph template items associated with this graph template */ $graph_template_items = api_graph_template_item_list($graph_template_id); $_xml = ""; if (sizeof($graph_template_items) > 0) { foreach ($graph_template_items as $graph_template_item) { $__xml = ""; foreach (array_keys($graph_template_items_fields) as $field_name) { if ($field_name == "data_template_item_id") { /* create an XML key for the 'data_template_item_id' field, making sure to resolve internal ID's */ $__xml .= package_xml_tag_get($field_name, xml_character_encode(package_hash_get($graph_template_item[$field_name], "data_template_item")), $indent + 3); }else{ /* create an XML key for each graph template item field */ $__xml .= package_xml_tag_get($field_name, xml_character_encode($graph_template_item[$field_name]), $indent + 3); } } /* append the result onto a temporary XML string */ $_xml .= package_xml_tag_get(package_hash_get($graph_template_item["id"], "graph_template_item"), $__xml, $indent + 2, true); } } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("items", $_xml, $indent + 1, true); /* * XML Tag: <inputs> */ /* obtain a list of all graph template item input specific fields */ $graph_template_inputs_fields = api_graph_template_item_input_form_list(); /* obtain a list of all graph template item inputs associated with this graph template */ $graph_template_inputs = api_graph_template_item_input_list($graph_template_id); $_xml = ""; if (sizeof($graph_template_inputs) > 0) { foreach ($graph_template_inputs as $graph_template_input) { $__xml = ""; foreach (array_keys($graph_template_inputs_fields) as $field_name) { /* create an XML key for each graph template item input field */ $__xml .= package_xml_tag_get($field_name, xml_character_encode($graph_template_input[$field_name]), $indent + 3); } /* obtain a list of each item associated with this graph template item input */ $graph_template_input_items = api_graph_template_item_input_item_list($graph_template_id); if (sizeof($graph_template_input_items) > 0) { $i = 0; $items_list = ""; foreach ($graph_template_input_items as $graph_template_item_id) { /* create a delimited list of each item, making sure to resolve internal ID's */ $items_list .= package_hash_get($graph_template_item_id, "graph_template_item") . (($i + 1) < sizeof($graph_template_input_items) ? "|" : ""); $i++; } } /* add the items list that we created above */ $__xml .= package_xml_tag_get("items", $items_list, $indent + 3); /* append the result onto a temporary XML string */ $_xml .= package_xml_tag_get(package_hash_get($graph_template_input["id"], "graph_template_input"), $__xml, $indent + 2, true); } } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("inputs", $_xml, $indent + 1, true); /* * XML Tag: <suggested_values> */ /* obtain a list of all suggested values associated with this graph template */ $graph_template_suggested_values = api_graph_template_suggested_values_list($graph_template_id); $_xml = ""; if (sizeof($graph_template_suggested_values) > 0) { $i = 0; foreach ($graph_template_suggested_values as $graph_template_suggested_value) { $__xml = ""; /* create an XML key for each suggested value field */ $__xml .= package_xml_tag_get("field_name", xml_character_encode($graph_template_suggested_value["field_name"]), $indent + 3); $__xml .= package_xml_tag_get("sequence", xml_character_encode($graph_template_suggested_value["sequence"]), $indent + 3); $__xml .= package_xml_tag_get("value", xml_character_encode($graph_template_suggested_value["value"]), $indent + 3); /* break out each row into its own key */ $_xml .= package_xml_tag_get("item_" . str_pad($i, 5, "0", STR_PAD_LEFT), $__xml, $indent + 2, true); $i++; } } /* append the result onto the final XML string */ $xml .= package_xml_tag_get("suggested_values", $_xml, $indent + 1, true); /* wrap the whole XML string into a 'graph_template' tag and return it */ $xml = package_xml_tag_get(package_hash_get($graph_template_id, "graph_template"), $xml, $indent, true); return $xml; }