function form_save() { /* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */ if (isset($_SESSION['dhtml_tree'])) { unset($_SESSION['dhtml_tree']); } if (isset($_POST['save_component_tree'])) { $save['id'] = $_POST['id']; $save['name'] = form_input_validate($_POST['name'], 'name', '', false, 3); $save['sort_type'] = form_input_validate($_POST['sort_type'], 'sort_type', '', true, 3); $save['last_modified'] = date('Y-m-d H:i:s', time()); $save['modified_by'] = $_SESSION['sess_user_id']; if (empty($save['id'])) { $save['user_id'] = $_SESSION['sess_user_id']; } if (!is_error_message()) { $tree_id = sql_save($save, 'graph_tree'); if ($tree_id) { raise_message(1); /* sort the tree using the algorithm chosen by the user */ api_tree_sort_tree(SORT_TYPE_TREE, $tree_id, $_POST['sort_type']); } else { raise_message(2); } } header("Location: tree.php?action=edit&header=false&id={$tree_id}"); } elseif (isset($_POST['save_component_tree_item'])) { $tree_item_id = api_tree_item_save($_POST['id'], $_POST['graph_tree_id'], $_POST['type'], $_POST['parent_item_id'], isset($_POST['title']) ? $_POST['title'] : '', isset($_POST['local_graph_id']) ? $_POST['local_graph_id'] : '0', isset($_POST['rra_id']) ? $_POST['rra_id'] : '0', isset($_POST['host_id']) ? $_POST['host_id'] : '0', isset($_POST['host_grouping_type']) ? $_POST['host_grouping_type'] : '1', isset($_POST['sort_children_type']) ? $_POST['sort_children_type'] : '1', isset($_POST['propagate_changes']) ? true : false); } }
# Zero means create a new one rather than save over an existing one $treeOpts["name"] = $name; if ($sortMethod == "manual" || $sortMethod == "alpha" || $sortMethod == "numeric" || $sortMethod == "natural") { $treeOpts["sort_type"] = $sortMethods[$sortMethod]; } else { echo "ERROR: Invalid sort-method: ({$sortMethod})\n"; display_help(); exit(1); } $existsAlready = db_fetch_cell("select id from graph_tree where name = '{$name}'"); if ($existsAlready) { echo "ERROR: Not adding tree - it already exists - tree-id: ({$existsAlready})\n"; exit(1); } $treeId = sql_save($treeOpts, "graph_tree"); api_tree_sort_tree(SORT_TYPE_TREE, $treeId, $treeOpts["sort_type"]); echo "Tree Created - tree-id: ({$treeId})\n"; exit(0); } elseif ($type == 'node') { # Add a new node to a tree if ($nodeType == "header" || $nodeType == "graph" || $nodeType == "host") { $itemType = $nodeTypes[$nodeType]; } else { echo "ERROR: Invalid node-type: ({$nodeType})\n"; display_help(); exit(1); } if (!is_numeric($parentNode)) { echo "ERROR: parent-node {$parentNode} must be numeric > 0\n"; display_help(); exit(1);
/** api_tree_item_save - saves the tree object and then resorts the tree * @arg $id - the branch id for the object * @arg $tree_id - the tree id for the object * @arg $type - the item type graph, host, leaf * @arg $parent_tree_item_id - The parent leaf for the object * @arg $title - The leaf title in the caseo a leaf * @arg $local_graph_id - The graph id in the case of a graph * @arg $rra_id - The default timespan in the case of a graph * @arg $host_id - The host id in the case of a graph * @arg $host_grouping_type - The sort order for the host under expanded hosts * @arg $sort_children - The sort type in the case of a leaf * @arg $propagate_changes - Wether the changes should be cascaded through all children * @returns - boolean true or false depending on the outcome of the operation */ 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); api_tree_get_lock('tree-lock', 10); $position = db_fetch_cell("SELECT MAX(position)+1 FROM graph_tree_items WHERE parent={$parent_tree_item_id} AND graph_tree_id={$tree_id}"); if ($local_graph_id > 0) { $exists = db_fetch_cell("SELECT id FROM graph_tree_items WHERE local_graph_id={$local_graph_id} AND parent={$parent_tree_item_id} AND graph_tree_id={$tree_id}"); if ($exists) { return false; } } elseif ($host_id > 0) { $exists = db_fetch_cell("SELECT id FROM graph_tree_items WHERE host_id={$host_id} AND parent={$parent_tree_item_id} AND graph_tree_id={$tree_id}"); if ($exists) { return false; } } $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["parent"] = $parent_tree_item_id; $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); $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) { api_tree_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) { api_tree_sort_tree(SORT_TYPE_TREE_ITEM, $tree_item_id, $sort_children_type); } } else { if ($parent_tree_item_id == 0) { api_tree_sort_tree(SORT_TYPE_TREE, $tree_id, $tree_sort_type); } else { api_tree_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) { $tree_items = db_fetch_assoc("SELECT gti.id\n\t\t\t\t\tFROM graph_tree_items AS gti\n\t\t\t\t\tWHERE gti.host_id=0\n\t\t\t\t\tAND gti.local_graph_id=0\n\t\t\t\t\tAND gti.parent={$parent_tree_item_id}\n\t\t\t\t\tAND gti.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) { api_tree_sort_tree(SORT_TYPE_TREE_ITEM, $item["id"], $sort_children_type); } } } } } else { raise_message(2); } } api_tree_release_lock('tree-lock'); return $tree_item_id; }