Frontend\Core\Engine\Navigation::getNavigationHTML PHP Method

getNavigationHTML() public static method

Get navigation HTML
public static getNavigationHTML ( string $type = 'page', integer $parentId, integer $depth = null, array $excludeIds = [], string $template = '/Core/Layout/Templates/Navigation.html.twig', integer $depthCounter = 1 ) : string
$type string The type of navigation the HTML should be build for.
$parentId integer The parentID to start of.
$depth integer The maximum depth to parse.
$excludeIds array PageIDs to be excluded.
$template string The template that will be used.
$depthCounter integer A counter that will hold the current depth.
return string
    public static function getNavigationHTML($type = 'page', $parentId = 0, $depth = null, $excludeIds = array(), $template = '/Core/Layout/Templates/Navigation.html.twig', $depthCounter = 1)
    {
        // get navigation
        $navigation = self::getNavigation();
        // merge the exclude ids with the previously set exclude ids
        $excludeIds = array_merge((array) $excludeIds, self::$excludedPageIds);
        // meta-navigation is requested but meta isn't enabled
        if ($type == 'meta' && (!Model::get('fork.settings')->get('Pages', 'meta_navigation', true) || !isset($navigation['meta']))) {
            return '';
        }
        // validate
        if (!isset($navigation[$type])) {
            throw new Exception('This type (' . $type . ') isn\'t a valid navigation type. Possible values are: page, footer, meta.');
        }
        if (!isset($navigation[$type][$parentId])) {
            throw new Exception('The parent (' . $parentId . ') doesn\'t exists.');
        }
        // special construction to merge home with its immediate children
        $mergedHome = false;
        while (true) {
            // loop elements
            foreach ($navigation[$type][$parentId] as $id => $page) {
                // home is a special item, it should live on the same depth
                if ($page['page_id'] == 1 && !$mergedHome) {
                    // extra checks otherwise exceptions will wbe triggered.
                    if (!isset($navigation[$type][$parentId]) || !is_array($navigation[$type][$parentId])) {
                        $navigation[$type][$parentId] = array();
                    }
                    if (!isset($navigation[$type][$page['page_id']]) || !is_array($navigation[$type][$page['page_id']])) {
                        $navigation[$type][$page['page_id']] = array();
                    }
                    // add children
                    $navigation[$type][$parentId] = array_merge($navigation[$type][$parentId], $navigation[$type][$page['page_id']]);
                    // mark as merged
                    $mergedHome = true;
                    // restart loop
                    continue 2;
                }
                // not hidden and not an action
                if ($page['hidden'] || $page['tree_type'] == 'direct_action') {
                    unset($navigation[$type][$parentId][$id]);
                    continue;
                }
                // authentication
                if (isset($page['data'])) {
                    // unserialize data
                    $page['data'] = unserialize($page['data']);
                    // if auth_required isset and is true
                    if (isset($page['data']['auth_required']) && $page['data']['auth_required']) {
                        // is profile logged? unset
                        if (!FrontendAuthentication::isLoggedIn()) {
                            unset($navigation[$type][$parentId][$id]);
                            continue;
                        }
                        // check if group auth is set
                        if (!empty($page['data']['auth_groups'])) {
                            $inGroup = false;
                            // loop group and set value true if one is found
                            foreach ($page['data']['auth_groups'] as $group) {
                                if (FrontendAuthentication::getProfile()->isInGroup($group)) {
                                    $inGroup = true;
                                }
                            }
                            // unset page if not in any of the groups
                            if (!$inGroup) {
                                unset($navigation[$type][$parentId][$id]);
                            }
                        }
                    }
                }
                // some ids should be excluded
                if (in_array($page['page_id'], (array) $excludeIds)) {
                    unset($navigation[$type][$parentId][$id]);
                    continue;
                }
                // if the item is in the selected page it should get an selected class
                if (in_array($page['page_id'], self::$selectedPageIds)) {
                    $navigation[$type][$parentId][$id]['selected'] = true;
                } else {
                    $navigation[$type][$parentId][$id]['selected'] = false;
                }
                // add nofollow attribute if needed
                if ($page['no_follow']) {
                    $navigation[$type][$parentId][$id]['nofollow'] = true;
                } else {
                    $navigation[$type][$parentId][$id]['nofollow'] = false;
                }
                // meta and footer subpages have the "page" type
                if ($type == 'meta' || $type == 'footer') {
                    $subType = 'page';
                } else {
                    $subType = $type;
                }
                // fetch children if needed
                if (isset($navigation[$subType][$page['page_id']]) && $page['page_id'] != 1 && ($depth == null || $depthCounter + 1 <= $depth)) {
                    $navigation[$type][$parentId][$id]['children'] = self::getNavigationHTML($subType, $page['page_id'], $depth, $excludeIds, $template, $depthCounter + 1);
                } else {
                    $navigation[$type][$parentId][$id]['children'] = false;
                }
                // add parent id
                $navigation[$type][$parentId][$id]['parent_id'] = $parentId;
                // add depth
                $navigation[$type][$parentId][$id]['depth'] = $depthCounter;
                // set link
                $navigation[$type][$parentId][$id]['link'] = static::getURL($page['page_id']);
                // is this an internal redirect?
                if (isset($page['redirect_page_id']) && $page['redirect_page_id'] != '') {
                    $navigation[$type][$parentId][$id]['link'] = static::getURL((int) $page['redirect_page_id']);
                }
                // is this an external redirect?
                if (isset($page['redirect_url']) && $page['redirect_url'] != '') {
                    $navigation[$type][$parentId][$id]['link'] = $page['redirect_url'];
                }
            }
            // break the loop (it is only used for the special construction with home)
            break;
        }
        // return parsed content
        return Model::get('templating')->render($template, array('navigation' => $navigation[$type][$parentId]));
    }

