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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Improvement - Brand picture - use localizable alternate text and title attributes for a picture
KrzysztofPajak53c8070f7d59
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Brand/PicturePopup.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Brand/PicturePopup.cshtmlnew file mode 100644index 000000000..b179291ce--- /dev/null+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Brand/PicturePopup.cshtml@@ -0,0 +1,87 @@+@using Microsoft.AspNetCore.Mvc.Razor;++@model BrandModel.PictureModel+@{+ Layout = Grand.Web.Admin.Extensions.Constants.Layout_AdminPopup;+ //page title+ ViewBag.Title = Loc["Admin.Catalog.Brands.Picture"];+}+<form asp-area="@Constants.AreaAdmin" asp-controller="Brand" asp-action="PicturePopup" enctype="multipart/form-data" method="post"+ asp-route-BrandId="@Context.Request.Query["BrandId"]">+++ <!-- #region languages template -->+ @{ Func<int, HelperResult> template = @<div class="form-body">+ <div class="form-group">+ <admin-label asp-for="@Model.Locales[item].AltAttribute" class="col-sm-3 control-label" />+ <div class="col-md-9 col-sm-9">+ <admin-input asp-for="@Model.Locales[item].AltAttribute" />+ <span asp-validation-for="@Model.Locales[item].AltAttribute"></span>+ </div>+ </div>+ <div class="form-group">+ <admin-label asp-for="@Model.Locales[item].TitleAttribute" class="col-sm-3 control-label" />+ <div class="col-md-9 col-sm-9">+ <admin-input asp-for="@Model.Locales[item].TitleAttribute" />+ <span asp-validation-for="@Model.Locales[item].TitleAttribute"></span>+ </div>+ </div>+ <input asp-for="@Model.Locales[item].LanguageId" type="hidden" />+ </div>;+ }++ <div asp-validation-summary="All"></div>+ <input asp-for="BrandId" type="hidden" />+ <input asp-for="Id" type="hidden" />+ <input asp-for="PictureUrl" type="hidden" />+ <div class="row">+ <div class="col-md-12">+ <div class="x_panel light form-fit">+ <div class="x_content form">+ <div class="form-horizontal">+ <div class="form-body">+ <div class="form-group">+ <admin-label asp-for="PictureUrl" />+ <div class="col-md-9 col-sm-9">+ <a href="@Model.PictureUrl" target="_blank"><img src="@Model.PictureUrl" width="150" /></a>+ </div>+ </div>+ <localized-editor localized-template=template name="picturevalue-localized" [email protected](c=>c.LanguageId).ToList()>+ <div class="form-body">+ <div class="form-group">+ <admin-label asp-for="AltAttribute" class="col-sm-3 control-label" />+ <div class="col-md-9 col-sm-9">+ <admin-input asp-for="AltAttribute" />+ <span asp-validation-for="AltAttribute"></span>+ </div>+ </div>+ <div class="form-group">+ <admin-label asp-for="TitleAttribute" class="col-sm-3 control-label" />+ <div class="col-md-9 col-sm-9">+ <admin-input asp-for="TitleAttribute" />+ <span asp-validation-for="TitleAttribute"></span>+ </div>+ </div>+ </div>+ </localized-editor>+ <div class="form-actions">+ <div class="row">+ <div class="offset-md-3 offset-sm-3 col-md-9 col-sm-9">+ <button class="k-button" type="submit" name="save"><i class="fa fa-check"></i>@Loc["Admin.Common.Save"] </button>+ </div>+ </div>+ </div>++ </div>+ </div>+ </div>+ </div>+ </div>+ </div>+ @if (ViewBag.RefreshPage == true)+ {+ <script>+ window.close();+ </script>+ }+</form>\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Brand/_CreateOrUpdate.TabInfo.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Brand/_CreateOrUpdate.TabInfo.cshtmlindex 4f4b27501..1bb862307 100644--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Brand/_CreateOrUpdate.TabInfo.cshtml+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Brand/_CreateOrUpdate.TabInfo.cshtml@@ -102,6 +102,12 @@}<admin-input asp-for="PictureId" /><span asp-validation-for="PictureId"></span>+ @if (!string.IsNullOrEmpty(Model.PictureId))+ {+ <label class="control-label">+ <a class='k-link' href='javascript:OpenWindow("@Url.Action("PicturePopup", "Brand", new { area = Constants.AreaAdmin })/?brandId=@(Model.Id)", 800, 600, true);'>@Loc["Admin.Common.Picture.Attributes"]</a>+ </label>+ }</div></div><div class="form-group">diff --git a/src/Web/Grand.Web.Admin/Controllers/BrandController.cs b/src/Web/Grand.Web.Admin/Controllers/BrandController.csindex 11a877d12..140db0ef3 100644--- a/src/Web/Grand.Web.Admin/Controllers/BrandController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/BrandController.cs@@ -65,7 +65,7 @@ namespace Grand.Web.Admin.Controllers#region Utilities- protected async Task<(bool allow, string message)> CheckAccessToCollection(Brand brand)+ protected async Task<(bool allow, string message)> CheckAccessToBrand(Brand brand){if (brand == null){@@ -281,6 +281,59 @@ namespace Grand.Web.Admin.Controllers#endregion+ #region Picture++ [PermissionAuthorizeAction(PermissionActionName.Preview)]+ public async Task<IActionResult> PicturePopup(string brandId)+ {+ var brand = await _brandService.GetBrandById(brandId);+ if (brand == null)+ return Content("Brand not exist");++ if (string.IsNullOrEmpty(brand.PictureId))+ return Content("Picture not exist");++ var permission = await CheckAccessToBrand(brand);+ if (!permission.allow)+ return Content(permission.message);++ var (model, picture) = await _brandViewModelService.PreparePictureModel(brand);+ //locales+ await AddLocales(_languageService, model.Locales, (locale, languageId) =>+ {+ locale.AltAttribute = picture?.GetTranslation(x => x.AltAttribute, languageId, false);+ locale.TitleAttribute = picture?.GetTranslation(x => x.TitleAttribute, languageId, false);+ });++ return View(model);+ }++ [PermissionAuthorizeAction(PermissionActionName.Edit)]+ [HttpPost]+ public async Task<IActionResult> PicturePopup(BrandModel.PictureModel model)+ {+ if (ModelState.IsValid)+ {+ var brand = await _brandService.GetBrandById(model.BrandId);+ if (brand == null)+ throw new ArgumentException("No brand found with the specified id");++ if (string.IsNullOrEmpty(brand.PictureId))+ throw new ArgumentException("No picture found with the specified id");++ await _brandViewModelService.UpdateBrandPicture(model);++ ViewBag.RefreshPage = true;+ return View(model);+ }++ Error(ModelState);++ return View(model);+ }++ #endregion+#region Export / Import[PermissionAuthorizeAction(PermissionActionName.Export)]@@ -335,7 +388,7 @@ namespace Grand.Web.Admin.Controllerspublic async Task<IActionResult> ListActivityLog(DataSourceRequest command, string brandId){var brand = await _brandService.GetBrandById(brandId);- var permission = await CheckAccessToCollection(brand);+ var permission = await CheckAccessToBrand(brand);if (!permission.allow)return ErrorForKendoGridJson(permission.message);diff --git a/src/Web/Grand.Web.Admin/Interfaces/IBrandViewModelService.cs b/src/Web/Grand.Web.Admin/Interfaces/IBrandViewModelService.csindex ddd4082a5..f7781aae2 100644--- a/src/Web/Grand.Web.Admin/Interfaces/IBrandViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Interfaces/IBrandViewModelService.cs@@ -1,4 +1,5 @@using Grand.Domain.Catalog;+using Grand.Domain.Media;using Grand.Web.Admin.Models.Catalog;using System.Collections.Generic;using System.Threading.Tasks;@@ -9,10 +10,12 @@ namespace Grand.Web.Admin.Interfaces{void PrepareSortOptionsModel(BrandModel model);Task PrepareLayoutsModel(BrandModel model);- Task PrepareDiscountModel(BrandModel model, Brand Brand, bool excludeProperties);+ Task PrepareDiscountModel(BrandModel model, Brand brand, bool excludeProperties);Task<Brand> InsertBrandModel(BrandModel model);- Task<Brand> UpdateBrandModel(Brand Brand, BrandModel model);- Task DeleteBrand(Brand Brand);- Task<(IEnumerable<BrandModel.ActivityLogModel> activityLogModels, int totalCount)> PrepareActivityLogModel(string BrandId, int pageIndex, int pageSize);+ Task<Brand> UpdateBrandModel(Brand brand, BrandModel model);+ Task DeleteBrand(Brand brand);+ Task<(BrandModel.PictureModel model, Picture Picture)> PreparePictureModel(Brand brand);+ Task UpdateBrandPicture(BrandModel.PictureModel model);+ Task<(IEnumerable<BrandModel.ActivityLogModel> activityLogModels, int totalCount)> PrepareActivityLogModel(string brandId, int pageIndex, int pageSize);}}diff --git a/src/Web/Grand.Web.Admin/Models/Catalog/BrandModel.cs b/src/Web/Grand.Web.Admin/Models/Catalog/BrandModel.csindex dd26293c3..7230a6356 100644--- a/src/Web/Grand.Web.Admin/Models/Catalog/BrandModel.cs+++ b/src/Web/Grand.Web.Admin/Models/Catalog/BrandModel.cs@@ -115,9 +115,41 @@ namespace Grand.Web.Admin.Models.Catalogpublic string CustomerEmail { get; set; }}+ public partial class PictureModel : BaseEntityModel, ILocalizedModel<PictureModel.PictureLocalizedModel>+ {+ public PictureModel()+ {+ Locales = new List<PictureLocalizedModel>();+ }++ public string BrandId { get; set; }+ [GrandResourceDisplayName("Admin.Catalog.Brands.Picture")]+ public string PictureUrl { get; set; }++ [GrandResourceDisplayName("Admin.Catalog.Brands.Picture.Fields.AltAttribute")]+ public string AltAttribute { get; set; }++ [GrandResourceDisplayName("Admin.Catalog.Brands.Picture.Fields.TitleAttribute")]+ public string TitleAttribute { get; set; }++ public IList<PictureLocalizedModel> Locales { get; set; }++ public partial class PictureLocalizedModel : ILocalizedModelLocal+ {+ public string LanguageId { get; set; }++ [GrandResourceDisplayName("Admin.Catalog.Brands.Picture.Fields.AltAttribute")]+ public string AltAttribute { get; set; }++ [GrandResourceDisplayName("Admin.Catalog.Brands.Picture.Fields.TitleAttribute")]+ public string TitleAttribute { get; set; }+ }+ }#endregion++}public partial class BrandLocalizedModel : ILocalizedModelLocal, ISlugModelLocaldiff --git a/src/Web/Grand.Web.Admin/Services/BrandViewModelService.cs b/src/Web/Grand.Web.Admin/Services/BrandViewModelService.csindex c7eb45cb6..a69dfdd44 100644--- a/src/Web/Grand.Web.Admin/Services/BrandViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/BrandViewModelService.cs@@ -9,6 +9,7 @@ using Grand.Business.Customers.Interfaces;using Grand.Business.Storage.Interfaces;using Grand.Domain.Catalog;using Grand.Domain.Discounts;+using Grand.Domain.Media;using Grand.Domain.Seo;using Grand.Infrastructure;using Grand.Web.Admin.Extensions;@@ -100,7 +101,7 @@ namespace Grand.Web.Admin.Services}- public virtual async Task PrepareDiscountModel(BrandModel model, Brand Brand, bool excludeProperties)+ public virtual async Task PrepareDiscountModel(BrandModel model, Brand brand, bool excludeProperties){if (model == null)throw new ArgumentNullException(nameof(model));@@ -110,9 +111,9 @@ namespace Grand.Web.Admin.Services.Select(d => d.ToModel(_dateTimeService)).ToList();- if (!excludeProperties && Brand != null)+ if (!excludeProperties && brand != null){- model.SelectedDiscountIds = Brand.AppliedDiscounts.ToArray();+ model.SelectedDiscountIds = brand.AppliedDiscounts.ToArray();}}@@ -197,6 +198,29 @@ namespace Grand.Web.Admin.Services//activity logawait _customerActivityService.InsertActivity("DeleteBrand", brand.Id, _translationService.GetResource("ActivityLog.DeleteBrand"), brand.Name);}+ public virtual async Task<(BrandModel.PictureModel model, Picture Picture)> PreparePictureModel(Brand brand)+ {+ var picture = await _pictureService.GetPictureById(brand.PictureId);+ var model = new BrandModel.PictureModel {+ Id = picture.Id,+ BrandId = brand.Id,+ PictureUrl = picture != null ? await _pictureService.GetPictureUrl(picture) : null,+ AltAttribute = picture?.AltAttribute,+ TitleAttribute = picture?.TitleAttribute,+ };+ return (model, picture);+ }+ public virtual async Task UpdateBrandPicture(BrandModel.PictureModel model)+ {+ var picture = await _pictureService.GetPictureById(model.Id);+ if (picture == null)+ throw new ArgumentException("No picture found with the specified id");++ //Update picture fields+ await _pictureService.UpdatField(picture, x => x.AltAttribute, model.AltAttribute);+ await _pictureService.UpdatField(picture, x => x.TitleAttribute, model.TitleAttribute);+ await _pictureService.UpdatField(picture, x => x.Locales, model.Locales.ToTranslationProperty());+ }public virtual async Task<(IEnumerable<BrandModel.ActivityLogModel> activityLogModels, int totalCount)> PrepareActivityLogModel(string brandId, int pageIndex, int pageSize){diff --git a/src/Web/Grand.Web.Admin/Services/CategoryViewModelService.cs b/src/Web/Grand.Web.Admin/Services/CategoryViewModelService.csindex a4a88b1e3..7a71492fd 100644--- a/src/Web/Grand.Web.Admin/Services/CategoryViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/CategoryViewModelService.cs@@ -270,8 +270,8 @@ namespace Grand.Web.Admin.Servicesawait _pictureService.UpdatField(picture, x => x.AltAttribute, model.AltAttribute);await _pictureService.UpdatField(picture, x => x.TitleAttribute, model.TitleAttribute);await _pictureService.UpdatField(picture, x => x.Locales, model.Locales.ToTranslationProperty());-}+public virtual async Task<(IEnumerable<CategoryModel.CategoryProductModel> categoryProductModels, int totalCount)> PrepareCategoryProductModel(string categoryId, int pageIndex, int pageSize){var productCategories = await _productCategoryService.GetProductCategoriesByCategoryId(categoryId,diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xmlindex c052bc2a2..8e1c346e5 100644--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml@@ -2079,6 +2079,15 @@<Resource Name="Admin.Catalog.Brands.Permisions"><Value>You can't edit this brand, because it can be used in many stores.</Value></Resource>+ <Resource Name="Admin.Catalog.Brands.Picture">+ <Value>Picture</Value>+ </Resource>+ <Resource Name="Admin.Catalog.Brands.Picture.Fields.AltAttribute">+ <Value>Picture alternate text</Value>+ </Resource>+ <Resource Name="Admin.Catalog.Brands.Picture.Fields.TitleAttribute">+ <Value>Picture title</Value>+ </Resource><Resource Name="Admin.Catalog.Brands.Updated"><Value>The brand has been updated successfully.</Value></Resource>@@ -14160,6 +14169,9 @@<Resource Name="Media.Brand.ImageAlternateTextFormat"><Value>Picture for brand {0}</Value></Resource>+ <Resource Name="Media.Brand.ImageLinkTitleFormat">+ <Value>Show brand for {0}</Value>+ </Resource><Resource Name="Media.Category.ImageAlternateTextFormat"><Value>Picture for category {0}</Value></Resource>diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.csindex 93df3dc56..3b35f18f4 100644--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.cs@@ -13,6 +13,7 @@ using MediatR;using System.Collections.Generic;using System.Threading;using System.Threading.Tasks;+using Grand.Business.Common.Extensions;namespace Grand.Web.Features.Handlers.Catalog{@@ -55,14 +56,22 @@ namespace Grand.Web.Features.Handlers.Catalogvar modelBrand = brand.ToModel(request.Language);//prepare picture model+ var picture = !string.IsNullOrEmpty(brand.PictureId) ? await _pictureService.GetPictureById(brand.PictureId) : null;modelBrand.PictureModel = new PictureModel{Id = brand.PictureId,FullSizeImageUrl = await _pictureService.GetPictureUrl(brand.PictureId),ImageUrl = await _pictureService.GetPictureUrl(brand.PictureId, _mediaSettings.BrandThumbPictureSize),- Title = string.Format(_translationService.GetResource("Media.Collection.ImageLinkTitleFormat"), brand.Name),- AlternateText = string.Format(_translationService.GetResource("Media.Collection.ImageAlternateTextFormat"), brand.Name)};+ //"title" attribute+ modelBrand.PictureModel.Title = (picture != null && !string.IsNullOrEmpty(picture.GetTranslation(x => x.TitleAttribute, request.Language.Id))) ?+ picture.GetTranslation(x => x.TitleAttribute, request.Language.Id) :+ string.Format(_translationService.GetResource("Media.Brand.ImageLinkTitleFormat"), brand.Name);+ //"alt" attribute+ modelBrand.PictureModel.AlternateText = (picture != null && !string.IsNullOrEmpty(picture.GetTranslation(x => x.AltAttribute, request.Language.Id))) ?+ picture.GetTranslation(x => x.AltAttribute, request.Language.Id) :+ string.Format(_translationService.GetResource("Media.Brand.ImageAlternateTextFormat"), brand.Name);+model.Add(modelBrand);}return model;diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryFeaturedProductsHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryFeaturedProductsHandler.csindex 201b41528..ae5ef0831 100644--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryFeaturedProductsHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryFeaturedProductsHandler.cs@@ -63,7 +63,7 @@ namespace Grand.Web.Features.Handlers.Catalog{var catModel = x.ToModel(request.Language);//prepare picture model- var picture = await _pictureService.GetPictureById(x.PictureId);+ var picture = !string.IsNullOrEmpty(x.PictureId) ? await _pictureService.GetPictureById(x.PictureId) : null;catModel.PictureModel = new PictureModel{Id = x.PictureId,diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryHandler.csindex d80f4cece..d83ed6b1f 100644--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryHandler.cs@@ -118,7 +118,7 @@ namespace Grand.Web.Features.Handlers.CatalogFlagStyle = x.FlagStyle};//prepare picture model- var picture = await _pictureService.GetPictureById(x.PictureId);+ var picture = !string.IsNullOrEmpty(x.PictureId) ? await _pictureService.GetPictureById(x.PictureId) : null;subCatModel.PictureModel = new PictureModel{Id = x.PictureId,