/** * Merges a page (and its subpages) into this navigation. * If the page already exists in the navigation, then it attempts to add * any new subpages of the page to it. If a subpages already exists in the navigation, then it * it recursively attempts to add its new subpages to it, and so on. * * @param Zend_Navigation_Page $page page to be merged * @return Zend_Navigation_Container $parentContainer the suggested parentContainer for the page. * The parentContainer must already be in the navigation and remain so throughout the merge. * @throws Zend_Navigation_Exception if a subpage is invalid * @throws RuntimeException if the page or parentContainer is invalid */ public function mergePage(Zend_Navigation_Page $page, Zend_Navigation_Container $parentContainer = null) { if (!$page->uid) { // we assume that every page has already been normalized throw new RuntimeException(__('The page must be normalized and have a valid uid.')); } if ($parentContainer === null) { $parentContainer = $this; } // save the child pages and remove them from the current page $childPages = $page->getPages(); $page->removePages($childPages); if (!($oldPage = $this->getPageByUid($page->uid))) { if ($parentContainer !== $this && !$this->hasPage($parentContainer, true)) { // we assume parentContainer is either the navigation object // or a descendant page of the navigation object throw RuntimeException(__('The parent container must either be the navigation object' . ' or a descendant subpage of the navigation object.')); } // add the page to the end of the parent container $pageOrder = $this->_getLastPageOrderInContainer($parentContainer) + 1; $page->setOrder($pageOrder); $this->addPageToContainer($page, $parentContainer); // set the new parent page $parentPage = $page; } else { // set the new parent page $parentPage = $oldPage; } // merge the child pages foreach ($childPages as $childPage) { $this->mergePage($childPage, $parentPage); } }