Skip to content

Commit

Permalink
GH-69 :: refactor personalization condition type
Browse files Browse the repository at this point in the history
  • Loading branch information
kentico-matthews committed Aug 29, 2024
1 parent e8a00d5 commit f2ec9db
Showing 1 changed file with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

using CMS.ContactManagement;
using CMS.DataEngine;

Expand All @@ -10,10 +8,10 @@
using Kentico.Xperience.Admin.Base.Forms;

[assembly: RegisterPersonalizationConditionType(
"TrainingGuides.Web.Features.Personalization",
typeof(IsInContactGroupConditionType),
"Is in contact group",
Description = "Evaluates if the current contact is in one of the contact groups.", IconClass = "icon-app-contact-groups", Hint = "Display to visitors who match at least one of the selected contact groups:")]
"TrainingGuides.Web.Features.Personalization",
typeof(IsInContactGroupConditionType),
"Is in contact group",
Description = "Evaluates if the current contact is in one of the contact groups.", IconClass = "icon-app-contact-groups", Hint = "Display to visitors who match at least one of the selected contact groups:")]

namespace TrainingGuides.Web.Features.Personalization;

Expand All @@ -22,35 +20,35 @@ namespace TrainingGuides.Web.Features.Personalization;
/// </summary>
public class IsInContactGroupConditionType : ConditionType
{
/// <summary>
/// Selected contact group code names.
/// </summary>
[ObjectSelectorComponent(PredefinedObjectType.CONTACTGROUP,
/// <summary>
/// Selected contact group code names.
/// </summary>
[ObjectSelectorComponent(PredefinedObjectType.CONTACTGROUP,
Label = "Contact groups",
Order = 0,
MaximumItems = 0)]
public IEnumerable<ObjectRelatedItem> SelectedContactGroups { get; set; } = Enumerable.Empty<ObjectRelatedItem>();
public IEnumerable<ObjectRelatedItem> SelectedContactGroups { get; set; } = Enumerable.Empty<ObjectRelatedItem>();


/// <summary>
/// Evaluate condition type.
/// </summary>
/// <returns>Returns <c>true</c> if implemented condition is met.</returns>
public override bool Evaluate()
{
var contact = ContactManagementContext.GetCurrentContact();
/// <summary>
/// Evaluate condition type.
/// </summary>
/// <returns>Returns <c>true</c> if implemented condition is met.</returns>
public override bool Evaluate()
{
var contact = ContactManagementContext.GetCurrentContact();

if (contact == null)
{
return false;
}
if (contact == null)
{
return false;
}

if (SelectedContactGroups == null || !SelectedContactGroups.Any())
{
return contact.ContactGroups.Count == 0;
}
if (SelectedContactGroups == null || !SelectedContactGroups.Any())
{
return contact.ContactGroups.Count == 0;
}

return contact.IsInAnyContactGroup(SelectedContactGroups.Select(c => c.ObjectCodeName).ToArray());
}
return contact.IsInAnyContactGroup(SelectedContactGroups.Select(c => c.ObjectCodeName).ToArray());
}
}

0 comments on commit f2ec9db

Please sign in to comment.