/// <summary>
        /// Loads the shortcodes.
        /// </summary>
        private void LoadLavaShortcodes()
        {
            if (LavaService.RockLiquidIsEnabled)
            {
                LoadShortcodes();
                return;
            }

            LavaShortcodeService lavaShortcodeService = new LavaShortcodeService(new RockContext());
            var lavaShortcodes = lavaShortcodeService.Queryable();

            if (tglShowActive.Checked)
            {
                lavaShortcodes = lavaShortcodes.Where(s => s.IsActive == true);
            }

            // To list the items from the database as we now need to add
            // items in c# assemblies
            var shortcodeList = lavaShortcodes.ToList();

            // Start with block items
            var shortcodeTypes = Rock.Reflection.FindTypes(typeof(ILavaShortcode)).Values.ToList();

            foreach (var shortcode in shortcodeTypes)
            {
                var shortcodeMetadataAttribute = shortcode.GetCustomAttributes(typeof(LavaShortcodeMetadataAttribute), true).FirstOrDefault() as LavaShortcodeMetadataAttribute;

                // ignore shortcodes with no metadata
                if (shortcodeMetadataAttribute == null)
                {
                    continue;
                }

                try
                {
                    var shortcodeInstance = Activator.CreateInstance(shortcode) as ILavaShortcode;

                    var shortcodeType = shortcodeInstance.ElementType;

                    shortcodeList.Add(new LavaShortcode
                    {
                        Id            = -1,
                        Name          = shortcodeMetadataAttribute.Name,
                        TagName       = shortcodeMetadataAttribute.TagName,
                        TagType       = (shortcodeType == LavaShortcodeTypeSpecifier.Inline) ? TagType.Inline : TagType.Block,
                        IsActive      = true,
                        IsSystem      = true,
                        Description   = shortcodeMetadataAttribute.Description,
                        Documentation = shortcodeMetadataAttribute.Documentation
                    });
                }
                catch (Exception ex)
                {
                    ExceptionLogService.LogException(ex);
                }
            }

            rptShortcodes.DataSource = shortcodeList.ToList().OrderBy(s => s.Name);
            rptShortcodes.DataBind();
        }
예제 #2
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            LavaShortcode lavaShortcode;
            var           rockContext          = new RockContext();
            var           lavaShortCodeService = new LavaShortcodeService(rockContext);

            int lavaShortCode = hfLavaShortcodeId.ValueAsInt();

            if (lavaShortCodeService.Queryable().Any(a => a.TagName == tbTagName.Text && a.Id != lavaShortCode))
            {
                Page.ModelState.AddModelError("DuplicateTag", "Tag with the same name is already in use.");
                return;
            }

            if (lavaShortCode == 0)
            {
                lavaShortcode = new LavaShortcode();
                lavaShortCodeService.Add(lavaShortcode);
            }
            else
            {
                lavaShortcode = lavaShortCodeService.Get(lavaShortCode);
            }

            lavaShortcode.Name                = tbLavaShortcodeName.Text;
            lavaShortcode.IsActive            = cbIsActive.Checked;
            lavaShortcode.Description         = tbDescription.Text;
            lavaShortcode.Documentation       = htmlDocumentation.Text;
            lavaShortcode.TagType             = rblTagType.SelectedValueAsEnum <TagType>();
            lavaShortcode.TagName             = tbTagName.Text;
            lavaShortcode.Markup              = ceMarkup.Text;
            lavaShortcode.Parameters          = kvlParameters.Value;
            lavaShortcode.EnabledLavaCommands = String.Join(",", lcpLavaCommands.SelectedLavaCommands);

            rockContext.SaveChanges();

            // unregister shortcode
            if (hfOriginalTagName.Value.IsNotNullOrWhiteSpace())
            {
                Template.UnregisterShortcode(hfOriginalTagName.Value);
            }

            // register shortcode
            if (lavaShortcode.TagType == TagType.Block)
            {
                Template.RegisterShortcode <DynamicShortcodeBlock>(lavaShortcode.TagName);
            }
            else
            {
                Template.RegisterShortcode <DynamicShortcodeInline>(lavaShortcode.TagName);
            }

            // (bug fix) Now we have to clear the entire LavaTemplateCache because it's possible that some other
            // usage of this shortcode is cached with a key we can't predict.
            LavaTemplateCache.Clear();

            NavigateToParentPage();
        }
