Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Improvements with customer attributes - allow setting an attribute as read-only

KrzysztofPajakb22e2991b95b
diff --git a/src/Core/Grand.Domain/Customers/CustomerAttribute.cs b/src/Core/Grand.Domain/Customers/CustomerAttribute.cs
index 263500a78..665d01d78 100644
--- a/src/Core/Grand.Domain/Customers/CustomerAttribute.cs
+++ b/src/Core/Grand.Domain/Customers/CustomerAttribute.cs
@@ -25,6 +25,11 @@ namespace Grand.Domain.Customers
/// </summary>
public bool IsRequired { get; set; }
+ /// <summary>
+ /// Gets or sets a value indicating whether the attribute is read only
+ /// </summary>
+ public bool IsReadOnly { get; set; }
+
/// <summary>
/// Gets or sets the attribute control type identifier
/// </summary>
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerAttribute/_CreateOrUpdate.TabInfo.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerAttribute/_CreateOrUpdate.TabInfo.cshtml
index 0128eac32..3c448ac68 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerAttribute/_CreateOrUpdate.TabInfo.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerAttribute/_CreateOrUpdate.TabInfo.cshtml
@@ -57,6 +57,16 @@
<span asp-validation-for="IsRequired"></span>
</div>
</div>
+ <div class="form-group">
+ <admin-label asp-for="IsReadOnly" />
+ <div class="col-md-9 col-sm-9">
+ <label class="mt-checkbox mt-checkbox-outline control control-checkbox">
+ <admin-input asp-for="IsReadOnly" />
+ <div class="control__indicator"></div>
+ </label>
+ <span asp-validation-for="IsReadOnly"></span>
+ </div>
+ </div>
<div class="form-group">
<admin-label asp-for="AttributeControlTypeId" />
<div class="col-md-9 col-sm-9">
@@ -64,7 +74,7 @@
var attributeControlTypes = ((AttributeControlType)Model.AttributeControlTypeId)
.ToSelectList(Context, valuesToExclude:
//custom customer attributes don't support some attribute control types
- new[] { (int)AttributeControlType.FileUpload, (int)AttributeControlType.Datepicker, (int)AttributeControlType.ColorSquares, (int)AttributeControlType.ImageSquares });
+ new[] { (int)AttributeControlType.FileUpload, (int)AttributeControlType.Datepicker, (int)AttributeControlType.ColorSquares, (int)AttributeControlType.ImageSquares, (int)AttributeControlType.ReadonlyCheckboxes });
}
<admin-select asp-for="AttributeControlTypeId" asp-items="attributeControlTypes" />
<span asp-validation-for="AttributeControlTypeId"></span>
diff --git a/src/Web/Grand.Web.Admin/Models/Customers/CustomerAttributeModel.cs b/src/Web/Grand.Web.Admin/Models/Customers/CustomerAttributeModel.cs
index 675da7ccf..a9e043c6e 100644
--- a/src/Web/Grand.Web.Admin/Models/Customers/CustomerAttributeModel.cs
+++ b/src/Web/Grand.Web.Admin/Models/Customers/CustomerAttributeModel.cs
@@ -13,12 +13,14 @@ namespace Grand.Web.Admin.Models.Customers
}
[GrandResourceDisplayName("Admin.Customers.CustomerAttributes.Fields.Name")]
-
public string Name { get; set; }
[GrandResourceDisplayName("Admin.Customers.CustomerAttributes.Fields.IsRequired")]
public bool IsRequired { get; set; }
+ [GrandResourceDisplayName("Admin.Customers.CustomerAttributes.Fields.IsReadOnly")]
+ public bool IsReadOnly { get; set; }
+
[GrandResourceDisplayName("Admin.Customers.CustomerAttributes.Fields.AttributeControlType")]
public int AttributeControlTypeId { get; set; }
[GrandResourceDisplayName("Admin.Customers.CustomerAttributes.Fields.AttributeControlType")]
diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
index 2e9ad9943..fffbf2874 100644
--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
@@ -6436,6 +6436,9 @@
<Resource Name="Admin.Customers.CustomerAttributes.Fields.DisplayOrder">
<Value>Display order</Value>
</Resource>
+ <Resource Name="Admin.Customers.CustomerAttributes.Fields.IsReadOnly">
+ <Value>Read only</Value>
+ </Resource>
<Resource Name="Admin.Customers.CustomerAttributes.Fields.IsRequired">
<Value>Required</Value>
</Resource>
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs
index 7ee205d8f..dfe483634 100644
--- a/src/Web/Grand.Web/Controllers/AccountController.cs
+++ b/src/Web/Grand.Web/Controllers/AccountController.cs
@@ -636,7 +636,7 @@ namespace Grand.Web.Controllers
return Challenge();
//custom customer attributes
- var customerAttributes = await _mediator.Send(new GetParseCustomAttributes() { Form = form });
+ var customerAttributes = await _mediator.Send(new GetParseCustomAttributes() { Form = form, CustomerCustomAttribute = _workContext.CurrentCustomer.Attributes.ToList() });
var customerAttributeWarnings = await customerAttributeParser.GetAttributeWarnings(customerAttributes);
foreach (var error in customerAttributeWarnings)
diff --git a/src/Web/Grand.Web/Features/Handlers/Customers/GetCustomAttributesHandler.cs b/src/Web/Grand.Web/Features/Handlers/Customers/GetCustomAttributesHandler.cs
index 6a08115d3..84c790732 100644
--- a/src/Web/Grand.Web/Features/Handlers/Customers/GetCustomAttributesHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Customers/GetCustomAttributesHandler.cs
@@ -35,6 +35,7 @@ namespace Grand.Web.Features.Handlers.Customers
Id = attribute.Id,
Name = attribute.GetTranslation(x => x.Name, request.Language.Id),
IsRequired = attribute.IsRequired,
+ IsReadOnly = attribute.IsReadOnly,
AttributeControlType = attribute.AttributeControlTypeId,
};
diff --git a/src/Web/Grand.Web/Features/Handlers/Customers/GetParseCustomAttributesHandler.cs b/src/Web/Grand.Web/Features/Handlers/Customers/GetParseCustomAttributesHandler.cs
index a62f51f29..5280dbd68 100644
--- a/src/Web/Grand.Web/Features/Handlers/Customers/GetParseCustomAttributesHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Customers/GetParseCustomAttributesHandler.cs
@@ -28,6 +28,14 @@ namespace Grand.Web.Features.Handlers.Customers
var attributes = await _customerAttributeService.GetAllCustomerAttributes();
foreach (var attribute in attributes)
{
+ if (attribute.IsReadOnly)
+ {
+ var attrReadOnly = request.CustomerCustomAttribute.FirstOrDefault(x => x.Key == attribute.Id);
+ if (attrReadOnly != null)
+ customAttributes.Add(attrReadOnly);
+
+ continue;
+ }
string controlId = string.Format("customer_attribute_{0}", attribute.Id);
switch (attribute.AttributeControlTypeId)
{
diff --git a/src/Web/Grand.Web/Features/Handlers/Customers/GetRegisterHandler.cs b/src/Web/Grand.Web/Features/Handlers/Customers/GetRegisterHandler.cs
index eb2795204..6f31b526d 100644
--- a/src/Web/Grand.Web/Features/Handlers/Customers/GetRegisterHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Customers/GetRegisterHandler.cs
@@ -118,7 +118,7 @@ namespace Grand.Web.Features.Handlers.Customers
Language = request.Language,
OverrideAttributes = request.OverrideCustomCustomerAttributes
});
- foreach (var item in customAttributes)
+ foreach (var item in customAttributes.Where(x=>!x.IsReadOnly))
{
model.CustomerAttributes.Add(item);
}
diff --git a/src/Web/Grand.Web/Features/Models/Customers/GetParseCustomAttributes.cs b/src/Web/Grand.Web/Features/Models/Customers/GetParseCustomAttributes.cs
index c4b533d08..09b7f7996 100644
--- a/src/Web/Grand.Web/Features/Models/Customers/GetParseCustomAttributes.cs
+++ b/src/Web/Grand.Web/Features/Models/Customers/GetParseCustomAttributes.cs
@@ -1,4 +1,5 @@
-using Grand.Domain.Common;
+
+using Grand.Domain.Common;
using MediatR;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
@@ -7,6 +8,12 @@ namespace Grand.Web.Features.Models.Customers
{
public class GetParseCustomAttributes : IRequest<IList<CustomAttribute>>
{
+ public GetParseCustomAttributes()
+ {
+ CustomerCustomAttribute = new List<CustomAttribute>();
+ }
public IFormCollection Form { get; set; }
+
+ public List<CustomAttribute> CustomerCustomAttribute { get; set; }
}
}
diff --git a/src/Web/Grand.Web/Models/Customer/CustomerAttributeModel.cs b/src/Web/Grand.Web/Models/Customer/CustomerAttributeModel.cs
index 4fd559400..95bbdae30 100644
--- a/src/Web/Grand.Web/Models/Customer/CustomerAttributeModel.cs
+++ b/src/Web/Grand.Web/Models/Customer/CustomerAttributeModel.cs
@@ -15,6 +15,7 @@ namespace Grand.Web.Models.Customer
public bool IsRequired { get; set; }
+ public bool IsReadOnly { get; set; }
/// <summary>
/// Default value for textboxes
/// </summary>
diff --git a/src/Web/Grand.Web/Views/Account/_CustomerAttributes.cshtml b/src/Web/Grand.Web/Views/Account/_CustomerAttributes.cshtml
index 57190adc7..d1d4ed6d1 100644
--- a/src/Web/Grand.Web/Views/Account/_CustomerAttributes.cshtml
+++ b/src/Web/Grand.Web/Views/Account/_CustomerAttributes.cshtml
@@ -5,6 +5,7 @@
string controlId = string.Format("customer_attribute_{0}", attribute.Id);
string textPrompt = attribute.Name;
string required = attribute.IsRequired ? "required" : "";
+ string readonlyAttr = attribute.IsReadOnly ? "disabled=\"disabled\"" : "";
<div class="row">
<div class="col-12 form-group custom-attributes">
@@ -13,7 +14,7 @@
case AttributeControlType.DropdownList:
{
<label for="@(controlId)" class="col-form-label">@textPrompt:</label>
- <select class="custom-select form-control" name="@(controlId)" id="@(controlId)">
+ <select class="custom-select form-control" name="@(controlId)" id="@(controlId)" @readonlyAttr>
@if (!attribute.IsRequired)
{
<option value="">---</option>
@@ -36,7 +37,7 @@
<fieldset>
<legend class="sr-only">@(controlId)</legend>
<label class="custom-control custom-radio">
- <input class="custom-control-input" id="@(controlId)_@(attributeValue.Id)" type="radio" name="@(controlId)" value="@attributeValue.Id" checked="@(attributeValue.IsPreSelected)" />
+ <input class="custom-control-input" id="@(controlId)_@(attributeValue.Id)" type="radio" name="@(controlId)" value="@attributeValue.Id" checked="@(attributeValue.IsPreSelected)" @readonlyAttr/>
<span class="custom-control-label"></span>
<span class="custom-control-description">@attributeValue.Name</span>
</label>
@@ -48,7 +49,6 @@
}
break;
case AttributeControlType.Checkboxes:
- case AttributeControlType.ReadonlyCheckboxes:
{
<span class="col-form-label">@textPrompt:</span>
<ul class="option-list pl-0">
@@ -57,7 +57,7 @@
<li>
<label for="@(controlId)_@(attributeValue.Id)" class="custom-control custom-checkbox">
- <input class="custom-control-input" id="@(controlId)_@(attributeValue.Id)" type="checkbox" name="@(controlId)" value="@(attributeValue.Id)" checked="@(attributeValue.IsPreSelected)" @(attribute.AttributeControlType == AttributeControlType.ReadonlyCheckboxes ? Html.Raw(" disabled=\"disabled\"") : null) />
+ <input class="custom-control-input" id="@(controlId)_@(attributeValue.Id)" type="checkbox" name="@(controlId)" value="@(attributeValue.Id)" checked="@(attributeValue.IsPreSelected)" @readonlyAttr/>
<span class="custom-control-label"></span>
<span class="custom-control-description">@attributeValue.Name</span>
</label>
@@ -71,14 +71,14 @@
{
<label for="@(controlId)" class="col-form-label">@textPrompt:</label>
<input name="@(controlId)" type="text" class="form-control textbox" id="@(controlId)" value="@(attribute.DefaultValue)"
- v-validate="'@required'" data-val-required="@(string.Format(Loc["Fields.IsRequired"], textPrompt))" />
+ v-validate="'@required'" data-val-required="@(string.Format(Loc["Fields.IsRequired"], textPrompt))" @readonlyAttr />
<span class="field-validation-error">{{veeErrors.first('@(controlId)')}}</span>
}
break;
case AttributeControlType.MultilineTextbox:
{
<label for="@(controlId)" class="col-form-label">@textPrompt:</label>
- <textarea class="form-control" id="@(controlId)" name="@(controlId)"
+ <textarea class="form-control" id="@(controlId)" name="@(controlId)" @readonlyAttr
v-validate="'@required'" data-val-required="@(string.Format(Loc["Fields.IsRequired"], textPrompt))">@(attribute.DefaultValue)</textarea>
<span class="field-validation-error">{{veeErrors.first('@(controlId)')}}</span>
}