Optional options for Rock.Lava.LavaHelper.GetCommonMergeFields
Example #1
0
        /// <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 = Rock.Web.Cache.GlobalAttributesCache.Read();
                if ( globalAttributes.LavaSupportLevel != Lava.LavaSupportLevel.NoLegacy )
                {
                    var legacyGlobalAttributeMergeFields = Rock.Web.Cache.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;
        }
Example #2
0
        /// <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);
        }
        /// <summary>
        /// Shows the list.
        /// </summary>
        public void ShowList()
        {
            using (var rockContext = new RockContext())
            {
                var channelQry = new InteractionChannelService(rockContext)
                                 .Queryable().AsNoTracking();

                var channelMediumValueId = gfFilter.GetUserPreference(MEDIUM_TYPE_FILTER).AsIntegerOrNull();
                if (channelMediumValueId.HasValue)
                {
                    channelQry = channelQry.Where(a => a.ChannelTypeMediumValueId == channelMediumValueId.Value);
                }

                if (!cbIncludeInactive.Checked)
                {
                    channelQry = channelQry.Where(a => a.IsActive);
                }

                if (!string.IsNullOrWhiteSpace(GetAttributeValue("InteractionChannels")))
                {
                    var selectedChannelIds = Array.ConvertAll(GetAttributeValue("InteractionChannels").Split(','), s => new Guid(s)).ToList();
                    channelQry = channelQry.Where(a => selectedChannelIds.Contains(a.Guid));
                }

                var personId = GetPersonId();
                if (personId.HasValue)
                {
                    var interactionQry = new InteractionService(rockContext).Queryable();
                    channelQry = channelQry.Where(a => interactionQry.Any(b => b.PersonAlias.PersonId == personId.Value && b.InteractionComponent.InteractionChannelId == a.Id));
                }

                // Parse the default template so that it does not need to be parsed multiple times.
                Template      defaultTemplate     = null;
                ILavaTemplate defaultLavaTemplate = null;

                if (LavaService.RockLiquidIsEnabled)
                {
                    defaultTemplate = Template.Parse(GetAttributeValue("DefaultTemplate"));

                    LavaHelper.VerifyParseTemplateForCurrentEngine(GetAttributeValue("DefaultTemplate"));
                }
                else
                {
                    var parseResult = LavaService.ParseTemplate(GetAttributeValue("DefaultTemplate"));

                    defaultLavaTemplate = parseResult.Template;
                }

                var options = new Rock.Lava.CommonMergeFieldsOptions();
                options.GetPageContext             = false;
                options.GetLegacyGlobalMergeFields = false;

                var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson, options);
                mergeFields.Add("ComponentListPage", LinkedPageRoute("ComponentListPage"));
                mergeFields.Add("SessionListPage", LinkedPageRoute("SessionListPage"));

                var channelItems = new List <ChannelItem>();

                if (LavaService.RockLiquidIsEnabled)
                {
                    foreach (var channel in channelQry)
                    {
                        if (!channel.IsAuthorized(Authorization.VIEW, CurrentPerson))
                        {
                            continue;
                        }
                        var channelMergeFields = new Dictionary <string, object>(mergeFields);
                        channelMergeFields.Add("InteractionChannel", channel);

                        string html = channel.ChannelListTemplate.IsNotNullOrWhiteSpace() ?
                                      channel.ChannelListTemplate.ResolveMergeFields(channelMergeFields) :
                                      defaultTemplate.Render(Hash.FromDictionary(channelMergeFields));

                        channelItems.Add(new ChannelItem
                        {
                            Id          = channel.Id,
                            ChannelHtml = html
                        });
                    }
                }
                else
                {
                    foreach (var channel in channelQry)
                    {
                        if (!channel.IsAuthorized(Authorization.VIEW, CurrentPerson))
                        {
                            continue;
                        }
                        var channelMergeFields = new Dictionary <string, object>(mergeFields);
                        channelMergeFields.Add("InteractionChannel", channel);

                        string html = channel.ChannelListTemplate.IsNotNullOrWhiteSpace() ?
                                      channel.ChannelListTemplate.ResolveMergeFields(channelMergeFields) :
                                      LavaService.RenderTemplate(defaultLavaTemplate, channelMergeFields).Text;

                        channelItems.Add(new ChannelItem
                        {
                            Id          = channel.Id,
                            ChannelHtml = html
                        });
                    }
                }

                rptChannel.DataSource = channelItems;
                rptChannel.DataBind();
            }
        }