Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Grand.Domain.Catalog;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Extensions;
using MediatR;

Expand Down Expand Up @@ -42,7 +43,7 @@ public ProductAttributeService(ICacheBase cacheBase,
private readonly IRepository<Product> _productRepository;
private readonly IMediator _mediator;
private readonly ICacheBase _cacheBase;

#endregion

#region Methods
Expand All @@ -52,20 +53,31 @@ public ProductAttributeService(ICacheBase cacheBase,
/// <summary>
/// Gets all product attributes
/// </summary>
/// <param name="storeId">Store ident</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Product attributes</returns>
public virtual async Task<IPagedList<ProductAttribute>> GetAllProductAttributes(int pageIndex = 0,
public virtual async Task<IPagedList<ProductAttribute>> GetAllProductAttributes(string storeId = "", int pageIndex = 0,
int pageSize = int.MaxValue)
{
var key = string.Format(CacheKey.PRODUCTATTRIBUTES_ALL_KEY, pageIndex, pageSize);
var key = string.Format(CacheKey.PRODUCTATTRIBUTES_ALL_KEY, storeId, pageIndex, pageSize);
return await _cacheBase.GetAsync(key, () =>
{
var query = from pa in _productAttributeRepository.Table
orderby pa.Name
select pa;

if (!string.IsNullOrEmpty(storeId))
//Limited to stores rules
query = from p in query
where !p.LimitedToStores || p.Stores.Contains(storeId)
select p;

query = query.OrderBy(pa => pa.Name);

return Task.FromResult(new PagedList<ProductAttribute>(query, pageIndex, pageSize));
});


}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public virtual async Task<IPagedList<Product>> GetProductsByDiscount(string disc
int pageSize = int.MaxValue)
{
var query = from c in _productRepository.Table
where c.AppliedDiscounts.Any(x => x == discountId)
select c;
where c.AppliedDiscounts.Any(x => x == discountId)
select c;

return await PagedList<Product>.Create(query, pageIndex, pageSize);
}
Expand Down Expand Up @@ -422,7 +422,7 @@ public virtual int GetCategoryProductNumber(Customer customer, IList<string> cat
categoryIds.Remove("");

var query = from p in _productRepository.Table
select p;
select p;

query = query.Where(p => p.Published && p.VisibleIndividually);

Expand All @@ -435,15 +435,15 @@ public virtual int GetCategoryProductNumber(Customer customer, IList<string> cat
//ACL (access control list)
var allowedCustomerGroupsIds = customer.GetCustomerGroupIds();
query = from p in query
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
select p;
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
select p;
}

if (!string.IsNullOrEmpty(storeId) && !ignoreStore)
//Limited to stores rules
query = from p in query
where !p.LimitedToStores || p.Stores.Contains(storeId)
select p;
where !p.LimitedToStores || p.Stores.Contains(storeId)
select p;

return Convert.ToInt32(query.Count());
}
Expand Down Expand Up @@ -561,15 +561,20 @@ public virtual int GetCategoryProductNumber(Customer customer, IList<string> cat
/// Gets products by product attribute
/// </summary>
/// <param name="productAttributeId">Product attribute identifier</param>
/// <param name="storeId">Store ident</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Products</returns>
public virtual async Task<IPagedList<Product>> GetProductsByProductAttributeId(string productAttributeId,
public virtual async Task<IPagedList<Product>> GetProductsByProductAttributeId(string productAttributeId, string storeId = "",
int pageIndex = 0, int pageSize = int.MaxValue)
{
var query = from p in _productRepository.Table
select p;
select p;
query = query.Where(x => x.ProductAttributeMappings.Any(y => y.ProductAttributeId == productAttributeId));

if (!string.IsNullOrEmpty(storeId))
query = query.Where(x => x.LimitedToStores || x.Stores.Contains(storeId));

query = query.OrderBy(x => x.Name);

return await PagedList<Product>.Create(query, pageIndex, pageSize);
Expand All @@ -587,7 +592,7 @@ public virtual async Task<IList<Product>> GetAssociatedProducts(string parentGro
string storeId = "", string vendorId = "", bool showHidden = false)
{
var query = from p in _productRepository.Table
select p;
select p;

query = query.Where(p => p.ParentGroupedProductId == parentGroupedProductId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Grand.Domain.Catalog;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Extensions;
using MediatR;

Expand Down Expand Up @@ -79,15 +80,24 @@ await Task.FromResult(_specificationAttributeRepository.Table
/// <summary>
/// Gets specification attributes
/// </summary>
/// <param name="storeId">Store ident</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Specification attributes</returns>
public virtual async Task<IPagedList<SpecificationAttribute>> GetSpecificationAttributes(int pageIndex = 0,
public virtual async Task<IPagedList<SpecificationAttribute>> GetSpecificationAttributes(string storeId = "", int pageIndex = 0,
int pageSize = int.MaxValue)
{
var query = from sa in _specificationAttributeRepository.Table
orderby sa.DisplayOrder
select sa;
select sa;

if (!string.IsNullOrEmpty(storeId))
//Limited to stores rules
query = from p in query
where !p.LimitedToStores || p.Stores.Contains(storeId)
select p;

query = query.OrderBy(sa => sa.DisplayOrder).ThenBy(sa => sa.Name);

return await PagedList<SpecificationAttribute>.Create(query, pageIndex, pageSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public interface IProductAttributeService
/// <summary>
/// Gets all product attributes
/// </summary>
/// <param name="storeId">Store ident</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Product attributes</returns>
Task<IPagedList<ProductAttribute>> GetAllProductAttributes(int pageIndex = 0, int pageSize = int.MaxValue);
Task<IPagedList<ProductAttribute>> GetAllProductAttributes(string storeId = "", int pageIndex = 0, int pageSize = int.MaxValue);

/// <summary>
/// Gets a product attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ int GetCategoryProductNumber(Customer customer, IList<string> categoryIds = null
/// Gets products by product attribute
/// </summary>
/// <param name="productAttributeId">Product attribute identifier</param>
/// <param name="storeId">Store ident</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Products</returns>
Task<IPagedList<Product>> GetProductsByProductAttributeId(string productAttributeId,
Task<IPagedList<Product>> GetProductsByProductAttributeId(string productAttributeId, string storeId = "",
int pageIndex = 0, int pageSize = int.MaxValue);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public interface ISpecificationAttributeService
/// <summary>
/// Gets specification attributes
/// </summary>
/// <param name="storeId">Store ident</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Specification attributes</returns>
Task<IPagedList<SpecificationAttribute>> GetSpecificationAttributes(int pageIndex = 0, int pageSize = int.MaxValue);
Task<IPagedList<SpecificationAttribute>> GetSpecificationAttributes(string storeId = "", int pageIndex = 0, int pageSize = int.MaxValue);

/// <summary>
/// Inserts a specification attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ public static partial class CacheKey
/// Key for caching
/// </summary>
/// <remarks>
/// {0} : page index
/// {1} : page size
/// {0} : store ID
/// {1} : page index
/// {2} : page size
/// </remarks>
public static string PRODUCTATTRIBUTES_ALL_KEY => "Grand.productattribute.all-{0}-{1}";
public static string PRODUCTATTRIBUTES_ALL_KEY => "Grand.productattribute.all-{0}-{1}-{2}";

/// <summary>
/// Key for caching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ public static IEnumerable<DefaultPermission> DefaultPermissions()
Permissions = [
StandardPermission.ManageAccessStoreManagerPanel,
StandardPermission.ManageProducts,
StandardPermission.ManageProductAttributes,
StandardPermission.ManageSpecificationAttributes,
StandardPermission.ManageFiles,
StandardPermission.ManagePictures,
StandardPermission.ManageCategories,
Expand Down
13 changes: 3 additions & 10 deletions src/Web/Grand.Web.Admin/Controllers/ProductAttributeController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Permissions;
using Grand.Domain.Seo;
Expand All @@ -25,15 +24,13 @@ public ProductAttributeController(
ILanguageService languageService,
ITranslationService translationService,
IContextAccessor contextAccessor,
IGroupService groupService,
SeoSettings seoSettings)
{
_productService = productService;
_productAttributeService = productAttributeService;
_languageService = languageService;
_translationService = translationService;
_contextAccessor = contextAccessor;
_groupService = groupService;
_seoSettings = seoSettings;
}

Expand All @@ -46,7 +43,6 @@ public ProductAttributeController(
private readonly ILanguageService _languageService;
private readonly ITranslationService _translationService;
private readonly IContextAccessor _contextAccessor;
private readonly IGroupService _groupService;
private readonly SeoSettings _seoSettings;

#endregion Fields
Expand All @@ -70,8 +66,7 @@ public IActionResult List()
[HttpPost]
public async Task<IActionResult> List(DataSourceRequest command)
{
var productAttributes = await _productAttributeService
.GetAllProductAttributes(command.Page - 1, command.PageSize);
var productAttributes = await _productAttributeService.GetAllProductAttributes(pageIndex: command.Page - 1, pageSize: command.PageSize);
var gridModel = new DataSourceResult {
Data = productAttributes.Select(x => x.ToModel()),
Total = productAttributes.TotalCount
Expand Down Expand Up @@ -102,8 +97,6 @@ public async Task<IActionResult> Create(ProductAttributeModel model, bool contin
string.IsNullOrEmpty(productAttribute.SeName) ? productAttribute.Name : productAttribute.SeName,
_seoSettings.ConvertNonWesternChars, _seoSettings.AllowUnicodeCharsInUrls,
_seoSettings.SeoCharConversion);
if (await _groupService.IsStoreManager(_contextAccessor.WorkContext.CurrentCustomer))
model.Stores = [_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId];

await _productAttributeService.InsertProductAttribute(productAttribute);

Expand Down Expand Up @@ -154,8 +147,7 @@ public async Task<IActionResult> Edit(ProductAttributeModel model, bool continue
string.IsNullOrEmpty(productAttribute.SeName) ? productAttribute.Name : productAttribute.SeName,
_seoSettings.ConvertNonWesternChars, _seoSettings.AllowUnicodeCharsInUrls,
_seoSettings.SeoCharConversion);
if (await _groupService.IsStoreManager(_contextAccessor.WorkContext.CurrentCustomer))
model.Stores = [_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId];

await _productAttributeService.UpdateProductAttribute(productAttribute);

Success(_translationService.GetResource("Admin.Catalog.Attributes.ProductAttributes.Updated"));
Expand Down Expand Up @@ -212,6 +204,7 @@ public async Task<IActionResult> UsedByProducts(DataSourceRequest command, strin
{
var orders = await _productService.GetProductsByProductAttributeId(
productAttributeId,
"",
command.Page - 1,
command.PageSize);
var gridModel = new DataSourceResult {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Permissions;
using Grand.Domain.Seo;
Expand All @@ -24,15 +23,13 @@ public SpecificationAttributeController(
ILanguageService languageService,
ITranslationService translationService,
IContextAccessor contextAccessor,
IGroupService groupService,
IProductService productService,
SeoSettings seoSettings)
{
_specificationAttributeService = specificationAttributeService;
_languageService = languageService;
_translationService = translationService;
_contextAccessor = contextAccessor;
_groupService = groupService;
_productService = productService;
_seoSettings = seoSettings;
}
Expand All @@ -53,10 +50,6 @@ public async Task<IActionResult> UsedByProducts(DataSourceRequest command, strin

var searchStoreId = string.Empty;

//limit for store manager
if (!string.IsNullOrEmpty(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))
searchStoreId = _contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;

var specificationProducts = new List<SpecificationAttributeModel.UsedByProductModel>();
var total = 0;

Expand Down Expand Up @@ -104,7 +97,6 @@ public async Task<IActionResult> UsedByProducts(DataSourceRequest command, strin
private readonly ILanguageService _languageService;
private readonly ITranslationService _translationService;
private readonly IContextAccessor _contextAccessor;
private readonly IGroupService _groupService;
private readonly SeoSettings _seoSettings;

#endregion Fields
Expand All @@ -126,8 +118,7 @@ public IActionResult List()
[PermissionAuthorizeAction(PermissionActionName.List)]
public async Task<IActionResult> List(DataSourceRequest command)
{
var specificationAttributes = await _specificationAttributeService
.GetSpecificationAttributes(command.Page - 1, command.PageSize);
var specificationAttributes = await _specificationAttributeService.GetSpecificationAttributes(pageIndex: command.Page - 1, pageSize: command.PageSize);
var gridModel = new DataSourceResult {
Data = specificationAttributes.Select(x => x.ToModel()),
Total = specificationAttributes.TotalCount
Expand Down Expand Up @@ -160,8 +151,7 @@ public async Task<IActionResult> Create(SpecificationAttributeModel model, bool
? specificationAttribute.Name
: specificationAttribute.SeName, _seoSettings.ConvertNonWesternChars,
_seoSettings.AllowUnicodeCharsInUrls, _seoSettings.SeoCharConversion);
if (await _groupService.IsStoreManager(_contextAccessor.WorkContext.CurrentCustomer))
model.Stores = [_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId];

await _specificationAttributeService.InsertSpecificationAttribute(specificationAttribute);

Success(_translationService.GetResource("Admin.Catalog.Attributes.SpecificationAttributes.Added"));
Expand Down Expand Up @@ -211,8 +201,7 @@ public async Task<IActionResult> Edit(SpecificationAttributeModel model, bool co
? specificationAttribute.Name
: specificationAttribute.SeName, _seoSettings.ConvertNonWesternChars,
_seoSettings.AllowUnicodeCharsInUrls, _seoSettings.SeoCharConversion);
if (await _groupService.IsStoreManager(_contextAccessor.WorkContext.CurrentCustomer))
model.Stores = [_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId];

await _specificationAttributeService.UpdateSpecificationAttribute(specificationAttribute);

Success(_translationService.GetResource("Admin.Catalog.Attributes.SpecificationAttributes.Updated"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public virtual async Task PrepareProductModel(ProductModel model, Product produc

model.AutoAddRequiredProducts = product.AutoAddRequiredProducts;
//product attributes
foreach (var productAttribute in await productAttributeService.GetAllProductAttributes())
foreach (var productAttribute in await productAttributeService.GetAllProductAttributes(contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))
model.AvailableProductAttributes.Add(new SelectListItem {
Text = productAttribute.Name,
Value = productAttribute.Id
Expand Down Expand Up @@ -1372,7 +1372,7 @@ public virtual async Task DeleteBulkEdit(IEnumerable<BulkEditProductModel> produ
var model = new ProductModel.ProductAttributeMappingModel {
ProductId = product.Id
};
foreach (var attribute in await productAttributeService.GetAllProductAttributes())
foreach (var attribute in await productAttributeService.GetAllProductAttributes(contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))
model.AvailableProductAttribute.Add(new SelectListItem {
Value = attribute.Id,
Text = attribute.Name
Expand All @@ -1384,7 +1384,7 @@ public virtual async Task DeleteBulkEdit(IEnumerable<BulkEditProductModel> produ
Product product, ProductAttributeMapping productAttributeMapping)
{
var model = productAttributeMapping.ToModel();
foreach (var attribute in await productAttributeService.GetAllProductAttributes())
foreach (var attribute in await productAttributeService.GetAllProductAttributes(contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))
model.AvailableProductAttribute.Add(new SelectListItem {
Value = attribute.Id,
Text = attribute.Name,
Expand All @@ -1396,7 +1396,7 @@ public virtual async Task DeleteBulkEdit(IEnumerable<BulkEditProductModel> produ
public virtual async Task<ProductModel.ProductAttributeMappingModel> PrepareProductAttributeMappingModel(
ProductModel.ProductAttributeMappingModel model)
{
foreach (var attribute in await productAttributeService.GetAllProductAttributes())
foreach (var attribute in await productAttributeService.GetAllProductAttributes(contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))
model.AvailableProductAttribute.Add(new SelectListItem {
Value = attribute.Id,
Text = attribute.Name
Expand Down
Loading