Example #1
0
function form_save()
{
    if ($_POST["action_post"] == "data_template_edit") {
        $data_input_fields = array();
        $data_template_item_fields = array();
        $suggested_value_fields = array();
        $rra_item_fields = array();
        /* cache all post field values */
        init_post_field_cache();
        reset($_POST);
        /* step #1: field parsing */
        while (list($name, $value) = each($_POST)) {
            if (substr($name, 0, 4) == "dsi|") {
                $matches = explode("|", $name);
                $data_template_item_fields[$matches[2]][$matches[1]] = $value;
            } else {
                if (substr($name, 0, 4) == "dif_") {
                    $field_name = substr($name, 4);
                    $data_input_fields[$field_name]["value"] = $value;
                    if (isset($_POST["t_dif_{$field_name}"])) {
                        $data_input_fields[$field_name]["t_value"] = html_boolean($_POST["t_dif_{$field_name}"]);
                    } else {
                        $data_input_fields[$field_name]["t_value"] = "0";
                    }
                } else {
                    if (substr($name, 0, 3) == "sv|") {
                        $matches = explode("|", $name);
                        $suggested_value_fields[$matches[1]][] = array("id" => $matches[2], "value" => $value);
                    } else {
                        if (substr($name, 0, 5) == "rrai|") {
                            $matches = explode("|", $name);
                            $rra_item_fields[$matches[2]][$matches[1]] = $value;
                        }
                    }
                }
            }
        }
        /* step #2: field validation */
        $form_data_template["template_name"] = $_POST["template_name"];
        $form_data_source["data_input_type"] = $_POST["data_input_type"];
        $form_data_source["t_name"] = html_boolean(isset($_POST["t_name"]) ? $_POST["t_name"] : "");
        $form_data_source["active"] = html_boolean(isset($_POST["active"]) ? $_POST["active"] : "");
        $form_data_source["t_active"] = html_boolean(isset($_POST["t_active"]) ? $_POST["t_active"] : "");
        $form_data_source["polling_interval"] = isset($_POST["polling_interval"]) ? $_POST["polling_interval"] : "";
        $form_data_source["t_polling_interval"] = html_boolean(isset($_POST["t_polling_interval"]) ? $_POST["t_polling_interval"] : "");
        field_register_error(api_data_source_fields_validate($form_data_source, $suggested_value_fields, "|field|", "sv||field|||id|"));
        field_register_error(api_data_source_input_fields_validate($data_input_fields, "|field|"));
        field_register_error(api_data_template_fields_validate($form_data_template, "|field|"));
        foreach ($data_template_item_fields as $data_template_item_id => $fields) {
            $form_data_source_item[$data_template_item_id]["t_rrd_maximum"] = html_boolean(isset($fields["t_rrd_maximum"]) ? $fields["t_rrd_maximum"] : "");
            $form_data_source_item[$data_template_item_id]["rrd_maximum"] = isset($fields["rrd_maximum"]) ? $fields["rrd_maximum"] : "";
            $form_data_source_item[$data_template_item_id]["t_rrd_minimum"] = html_boolean(isset($fields["t_rrd_minimum"]) ? $fields["t_rrd_minimum"] : "");
            $form_data_source_item[$data_template_item_id]["rrd_minimum"] = isset($fields["rrd_minimum"]) ? $fields["rrd_minimum"] : "";
            $form_data_source_item[$data_template_item_id]["t_rrd_heartbeat"] = html_boolean(isset($fields["t_rrd_heartbeat"]) ? $fields["t_rrd_heartbeat"] : "");
            $form_data_source_item[$data_template_item_id]["rrd_heartbeat"] = isset($fields["rrd_heartbeat"]) ? $fields["rrd_heartbeat"] : "";
            $form_data_source_item[$data_template_item_id]["t_data_source_type"] = html_boolean(isset($fields["t_data_source_type"]) ? $fields["t_data_source_type"] : "");
            $form_data_source_item[$data_template_item_id]["data_source_type"] = $fields["data_source_type"];
            $form_data_source_item[$data_template_item_id]["data_source_name"] = $fields["data_source_name"];
            $form_data_source_item[$data_template_item_id]["field_input_value"] = isset($fields["field_input_value"]) ? $fields["field_input_value"] : "";
            api_data_source_item_fields_validate($form_data_source_item[$data_template_item_id], "dsi||field|||id|");
        }
        /* step #3: field save */
        if (!is_error_message()) {
            $data_template_id = api_data_template_save($_POST["data_template_id"], $form_data_template + $form_data_source);
            if ($data_template_id) {
                /* copy down the selected rra preset into the data template if a preset is selected */
                api_data_template_rra_item_clear($data_template_id);
                api_data_template_preset_rra_item_copy($data_template_id, $_POST["preset_rra_id"]);
                /* save suggested values (for the 'name' field) */
                api_data_template_suggested_values_save($data_template_id, $suggested_value_fields);
                /* save custom data input data */
                api_data_template_input_fields_save($data_template_id, $data_input_fields);
                /* save each data template item on the form */
                foreach (array_keys($data_template_item_fields) as $data_template_item_id) {
                    $form_data_source_item[$data_template_item_id]["data_template_id"] = $data_template_id;
                    $data_template_item_id = api_data_template_item_save($data_template_item_id, $form_data_source_item[$data_template_item_id]);
                    if (!$data_template_item_id) {
                        raise_message(2);
                    }
                }
            } else {
                raise_message(2);
            }
        }
        if (is_error_message()) {
            if (isset($_POST["redirect_item_add"])) {
                $action = "item_add";
            } else {
                $action = "edit";
            }
            header("Location: data_templates.php?action={$action}" . (empty($data_template_id) ? "&id=" . $_POST["data_template_id"] : "&id={$data_template_id}") . (isset($_POST["data_input_type"]) ? "&data_input_type=" . $_POST["data_input_type"] : "") . (isset($_POST["dif_script_id"]) ? "&script_id=" . $_POST["dif_script_id"] : "") . (isset($_POST["dif_data_query_id"]) ? "&data_query_id=" . $_POST["dif_data_query_id"] : ""));
        } else {
            raise_message(1);
            header("Location: data_templates.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"] == "remove") {
                foreach ($selected_rows as $data_template_id) {
                    api_data_template_remove($data_template_id);
                }
            } else {
                if ($_POST["box-1-action-area-type"] == "duplicate") {
                    // yet yet coded
                }
            }
            header("Location: data_templates.php");
            /* 'filter' area at the bottom of the box */
        } else {
            if ($_POST["action_post"] == "data_template_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: data_templates.php{$get_string}");
            }
        }
    }
}
Example #2
0
function package_data_template_import(&$xml_array, $package_id, $object_hash) {
	require_once(CACTI_BASE_PATH . "/lib/data_preset/data_preset_rra_info.php");
	require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php");
	require_once(CACTI_BASE_PATH . "/lib/data_template/data_template_info.php");
	require_once(CACTI_BASE_PATH . "/lib/data_template/data_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 data template specific fields */
	$data_template_fields = api_data_template_form_list();

	if (isset($xml_array["template"])) {
		/* get the base fields from the xml */
		foreach (array_keys($data_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: <data_source>
	 */

	/* obtain a list of all data source specific fields */
	$data_source_fields = api_data_source_form_list();

	if (isset($xml_array["data_source"])) {
		/* get the base fields from the xml */
		foreach (array_keys($data_source_fields) as $field_name) {
			if (isset($xml_array["data_source"][$field_name])) {
				$save_fields[$field_name] = xml_character_decode($xml_array["data_source"][$field_name]);
			}
		}
	}

	/* make sure we got the required information before trying to save */
	if ((isset($xml_array["template"])) && (isset($xml_array["data_source"]))) {
		/* save the data template field to the database and register its new id */
		$data_template_id = package_hash_update($object_hash, api_data_template_save(0, $save_fields));
	}

	/* make sure the save completed successfully */
	if ($data_template_id === false) {
		return;
	}

	/*
	 * XML Tag: <items>
	 */

	/* obtain a list of all data source item specific fields */
	$data_source_item_fields = api_data_source_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 $data_template_item_hash => $data_template_item) {
			$save_fields = array();

			/* make sure that each field is associated with the new data template */
			$save_fields["data_template_id"] = $data_template_id;

			/* get the base fields from the xml */
			foreach (array_keys($data_source_item_fields) as $field_name) {
				if (isset($data_template_item[$field_name])) {
					$save_fields[$field_name] = xml_character_decode($data_template_item[$field_name]);
				}
			}

			/* save the data template item to the database and register its new id */
			package_hash_update($data_template_item_hash, api_data_template_item_save(0, $save_fields));
		}
	}

	/*
	 * XML Tag: <fields>
	 */

	if (isset($xml_array["fields"])) {
		$save_fields = array();

		/* get the base fields from the xml */
		foreach ($xml_array["fields"] as $field_array) {
			if ((isset($field_array["name"])) && (isset($field_array["t_value"])) && (isset($field_array["value"]))) {
				/* build an array containing each data input field */
				if ($field_array["name"] == "data_query_id") {
					$field_value = package_hash_resolve($field_array["value"]);
				}else if ($field_array["name"] == "script_id") {
					$field_value = package_hash_resolve($field_array["value"]);
				}else{
					$field_value = xml_character_decode($field_array["value"]);
				}

				$save_fields{$field_array["name"]} = array("t_value" => $field_array["t_value"], "value" => $field_value);
			}
		}

		/* save the fields to the database */
		api_data_template_input_fields_save($data_template_id, $save_fields);
	}

	/*
	 * XML Tag: <rra_items>
	 */

	/* obtain a list of all rra item preset specific fields */
	$rra_items_fields = api_data_preset_rra_item_form_list();

	if ((isset($xml_array["rra_items"])) && (is_array($xml_array["rra_items"]))) {
		$save_fields = array();

		/* get the base fields from the xml */
		foreach ($xml_array["rra_items"] as $rra_item) {
			$save_fields = array();

			/* make sure that each field is associated with the new data template */
			$save_fields["data_template_id"] = $data_template_id;

			/* get the base fields from the xml */
			foreach (array_keys($rra_items_fields) as $field_name) {
				if (isset($rra_item[$field_name])) {
					$save_fields[$field_name] = xml_character_decode($rra_item[$field_name]);
				}
			}

			/* save the rra item to the database and register its new id */
			api_data_template_rra_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_data_template_suggested_values_save($data_template_id, $save_fields);
	}
}