MenuModule::toString PHP Method

toString() public method

public toString ( string $HighlightRoute = '' ) : string
$HighlightRoute string
return string
        public function toString($HighlightRoute = '')
        {
            if ($HighlightRoute == '') {
                $HighlightRoute = $this->_HighlightRoute;
            }
            if ($HighlightRoute == '') {
                $HighlightRoute = Gdn_Url::Request();
            }
            $this->fireEvent('BeforeToString');
            $Username = '';
            $UserID = '';
            $Session_TransientKey = '';
            $Session = Gdn::session();
            $Admin = false;
            if ($Session->isValid() === true) {
                $UserID = $Session->User->UserID;
                $Username = $Session->User->Name;
                $Session_TransientKey = $Session->TransientKey();
                $Admin = $Session->User->Admin > 0 ? true : false;
            }
            $Menu = '';
            if (count($this->Items) > 0) {
                // Apply the menu group sort if present...
                if (is_array($this->Sort)) {
                    $Items = array();
                    $Count = count($this->Sort);
                    for ($i = 0; $i < $Count; ++$i) {
                        $Group = $this->Sort[$i];
                        if (array_key_exists($Group, $this->Items)) {
                            $Items[$Group] = $this->Items[$Group];
                            unset($this->Items[$Group]);
                        }
                    }
                    foreach ($this->Items as $Group => $Links) {
                        $Items[$Group] = $Links;
                    }
                } else {
                    $Items = $this->Items;
                }
                foreach ($Items as $GroupName => $Links) {
                    $ItemCount = 0;
                    $LinkCount = 0;
                    $OpenGroup = false;
                    $Group = '';
                    foreach ($Links as $Key => $Link) {
                        $CurrentLink = false;
                        $ShowLink = false;
                        $RequiredPermissions = array_key_exists('Permission', $Link) ? $Link['Permission'] : false;
                        if ($RequiredPermissions !== false && !is_array($RequiredPermissions)) {
                            $RequiredPermissions = explode(',', $RequiredPermissions);
                        }
                        // Show if there are no permissions or the user has ANY of the specified permissions or the user is admin
                        $ShowLink = $Admin || $RequiredPermissions === false || Gdn::session()->checkPermission($RequiredPermissions, false);
                        if ($ShowLink === true) {
                            if ($ItemCount == 1) {
                                $Group .= '<ul>';
                                $OpenGroup = true;
                            } elseif ($ItemCount > 1) {
                                $Group .= "</li>\r\n";
                            }
                            $Url = val('Url', $Link);
                            if (substr($Link['Text'], 0, 1) === '\\') {
                                $Text = substr($Link['Text'], 1);
                            } else {
                                $Text = str_replace('{Username}', $Username, $Link['Text']);
                            }
                            $Attributes = val('Attributes', $Link, array());
                            $AnchorAttributes = val('AnchorAttributes', $Link, array());
                            if ($Url !== false) {
                                $Url = url(str_replace(array('{Username}', '{UserID}', '{Session_TransientKey}'), array(urlencode($Username), $UserID, $Session_TransientKey), $Link['Url']));
                                $CurrentLink = $Url == url($HighlightRoute);
                                $CssClass = val('class', $Attributes, '');
                                if ($CurrentLink) {
                                    $Attributes['class'] = $CssClass . ' Highlight';
                                }
                                $Group .= '<li' . Attribute($Attributes) . '><a' . Attribute($AnchorAttributes) . ' href="' . $Url . '">' . $Text . '</a>';
                                ++$LinkCount;
                            } else {
                                $Group .= '<li' . Attribute($Attributes) . '>' . $Text;
                            }
                            ++$ItemCount;
                        }
                    }
                    if ($OpenGroup === true) {
                        $Group .= "</li>\r\n</ul>\r\n";
                    }
                    if ($Group != '' && $LinkCount > 0) {
                        $Menu .= $Group . "</li>\r\n";
                    }
                }
                if ($Menu != '') {
                    $Menu = '<ul id="' . $this->HtmlId . '"' . ($this->CssClass != '' ? ' class="' . $this->CssClass . '"' : '') . '>' . $Menu . '</ul>';
                }
            }
            return $Menu;
        }