Example #1
0
function api_graph_tree_item_reparent($graph_tree_item_id, $new_parent_id) {
	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_info.php");
	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_utility.php");

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

	/* find out which tree this item belongs to */
	$graph_tree_item = api_graph_tree_item_get($graph_tree_item_id);
	$parent_graph_tree_item = api_graph_tree_item_get($new_parent_id);

	/* make sure the parent id actually changed */
	if (api_graph_tree_item_parent_get($graph_tree_item_id) == $new_parent_id) {
		return true;
	}

	/* get current key so we can do an sql select on it */
	$old_order_key = $graph_tree_item["order_key"];
	$new_order_key = api_graph_tree_item_available_order_key_get($graph_tree_item["graph_tree_id"], $parent_graph_tree_item["order_key"]);

	/* make sure nothing bad happens */
	if ((!api_graph_tree_item_order_key_validate($old_order_key)) || (!api_graph_tree_item_order_key_validate($new_order_key))) {
		return false;
	}

	/* get the item depth of the source and destination branches */
	$old_starting_depth = api_graph_tree_item_depth_get($old_order_key);
	$new_starting_depth = api_graph_tree_item_depth_get($new_order_key);

	/* get the parent order key prefix for the source and destination branches */
	$old_base_prefix = substr($old_order_key, 0, ($old_starting_depth * CHARS_PER_TIER));
	$new_base_prefix = substr($new_order_key, 0, ($new_starting_depth * CHARS_PER_TIER));

	/* prevent possible collisions */
	db_execute("UPDATE graph_tree_items SET order_key = CONCAT('x',order_key) WHERE order_key LIKE '" . sql_sanitize($old_base_prefix) . "%%' AND graph_tree_id = " . $graph_tree_item["graph_tree_id"]);

	/* truncate */
	if ($new_starting_depth >= $old_starting_depth) {
		db_execute("UPDATE graph_tree_items SET order_key = SUBSTRING(REPLACE(order_key, 'x" . sql_sanitize($old_base_prefix) . "', '" . sql_sanitize($new_base_prefix) . "'), 1, " . (MAX_TREE_DEPTH * CHARS_PER_TIER) . ") WHERE order_key LIKE 'x" . sql_sanitize($old_base_prefix) . "%%' AND graph_tree_id = " . $graph_tree_item["graph_tree_id"]);
	/* append */
	}else{
		db_execute("UPDATE graph_tree_items SET order_key = CONCAT(REPLACE(order_key, 'x" . sql_sanitize($old_base_prefix) . "', '" . sql_sanitize($new_base_prefix) . "'), '" . str_repeat('0', (strlen($old_base_prefix) - strlen($new_base_prefix))) . "') WHERE order_key LIKE 'x" . sql_sanitize($old_base_prefix) . "%%' AND graph_tree_id = " . $graph_tree_item["graph_tree_id"]);
	}
}
Example #2
0
function api_tree_item_save($id, $tree_id, $type, $parent_tree_item_id, $title, $local_graph_id, $rra_id,
	$host_id, $host_grouping_type, $sort_children_type, $propagate_changes) {

	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_utility.php");

	$parent_order_key = db_fetch_cell("select order_key from graph_tree_items where id=$parent_tree_item_id");

	/* fetch some cache variables */
	if (empty($id)) {
		/* new/save - generate new order key */
		$order_key = api_graph_tree_item_available_order_key_get($parent_order_key, "graph_tree_items", "order_key", "graph_tree_id=$tree_id");
	}else{
		/* edit/save - use old order_key */
		$order_key = db_fetch_cell("select order_key from graph_tree_items where id=$id");
	}

	/* duplicate graph check */
	$search_key = substr($parent_order_key, 0, (api_graph_tree_item_depth_get($parent_order_key) * CHARS_PER_TIER));
	if (($type == TREE_ITEM_TYPE_GRAPH) && (sizeof(db_fetch_assoc("select id from graph_tree_items where local_graph_id='$local_graph_id' and graph_tree_id='$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 0;
	}

	$save["id"] = $id;
	$save["graph_tree_id"] = $tree_id;
	$save["title"] = form_input_validate($title, "title", "", ($type == TREE_ITEM_TYPE_HEADER ? false : true), 3);
	$save["order_key"] = $order_key;
	$save["local_graph_id"] = form_input_validate($local_graph_id, "local_graph_id", "", true, 3);
	$save["rra_id"]	= form_input_validate($rra_id, "rra_id", "", true, 3);
	$save["host_id"] = form_input_validate($host_id, "host_id", "", true, 3);
	$save["host_grouping_type"] = form_input_validate($host_grouping_type, "host_grouping_type", "", true, 3);
	$save["sort_children_type"] = form_input_validate($sort_children_type, "sort_children_type", "", true, 3);

	$tree_item_id = 0;

	if (!is_error_message()) {
		$tree_item_id = sql_save($save, "graph_tree_items");

		if ($tree_item_id) {
			raise_message(1);

			/* re-parent the branch if the parent item has changed */
			if ($parent_tree_item_id != $tree_item_id) {
				reparent_branch($tree_item_id, $parent_tree_item_id);
			}

			$tree_sort_type = db_fetch_cell("select sort_type from graph_tree where id='$tree_id'");

			/* tree item ordering */
			if ($tree_sort_type == TREE_ORDERING_NONE) {
				/* resort our parent */
				$parent_sorting_type = db_fetch_cell("select sort_children_type from graph_tree_items where id=$parent_tree_item_id");
				if ((!empty($parent_tree_item_id)) && ($parent_sorting_type != TREE_ORDERING_NONE)) {
					sort_tree(SORT_TYPE_TREE_ITEM, $parent_tree_item_id, $parent_sorting_type);
				}

				/* if this is a header, sort direct children */
				if (($type == TREE_ITEM_TYPE_HEADER) && ($sort_children_type != TREE_ORDERING_NONE)) {
					sort_tree(SORT_TYPE_TREE_ITEM, $tree_item_id, $sort_children_type);
				}
			/* tree ordering */
			}else{
				/* potential speed savings for large trees */
				if (api_graph_tree_item_depth_get($save["order_key"]) == 1) {
					sort_tree(SORT_TYPE_TREE, $tree_id, $tree_sort_type);
				}else{
					sort_tree(SORT_TYPE_TREE_ITEM, $parent_tree_item_id, $tree_sort_type);
				}
			}

			/* if the user checked the 'Propagate Changes' box */
			if (($type == TREE_ITEM_TYPE_HEADER) && ($propagate_changes == true)) {
				$search_key = preg_replace("/0+$/", "", $order_key);

				$tree_items = db_fetch_assoc("select
					graph_tree_items.id
					from graph_tree_items
					where graph_tree_items.host_id = 0
					and graph_tree_items.local_graph_id = 0
					and graph_tree_items.title != ''
					and graph_tree_items.order_key like '$search_key%%'
					and graph_tree_items.graph_tree_id='$tree_id'");

				if (sizeof($tree_items) > 0) {
					foreach ($tree_items as $item) {
						db_execute("update graph_tree_items set sort_children_type = '$sort_children_type' where id = '" . $item["id"] . "'");

						if ($sort_children_type != TREE_ORDERING_NONE) {
							sort_tree(SORT_TYPE_TREE_ITEM, $item["id"], $sort_children_type);
						}
					}
				}
			}
		}else{
			raise_message(2);
		}
	}

	return $tree_item_id;
}