/** * Retrieve the current menu hierarchy as a two-dimensional array. Each row in * the array includes a 'depth' column (relative to the root of the tree) to * aid in formatting the tree for display. * * @param QubitMenu $topMenu top ancestor * @param array $options optional parameters * @return array of menu columns, with an additional 'depth' column */ public static function getTree(QubitMenu $topMenu, $options = array()) { $maxDepth = 0; if (isset($options['maxDepth']) && is_int($options['maxDepth'])) { $maxDepth = $options['maxDepth'] > 0 ? $options['maxDepth'] : 0; } // Get all descendents of "top" menu $criteria = new Criteria(); $criteria->add(QubitMenu::LFT, $topMenu->getLft(), Criteria::GREATER_THAN); $criteria->addAnd(QubitMenu::RGT, $topMenu->getRgt(), Criteria::LESS_THAN); $criteria->addAscendingOrderByColumn(QubitMenu::LFT); $menus = QubitMenu::get($criteria); // labouriously calculate depth of current menu from top of hierarchy by // looping through results and tracking "ancestors" $ancestors = array($topMenu->id); foreach ($menus as $menu) { $thisParentId = $menu->getParentId(); if ($ancestors[count($ancestors) - 1] != $thisParentId) { if (!in_array($thisParentId, $ancestors)) { array_push($ancestors, $thisParentId); } else { while ($ancestors[count($ancestors) - 1] != $thisParentId) { array_pop($ancestors); } } } // Limit depth of descendants to $maxDepth $depth = count($ancestors); if ($maxDepth == 0 || $depth <= $maxDepth) { $menuTree[] = array('id' => $menu->id, 'parentId' => $menu->getParentId(), 'name' => $menu->getName(array('cultureFallback' => true)), 'label' => $menu->getLabel(array('cultureFallback' => true)), 'depth' => $depth, 'protected' => $menu->isProtected() ? true : false); } } return $menuTree; }
public static function getmenusRelatedByparentIdById($id, array $options = array()) { $criteria = new Criteria(); self::addmenusRelatedByparentIdCriteriaById($criteria, $id); return QubitMenu::get($criteria, $options); }