/// <summary> /// Returns an html representation of object that is available to lava. /// </summary> /// <param name="lavaObject">The liquid object.</param> /// <param name="rockContext">The rock context.</param> /// <param name="preText">The pre text.</param> /// <param name="postText">The post text.</param> /// <returns></returns> public static string lavaDebugInfo(this object lavaObject, RockContext rockContext = null, string preText = "", string postText = "") { //return liquidObject.LiquidizeChildren( 0, rockContext ).ToJson(); StringBuilder lavaDebugPanel = new StringBuilder(); lavaDebugPanel.Append("<div class='alert alert-info lava-debug'><h4>Lava Debug Info</h4>"); lavaDebugPanel.Append(preText); lavaDebugPanel.Append("<p>Below is a listing of available merge fields for this block. Find out more on Lava at <a href='http://www.rockrms.com/lava' target='_blank'>rockrms.com/lava</a>."); lavaDebugPanel.Append(formatLavaDebugInfo(lavaObject.LiquidizeChildren(0, rockContext))); // Add a 'GlobalAttribute' entry if it wasn't part of the LavaObject if (!(lavaObject is IDictionary <string, object>) || !((IDictionary <string, object>)lavaObject).Keys.Contains("GlobalAttribute")) { var globalAttributes = new Dictionary <string, object>(); // Lava Help Text does special stuff for GlobalAttribute, but it still needs the list of possible Global Attribute MergeFields to generate the help text globalAttributes.Add("GlobalAttribute", GlobalAttributesCache.GetLegacyMergeFields(null)); lavaDebugPanel.Append(formatLavaDebugInfo(globalAttributes.LiquidizeChildren(0, rockContext))); } lavaDebugPanel.Append(postText); lavaDebugPanel.Append("</div>"); return(lavaDebugPanel.ToString()); }
/// <summary> /// Returns an html representation of object that is available to lava. /// </summary> /// <param name="lavaObject">The liquid object.</param> /// <param name="rockContext">The rock context.</param> /// <param name="preText">The pre text.</param> /// <param name="postText">The post text.</param> /// <returns></returns> public static string GetLavaDataObjectInfo(this object lavaObject, RockContext rockContext = null, string preText = "", string postText = "") { StringBuilder lavaDebugPanel = new StringBuilder(); lavaDebugPanel.Append("<div class='alert alert-info lava-debug'><h4>Lava Debug Info</h4>"); lavaDebugPanel.Append(preText); lavaDebugPanel.Append("<p>Below is a listing of available merge fields for this block. Find out more on Lava at <a href='http://www.rockrms.com/lava' target='_blank'>rockrms.com/lava</a>."); int maxWaitMS = 10000; System.Web.HttpContext taskContext = System.Web.HttpContext.Current; var formatLavaTask = new Task(() => { System.Web.HttpContext.Current = taskContext; lavaDebugPanel.Append(FormatLavaDataObjectInfo(lavaObject.GetLavaDataObjectChildInfo(0, rockContext))); }); formatLavaTask.Start(); if (!formatLavaTask.Wait(maxWaitMS)) { return("<div class='alert alert-warning lava-debug'>Warning: Timeout generating Lava Help</div>"); } // Add a 'GlobalAttribute' entry if it wasn't part of the LavaObject if (!(lavaObject is IDictionary <string, object>) || !((IDictionary <string, object>)lavaObject).Keys.Contains("GlobalAttribute")) { var globalAttributes = new Dictionary <string, object>(); // Lava Help Text does special stuff for GlobalAttribute, but it still needs the list of possible Global Attribute MergeFields to generate the help text globalAttributes.Add("GlobalAttribute", GlobalAttributesCache.GetLegacyMergeFields(null)); lavaDebugPanel.Append(FormatLavaDataObjectInfo(globalAttributes.GetLavaDataObjectChildInfo(0, rockContext))); } lavaDebugPanel.Append(postText); lavaDebugPanel.Append("</div>"); return(lavaDebugPanel.ToString()); }
/// <summary> /// Gets the common merge fields for Lava operations. By default it'll include CurrentPerson, Context, PageParameter, and Campuses /// </summary> /// <param name="rockPage">The rock page.</param> /// <param name="currentPerson">The current person.</param> /// <param name="options">The options.</param> /// <returns></returns> public static Dictionary <string, object> GetCommonMergeFields(RockPage rockPage, Person currentPerson = null, CommonMergeFieldsOptions options = null) { var mergeFields = new Dictionary <string, object>(); if (rockPage == null && HttpContext.Current != null) { rockPage = HttpContext.Current.Handler as RockPage; } if (options == null) { options = new CommonMergeFieldsOptions(); } if (currentPerson == null) { if (rockPage != null) { currentPerson = rockPage.CurrentPerson; } else if (HttpContext.Current != null && HttpContext.Current.Items.Contains("CurrentPerson")) { currentPerson = HttpContext.Current.Items["CurrentPerson"] as Person; } } if (options.GetLegacyGlobalMergeFields) { var globalAttributes = GlobalAttributesCache.Get(); if (globalAttributes.LavaSupportLevel != Lava.LavaSupportLevel.NoLegacy) { var legacyGlobalAttributeMergeFields = GlobalAttributesCache.GetLegacyMergeFields(currentPerson); foreach (var legacyGlobalAttributeMergeField in legacyGlobalAttributeMergeFields) { mergeFields.Add(legacyGlobalAttributeMergeField.Key, legacyGlobalAttributeMergeField.Value); } } } if (options.GetPageContext && rockPage != null) { var contextObjects = new Dictionary <string, object>(); foreach (var contextEntityType in rockPage.GetContextEntityTypes()) { var contextEntity = 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); } } HttpRequest request = null; try { if (rockPage != null) { request = rockPage.Request; } else if (HttpContext.Current != null) { request = HttpContext.Current.Request; } } catch { // intentionally ignore exception (.Request will throw an exception instead of simply returning null if it isn't available) } if (options.GetPageParameters && rockPage != null && request != null) { mergeFields.Add("PageParameter", rockPage.PageParameters()); } if (options.GetOSFamily || options.GetDeviceFamily) { if (request != null && !string.IsNullOrEmpty(request.UserAgent)) { Parser uaParser = Parser.GetDefault(); ClientInfo client = uaParser.Parse(request.UserAgent); if (options.GetOSFamily) { mergeFields.Add("OSFamily", client.OS.Family.ToLower()); } if (options.GetDeviceFamily) { mergeFields.Add("DeviceFamily", client.Device.Family); } } } if (options.GetCurrentPerson) { if (currentPerson != null) { mergeFields.Add("CurrentPerson", currentPerson); } } if (options.GetCampuses) { mergeFields.Add("Campuses", CampusCache.All()); } return(mergeFields); }