Skip to content

Commit

Permalink
feat(TagManager): add option to configure displaying tags in the admi…
Browse files Browse the repository at this point in the history
…nistration. Defaults to true
  • Loading branch information
bkapustik committed May 13, 2024
1 parent b01c163 commit d25c1af
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class CodeSnippetConfigurationModel
[VisibleIfEqualTo(nameof(TagType), CustomSnippetFactory.TAG_TYPE_NAME)]
public string? Code { get; set; }

[RadioGroupComponent(Label = "Tag location", Order = 5, Options = CodeSnippetLocationsExtensions.FormComponentOptions)]
[RadioGroupComponent(Label = "Tag location", Order = 5, Options = CodeSnippetExtensions.LocationFormComponentOptions)]
[VisibleIfEqualTo(nameof(TagType), CustomSnippetFactory.TAG_TYPE_NAME)]
public string? Location { get; set; }

Expand All @@ -38,6 +38,9 @@ internal class CodeSnippetConfigurationModel
[ObjectIdSelectorComponent(objectType: ConsentInfo.OBJECT_TYPE, Label = "Consent", Order = 6, Placeholder = "No consent needed")]
public IEnumerable<int> ConsentIDs { get; set; } = [];

[DropDownComponent(Label = "Kentico administration Display Mode", Options = CodeSnippetExtensions.DisplayModeFormComponentOptions, Order = 7)]
public string DisplayMode { get; set; } = "None";

public void MapToChannelCodeSnippetInfo(ChannelCodeSnippetItemInfo info)
{
info.ChannelCodeSnippetItemChannelId = ChannelIDs.FirstOrDefault();
Expand All @@ -47,13 +50,19 @@ public void MapToChannelCodeSnippetInfo(ChannelCodeSnippetItemInfo info)
info.ChannelCodeSnippetItemName = Name;
info.ChannelCodeSnippetItemIdentifier = TagIdentifier;
info.ChannelCodeSnippetItemCode = Code;
info.ChannelCodeSnippetAdministrationDisplayMode = DisplayMode;
}
}

internal static class CodeSnippetLocationsExtensions
internal static class CodeSnippetExtensions
{
public const string FormComponentOptions = $"{nameof(CodeSnippetLocations.HeadTop)};Insert at the top of the head\r\n" +
public const string LocationFormComponentOptions = $"{nameof(CodeSnippetLocations.HeadTop)};Insert at the top of the head\r\n" +
$"{nameof(CodeSnippetLocations.HeadBottom)};Insert at the bottom of the head\r\n" +
$"{nameof(CodeSnippetLocations.BodyTop)};Insert at the top of the body\r\n" +
$"{nameof(CodeSnippetLocations.BodyBottom)};Insert at the bottom of the body\r\n";

public const string DisplayModeFormComponentOptions = $"{nameof(CodeSnippetAdministrationDisplayMode.None)};Do not dispaly in Administration\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.PreviewOnly)};Display in the Preview only\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.PageBuilderOnly)};Display in the PageBuilder only\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.Both)};Display in Preview and Pagebuilder";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CMS.Helpers;
using CMS.ContentEngine;
using CMS.DataProtection;

using Kentico.Xperience.TagManager.Admin;

[assembly: RegisterObjectType(typeof(ChannelCodeSnippetItemInfo), ChannelCodeSnippetItemInfo.OBJECT_TYPE)]
Expand Down Expand Up @@ -120,7 +121,7 @@ public virtual string ChannelCodeSnippetItemLocation


/// <summary>
/// Channel code snippet GTMID.
/// Channel code snippet third party identifier.
/// </summary>
[DatabaseField]
public virtual string ChannelCodeSnippetItemIdentifier
Expand All @@ -140,6 +141,16 @@ public virtual string ChannelCodeSnippetItemCode
set => SetValue(nameof(ChannelCodeSnippetItemCode), value, String.Empty);
}

