Esempio n. 1
0
function api_graph_tree_item_parent_get($graph_tree_item_id) {
	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_info.php");

	/* sanity checks */
	validate_id_die($graph_tree_item_id, "graph_tree_item_id");

	/* get a copy of the current graph tree item */
	$graph_tree_item = api_graph_tree_item_get($graph_tree_item_id);

	return api_graph_tree_item_parent_get_bykey($graph_tree_item["order_key"], $graph_tree_item["graph_tree_id"]);
}
Esempio n. 2
0
function api_graph_tree_item_save($graph_tree_item_id, &$_fields_graph_tree_item) {
	require_once(CACTI_BASE_PATH . "/include/graph_tree/graph_tree_constants.php");
	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_utility.php");
	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_info.php");

	/* sanity checks */
	validate_id_die($graph_tree_item_id, "graph_tree_item_id", true);

	/* sanity check for $graph_tree_id */
	if ((empty($graph_tree_item_id)) && (empty($_fields_graph_tree_item["graph_tree_id"]))) {
		log_save("Required graph_tree_id when graph_tree_item_id = 0", SEV_ERROR);
		return false;
	}else if ((isset($_fields_graph_tree_item["graph_tree_id"])) && (!db_integer_validate($_fields_graph_tree_item["graph_tree_id"]))) {
		return false;
	}

	/* sanity check for $item_type */
	if ((!isset($_fields_graph_tree_item["item_type"])) || (!db_integer_validate($_fields_graph_tree_item["item_type"]))) {
		log_save("Missing required item_type", SEV_ERROR);
		return false;
	}

	/* sanity check for $item_value */
	if ((empty($graph_tree_item_id)) && (empty($_fields_graph_tree_item["item_value"]))) {
		log_save("Required item_value when graph_tree_item_id = 0", SEV_ERROR);
		return false;
	}else if ((isset($_fields_graph_tree_item["item_value"])) && ( (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_GRAPH) || ($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HOST)) && (!db_integer_validate($_fields_graph_tree_item["item_value"])) )) {
		return false;
	}

	/* sanity check for $parent_item_id */
	if ((!isset($_fields_graph_tree_item["parent_item_id"])) || (!db_integer_validate($_fields_graph_tree_item["parent_item_id"], true))) {
		log_save("Missing required parent_item_id", SEV_ERROR);
		return false;
	}

	/* field: id */
	$_fields["id"] = array("type" => DB_TYPE_INTEGER, "value" => $graph_tree_item_id);

	/* field: graph_tree_id */
	if (isset($_fields_graph_tree_item["graph_tree_id"])) {
		$_fields["graph_tree_id"] = array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph_tree_item["graph_tree_id"]);
	}

	/* get a copy of the parent tree item id */
	if ($_fields_graph_tree_item["parent_item_id"] == "0") {
		$parent_order_key = "";
		$parent_sort_type = TREE_ORDERING_NONE;
	}else{
		$parent_graph_tree_item = api_graph_tree_item_get($_fields_graph_tree_item["parent_item_id"]);
		$parent_order_key = $parent_graph_tree_item["order_key"];
		$parent_sort_type = $parent_graph_tree_item["sort_children_type"];
	}

	/* generate a new order key if this is a new graph tree item */
	if (empty($graph_tree_item_id)) {
		$_fields["order_key"] = array("type" => DB_TYPE_STRING, "value" => api_graph_tree_item_available_order_key_get($_fields_graph_tree_item["graph_tree_id"], $parent_order_key));
	}else{
		$graph_tree_item = api_graph_tree_item_get($graph_tree_item_id);
		$_fields["order_key"] = array("type" => DB_TYPE_STRING, "value" => $graph_tree_item["order_key"]);
	}

	/* if this item is a graph, make sure it is not being added to the same branch twice */
	$search_key = substr($parent_order_key, 0, (api_graph_tree_item_depth_get($parent_order_key) * CHARS_PER_TIER));
	if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_GRAPH) && (sizeof(db_fetch_assoc("select id from graph_tree_items where item_value = " . $_fields_graph_tree_item["item_value"] . " and item_type = " . TREE_ITEM_TYPE_GRAPH . " and graph_tree_id = " . $_fields_graph_tree_item["graph_tree_id"] . " and order_key like '$search_key" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', (MAX_TREE_DEPTH * CHARS_PER_TIER) - (strlen($search_key) + CHARS_PER_TIER)) . "'")) > 0)) {
		return true;
	}

	/* convert the input array into something that is compatible with db_replace() */
	$_fields += sql_get_database_field_array($_fields_graph_tree_item, api_graph_tree_item_form_list());

	/* check for an empty field list */
	if (sizeof($_fields) == 1) {
		return true;
	}

	if (db_replace("graph_tree_items", $_fields, array("id"))) {
		if (empty($graph_tree_item_id)) {
			$graph_tree_item_id = db_fetch_insert_id();
		}

		/* re-parent the branch if the parent item has changed */
		if ($_fields_graph_tree_item["parent_item_id"] != api_graph_tree_item_parent_get_bykey($_fields["order_key"]["value"], $_fields_graph_tree_item["graph_tree_id"])) {
			api_graph_tree_item_reparent($graph_tree_item_id, $_fields_graph_tree_item["parent_item_id"]);
		}

		$parent_tree = api_graph_tree_get($_fields_graph_tree_item["graph_tree_id"]);

		/* tree item ordering */
		if ($parent_tree["sort_type"] == TREE_ORDERING_NONE) {
			/* resort our parent */
			if ($parent_sort_type != TREE_ORDERING_NONE) {
				echo $parent_sort_type;
				api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $_fields_graph_tree_item["parent_item_id"], $parent_sort_type);
			}

			/* if this is a header, sort direct children */
			if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HEADER) && ($parent_sort_type != TREE_ORDERING_NONE)) {
				api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $graph_tree_item_id, $parent_sort_type);
			}
		/* tree ordering */
		}else{
			/* potential speed savings for large trees */
			if (api_graph_tree_item_depth_get($_fields["order_key"]["value"]) == 1) {
				api_graph_tree_item_sort(SORT_TYPE_TREE, $_fields_graph_tree_item["graph_tree_id"], $parent_tree["sort_type"]);
			}else{
				api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $_fields_graph_tree_item["parent_item_id"], $parent_tree["sort_type"]);
			}
		}

		/* if the user checked the 'Propagate Changes' box */
		if (($_fields_graph_tree_item["item_type"] == TREE_ITEM_TYPE_HEADER) && (isset($_fields_graph_tree_item["sort_children_type"])) && (!empty($_fields_graph_tree_item["propagate_changes"]))) {
			$graph_tree_items = api_graph_tree_item_list($_fields_graph_tree_item["graph_tree_id"], array("item_type" => TREE_ITEM_TYPE_HEADER), $graph_tree_item_id, false, false);

			if (is_array($graph_tree_items) > 0) {
				foreach ($graph_tree_items as $graph_tree_item) {
					db_update("graph_tree_items",
						array(
							"id" => array("type" => DB_TYPE_INTEGER, "value" => $graph_tree_item["id"]),
							"sort_children_type" => array("type" => DB_TYPE_INTEGER, "value" => $_fields_graph_tree_item["sort_children_type"])
							),
						array("id"));

					if ($_fields_graph_tree_item["sort_children_type"] != TREE_ORDERING_NONE) {
						api_graph_tree_item_sort(SORT_TYPE_TREE_ITEM, $graph_tree_item["id"], $_fields_graph_tree_item["sort_children_type"]);
					}
				}
			}
		}

		return $graph_tree_item_id;
	}else{
		return false;
	}
}