예제 #3
0
        private static int LoadByTagName2(string tagName, RockContext rockContext)
        {
            var lavaShortcodeService = new LavaShortcodeService(rockContext);

            return(lavaShortcodeService
                   .Queryable().AsNoTracking()
                   .Where(c => c.TagName == tagName)
                   .Select(c => c.Id)
                   .FirstOrDefault());
        }
예제 #4
0
        private static int LoadByGuid2(Guid guid, RockContext rockContext)
        {
            var lavaShortcodeService = new LavaShortcodeService(rockContext);

            return(lavaShortcodeService
                   .Queryable().AsNoTracking()
                   .Where(c => c.Guid.Equals(guid))
                   .Select(c => c.Id)
                   .FirstOrDefault());
        }
예제 #5
0
        /// <summary>
        /// Loads the shortcodes.
        /// </summary>
        private void LoadShortcodes()
        {
            LavaShortcodeService lavaShortcodeService = new LavaShortcodeService(new RockContext());
            var lavaShortcodes = lavaShortcodeService.Queryable();

            if (tglShowActive.Checked)
            {
                lavaShortcodes = lavaShortcodes.Where(s => s.IsActive == true);
            }

            rptShortcodes.DataSource = lavaShortcodes.ToList().OrderBy(s => s.Name);
            rptShortcodes.DataBind();
        }
예제 #6
0
        private static LavaShortcodeCache LoadById2(int id, RockContext rockContext)
        {
            var lavaShortcodeService = new LavaShortcodeService(rockContext);
            var shortcodeModel       = lavaShortcodeService
                                       .Queryable().AsNoTracking()
                                       .FirstOrDefault(c => c.Id == id);

            if (shortcodeModel != null)
            {
                return(new LavaShortcodeCache(shortcodeModel));
            }

            return(null);
        }
예제 #7
0
            /// <summary>
            /// Gets a shortcode definition for the specified shortcode name.
            /// </summary>
            /// <param name="shortcodeName"></param>
            /// <returns></returns>
            public DynamicShortcodeDefinition GetShortcodeDefinition(string shortcodeName)
            {
                if (_cachedShortcodes.ContainsKey(shortcodeName))
                {
                    return(_cachedShortcodes[shortcodeName]);
                }

                // Get the shortcode and add it to the cache.
                var rockContext = new RockContext();

                var lavaShortcodeService = new LavaShortcodeService(rockContext);

                var shortcode = lavaShortcodeService.Queryable().Where(c => c.TagName == shortcodeName).FirstOrDefault();

                var shortcodeDefinition = GetShortcodeDefinitionFromShortcodeEntity(shortcode);

                if (shortcode != null)
                {
                    _cachedShortcodes.Add(shortcode.TagName, shortcodeDefinition);
                }

                return(shortcodeDefinition);
            }
