Backend\Core\Engine\Navigation::buildNavigation PHP Method

buildNavigation() private method

Build navigation tree for a parent id.
private buildNavigation ( integer $parentId, string &$output, integer $depth = 1 )
$parentId integer The id of the parent.
$output string The output, will all output.
$depth integer The current depth.
    private function buildNavigation($parentId, &$output, $depth = 1)
    {
        // prefix every line with some tabs based on the depth
        $prefix = str_repeat("\t", $depth);
        // get navigation for backend
        $navigation = (array) BackendModel::getContainer()->get('database')->getRecords('SELECT bn.*, COUNT(bn2.id) AS num_children
             FROM backend_navigation AS bn
             LEFT OUTER JOIN backend_navigation AS bn2 ON bn2.parent_id = bn.id
             WHERE bn.parent_id = ?
             GROUP BY bn.id
             ORDER BY bn.sequence ASC', array((int) $parentId));
        // output
        foreach ($navigation as $i => $item) {
            // check if this item has children, later used
            $hasChildren = (bool) $item['num_children'];
            $hasSelectedFor = $item['selected_for'] !== null;
            // no url set, fetch the url of the first child
            if ($item['url'] == '') {
                $item['url'] = $this->getNavigationUrl($item['id']);
            }
            // general
            $output .= $prefix . "array(\n";
            $output .= $prefix . "\t'url' => '" . $item['url'] . "',\n";
            $output .= $prefix . "\t'label' => '" . $item['label'] . "'" . ($hasChildren || $hasSelectedFor ? ',' : '') . "\n";
            // selected for
            if ($hasSelectedFor) {
                // unserialize
                $selectedFor = unserialize($item['selected_for']);
                // make sure we have items
                if (!empty($selectedFor)) {
                    // open
                    $output .= $prefix . "\t'selected_for' => array(\n";
                    // add each item
                    foreach ($selectedFor as $ii => $selectedItem) {
                        $output .= $prefix . "\t\t'" . $selectedItem . "'" . ($ii < count($selectedFor) - 1 ? ',' : '') . "\n";
                    }
                    // close
                    $output .= $prefix . "\t)" . ($hasChildren ? ',' : '') . "\n";
                }
            }
            // add children
            if ($hasChildren) {
                // open
                $output .= $prefix . "\t'children' => array(\n";
                // children
                $this->buildNavigation($item['id'], $output, $depth + 2);
                // close
                $output .= $prefix . "\t)\n";
            }
            // close
            $output .= $prefix . ')' . ($i < count($navigation) - 1 ? ',' : '') . "\n";
        }
    }