Example #1
0
        private void Render()
        {
            string content = null;

            try
            {
                PageCache currentPage = PageCache.Get(RockPage.PageId);
                PageCache rootPage    = null;

                var pageRouteValuePair = GetAttributeValue(AttributeKey.RootPage).SplitDelimitedValues(false).AsGuidOrNullList();
                if (pageRouteValuePair.Any() && pageRouteValuePair[0].HasValue && !pageRouteValuePair[0].Value.IsEmpty())
                {
                    rootPage = PageCache.Get(pageRouteValuePair[0].Value);
                }

                // If a root page was not found, use current page
                if (rootPage == null)
                {
                    rootPage = currentPage;
                }

                int levelsDeep = Convert.ToInt32(GetAttributeValue(AttributeKey.NumberofLevels));

                Dictionary <string, string> pageParameters = null;
                if (GetAttributeValue(AttributeKey.IncludeCurrentParameters).AsBoolean())
                {
                    pageParameters = CurrentPageReference.Parameters;
                }

                NameValueCollection queryString = null;
                if (GetAttributeValue(AttributeKey.IncludeCurrentQueryString).AsBoolean())
                {
                    queryString = CurrentPageReference.QueryString;
                }

                // Get list of pages in current page's hierarchy
                var pageHeirarchy = new List <int>();
                if (currentPage != null)
                {
                    pageHeirarchy = currentPage.GetPageHierarchy().Select(p => p.Id).ToList();
                }

                // Get default merge fields.
                var pageProperties = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);
                pageProperties.Add("Site", GetSiteProperties(RockPage.Site));
                pageProperties.Add("IncludePageList", GetIncludePageList());
                pageProperties.Add("CurrentPage", this.PageCache);

                using (var rockContext = new RockContext())
                {
                    pageProperties.Add("Page", rootPage.GetMenuProperties(levelsDeep, CurrentPerson, rockContext, pageHeirarchy, pageParameters, queryString));
                }

                if (LavaService.RockLiquidIsEnabled)
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    var lavaTemplate = GetTemplate();
#pragma warning restore CS0618 // Type or member is obsolete

                    // Apply Enabled Lava Commands
                    var enabledCommands = GetAttributeValue(AttributeKey.EnabledLavaCommands);
                    lavaTemplate.Registers.AddOrReplace("EnabledCommands", enabledCommands);

                    content = lavaTemplate.Render(Hash.FromDictionary(pageProperties));

                    // Check for Lava rendering errors.
                    if (lavaTemplate.Errors.Any())
                    {
                        throw lavaTemplate.Errors.First();
                    }
                }
                else
                {
                    var templateText = GetAttributeValue(AttributeKey.Template);

                    // Apply Enabled Lava Commands
                    var lavaContext = LavaService.NewRenderContext(pageProperties);

                    var enabledCommands = GetAttributeValue(AttributeKey.EnabledLavaCommands);

                    lavaContext.SetEnabledCommands(enabledCommands.SplitDelimitedValues());

                    var result = LavaService.RenderTemplate(templateText,
                                                            new LavaRenderParameters {
                        Context = lavaContext, CacheKey = CacheKey()
                    });

                    content = result.Text;

                    if (result.HasErrors)
                    {
                        throw result.GetLavaException("PageMenu Block Lava Error");
                    }
                }

                phContent.Controls.Clear();
                phContent.Controls.Add(new LiteralControl(content));
            }
            catch (Exception ex)
            {
                LogException(ex);

                // Create a block showing the error and the attempted content render.
                // Show the error first to ensure that it is visible, because the rendered content may disrupt subsequent output if it is malformed.
                StringBuilder errorMessage = new StringBuilder();
                errorMessage.Append("<div class='alert alert-warning'>");
                errorMessage.Append("<h4>Warning</h4>");
                errorMessage.Append("An error has occurred while generating the page menu. Error details:<br/>");
                errorMessage.Append(ex.Message);

                if (!string.IsNullOrWhiteSpace(content))
                {
                    errorMessage.Append("<h4>Rendered Content</h4>");
                    errorMessage.Append(content);
                    errorMessage.Append("</div>");
                }

                phContent.Controls.Add(new LiteralControl(errorMessage.ToString()));
            }
        }
