function generate_tree($id, $mode = '') { # selected item # get childs $child_array = get_childs($id, $mode); # get information of selected item $query = 'SELECT DISTINCT attr_value as name,ItemLinks.fk_id_item AS item_id, (SELECT config_class FROM ConfigItems,ConfigClasses WHERE id_class=fk_id_class AND id_item=item_id) AS type, (SELECT attr_value FROM ConfigValues, ItemLinks, ConfigAttrs, ConfigClasses WHERE ConfigValues.fk_id_item = ItemLinks.fk_item_linked2 AND id_attr = ConfigValues.fk_id_attr AND attr_name = "icon_image" AND id_class = fk_id_class AND config_class = "os" AND ItemLinks.fk_id_item = item_id ) AS os_icon FROM ConfigValues,ItemLinks,ConfigAttrs,ConfigClasses WHERE ItemLinks.fk_id_item=ConfigValues.fk_id_item AND id_attr=ItemLinks.fk_id_attr AND ConfigAttrs.visible="yes" AND fk_id_class=id_class AND (SELECT naming_attr FROM ConfigAttrs WHERE id_attr=ConfigValues.fk_id_attr)="yes" AND ItemLinks.fk_id_item="' . $id . '" ORDER BY config_class DESC,attr_value'; $selected_item_info = db_handler($query, "assoc", "Get informations on selected item"); # get informations about host # prepend inforomation from selected host to the top of the list array_unshift($child_array, get_informations($id)); # set values of selected item AND put all childs (generated bevore) in it. $root_item = array($id => array("id" => $id, "selected" => TRUE, "status" => 'open', "name" => $selected_item_info["name"], "type" => $selected_item_info["type"], "os_icon" => $selected_item_info["os_icon"], "childs" => $child_array)); # get parents $parents_flat = get_parents($id); # make the parents array ordered top2down $parents_flat = array_reverse($parents_flat); # prepare list (if there are parents call the prepare function if (!empty($parents_flat)) { $tree = prepare_dependency($parents_flat, $root_item); } else { $tree = $root_item; } ## Display the top tree item # check for parent loop error if (isset($tree[0]["status"]) and $tree[0]["status"] == "loop_error") { # make a error item at the top $dependency_tree = array("root" => array("id" => "root", "status" => 'open', "name" => TXT_DEPVIEW_ERROR_LOOP, "type" => "warn", "childs" => $tree)); } else { # make a root tree at the top $dependency_tree = array("root" => array("id" => "root", "status" => 'open', "name" => "Top level", "type" => "parent", "childs" => $tree)); } //echo "<pre>"; //var_dump($tree); //echo "</pre>"; echo '<div>'; displayTree_list($dependency_tree); echo '</div>'; }
function prepare_dependency($source, &$root_item, $level = 0) { # root_item has the selected items information and its childs # this runs level array $p_array = $source[$level]; # next level $next_level = $level + 1; if (!empty($source[$next_level]) and is_array($source[$next_level])) { # if there is a next level, so go to it $result = prepare_dependency($source, $root_item, $next_level); # make a subgroup and pack the returning infos into it $p_array["group"]["id"] = $level; $p_array["group"]["name"] = "Parent level"; $p_array["group"]["status"] = 'open'; $p_array["group"]["type"] = 'parent'; $p_array["group"]["childs"] = $result; } elseif (!empty($p_array[0]["child"]) and !empty($root_item[$p_array[0]["child"]])) { # reached last level, the child id exists also in root_item (with its informations) # When there are more than one parents, put the child in a subgroup if (count($p_array) > 1) { $p_array["group"]["id"] = $level; $p_array["group"]["name"] = "Parent level"; $p_array["group"]["status"] = 'open'; $p_array["group"]["type"] = 'parent'; $p_array["group"]["childs"][] = $root_item[$p_array[0]["child"]]; } elseif (count($p_array) == 1) { # there is only one parent, so give the child directly to the parent $p_array[0]["childs"][] = $root_item[$p_array[0]["child"]]; } } return $p_array; }