/// <summary>
/// Channel code snippet administration display mode.
/// </summary>
[DatabaseField]
public virtual string ChannelCodeSnippetAdministrationDisplayMode
{
get => ValidationHelper.GetString(GetValue(nameof(ChannelCodeSnippetAdministrationDisplayMode)), String.Empty);
set => SetValue(nameof(ChannelCodeSnippetAdministrationDisplayMode), value, String.Empty);
}


/// <summary>
/// Channel code snippet consent ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ private static void InstallChannelCodeSnippetClass(ResourceInfo resourceInfo)
};
formInfo.AddFormItem(formItem);

formItem = new FormFieldInfo
{
Name = nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetAdministrationDisplayMode),
Visible = false,
Precision = 0,
Size = 200,
DataType = FieldDataType.Text,
Enabled = true,
AllowEmpty = true
};
formInfo.AddFormItem(formItem);

formItem = new FormFieldInfo
{
Name = nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected override CodeSnippetConfigurationModel Model
ChannelIDs = [info.ChannelCodeSnippetItemChannelId],
Name = info.ChannelCodeSnippetItemName,
Code = info.ChannelCodeSnippetItemCode,
DisplayMode = info.ChannelCodeSnippetAdministrationDisplayMode,
TagType = info.ChannelCodeSnippetItemType,
ConsentIDs = info.ChannelCodeSnippetItemConsentId == 0 ? [] : [info.ChannelCodeSnippetItemConsentId],
TagIdentifier = info.ChannelCodeSnippetItemIdentifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kentico.Xperience.TagManager.Rendering;

public enum CodeSnippetAdministrationDisplayMode
{
None = default,
PreviewOnly,
PageBuilderOnly,
Both
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class CodeSnippetDto
public int ID { get; init; }
public string? Code { get; init; }
public CodeSnippetLocations Location { get; init; }
public CodeSnippetAdministrationDisplayMode DisplayMode { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using CMS.ContactManagement;

using Kentico.Content.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using Kentico.Web.Mvc;

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
Expand All @@ -18,14 +23,17 @@ internal class CodeSnippetTagHelperComponent : TagHelperComponent
private readonly IChannelCodeSnippetsService codeSnippetsContext;
private readonly IUrlHelperFactory urlHelperFactory;
private readonly IFileVersionProvider fileVersionProvider;
private readonly IHttpContextAccessor httpContextAccessor;

public CodeSnippetTagHelperComponent(
IChannelCodeSnippetsService codeSnippetsContext,
IUrlHelperFactory urlHelperFactory,
IFileVersionProvider fileVersionProvider)
IFileVersionProvider fileVersionProvider,
IHttpContextAccessor httpContextAccessor)
{
this.codeSnippetsContext = codeSnippetsContext;
this.urlHelperFactory = urlHelperFactory;
this.httpContextAccessor = httpContextAccessor;
this.fileVersionProvider = fileVersionProvider;
}

Expand All @@ -40,42 +48,92 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
{
var contact = ContactManagementContext.CurrentContact;

var codeSnippets = await codeSnippetsContext.GetConsentedCodeSnippets(contact);

if (string.Equals(context.TagName, HeadTag, StringComparison.OrdinalIgnoreCase))
{
ProcessHead(output, await codeSnippetsContext.GetConsentedCodeSnippets(contact));
ProcessHead(output, codeSnippets, httpContextAccessor.HttpContext);
}

if (string.Equals(context.TagName, BodyTag, StringComparison.OrdinalIgnoreCase))
{
ProcessBody(output, await codeSnippetsContext.GetConsentedCodeSnippets(contact));
ProcessBody(output, codeSnippets, httpContextAccessor.HttpContext);
}
}

private static void ProcessHead(
TagHelperOutput output,
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets)
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets,
HttpContext? httpContext)
{
foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.HeadTop])
bool isEditMode = httpContext.Kentico().PageBuilder().EditMode;
bool isPreviewMode = httpContext.Kentico().Preview().Enabled;

var headTopSnippets = codeSnippets[CodeSnippetLocations.HeadTop];
var headBottomSnippets = codeSnippets[CodeSnippetLocations.HeadBottom];

if (isEditMode)
{
headTopSnippets = headTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);

