function __construct($menuPath, $label, $link) { parent::__construct(); $this->menuPath = $menuPath; $this->setLabel($label); $this->setLink($link); }
/** * Generic algorithm for building a menu tree from a list of objects implementing WFMenuTreeBuilding. * * This algorithm is intended to adapt a flat list of objects which contain information on their place within a hierarchy. * For instance, this could be a list of "web page objects" that each know about their proper place in a hierarchical navigation system. * * This algorithm takes an array of objects, each having a menuPath in the form of "a/b/c/d", and returns them in tree form. The tree form is suitable for WFDynarchMenu, WFYAHOO_widget_Menu, or other menu subsytems. * * Items can be both a menu item and have children. Any nodes along the way that aren't explicitly specified will be created for you with a label but no link. * * IMPORTANT: The order of the items passed in is important. They should be sorted such that the nodes appear in order of appearance, if you were walking through them recursively. * * EXAMPLE: * a/b/ * a/b/c * a/b/d * a/c/a * a/c/b * * @param array A one-dimensional array of objects implementing WFMenuTreeBuilding. * @return array An arry of these same objects, but converted to tree form. */ static function menuTreeBuildingToMenuTree($allItems) { // loop through items building the tree $menuTree = array(); $pathStack = array(); $topMenuItem = NULL; $allPaths = array(); // preflight run to make sure that all path parts are represented $newAllItems = array(); foreach ($allItems as $item) { $allPaths[$item->menuPath()] = true; // if the container(s) of the current path don't yet have an item, we need to create fake WFMenuItemBasic items to represent them. $allPathsPartCheck = NULL; foreach (explode('/', $item->menuPath()) as $part) { if ($allPathsPartCheck !== NULL) { $allPathsPartCheck .= '/'; } $allPathsPartCheck .= $part; if (!isset($allPaths[$allPathsPartCheck])) { $fakePathItem = new WFMenuItemBasic(); $fakePathItem->setLabel($part); $fakePathItem->setMenuPath($allPathsPartCheck); // add the fake item $newAllItems[] = $fakePathItem; $allPaths[$allPathsPartCheck] = true; } } $newAllItems[] = $item; } foreach ($newAllItems as $item) { $proxyMenuItem = new WFMenuItemProxy($item); // is this menu item a sub-item of the previous one? if so, add it to the current place in the tree if ($topMenuItem and strstr($item->menuPath(), $topMenuItem->menuPath() . '/')) { $topMenuItem->addChild($proxyMenuItem); } else { // pop the stack of menu items until the curernt item fits under that place, or we reach the root while (($topMenuItem = array_pop($pathStack)) !== NULL) { if (strstr($item->menuPath(), $topMenuItem->menuPath() . '/')) { array_push($pathStack, $topMenuItem); break; } } // should the item be added to the ROOT or to the current menu item? if ($topMenuItem === NULL) { $menuTree[] = $proxyMenuItem; } else { $topMenuItem->addChild($proxyMenuItem); } } array_push($pathStack, $item); $topMenuItem = $item; } return $menuTree; }