/// <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(); }
/// <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(); }
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()); }
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()); }
/// <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(); }
/// <summary> /// Shows the detail. /// </summary> /// <param name="lavaShortcodeId">The Lava Shortcode identifier.</param> public void ShowDetail(int lavaShortcodeId) { pnlDetails.Visible = true; // Load depending on Add(0) or Edit LavaShortcode lavaShortcode = null; if (!lavaShortcodeId.Equals(0)) { lavaShortcode = new LavaShortcodeService(new RockContext()).Get(lavaShortcodeId); lActionTitle.Text = ActionTitle.Edit(LavaShortcode.FriendlyTypeName).FormatAsHtmlTitle(); pdAuditDetails.SetEntity(lavaShortcode, ResolveRockUrl("~")); } if (lavaShortcode == null) { lavaShortcode = new LavaShortcode { Id = 0 }; lActionTitle.Text = ActionTitle.Add(LavaShortcode.FriendlyTypeName).FormatAsHtmlTitle(); // hide the panel drawer that show created and last modified dates pdAuditDetails.Visible = false; } hfLavaShortcodeId.Value = lavaShortcode.Id.ToString(); // render UI based on Authorized and IsSystem bool readOnly = false; nbEditModeMessage.Text = string.Empty; if (!IsUserAuthorized(Authorization.EDIT)) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(Campus.FriendlyTypeName); } if (lavaShortcode.IsSystem) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlySystem(Campus.FriendlyTypeName); } if (readOnly) { ShowReadonlyDetails(lavaShortcode); } else { ShowEditDetails(lavaShortcode); } }
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); }
/// <summary> /// Raises the <see cref="E:System.Web.UI.Control.Load" /> event. /// </summary> /// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param> protected override void OnLoad(EventArgs e) { RouteCollection routes = RouteTable.Routes; PageRouteService pageRouteService = new PageRouteService(new Rock.Data.RockContext()); var pageRoutes = pageRouteService.Queryable().ToList(); // Check to see if we have any missing routes. If so, simply run reregister. foreach (var pageRoute in pageRoutes) { var route = routes.OfType <Route>().Where(a => a.Url == pageRoute.Route && a.PageIds().Contains(pageRoute.PageId)).FirstOrDefault(); if (route == null) { nbNotification.NotificationBoxType = Rock.Web.UI.Controls.NotificationBoxType.Warning; nbNotification.Text = "Routes were out-of-date. Running reregister routes."; ReRegisterRoutes(); break; } } // Check to see if we have any missing shortcodes var outOfDate = RockDateTime.Now.AddMinutes(-30); var sc = new LavaShortcodeService(new Rock.Data.RockContext()).Queryable().Where(l => l.CreatedDateTime > outOfDate || l.ModifiedDateTime > outOfDate).ToList(); if (sc.Count > 0) { nbNotification.NotificationBoxType = Rock.Web.UI.Controls.NotificationBoxType.Warning; nbNotification.Text = "Shortcodes were out-of-date. Running register shortcodes. " + sc.Count; foreach (var code in sc) { // register shortcode if (code.TagType == TagType.Block) { Template.RegisterShortcode <DynamicShortcodeBlock>(code.TagName); } else { Template.RegisterShortcode <DynamicShortcodeInline>(code.TagName); } } LavaShortcodeCache.Clear(); } }
/// <summary> /// Handles the Click event of the btnDelete 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 btnDelete_Click(object sender, EventArgs e) { LinkButton btn = ( LinkButton )sender; RepeaterItem item = ( RepeaterItem )btn.NamingContainer; HiddenField hfShortcodeId = ( HiddenField )item.FindControl("hfShortcodeId"); var rockContext = new RockContext(); LavaShortcodeService lavaShortcodeService = new LavaShortcodeService(rockContext); LavaShortcode lavaShortcode = lavaShortcodeService.Get(hfShortcodeId.ValueAsInt()); if (lavaShortcode != null) { // unregister the shortcode Template.UnregisterShortcode(lavaShortcode.TagName); lavaShortcodeService.Delete(lavaShortcode); rockContext.SaveChanges(); } LoadShortcodes(); }
/// <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); }
/// <summary> /// Handles the Click event of the btnEdit 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 btnEdit_Click(object sender, EventArgs e) { var lavaShortcode = new LavaShortcodeService(new RockContext()).Get(hfLavaShortcodeId.ValueAsInt()); ShowEditDetails(lavaShortcode); }
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(); }