function menu_local_tasks($level = 0) {
static $tabs = array(), $parents = array(), $parents_done = array();
if (empty($tabs)) {
$router_item = menu_get_item();
$map = arg(NULL);
do {
// Tabs are router items that have the same parent. If there is a new
// parent, let's add it the queue.
if (!empty($router_item->parent)) {
$parents[] = $router_item->parent;
// Do not add the same item twice.
$router_item->parent = '';
}
$parent = array_shift($parents);
// Do not process the same parent twice.
if (isset($parents_done[$parent])) {
continue;
}
// This loads all the tabs.
$result = db_query("SELECT * FROM {menu} WHERE parent = '%s' AND tab = 1 ORDER BY vancode", $parent);
$tabs_current = array();
unset($next_router_item);
while ($item = db_fetch_object($result)) {
// This call changes the path from for example user/[] to user/123 and
// also determines whether we are allowed to access it.
list($access, , $path) = _menu_access($item, $map, MENU_RENDER_LINK);
if ($access) {
// MENU_DEFAULT_LOCAL_TASK links to its parent.
$link = l($item->title, $item->type == MENU_DEFAULT_LOCAL_TASK ? substr($path, 0, strrpos($path, '/')) : $path);
$depth = $item->depth;
// We check for the active tab.
if ($item->path == $router_item->path || (!$router_item->tab && $item->type == MENU_DEFAULT_LOCAL_TASK) || $path == $_GET['q']) {
$tabs_current[] = array('class' => 'active', 'data' => $link);
if ($item->parent) {
// Let's try to find the router item one level up.
$next_router_item = db_fetch_object(db_query("SELECT path, tab, parent FROM {menu} WHERE path = '%s'", $item->parent));
}
// We will need to inspect one level down.
$parents[] = $item->path;
}
else {
$tabs_current[] = $link;
}
}
}
// If there are tabs, let's add them
if ($tabs_current) {
$tabs[$depth] = $tabs_current;
}
$parents_done[$parent] = TRUE;
if (isset($next_router_item)) {
$router_item = $next_router_item;
}
else {
unset($router_item);
}
} while ($parents);
// Sort by depth
ksort($tabs);
// Remove the depth, we are interested only in their relative placement.
$tabs = array_values($tabs);
}
return isset($tabs[$level]) ? $tabs[$level] : array();
}