/**
  * Build the navigation XML and append it to the root element
  * Copied method -- TODO cleanup
  */
 public function buildNavigationXML($root)
 {
     $xpath = new DOMXPath($this->document);
     $template = new XMLDocument();
     $template->preserveWhiteSpace = false;
     $template->load(TEMPLATES . '/navigation.xml');
     $navigation = $this->document->importNode($template->documentElement, true);
     $first_child = $navigation->firstChild;
     // Force visible to be set:
     foreach ($xpath->query('group//item[not(@visible)]', $navigation) as $item) {
         $item->setAttribute('visible', 'yes');
     }
     // Set all items as inactive:
     foreach ($xpath->query('group//item', $navigation) as $item) {
         $item->setAttribute('active', 'no');
     }
     // Add section navigation:
     foreach (new SectionIterator() as $section) {
         // Find the navigation group:
         $group = $xpath->query(sprintf('group[@name = "%s"]', htmlentities($section->{'navigation-group'})), $navigation);
         $group = $group->item(0);
         if (is_null($group)) {
             $group = $this->document->createElement('group');
             $group->setAttribute('name', $section->{'navigation-group'});
             $group->setAttribute('type', 'sections');
             $first_child->parentNode->insertBefore($group, $first_child);
         }
         $item = $this->document->createElement('item');
         $item->setAttribute('link', 'publish/' . $section->handle);
         $item->setAttribute('name', $section->name);
         $item->setAttribute('type', 'section');
         $item->setAttribute('visible', $section->{'hidden-from-publish-menu'} == 'no' ? 'yes' : 'no');
         $item->setAttribute('active', 'no');
         $group->appendChild($item);
         // New link:
         $sub_item = clone $item;
         $sub_item->setAttribute('link', 'publish/' . $section->handle . '/new');
         $sub_item->setAttribute('name', __('New'));
         $item->appendChild($sub_item);
         // Edit link:
         $sub_item = clone $sub_item;
         $sub_item->setAttribute('link', 'publish/' . $section->handle . '/edit');
         $sub_item->setAttribute('name', __('Edit'));
         $sub_item->setAttribute('visible', 'no');
         $item->appendChild($sub_item);
     }
     // Remove empty groups:
     foreach ($xpath->query('group[not(item)]', $navigation) as $group) {
         $group->parentNode->removeChild($group);
     }
     // Assign handles to all groups:
     foreach ($xpath->query('group[not(@handle)]', $navigation) as $group) {
         $group->setAttribute('handle', Lang::createHandle($group->getAttribute('name')));
     }
     // Find active page:
     $active = $xpath->query(sprintf('group//item[@link = "%s"]', htmlentities($this->path)), $navigation);
     $active = $active->item(0);
     if ($active instanceof DOMElement) {
         $active->setAttribute('active', 'yes');
     }
     //$this->document->formatOutput = true;
     //echo '<pre>', htmlentities($this->document->saveXML($navigation)); exit;
     $root->appendChild($navigation);
 }