Try your search with a different keyword or use * as a wildcard.
using Microsoft.AspNetCore.Mvc.Rendering;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Discounts;
using Nop.Services.Catalog;
using Nop.Services.Directory;
using Nop.Services.Discounts;
using Nop.Services.Localization;
using Nop.Services.Seo;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Framework.Extensions;
using Nop.Web.Framework.Factories;
using Nop.Web.Framework.Models.Extensions;
namespace Nop.Web.Areas.Admin.Factories;
///
/// Represents the category model factory implementation
///
public partial class CategoryModelFactory : ICategoryModelFactory
{
#region Fields
protected readonly CatalogSettings _catalogSettings;
protected readonly CurrencySettings _currencySettings;
protected readonly ICurrencyService _currencyService;
protected readonly IAclSupportedModelFactory _aclSupportedModelFactory;
protected readonly IBaseAdminModelFactory _baseAdminModelFactory;
protected readonly ICategoryService _categoryService;
protected readonly IDiscountService _discountService;
protected readonly IDiscountSupportedModelFactory _discountSupportedModelFactory;
protected readonly ILocalizationService _localizationService;
protected readonly ILocalizedModelFactory _localizedModelFactory;
protected readonly IProductService _productService;
protected readonly IStoreMappingSupportedModelFactory _storeMappingSupportedModelFactory;
protected readonly IUrlRecordService _urlRecordService;
#endregion
#region Ctor
public CategoryModelFactory(CatalogSettings catalogSettings,
CurrencySettings currencySettings,
ICurrencyService currencyService,
IAclSupportedModelFactory aclSupportedModelFactory,
IBaseAdminModelFactory baseAdminModelFactory,
ICategoryService categoryService,
IDiscountService discountService,
IDiscountSupportedModelFactory discountSupportedModelFactory,
ILocalizationService localizationService,
ILocalizedModelFactory localizedModelFactory,
IProductService productService,
IStoreMappingSupportedModelFactory storeMappingSupportedModelFactory,
IUrlRecordService urlRecordService)
{
_catalogSettings = catalogSettings;
_currencySettings = currencySettings;
_currencyService = currencyService;
_aclSupportedModelFactory = aclSupportedModelFactory;
_baseAdminModelFactory = baseAdminModelFactory;
_categoryService = categoryService;
_discountService = discountService;
_discountSupportedModelFactory = discountSupportedModelFactory;
_localizationService = localizationService;
_localizedModelFactory = localizedModelFactory;
_productService = productService;
_storeMappingSupportedModelFactory = storeMappingSupportedModelFactory;
_urlRecordService = urlRecordService;
}
#endregion
#region Utilities
///
/// Prepare category product search model
///
/// Category product search model
/// Category
/// Category product search model
protected virtual CategoryProductSearchModel PrepareCategoryProductSearchModel(CategoryProductSearchModel searchModel, Category category)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(category);
searchModel.CategoryId = category.Id;
//prepare page parameters
searchModel.SetGridPageSize();
return searchModel;
}
#endregion
#region Methods
///
/// Prepare category search model
///
/// Category search model
///
/// A task that represents the asynchronous operation
/// The task result contains the category search model
///
public virtual async Task PrepareCategorySearchModelAsync(CategorySearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//prepare available stores
await _baseAdminModelFactory.PrepareStoresAsync(searchModel.AvailableStores);
searchModel.HideStoresList = _catalogSettings.IgnoreStoreLimitations || searchModel.AvailableStores.SelectionIsNotPossible();
//prepare "published" filter (0 - all; 1 - published only; 2 - unpublished only)
searchModel.AvailablePublishedOptions.Add(new SelectListItem
{
Value = "0",
Text = await _localizationService.GetResourceAsync("Admin.Catalog.Categories.List.SearchPublished.All")
});
searchModel.AvailablePublishedOptions.Add(new SelectListItem
{
Value = "1",
Text = await _localizationService.GetResourceAsync("Admin.Catalog.Categories.List.SearchPublished.PublishedOnly")
});
searchModel.AvailablePublishedOptions.Add(new SelectListItem
{
Value = "2",
Text = await _localizationService.GetResourceAsync("Admin.Catalog.Categories.List.SearchPublished.UnpublishedOnly")
});
//prepare page parameters
searchModel.SetGridPageSize();
return searchModel;
}
///
/// Prepare paged category list model
///
/// Category search model
///
/// A task that represents the asynchronous operation
/// The task result contains the category list model
///
public virtual async Task PrepareCategoryListModelAsync(CategorySearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//get categories
var categories = await _categoryService.GetAllCategoriesAsync(categoryName: searchModel.SearchCategoryName,
showHidden: true,
storeId: searchModel.SearchStoreId,
pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize,
overridePublished: searchModel.SearchPublishedId == 0 ? null : (bool?)(searchModel.SearchPublishedId == 1));
//prepare grid model
var model = await new CategoryListModel().PrepareToGridAsync(searchModel, categories, () =>
{
return categories.SelectAwait(async category =>
{
//fill in model values from the entity
var categoryModel = category.ToModel();
//fill in additional values (not existing in the entity)
categoryModel.Breadcrumb = await _categoryService.GetFormattedBreadCrumbAsync(category);
categoryModel.SeName = await _urlRecordService.GetSeNameAsync(category, 0, true, false);
return categoryModel;
});
});
return model;
}
///
/// Prepare category model
///
/// Category model
/// Category
/// Whether to exclude populating of some properties of model
///
/// A task that represents the asynchronous operation
/// The task result contains the category model
///
public virtual async Task PrepareCategoryModelAsync(CategoryModel model, Category category, bool excludeProperties = false)
{
Func localizedModelConfiguration = null;
if (category != null)
{
//fill in model values from the entity
if (model == null)
{
model = category.ToModel();
model.SeName = await _urlRecordService.GetSeNameAsync(category, 0, true, false);
}
//prepare nested search model
PrepareCategoryProductSearchModel(model.CategoryProductSearchModel, category);
//define localized model configuration action
localizedModelConfiguration = async (locale, languageId) =>
{
locale.Name = await _localizationService.GetLocalizedAsync(category, entity => entity.Name, languageId, false, false);
locale.Description = await _localizationService.GetLocalizedAsync(category, entity => entity.Description, languageId, false, false);
locale.MetaKeywords = await _localizationService.GetLocalizedAsync(category, entity => entity.MetaKeywords, languageId, false, false);
locale.MetaDescription = await _localizationService.GetLocalizedAsync(category, entity => entity.MetaDescription, languageId, false, false);
locale.MetaTitle = await _localizationService.GetLocalizedAsync(category, entity => entity.MetaTitle, languageId, false, false);
locale.SeName = await _urlRecordService.GetSeNameAsync(category, languageId, false, false);
};
}
//set default values for the new model
if (category == null)
{
model.PageSize = _catalogSettings.DefaultCategoryPageSize;
model.PageSizeOptions = _catalogSettings.DefaultCategoryPageSizeOptions;
model.Published = true;
model.IncludeInTopMenu = true;
model.AllowCustomersToSelectPageSize = true;
model.PriceRangeFiltering = true;
model.ManuallyPriceRange = true;
model.PriceFrom = NopCatalogDefaults.DefaultPriceRangeFrom;
model.PriceTo = NopCatalogDefaults.DefaultPriceRangeTo;
}
model.PrimaryStoreCurrencyCode = (await _currencyService.GetCurrencyByIdAsync(_currencySettings.PrimaryStoreCurrencyId)).CurrencyCode;
//prepare localized models
if (!excludeProperties)
model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);
//prepare available category templates
await _baseAdminModelFactory.PrepareCategoryTemplatesAsync(model.AvailableCategoryTemplates, false);
//prepare available parent categories
await _baseAdminModelFactory.PrepareCategoriesAsync(model.AvailableCategories,
defaultItemText: await _localizationService.GetResourceAsync("Admin.Catalog.Categories.Fields.Parent.None"));
//prepare model discounts
var availableDiscounts = await _discountService.GetAllDiscountsAsync(DiscountType.AssignedToCategories, showHidden: true, isActive: null);
await _discountSupportedModelFactory.PrepareModelDiscountsAsync(model, category, availableDiscounts, excludeProperties);
//prepare model customer roles
await _aclSupportedModelFactory.PrepareModelCustomerRolesAsync(model, category, excludeProperties);
//prepare model stores
await _storeMappingSupportedModelFactory.PrepareModelStoresAsync(model, category, excludeProperties);
return model;
}
///
/// Prepare paged category product list model
///
/// Category product search model
/// Category
///
/// A task that represents the asynchronous operation
/// The task result contains the category product list model
///
public virtual async Task PrepareCategoryProductListModelAsync(CategoryProductSearchModel searchModel, Category category)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(category);
//get product categories
var productCategories = await _categoryService.GetProductCategoriesByCategoryIdAsync(category.Id,
showHidden: true,
pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);
//prepare grid model
var model = await new CategoryProductListModel().PrepareToGridAsync(searchModel, productCategories, () =>
{
return productCategories.SelectAwait(async productCategory =>
{
//fill in model values from the entity
var categoryProductModel = productCategory.ToModel();
//fill in additional values (not existing in the entity)
categoryProductModel.ProductName = (await _productService.GetProductByIdAsync(productCategory.ProductId))?.Name;
return categoryProductModel;
});
});
return model;
}
///
/// Prepare product search model to add to the category
///
/// Product search model to add to the category
///
/// A task that represents the asynchronous operation
/// The task result contains the product search model to add to the category
///
public virtual async Task PrepareAddProductToCategorySearchModelAsync(AddProductToCategorySearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//prepare available categories
await _baseAdminModelFactory.PrepareCategoriesAsync(searchModel.AvailableCategories);
//prepare available manufacturers
await _baseAdminModelFactory.PrepareManufacturersAsync(searchModel.AvailableManufacturers);
//prepare available stores
await _baseAdminModelFactory.PrepareStoresAsync(searchModel.AvailableStores);
//prepare available vendors
await _baseAdminModelFactory.PrepareVendorsAsync(searchModel.AvailableVendors);
//prepare available product types
await _baseAdminModelFactory.PrepareProductTypesAsync(searchModel.AvailableProductTypes);
//prepare page parameters
searchModel.SetPopupGridPageSize();
return searchModel;
}
///
/// Prepare paged product list model to add to the category
///
/// Product search model to add to the category
///
/// A task that represents the asynchronous operation
/// The task result contains the product list model to add to the category
///
public virtual async Task PrepareAddProductToCategoryListModelAsync(AddProductToCategorySearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//get products
var products = await _productService.SearchProductsAsync(showHidden: true,
categoryIds: new List { searchModel.SearchCategoryId },
manufacturerIds: new List { searchModel.SearchManufacturerId },
storeId: searchModel.SearchStoreId,
vendorId: searchModel.SearchVendorId,
productType: searchModel.SearchProductTypeId > 0 ? (ProductType?)searchModel.SearchProductTypeId : null,
keywords: searchModel.SearchProductName,
pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);
//prepare grid model
var model = await new AddProductToCategoryListModel().PrepareToGridAsync(searchModel, products, () =>
{
return products.SelectAwait(async product =>
{
var productModel = product.ToModel();
productModel.SeName = await _urlRecordService.GetSeNameAsync(product, 0, true, false);
return productModel;
});
});
return model;
}
#endregion
}