forked from grandnode/grandnode
-
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
KrzysztofPajak
committed
Mar 6, 2020
1 parent
356255d
commit 6601e0a
Showing
9 changed files
with
727 additions
and
405 deletions.
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
Grand.Web/Commands/Handler/ContactAttributeChangeCommandHandler.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,155 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Grand.Core.Domain.Catalog; | ||
using Grand.Services.Media; | ||
using Grand.Services.Messages; | ||
using Grand.Web.Commands.Models; | ||
using MediatR; | ||
|
||
namespace Grand.Web.Commands.Handler | ||
{ | ||
public class ContactAttributeChangeCommandHandler : IRequestHandler<ContactAttributeChangeCommandModel, (IList<string> enabledAttributeIds, IList<string> disabledAttributeIds)> | ||
{ | ||
private readonly IContactAttributeService _contactAttributeService; | ||
private readonly IContactAttributeParser _contactAttributeParser; | ||
private readonly IDownloadService _downloadService; | ||
|
||
public ContactAttributeChangeCommandHandler(IContactAttributeService contactAttributeService, IContactAttributeParser contactAttributeParser, | ||
IDownloadService downloadService) | ||
{ | ||
_contactAttributeService = contactAttributeService; | ||
_contactAttributeParser = contactAttributeParser; | ||
_downloadService = downloadService; | ||
} | ||
|
||
public async Task<(IList<string> enabledAttributeIds, IList<string> disabledAttributeIds)> Handle(ContactAttributeChangeCommandModel request, CancellationToken cancellationToken) | ||
{ | ||
var attributeXml = await ParseContactAttributes(request); | ||
|
||
var enabledAttributeIds = new List<string>(); | ||
var disabledAttributeIds = new List<string>(); | ||
var attributes = await _contactAttributeService.GetAllContactAttributes(request.Store.Id); | ||
foreach (var attribute in attributes) | ||
{ | ||
var conditionMet = await _contactAttributeParser.IsConditionMet(attribute, attributeXml); | ||
if (conditionMet.HasValue) | ||
{ | ||
if (conditionMet.Value) | ||
enabledAttributeIds.Add(attribute.Id); | ||
else | ||
disabledAttributeIds.Add(attribute.Id); | ||
} | ||
} | ||
return (enabledAttributeIds, disabledAttributeIds); | ||
} | ||
|
||
private async Task<string> ParseContactAttributes(ContactAttributeChangeCommandModel request) | ||
{ | ||
string attributesXml = ""; | ||
var contactAttributes = await _contactAttributeService.GetAllContactAttributes(request.Store.Id); | ||
foreach (var attribute in contactAttributes) | ||
{ | ||
string controlId = string.Format("contact_attribute_{0}", attribute.Id); | ||
switch (attribute.AttributeControlType) | ||
{ | ||
case AttributeControlType.DropdownList: | ||
case AttributeControlType.RadioList: | ||
case AttributeControlType.ColorSquares: | ||
case AttributeControlType.ImageSquares: | ||
{ | ||
var ctrlAttributes = request.Form[controlId]; | ||
if (!String.IsNullOrEmpty(ctrlAttributes)) | ||
{ | ||
attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, | ||
attribute, ctrlAttributes); | ||
|
||
} | ||
} | ||
break; | ||
case AttributeControlType.Checkboxes: | ||
{ | ||
var cblAttributes = request.Form[controlId].ToString(); | ||
if (!String.IsNullOrEmpty(cblAttributes)) | ||
{ | ||
foreach (var item in cblAttributes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) | ||
{ | ||
attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, attribute, item); | ||
} | ||
} | ||
} | ||
break; | ||
case AttributeControlType.ReadonlyCheckboxes: | ||
{ | ||
//load read-only (already server-side selected) values | ||
var attributeValues = attribute.ContactAttributeValues; | ||
foreach (var selectedAttributeId in attributeValues | ||
.Where(v => v.IsPreSelected) | ||
.Select(v => v.Id) | ||
.ToList()) | ||
{ | ||
attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, | ||
attribute, selectedAttributeId.ToString()); | ||
} | ||
} | ||
break; | ||
case AttributeControlType.TextBox: | ||
case AttributeControlType.MultilineTextbox: | ||
{ | ||
var ctrlAttributes = request.Form[controlId].ToString(); | ||
if (!String.IsNullOrEmpty(ctrlAttributes)) | ||
{ | ||
string enteredText = ctrlAttributes.Trim(); | ||
attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, | ||
attribute, enteredText); | ||
} | ||
} | ||
break; | ||
case AttributeControlType.Datepicker: | ||
{ | ||
var date = request.Form[controlId + "_day"]; | ||
var month = request.Form[controlId + "_month"]; | ||
var year = request.Form[controlId + "_year"]; | ||
DateTime? selectedDate = null; | ||
try | ||
{ | ||
selectedDate = new DateTime(Int32.Parse(year), Int32.Parse(month), Int32.Parse(date)); | ||
} | ||
catch { } | ||
if (selectedDate.HasValue) | ||
{ | ||
attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, | ||
attribute, selectedDate.Value.ToString("D")); | ||
} | ||
} | ||
break; | ||
case AttributeControlType.FileUpload: | ||
{ | ||
Guid downloadGuid; | ||
Guid.TryParse(request.Form[controlId], out downloadGuid); | ||
var download = await _downloadService.GetDownloadByGuid(downloadGuid); | ||
if (download != null) | ||
{ | ||
attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, | ||
attribute, download.DownloadGuid.ToString()); | ||
} | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
//validate conditional attributes (if specified) | ||
foreach (var attribute in contactAttributes) | ||
{ | ||
var conditionMet = await _contactAttributeParser.IsConditionMet(attribute, attributesXml); | ||
if (conditionMet.HasValue && !conditionMet.Value) | ||
attributesXml = _contactAttributeParser.RemoveContactAttribute(attributesXml, attribute); | ||
} | ||
|
||
return attributesXml; | ||
} | ||
} | ||
} |
Oops, something went wrong.