headBottomSnippets = headBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);
}
else if (isPreviewMode)
{
headTopSnippets = headTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);

headBottomSnippets = headBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);
}

foreach (var codeSnippet in headTopSnippets)
{
output.PreContent.AppendHtml(codeSnippet.Code);
}

foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.HeadBottom])
foreach (var codeSnippet in headBottomSnippets)
{
output.PostContent.AppendHtml(codeSnippet.Code);
}
}

private void ProcessBody(
TagHelperOutput output,
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets)
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets,
HttpContext? httpContext)
{
foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.BodyTop])
bool isEditMode = httpContext.Kentico().PageBuilder().EditMode;
bool isPreviewMode = httpContext.Kentico().Preview().Enabled;

var bodyTopSnippets = codeSnippets[CodeSnippetLocations.BodyTop];
var bodyBottomSnippets = codeSnippets[CodeSnippetLocations.BodyBottom];

if (isEditMode)
{
bodyTopSnippets = bodyTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);

bodyBottomSnippets = bodyBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);
}
else if (isPreviewMode)
{
bodyTopSnippets = bodyTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);

bodyBottomSnippets = bodyBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);
}

foreach (var codeSnippet in bodyTopSnippets)
{
output.PreContent.AppendHtml(codeSnippet.Code);
}

foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.BodyBottom])
foreach (var codeSnippet in bodyBottomSnippets)
{
output.PostContent.AppendHtml(codeSnippet.Code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class DefaultChannelCodeSnippetsService : IChannelCodeSnippetsService
{
private readonly IConsentAgreementService consentAgreementService;
private readonly IWebsiteChannelContext channelContext;

private readonly IChannelCodeSnippetItemInfoProvider codeSnippetInfoProvider;
private readonly IProgressiveCache cache;

Expand Down Expand Up @@ -70,6 +71,7 @@ async Task<ILookup<CodeSnippetLocations, CodeSnippetDto>> GetCodeSnippetsInterna
.Columns(nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemLocation),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemCode),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemConsentId),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetAdministrationDisplayMode),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemIdentifier),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemID),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemType),
Expand Down Expand Up @@ -99,13 +101,18 @@ private static IEnumerable<CodeSnippetDto> CreateCodeSnippet(ChannelCodeSnippetI

var tags = new List<CodeSnippetDto>();

if (!Enum.TryParse(snippetInfo.ChannelCodeSnippetAdministrationDisplayMode, out CodeSnippetAdministrationDisplayMode displayMode))
{
displayMode = CodeSnippetAdministrationDisplayMode.None;
}
if (snippetSettings.TagTypeName != CustomSnippetFactory.TAG_TYPE_NAME)
{
tags.AddRange(snippetFactory.CreateCodeSnippets(snippetInfo.ChannelCodeSnippetItemIdentifier).Select(x => new CodeSnippetDto
{
Location = x.Location,
Code = x.Code,
ID = snippetInfo.ChannelCodeSnippetItemID
ID = snippetInfo.ChannelCodeSnippetItemID,
DisplayMode = displayMode
}));
}
else
Expand All @@ -117,6 +124,7 @@ private static IEnumerable<CodeSnippetDto> CreateCodeSnippet(ChannelCodeSnippetI
Location = Enum.TryParse(snippetInfo.ChannelCodeSnippetItemLocation, out CodeSnippetLocations location)
? location
: throw new InvalidOperationException("Invalid Channel Tag Location."),
DisplayMode = displayMode
});

tags.Add(tag);
Expand All @@ -130,7 +138,8 @@ private static CodeSnippetDto AdjustCustomCodeSnippet(CodeSnippetDto codeSnippet
{
Code = codeSnippet.Code != null ? AddSnippetIds(codeSnippet.ID, codeSnippet.Code!) : codeSnippet.Code,
ID = codeSnippet.ID,
Location = codeSnippet.Location
Location = codeSnippet.Location,
DisplayMode = codeSnippet.DisplayMode
};

private static string AddSnippetIds(int codeSnippetId, string codeSnippet) =>
Expand Down

0 comments on commit d25c1af

Please sign in to comment.