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.

Improvement - Collection picture - use localizable alternate text and title attributes for a picture

KrzysztofPajake424fcb811ab
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Collection/_CreateOrUpdate.TabInfo.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Collection/_CreateOrUpdate.TabInfo.cshtml
index dd4f38e6c..1dc975850 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Collection/_CreateOrUpdate.TabInfo.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Collection/_CreateOrUpdate.TabInfo.cshtml
@@ -103,6 +103,12 @@
<div class="col-md-9 col-sm-9">
<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", "Collection", new { area = Constants.AreaAdmin })/?collectionId=@(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/CollectionController.cs b/src/Web/Grand.Web.Admin/Controllers/CollectionController.cs
index 79ecc26d8..e14939a38 100644
--- a/src/Web/Grand.Web.Admin/Controllers/CollectionController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/CollectionController.cs
@@ -10,6 +10,7 @@ using Grand.Infrastructure;
using Grand.Web.Admin.Extensions;
using Grand.Web.Admin.Interfaces;
using Grand.Web.Admin.Models.Catalog;
+using Grand.Web.Admin.Models.Common;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Security.Authorization;
@@ -26,6 +27,7 @@ namespace Grand.Web.Admin.Controllers
public partial class CollectionController : BaseAdminController
{
#region Fields
+
private readonly ICollectionViewModelService _collectionViewModelService;
private readonly ICollectionService _collectionService;
private readonly IWorkContext _workContext;
@@ -35,6 +37,8 @@ namespace Grand.Web.Admin.Controllers
private readonly IGroupService _groupService;
private readonly IExportManager _exportManager;
private readonly IImportManager _importManager;
+ private readonly IPictureViewModelService _pictureViewModelService;
+
#endregion
#region Constructors
@@ -48,7 +52,8 @@ namespace Grand.Web.Admin.Controllers
ITranslationService translationService,
IGroupService groupService,
IExportManager exportManager,
- IImportManager importManager)
+ IImportManager importManager,
+ IPictureViewModelService pictureViewModelService)
{
_collectionViewModelService = collectionViewModelService;
_collectionService = collectionService;
@@ -59,6 +64,7 @@ namespace Grand.Web.Admin.Controllers
_groupService = groupService;
_exportManager = exportManager;
_importManager = importManager;
+ _pictureViewModelService = pictureViewModelService;
}
#endregion
@@ -281,6 +287,58 @@ namespace Grand.Web.Admin.Controllers
#endregion
+ #region Picture
+
+ [PermissionAuthorizeAction(PermissionActionName.Preview)]
+ public async Task<IActionResult> PicturePopup(string collectionId)
+ {
+ var collection = await _collectionService.GetCollectionById(collectionId);
+ if (collection == null)
+ return Content("Collection not exist");
+
+ if (string.IsNullOrEmpty(collection.PictureId))
+ return Content("Picture not exist");
+
+ var permission = await CheckAccessToCollection(collection);
+ if (!permission.allow)
+ return Content(permission.message);
+
+ return View("PicturePopup", await _pictureViewModelService.PreparePictureModel(collection.PictureId, collection.Id));
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ [HttpPost]
+ public async Task<IActionResult> PicturePopup(PictureModel model)
+ {
+ if (ModelState.IsValid)
+ {
+ var collection = await _collectionService.GetCollectionById(model.ObjectId);
+ if (collection == null)
+ throw new ArgumentException("No collection found with the specified id");
+
+ var permission = await CheckAccessToCollection(collection);
+ if (!permission.allow)
+ return Content(permission.message);
+
+ if (string.IsNullOrEmpty(collection.PictureId))
+ throw new ArgumentException("No picture found with the specified id");
+
+ if (collection.PictureId != model.Id)
+ throw new ArgumentException("Picture ident doesn't fit with collection");
+
+ await _pictureViewModelService.UpdatePicture(model);
+
+ ViewBag.RefreshPage = true;
+ return View("PicturePopup", model);
+ }
+
+ Error(ModelState);
+
+ return View("PicturePopup", model);
+ }
+
+ #endregion
+
#region Export / Import
[PermissionAuthorizeAction(PermissionActionName.Export)]
diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionAllHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionAllHandler.cs
index 231dffbaf..1413d9254 100644
--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionAllHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionAllHandler.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
{
@@ -52,18 +53,26 @@ namespace Grand.Web.Features.Handlers.Catalog
var collections = await _collectionService.GetAllCollections(storeId: request.Store.Id);
foreach (var collection in collections)
{
- var modelMan = collection.ToModel(request.Language);
+ var modelcollection = collection.ToModel(request.Language);
//prepare picture model
- modelMan.PictureModel = new PictureModel
+ var picture = !string.IsNullOrEmpty(collection.PictureId) ? await _pictureService.GetPictureById(collection.PictureId) : null;
+ modelcollection.PictureModel = new PictureModel
{
Id = collection.PictureId,
FullSizeImageUrl = await _pictureService.GetPictureUrl(collection.PictureId),
ImageUrl = await _pictureService.GetPictureUrl(collection.PictureId, _mediaSettings.CollectionThumbPictureSize),
- Title = string.Format(_translationService.GetResource("Media.Collection.ImageLinkTitleFormat"), modelMan.Name),
- AlternateText = string.Format(_translationService.GetResource("Media.Collection.ImageAlternateTextFormat"), modelMan.Name)
};
- model.Add(modelMan);
+ //"title" attribute
+ modelcollection.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.Collection.ImageLinkTitleFormat"), modelcollection.Name);
+ //"alt" attribute
+ modelcollection.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.Collection.ImageAlternateTextFormat"), modelcollection.Name);
+
+ model.Add(modelcollection);
}
return model;
}
diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionFeaturedProductsHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionFeaturedProductsHandler.cs
index 7018e6bb8..da7a718f1 100644
--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionFeaturedProductsHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionFeaturedProductsHandler.cs
@@ -18,6 +18,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
+using Grand.Business.Common.Extensions;
namespace Grand.Web.Features.Handlers.Catalog
{
@@ -51,29 +52,39 @@ namespace Grand.Web.Features.Handlers.Catalog
public async Task<IList<CollectionModel>> Handle(GetCollectionFeaturedProducts request, CancellationToken cancellationToken)
{
- string manufCacheKey = string.Format(CacheKeyConst.COLLECTION_FEATURED_PRODUCT_HOMEPAGE_KEY,
+ string collectionCacheKey = string.Format(CacheKeyConst.COLLECTION_FEATURED_PRODUCT_HOMEPAGE_KEY,
string.Join(",", request.Customer.GetCustomerGroupIds()), request.Store.Id,
request.Language.Id);
- var model = await _cacheBase.GetAsync(manufCacheKey, async () =>
+ var model = await _cacheBase.GetAsync(collectionCacheKey, async () =>
{
- var manufList = new List<CollectionModel>();
- var manufmodel = await _collectionService.GetAllCollectionFeaturedProductsOnHomePage();
- foreach (var x in manufmodel)
+ var collectionList = new List<CollectionModel>();
+ var collectionmodel = await _collectionService.GetAllCollectionFeaturedProductsOnHomePage();
+ foreach (var x in collectionmodel)
{
- var manModel = x.ToModel(request.Language);
+ var colModel = x.ToModel(request.Language);
//prepare picture model
- manModel.PictureModel = new PictureModel
+ var picture = !string.IsNullOrEmpty(x.PictureId) ? await _pictureService.GetPictureById(x.PictureId) : null;
+ colModel.PictureModel = new PictureModel
{
Id = x.PictureId,
FullSizeImageUrl = await _pictureService.GetPictureUrl(x.PictureId),
ImageUrl = await _pictureService.GetPictureUrl(x.PictureId, _mediaSettings.CategoryThumbPictureSize),
- Title = string.Format(_translationService.GetResource("Media.Category.ImageLinkTitleFormat"), manModel.Name),
- AlternateText = string.Format(_translationService.GetResource("Media.Category.ImageAlternateTextFormat"), manModel.Name)
+ //Title = string.Format(_translationService.GetResource("Media.Category.ImageLinkTitleFormat"), manModel.Name),
+ //AlternateText = string.Format(_translationService.GetResource("Media.Category.ImageAlternateTextFormat"), manModel.Name)
};
- manufList.Add(manModel);
+ //"title" attribute
+ colModel.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.Collection.ImageLinkTitleFormat"), colModel.Name);
+ //"alt" attribute
+ colModel.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.Collection.ImageAlternateTextFormat"), colModel.Name);
+
+ collectionList.Add(colModel);
}
- return manufList;
+ return collectionList;
});
foreach (var item in model)