-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5970dd0
commit 9689687
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/TrainingGuides.Web/Features/Personalization/IsInContactGroupConditionType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Collections.Generic; | ||
|
||
using CMS.ContactManagement; | ||
using CMS.DataEngine; | ||
|
||
using TrainingGuides.Web.Features.Personalization; | ||
|
||
using Kentico.PageBuilder.Web.Mvc.Personalization; | ||
using Kentico.Xperience.Admin.Base.FormAnnotations; | ||
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:")] | ||
|
||
namespace TrainingGuides.Web.Features.Personalization; | ||
|
||
/// <summary> | ||
/// Personalization condition type based on contact group. | ||
/// </summary> | ||
public class IsInContactGroupConditionType : ConditionType | ||
{ | ||
/// <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>(); | ||
|
||
|
||
/// <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 (SelectedContactGroups == null || !SelectedContactGroups.Any()) | ||
{ | ||
return contact.ContactGroups.Count == 0; | ||
} | ||
|
||
return contact.IsInAnyContactGroup(SelectedContactGroups.Select(c => c.ObjectCodeName).ToArray()); | ||
} | ||
} | ||
|