Example #2
0
        private void Render()
        {
            try
            {
                PageCache currentPage = PageCache.Read(RockPage.PageId);
                PageCache rootPage    = null;

                Guid pageGuid = Guid.Empty;
                if (Guid.TryParse(GetAttributeValue(ROOT_PAGE), out pageGuid))
                {
                    rootPage = PageCache.Read(pageGuid);
                }

                // If a root page was not found, use current page
                if (rootPage == null)
                {
                    rootPage = currentPage;
                }

                int levelsDeep = Convert.ToInt32(GetAttributeValue(NUM_LEVELS));

                Dictionary <string, string> pageParameters = null;
                if (GetAttributeValue("IncludeCurrentParameters").AsBoolean())
                {
                    pageParameters = CurrentPageReference.Parameters;
                }

                NameValueCollection queryString = null;
                if (GetAttributeValue("IncludeCurrentQueryString").AsBoolean())
                {
                    queryString = CurrentPageReference.QueryString;
                }

                // Get list of pages in current page's heirarchy
                var pageHeirarchy = new List <int>();
                if (currentPage != null)
                {
                    pageHeirarchy = currentPage.GetPageHierarchy().Select(p => p.Id).ToList();
                }

                // add context to merge fields
                var contextEntityTypes = RockPage.GetContextEntityTypes();
                var contextObjects     = new Dictionary <string, object>();
                foreach (var conextEntityType in contextEntityTypes)
                {
                    var contextObject = RockPage.GetCurrentContext(conextEntityType);
                    contextObjects.Add(conextEntityType.FriendlyName, contextObject);
                }

                var pageProperties = new Dictionary <string, object>();
                pageProperties.Add("CurrentPerson", CurrentPerson);
                pageProperties.Add("Context", contextObjects);
                pageProperties.Add("Site", GetSiteProperties(RockPage.Site));
                pageProperties.Add("IncludePageList", GetIncludePageList());

                using (var rockContext = new RockContext())
                {
                    pageProperties.Add("Page", rootPage.GetMenuProperties(levelsDeep, CurrentPerson, rockContext, pageHeirarchy, pageParameters, queryString));
                }
                string content = GetTemplate().Render(Hash.FromDictionary(pageProperties));

                // check for errors
                if (content.Contains("error"))
                {
                    content = "<div class='alert alert-warning'><h4>Warning</h4>" + content + "</div>";
                }

                phContent.Controls.Clear();
                phContent.Controls.Add(new LiteralControl(content));

                // add debug info
                if (GetAttributeValue("EnableDebug").AsBoolean() && IsUserAuthorized(Authorization.EDIT))
                {
                    StringBuilder tipInfo = new StringBuilder();
                    tipInfo.Append("<p /><div class='alert alert-success' style='clear: both;'><h4>Page Menu Tips</h4>");

                    tipInfo.Append("<p><em>Note:</em> If a page or group of pages is not in the data above check the following: <ul>");
                    tipInfo.Append("<li>The parent page has 'Show Child Pages' enabled in the 'Page Properties' > 'Display Settings'</li>");
                    tipInfo.Append("<li>Check the 'Display Settings' on the child pages</li>");
                    tipInfo.Append("<li>Check the security of the child pages</li>");
                    tipInfo.Append("</ul><br /></p>");
                    tipInfo.Append("</div>");

                    phContent.Controls.Add(new LiteralControl(tipInfo.ToString() + pageProperties.lavaDebugInfo()));
                }
            }
            catch (Exception ex)
            {
                StringBuilder errorMessage = new StringBuilder();
                errorMessage.Append("<div class='alert alert-warning'>");
                errorMessage.Append("An error has occurred while generating the page menu. Error details:");
                errorMessage.Append(ex.Message);
                errorMessage.Append("</div>");

                phContent.Controls.Add(new LiteralControl(errorMessage.ToString()));
            }
        }
