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) { global $config; input_validate_input_number($tree_id); input_validate_input_number($parent_tree_item_id); include_once($config["library_path"] . "/tree.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 = get_next_tree_id($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, (tree_tier($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 rra_id='$rra_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($parent_tree_item_id, $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 (tree_tier($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; }
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) { global $config; input_validate_input_number($tree_id); input_validate_input_number($parent_tree_item_id); include_once $config["library_path"] . "/tree.php"; db_execute("LOCK TABLES graph_tree_items WRITE, graph_tree READ, graph_templates_graph READ, host READ"); $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 = get_next_tree_id($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, tree_tier($parent_order_key) * CHARS_PER_TIER); if ($id == 0 && $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) { $value = db_fetch_cell("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)) . "'"); db_execute("UNLOCK TABLES"); return $value; } /* Duplicate header check */ if ($id == 0 && $type == TREE_ITEM_TYPE_HEADER) { if (sizeof(db_fetch_assoc("SELECT id FROM graph_tree_items WHERE title='{$title}' 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)) . "' AND order_key NOT LIKE '{$search_key}" . str_repeat('0', MAX_TREE_DEPTH * CHARS_PER_TIER - strlen($search_key)) . "%'")) > 0) { $value = db_fetch_cell("select id from graph_tree_items where title='{$title}' 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)) . "'"); db_execute("UNLOCK TABLES"); return $value; } } /* Duplicate host check */ if ($id == 0 && $type == TREE_ITEM_TYPE_HOST && sizeof(db_fetch_assoc("select id from graph_tree_items where host_id='{$host_id}' and 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) { $value = db_fetch_cell("select id from graph_tree_items where host_id='{$host_id}' and 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)) . "'"); db_execute("UNLOCK TABLES"); return $value; } $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($parent_tree_item_id, $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 (tree_tier($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\n\t\t\t\t\tgraph_tree_items.id\n\t\t\t\t\tfrom graph_tree_items\n\t\t\t\t\twhere graph_tree_items.host_id = 0\n\t\t\t\t\tand graph_tree_items.local_graph_id = 0\n\t\t\t\t\tand graph_tree_items.title != ''\n\t\t\t\t\tand graph_tree_items.order_key like '{$search_key}%%'\n\t\t\t\t\tand 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); } } db_execute("UNLOCK TABLES"); return $tree_item_id; }
function reparent_branch($new_parent_id, $tree_item_id) { if (empty($tree_item_id)) { return 0; } /* get the current tree_id */ $graph_tree_id = db_fetch_cell("select graph_tree_id from graph_tree_items where id={$tree_item_id}"); /* make sure the parent id actually changed */ if (get_parent_id($tree_item_id, "graph_tree_items", "graph_tree_id={$graph_tree_id}") == $new_parent_id) { return 0; } /* get current key so we can do a sql select on it */ $old_order_key = db_fetch_cell("select order_key from graph_tree_items where id={$tree_item_id}"); $new_order_key = get_next_tree_id(db_fetch_cell("select order_key from graph_tree_items where id={$new_parent_id}"), "graph_tree_items", "order_key", "graph_tree_id={$graph_tree_id}"); /* yeah, this would be really bad */ if (empty($old_order_key)) { return 0; } $old_starting_tier = tree_tier($old_order_key); $new_starting_tier = tree_tier($new_order_key); $new_base_tier = substr($new_order_key, 0, $new_starting_tier * CHARS_PER_TIER); $old_base_tier = substr($old_order_key, 0, $old_starting_tier * CHARS_PER_TIER); /* prevent possible collisions */ db_execute("update graph_tree_items set order_key = CONCAT('x',order_key) where order_key like '{$old_base_tier}%%' and graph_tree_id={$graph_tree_id}"); /* truncate */ if ($new_starting_tier >= $old_starting_tier) { db_execute("update graph_tree_items set order_key = SUBSTRING(REPLACE(order_key, 'x{$old_base_tier}', '{$new_base_tier}'), 1, " . MAX_TREE_DEPTH * CHARS_PER_TIER . ") where order_key like 'x{$old_base_tier}%%' and graph_tree_id={$graph_tree_id}"); /* append */ } else { db_execute("update graph_tree_items set order_key = CONCAT(REPLACE(order_key, 'x{$old_base_tier}', '{$new_base_tier}'), '" . str_repeat('0', strlen($old_base_tier) - strlen($new_base_tier)) . "') where order_key like 'x{$old_base_tier}%%' and graph_tree_id={$graph_tree_id}"); } }
function reparent_branch($new_parent_id, $tree_item_id) { if (empty($tree_item_id)) { return 0; } /* get the current tree_id */ $graph_tree_id = db_fetch_cell("select graph_tree_id from graph_tree_items where id=$tree_item_id"); /* make sure the parent id actually changed */ if (get_parent_id($tree_item_id, "graph_tree_items", "graph_tree_id=$graph_tree_id") == $new_parent_id) { return 0; } /* get current key so we can do a sql select on it */ $old_order_key = db_fetch_cell("select order_key from graph_tree_items where id=$tree_item_id"); $new_order_key = get_next_tree_id(db_fetch_cell("select order_key from graph_tree_items where id=$new_parent_id"), "graph_tree_items", "order_key", "graph_tree_id=$graph_tree_id"); /* yeah, this would be really bad */ if (empty($old_order_key)) { return 0; } $old_starting_tier = tree_tier($old_order_key); $new_starting_tier = tree_tier($new_order_key); $new_base_tier = substr($new_order_key, 0, ($new_starting_tier * CHARS_PER_TIER)); $old_base_tier = substr($old_order_key, 0, ($old_starting_tier * CHARS_PER_TIER)); /* prevent possible collisions */ db_execute("update graph_tree_items set order_key = ".sql_function_concat("'x'","order_key")." where order_key like '$old_base_tier%%' and graph_tree_id=$graph_tree_id"); /* truncate */ if ($new_starting_tier >= $old_starting_tier) { db_execute("update graph_tree_items set order_key = ".sql_function_substr()."(".sql_function_replace()."(order_key, 'x$old_base_tier', '$new_base_tier'), 1, " . (MAX_TREE_DEPTH * CHARS_PER_TIER) . ") where order_key like 'x$old_base_tier%%' and graph_tree_id=$graph_tree_id"); /* append */ }else{ db_execute("update graph_tree_items set order_key = ".sql_function_concat(sql_function_replace()."(order_key, 'x$old_base_tier', '$new_base_tier')"," '" . str_repeat('0', (strlen($old_base_tier) - strlen($new_base_tier))) . "'")." where order_key like 'x$old_base_tier%%' and graph_tree_id=$graph_tree_id"); } }