Usage Example

Beispiel #1
0
 /**
  * Get the subnavigation html
  *   syntax: {{ getsubnavigation($type, $parentId, $startdepth, $enddepth, $excludeIds-splitted-by-dash, $template) }}
  *
  *   NOTE: When supplying more than 1 ID to exclude, the single quotes around the dash-separated list are mandatory.
  *
  * @param string $type       The type of navigation, possible values are: page, footer.
  * @param int    $pageId     The parent wherefore the navigation should be build.
  * @param int    $startDepth The depth to start from.
  * @param int    $endDepth   The maximum depth that has to be build.
  * @param string $excludeIds Which pageIds should be excluded (split them by -).
  * @param string $template        The template that will be used.
  *
  * @return string
  */
 public static function getSubNavigation($type = 'page', $pageId = 0, $startDepth = 1, $endDepth = null, $excludeIds = null, $template = '/Core/Layout/Templates/Navigation.html.twig')
 {
     // build excludeIds
     if ($excludeIds !== null) {
         $excludeIds = (array) explode('-', $excludeIds);
     }
     // get info about the given page
     $pageInfo = Navigation::getPageInfo($pageId);
     // validate page info
     if ($pageInfo === false) {
         return '';
     }
     // split URL into chunks
     $chunks = (array) explode('/', $pageInfo['full_url']);
     // remove language chunk
     $hasMultiLanguages = FrontendModel::getContainer()->getParameter('site.multilanguage');
     $chunks = $hasMultiLanguages ? (array) array_slice($chunks, 2) : (array) array_slice($chunks, 1);
     if (count($chunks) == 0) {
         $chunks[0] = '';
     }
     // init var
     $parentURL = '';
     // build url
     for ($i = 0; $i < $startDepth - 1; ++$i) {
         $parentURL .= $chunks[$i] . '/';
     }
     // get parent ID
     $parentID = Navigation::getPageId($parentURL);
     try {
         // get HTML
         $return = (string) Navigation::getNavigationHTML($type, $parentID, $endDepth, $excludeIds, (string) $template);
     } catch (Exception $e) {
         return '';
     }
     // return the var
     if ($return != '') {
         return $return;
     }
     // fallback
     return;
 }