Example #3
0
        public override async Task RenderAsync(TextWriter writer)
        {
            try
            {
                PageCache currentPage = PageCache.Get(RockPage.PageId);
                PageCache rootPage    = null;

                var pageRouteValuePair = GetAttributeValue(ROOT_PAGE).SplitDelimitedValues(false).AsGuidOrNullList();
                if (pageRouteValuePair.Any() && pageRouteValuePair[0].HasValue && !pageRouteValuePair[0].Value.IsEmpty())
                {
                    rootPage = PageCache.Get(pageRouteValuePair[0].Value);
                }

                // If a root page was not found, use current page
                if (rootPage == null)
                {
                    rootPage = currentPage;
                }

                int levelsDeep = Convert.ToInt32(GetAttributeValue(NUM_LEVELS));

                Dictionary <string, string> pageParameters = null;
                //if ( GetAttributeValue( "IncludeCurrentParameters" ).AsBoolean() )
                //{
                //    pageParameters = CurrentPageReference.Parameters;
                //}

                NameValueCollection queryString = null;
                //if ( GetAttributeValue( "IncludeCurrentQueryString" ).AsBoolean() )
                //{
                //    queryString = CurrentPageReference.QueryString;
                //}

                // Get list of pages in current page's hierarchy
                var pageHeirarchy = new List <int>();
                if (currentPage != null)
                {
                    pageHeirarchy = currentPage.GetPageHierarchy().Select(p => p.Id).ToList();
                }

                // Add context to merge fields
                //var contextEntityTypes = RockPage.GetContextEntityTypes();
                //var contextObjects = new Dictionary<string, object>();
                //foreach ( var conextEntityType in contextEntityTypes )
                //{
                //    var contextObject = RockPage.GetCurrentContext( conextEntityType );
                //    contextObjects.Add( conextEntityType.FriendlyName, contextObject );
                //}

                var pageProperties = new Dictionary <string, object>();
                pageProperties.Add("CurrentPerson", CurrentPerson);
                //pageProperties.Add( "Context", contextObjects );
                pageProperties.Add("Site", GetSiteProperties(RockPage.Site));
                pageProperties.Add("IncludePageList", GetIncludePageList());
                pageProperties.Add("CurrentPage", this.PageCache);

                using (var rockContext = new RockContext())
                {
                    pageProperties.Add("Page", rootPage.GetMenuProperties(levelsDeep, CurrentPerson, rockContext, pageHeirarchy, pageParameters, queryString));
                }

                var lavaTemplate = GetTemplate();

                // Apply Enabled Lava Commands
                var enabledCommands = GetAttributeValue("EnabledLavaCommands");
                lavaTemplate.Registers.AddOrReplace("EnabledCommands", enabledCommands);

                string content = lavaTemplate.Render(Hash.FromDictionary(pageProperties));

                // check for errors
                if (content.Contains("error"))
                {
                    content = "<div class='alert alert-warning'><h4>Warning</h4>" + content + "</div>";
                }

                await writer.WriteAsync(content);
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                StringBuilder errorMessage = new StringBuilder();
                errorMessage.Append("<div class='alert alert-warning'>");
                errorMessage.Append("An error has occurred while generating the page menu. Error details:");
                errorMessage.Append(ex.Message);
                errorMessage.Append("</div>");

                await writer.WriteAsync(errorMessage.ToString());
            }
        }
Example #4
0
        private void Render()
        {
            PageCache currentPage = PageCache.Read(RockPage.PageId);
            PageCache rootPage    = null;

            Guid pageGuid = Guid.Empty;

            if (Guid.TryParse(GetAttributeValue(ROOT_PAGE), out pageGuid))
            {
                rootPage = PageCache.Read(pageGuid);
            }

            // If a root page was not found, use current page
            if (rootPage == null)
            {
                rootPage = currentPage;
            }

            int levelsDeep = Convert.ToInt32(GetAttributeValue(NUM_LEVELS));

            Dictionary <string, string> pageParameters = null;

            if (GetAttributeValue("IncludeCurrentParameters").AsBoolean())
            {
                pageParameters = CurrentPageReference.Parameters;
            }

            NameValueCollection queryString = null;

            if (GetAttributeValue("IncludeCurrentQueryString").AsBoolean())
            {
                queryString = CurrentPageReference.QueryString;
            }

            // Get list of pages in curren't page's heirarchy
            var pageHeirarchy = new List <int>();

            if (currentPage != null)
            {
                pageHeirarchy = currentPage.GetPageHierarchy().Select(p => p.Id).ToList();
            }

            var pageProperties = new Dictionary <string, object>();

            pageProperties.Add("page", rootPage.GetMenuProperties(levelsDeep, CurrentPerson, pageHeirarchy, pageParameters, queryString));
            string content = GetTemplate().Render(Hash.FromDictionary(pageProperties));

            // check for errors
            if (content.Contains("No such template"))
            {
                // get template name
                Match match = Regex.Match(GetAttributeValue("Template"), @"'([^']*)");
                if (match.Success)
                {
                    content = String.Format("<div class='alert alert-warning'><h4>Warning</h4>Could not find the template _{1}.liquid in {0}.</div>", ResolveRockUrl("~~/Assets/Liquid"), match.Groups[1].Value);
                }
                else
                {
                    content = "<div class='alert alert-warning'><h4>Warning</h4>Unable to parse the template name from settings.</div>";
                }
            }

            if (content.Contains("error"))
            {
                content = "<div class='alert alert-warning'><h4>Warning</h4>" + content + "</div>";
            }

            phContent.Controls.Clear();
            phContent.Controls.Add(new LiteralControl(content));

            // add debug info
            if (GetAttributeValue("EnableDebug").AsBoolean())
            {
                StringBuilder debugInfo = new StringBuilder();
                debugInfo.Append("<p /><div class='alert alert-info'><h4>Debug Info</h4>");

                debugInfo.Append("<p><em>Note:</em> If a page or group of pages is not in the data above check the following: <ul>");
                debugInfo.Append("<li>The parent page has 'Show Child Pages' enabled in the 'Page Properties' > 'Display Settings'</li>");
                debugInfo.Append("<li>Check the 'Display Settings' on the child pages</li>");
                debugInfo.Append("<li>Check the security of the child pages</li>");
                debugInfo.Append("</ul><br /></p>");

                debugInfo.Append("<pre>");
                debugInfo.Append("<p /><strong>Page Data</strong> (referenced as 'page.' in Liquid)<br>");
                debugInfo.Append(rootPage.GetMenuProperties(levelsDeep, CurrentPerson, pageHeirarchy, pageParameters, queryString).LiquidHelpText() + "</pre>");

                debugInfo.Append("</div>");
                phContent.Controls.Add(new LiteralControl(debugInfo.ToString()));
            }
        }
