Esempio n. 1
0
        /// <summary>
        /// Writes the <see cref="T:System.Web.UI.WebControls.CompositeControl" /> content to the specified <see cref="T:System.Web.UI.HtmlTextWriter" /> object, for display on the client.
        /// </summary>
        /// <param name="writer">An <see cref="T:System.Web.UI.HtmlTextWriter" /> that represents the output stream to render HTML content on the client.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            var blockCache = _rockBlock.BlockCache;

            string preHtml   = string.Empty;
            string postHtml  = string.Empty;
            string appRoot   = _rockBlock.ResolveRockUrl("~/");
            string themeRoot = _rockBlock.ResolveRockUrl("~~/");

            if (_rockBlock.Visible)
            {
                if (!string.IsNullOrWhiteSpace(blockCache.PreHtml))
                {
                    preHtml = blockCache.PreHtml.Replace("~~/", themeRoot).Replace("~/", appRoot);
                }

                if (!string.IsNullOrWhiteSpace(blockCache.PostHtml))
                {
                    postHtml = blockCache.PostHtml.Replace("~~/", themeRoot).Replace("~/", appRoot);
                }

                if (preHtml.HasMergeFields() || postHtml.HasMergeFields())
                {
                    var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(_rockBlock.RockPage);
                    preHtml  = preHtml.ResolveMergeFields(mergeFields, "All");
                    postHtml = postHtml.ResolveMergeFields(mergeFields, "All");
                }
            }

            StringBuilder  sbOutput = null;
            StringWriter   swOutput = null;
            HtmlTextWriter twOutput = null;

            if (_rockBlock.BlockCache.OutputCacheDuration > 0)
            {
                sbOutput = new StringBuilder();
                swOutput = new StringWriter(sbOutput);
                twOutput = new HtmlTextWriter(swOutput);
            }

            // Create block wrapper
            string blockTypeCss = blockCache.BlockType != null ? blockCache.BlockType.Name : "";
            var    parts        = blockTypeCss.Split(new char[] { '>' });

            if (parts.Length > 1)
            {
                blockTypeCss = parts[parts.Length - 1].Trim();
            }
            blockTypeCss = blockTypeCss.Replace(' ', '-').ToLower();
            string blockInstanceCss = "block-instance js-block-instance " +
                                      blockTypeCss +
                                      (string.IsNullOrWhiteSpace(blockCache.CssClass) ? "" : " " + blockCache.CssClass.Trim()) +
                                      (_rockBlock.UserCanEdit || _rockBlock.UserCanAdministrate ? " can-configure " : "");

            writer.Write(preHtml);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Format("bid_{0}", blockCache.Id));
            writer.AddAttribute("data-zone-location", blockCache.BlockLocation.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Class, blockInstanceCss);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "block-content");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            if (blockCache.OutputCacheDuration > 0)
            {
                twOutput.Write(preHtml);
                twOutput.AddAttribute(HtmlTextWriterAttribute.Id, string.Format("bid_{0}", blockCache.Id));
                twOutput.AddAttribute("data-zone-location", blockCache.BlockLocation.ToString());
                twOutput.AddAttribute(HtmlTextWriterAttribute.Class, blockInstanceCss);
                twOutput.RenderBeginTag(HtmlTextWriterTag.Div);

                twOutput.AddAttribute(HtmlTextWriterAttribute.Class, "block-content");
                twOutput.RenderBeginTag(HtmlTextWriterTag.Div);
            }

            if (_rockBlock.PageCache.IncludeAdminFooter && _adminControls.Any())
            {
                // Add the config buttons
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "block-configuration config-bar");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "fa fa-arrow-circle-right");
                writer.RenderBeginTag(HtmlTextWriterTag.I);
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "block-configuration-bar");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(string.IsNullOrWhiteSpace(blockCache.Name) ? blockCache.BlockType.Name : blockCache.Name);
                writer.RenderEndTag();

                foreach (Control configControl in _adminControls)
                {
                    configControl.RenderControl(writer);
                }

                writer.RenderEndTag();  // block-configuration-bar
                writer.RenderEndTag();  // config-bar
            }

            _rockBlock.RenderControl(writer);

            writer.RenderEndTag();  // block-content
            writer.RenderEndTag();  // block-instance
            writer.Write(postHtml);

            if (blockCache.OutputCacheDuration > 0)
            {
                base.Render(twOutput);

                twOutput.RenderEndTag();  // block-content
                twOutput.RenderEndTag();  // block-instance
                twOutput.Write(postHtml);

                var    expiration     = RockDateTime.Now.AddSeconds(blockCache.OutputCacheDuration);
                string _blockCacheKey = string.Format("Rock:BlockOutput:{0}", blockCache.Id);
                RockCache.AddOrUpdate(_blockCacheKey, sbOutput.ToString(), expiration);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Writes the <see cref="T:System.Web.UI.WebControls.CompositeControl" /> content to the specified <see cref="T:System.Web.UI.HtmlTextWriter" /> object, for display on the client.
        /// </summary>
        /// <param name="writer">An <see cref="T:System.Web.UI.HtmlTextWriter" /> that represents the output stream to render HTML content on the client.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            var blockCache = _rockBlock.BlockCache;

            string preHtml   = string.Empty;
            string postHtml  = string.Empty;
            string appRoot   = _rockBlock.ResolveRockUrl("~/");
            string themeRoot = _rockBlock.ResolveRockUrl("~~/");

            if (_rockBlock.Visible)
            {
                if (!string.IsNullOrWhiteSpace(blockCache.PreHtml))
                {
                    preHtml = blockCache.PreHtml.Replace("~~/", themeRoot).Replace("~/", appRoot);
                }

                if (!string.IsNullOrWhiteSpace(blockCache.PostHtml))
                {
                    postHtml = blockCache.PostHtml.Replace("~~/", themeRoot).Replace("~/", appRoot);
                }

                if (preHtml.HasMergeFields() || postHtml.HasMergeFields())
                {
                    var mergeFields = Rock.Web.Cache.GlobalAttributesCache.GetMergeFields(_rockBlock.CurrentPerson);
                    mergeFields.Add("CurrentPerson", _rockBlock.CurrentPerson);
                    mergeFields.Add("Campuses", CampusCache.All());
                    mergeFields.Add("PageParameter", _rockBlock.PageParameters());

                    var contextObjects = new Dictionary <string, object>();
                    foreach (var contextEntityType in _rockBlock.RockPage.GetContextEntityTypes())
                    {
                        var contextEntity = _rockBlock.RockPage.GetCurrentContext(contextEntityType);
                        if (contextEntity != null && contextEntity is DotLiquid.ILiquidizable)
                        {
                            var type = Type.GetType(contextEntityType.AssemblyName ?? contextEntityType.Name);
                            if (type != null)
                            {
                                contextObjects.Add(type.Name, contextEntity);
                            }
                        }
                    }

                    if (contextObjects.Any())
                    {
                        mergeFields.Add("Context", contextObjects);
                    }

                    preHtml  = preHtml.ResolveMergeFields(mergeFields);
                    postHtml = postHtml.ResolveMergeFields(mergeFields);
                }
            }

            StringBuilder  sbOutput = null;
            StringWriter   swOutput = null;
            HtmlTextWriter twOutput = null;

            if (_rockBlock.BlockCache.OutputCacheDuration > 0)
            {
                sbOutput = new StringBuilder();
                swOutput = new StringWriter(sbOutput);
                twOutput = new HtmlTextWriter(swOutput);
            }

            // Create block wrapper
            string blockTypeCss = blockCache.BlockType != null ? blockCache.BlockType.Name : "";
            var    parts        = blockTypeCss.Split(new char[] { '>' });

            if (parts.Length > 1)
            {
                blockTypeCss = parts[parts.Length - 1].Trim();
            }
            blockTypeCss = blockTypeCss.Replace(' ', '-').ToLower();
            string blockInstanceCss = "block-instance " +
                                      blockTypeCss +
                                      (string.IsNullOrWhiteSpace(blockCache.CssClass) ? "" : " " + blockCache.CssClass.Trim()) +
                                      (_rockBlock.UserCanEdit || _rockBlock.UserCanAdministrate ? " can-configure " : "");

            writer.Write(preHtml);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Format("bid_{0}", blockCache.Id));
            writer.AddAttribute("data-zone-location", blockCache.BlockLocation.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Class, blockInstanceCss);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "block-content");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            if (blockCache.OutputCacheDuration > 0)
            {
                twOutput.Write(preHtml);
                twOutput.AddAttribute(HtmlTextWriterAttribute.Id, string.Format("bid_{0}", blockCache.Id));
                twOutput.AddAttribute("data-zone-location", blockCache.BlockLocation.ToString());
                twOutput.AddAttribute(HtmlTextWriterAttribute.Class, blockInstanceCss);
                twOutput.RenderBeginTag(HtmlTextWriterTag.Div);

                twOutput.AddAttribute(HtmlTextWriterAttribute.Class, "block-content");
                twOutput.RenderBeginTag(HtmlTextWriterTag.Div);
            }

            if (_rockBlock.PageCache.IncludeAdminFooter && _adminControls.Any())
            {
                // Add the config buttons
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "block-configuration config-bar");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "fa fa-arrow-circle-right");
                writer.RenderBeginTag(HtmlTextWriterTag.I);
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "block-configuration-bar");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(string.IsNullOrWhiteSpace(blockCache.Name) ? blockCache.BlockType.Name : blockCache.Name);
                writer.RenderEndTag();

                foreach (Control configControl in _adminControls)
                {
                    configControl.RenderControl(writer);
                }

                writer.RenderEndTag();  // block-configuration-bar
                writer.RenderEndTag();  // config-bar
            }

            _rockBlock.RenderControl(writer);

            writer.RenderEndTag();  // block-content
            writer.RenderEndTag();  // block-instance
            writer.Write(postHtml);

            if (blockCache.OutputCacheDuration > 0)
            {
                base.Render(twOutput);

                twOutput.RenderEndTag();  // block-content
                twOutput.RenderEndTag();  // block-instance
                twOutput.Write(postHtml);

                CacheItemPolicy cacheDuration = new CacheItemPolicy();
                cacheDuration.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(blockCache.OutputCacheDuration);

                RockMemoryCache cache          = RockMemoryCache.Default;
                string          _blockCacheKey = string.Format("Rock:BlockOutput:{0}", blockCache.Id);
                cache.Set(_blockCacheKey, sbOutput.ToString(), cacheDuration);
            }
        }