예제 #8
0
        public void WebsiteLavaShortcodeProvider_ModifiedShortcode_ReturnsCorrectVersionAfterModification()
        {
            var options = new LavaEngineConfigurationOptions();

            ILavaTemplateCacheService cacheService = new WebsiteLavaTemplateCacheService();

            options.CacheService = cacheService;

            TestHelper.ExecuteForActiveEngines((defaultEngineInstance) =>
            {
                if (defaultEngineInstance.GetType() == typeof(DotLiquidEngine) ||
                    defaultEngineInstance.GetType() == typeof(RockLiquidEngine))
                {
                    Debug.Write("Shortcode caching is not currently implemented for RockLiquid/DotLiquid.");
                    return;
                }

                var engine = LavaService.NewEngineInstance(defaultEngineInstance.GetType(), options);

                var shortcodeProvider = new TestLavaDynamicShortcodeProvider();

                var rockContext          = new RockContext();
                var lavaShortCodeService = new LavaShortcodeService(rockContext);

                // Create a new Shortcode.
                var shortcodeGuid1 = TestGuids.Shortcodes.ShortcodeTest1.AsGuid();

                var lavaShortcode = lavaShortCodeService.Queryable().FirstOrDefault(x => x.Guid == shortcodeGuid1);

                if (lavaShortcode == null)
                {
                    lavaShortcode = new LavaShortcode();

                    lavaShortCodeService.Add(lavaShortcode);
                }

                lavaShortcode.Guid        = shortcodeGuid1;
                lavaShortcode.TagName     = "TestShortcode1";
                lavaShortcode.Name        = "Test Shortcode 1";
                lavaShortcode.IsActive    = true;
                lavaShortcode.Description = "Test shortcode";
                lavaShortcode.TagType     = TagType.Inline;

                lavaShortcode.Markup = "Hello!";

                rockContext.SaveChanges();

                shortcodeProvider.RegisterShortcode(engine, lavaShortcode);

                // Resolve a template using the new shortcode and verify the result.
                engine.ClearTemplateCache();

                shortcodeProvider.ClearCache();

                LavaService.SetCurrentEngine(engine);

                TestHelper.AssertTemplateOutput(engine, "Hello!", "{[ TestShortcode1 ]}");

                lavaShortcode.Markup = "Goodbye!";

                rockContext.SaveChanges();

                shortcodeProvider.ClearCache();

                engine.ClearTemplateCache();

                TestHelper.AssertTemplateOutput(engine, "Goodbye!", "{[ TestShortcode1 ]}");
            });
        }
        /// <summary>
        /// Loads the shortcodes.
        /// </summary>
        private void LoadShortcodes()
        {
            LavaShortcodeService lavaShortcodeService = new LavaShortcodeService(new RockContext());
            var lavaShortcodes = lavaShortcodeService.Queryable();

            if (tglShowActive.Checked)
            {
                lavaShortcodes = lavaShortcodes.Where(s => s.IsActive == true);
            }

            // To list the items from the database as we now need to add
            // items in c# assemblies
            var shortcodeList = lavaShortcodes.ToList();

            // Start with block items
            foreach (var shortcodeInCode in Rock.Reflection.FindTypes(typeof(Rock.Lava.Shortcodes.RockLavaShortcodeBlockBase)).ToList())
            {
                var shortcode = shortcodeInCode.Value;
                var shortcodeMetadataAttribute = shortcode.GetCustomAttributes(typeof(LavaShortcodeMetadataAttribute), true).FirstOrDefault() as LavaShortcodeMetadataAttribute;

                // ignore shortcodes with no metadata
                if (shortcodeMetadataAttribute == null)
                {
                    continue;
                }

                shortcodeList.Add(new LavaShortcode {
                    Id            = -1,
                    Name          = shortcodeMetadataAttribute.Name,
                    TagName       = shortcodeMetadataAttribute.TagName,
                    TagType       = TagType.Block,
                    IsActive      = true,
                    IsSystem      = true,
                    Description   = shortcodeMetadataAttribute.Description,
                    Documentation = shortcodeMetadataAttribute.Documentation
                });
            }

            // Next add inline items
            foreach (var shortcodeInCode in Rock.Reflection.FindTypes(typeof(Rock.Lava.Shortcodes.RockLavaShortcodeBase)).ToList())
            {
                var shortcode = shortcodeInCode.Value;
                var shortcodeMetadataAttribute = shortcode.GetCustomAttributes(typeof(LavaShortcodeMetadataAttribute), true).FirstOrDefault() as LavaShortcodeMetadataAttribute;

                // ignore shortcodes with no metadata
                if (shortcodeMetadataAttribute == null)
                {
                    continue;
                }

                shortcodeList.Add(new LavaShortcode
                {
                    Id            = -1,
                    Name          = shortcodeMetadataAttribute.Name,
                    TagName       = shortcodeMetadataAttribute.TagName,
                    TagType       = TagType.Inline,
                    IsActive      = true,
                    IsSystem      = true,
                    Description   = shortcodeMetadataAttribute.Description,
                    Documentation = shortcodeMetadataAttribute.Documentation
                });
            }

            rptShortcodes.DataSource = shortcodeList.ToList().OrderBy(s => s.Name);
            rptShortcodes.DataBind();
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            LavaShortcode lavaShortcode;
            var           rockContext          = new RockContext();
            var           lavaShortCodeService = new LavaShortcodeService(rockContext);

            int lavaShortCode = hfLavaShortcodeId.ValueAsInt();

            if (lavaShortCodeService.Queryable().Any(a => a.TagName == tbTagName.Text && a.Id != lavaShortCode))
            {
                Page.ModelState.AddModelError("DuplicateTag", "Tag with the same name is already in use.");
                return;
            }

            if (lavaShortCode == 0)
            {
                lavaShortcode = new LavaShortcode();
                lavaShortCodeService.Add(lavaShortcode);
            }
            else
            {
                lavaShortcode = lavaShortCodeService.Get(lavaShortCode);
            }

            var oldTagName = hfOriginalTagName.Value;

            lavaShortcode.Name                = tbLavaShortcodeName.Text;
            lavaShortcode.IsActive            = cbIsActive.Checked;
            lavaShortcode.Description         = tbDescription.Text;
            lavaShortcode.Documentation       = htmlDocumentation.Text;
            lavaShortcode.TagType             = rblTagType.SelectedValueAsEnum <TagType>();
            lavaShortcode.TagName             = tbTagName.Text.Trim();
            lavaShortcode.Markup              = ceMarkup.Text;
            lavaShortcode.Parameters          = kvlParameters.Value;
            lavaShortcode.EnabledLavaCommands = String.Join(",", lcpLavaCommands.SelectedLavaCommands);

            rockContext.SaveChanges();

            if (LavaService.RockLiquidIsEnabled)
            {
                // unregister shortcode
                if (oldTagName.IsNotNullOrWhiteSpace())
                {
                    Template.UnregisterShortcode(oldTagName);
                }

                // Register the new shortcode definition. Note that RockLiquid shortcode tags are case-sensitive.
                if (lavaShortcode.TagType == TagType.Block)
                {
                    Template.RegisterShortcode <Rock.Lava.Shortcodes.DynamicShortcodeBlock>(lavaShortcode.TagName);
                }
                else
                {
                    Template.RegisterShortcode <Rock.Lava.Shortcodes.DynamicShortcodeInline>(lavaShortcode.TagName);
                }

                // (bug fix) Now we have to clear the entire LavaTemplateCache because it's possible that some other
                // usage of this shortcode is cached with a key we can't predict.
#pragma warning disable CS0618 // Type or member is obsolete
                // This obsolete code can be deleted when support for the DotLiquid Lava implementation is removed.
                LavaTemplateCache.Clear();
#pragma warning restore CS0618 // Type or member is obsolete
            }

            if (oldTagName.IsNotNullOrWhiteSpace())
            {
                LavaService.DeregisterShortcode(oldTagName);
            }

            // Register the new shortcode definition.
            LavaService.RegisterShortcode(lavaShortcode.TagName, (shortcodeName) => WebsiteLavaShortcodeProvider.GetShortcodeDefinition(shortcodeName));

            LavaService.ClearTemplateCache();

            NavigateToParentPage();
        }