Example #5
0
        private void Render()
        {
            try
            {
                PageCache currentPage = PageCache.Read(RockPage.PageId);
                PageCache rootPage    = null;

                Guid pageGuid = Guid.Empty;
                if (Guid.TryParse(GetAttributeValue(ROOT_PAGE), out pageGuid))
                {
                    rootPage = PageCache.Read(pageGuid);
                }

                // If a root page was not found, use current page
                if (rootPage == null)
                {
                    rootPage = currentPage;
                }

                int levelsDeep = Convert.ToInt32(GetAttributeValue(NUM_LEVELS));

                Dictionary <string, string> pageParameters = null;
                if (GetAttributeValue("IncludeCurrentParameters").AsBoolean())
                {
                    pageParameters = CurrentPageReference.Parameters;
                }

                NameValueCollection queryString = null;
                if (GetAttributeValue("IncludeCurrentQueryString").AsBoolean())
                {
                    queryString = CurrentPageReference.QueryString;
                }

                // Get list of pages in current page's heirarchy
                var pageHeirarchy = new List <int>();
                if (currentPage != null)
                {
                    pageHeirarchy = currentPage.GetPageHierarchy().Select(p => p.Id).ToList();
                }

                // add context to merge fields
                var contextEntityTypes = RockPage.GetContextEntityTypes();
                var contextObjects     = new Dictionary <string, object>();
                foreach (var conextEntityType in contextEntityTypes)
                {
                    var contextObject = RockPage.GetCurrentContext(conextEntityType);
                    contextObjects.Add(conextEntityType.FriendlyName, contextObject);
                }

                var pageProperties = new Dictionary <string, object>();
                pageProperties.Add("CurrentPerson", CurrentPerson);
                pageProperties.Add("Context", contextObjects);
                pageProperties.Add("Site", GetSiteProperties(RockPage.Site));
                pageProperties.Add("IncludePageList", GetIncludePageList());

                using (var rockContext = new RockContext())
                {
                    pageProperties.Add("Page", rootPage.GetMenuProperties(levelsDeep, CurrentPerson, rockContext, pageHeirarchy, pageParameters, queryString));
                }
                string content = GetTemplate().Render(Hash.FromDictionary(pageProperties));

                // check for errors
                if (content.Contains("error"))
                {
                    content = "<div class='alert alert-warning'><h4>Warning</h4>" + content + "</div>";
                }

                phContent.Controls.Clear();
                phContent.Controls.Add(new LiteralControl(content));
            }
            catch (Exception ex)
            {
                StringBuilder errorMessage = new StringBuilder();
                errorMessage.Append("<div class='alert alert-warning'>");
                errorMessage.Append("An error has occurred while generating the page menu. Error details:");
                errorMessage.Append(ex.Message);
                errorMessage.Append("</div>");

                phContent.Controls.Add(new LiteralControl(errorMessage.ToString()));
            }
        }