Skip to content

Commit

Permalink
Improvements - Grand.Web - Product pictures - use localizable alterna…
Browse files Browse the repository at this point in the history
…te text and title attributes for a picture
  • Loading branch information
KrzysztofPajak committed Aug 13, 2021
1 parent 8747955 commit 7574ca4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ private async Task<ProductDetailsModel> PrepareProductDetailsModel(Store store,

#region Product specifications

model.ProductSpecifications = await _mediator.Send(new GetProductSpecification()
{
model.ProductSpecifications = await _mediator.Send(new GetProductSpecification() {
Language = _workContext.WorkingLanguage,
Product = product
});
Expand All @@ -274,8 +273,7 @@ private async Task<ProductDetailsModel> PrepareProductDetailsModel(Store store,

#region Product review overview

model.ProductReviewOverview = await _mediator.Send(new GetProductReviewOverview()
{
model.ProductReviewOverview = await _mediator.Send(new GetProductReviewOverview() {
Product = product,
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore
Expand All @@ -299,16 +297,16 @@ private async Task<ProductDetailsModel> PrepareProductDetailsModel(Store store,
_workContext.WorkingLanguage.Id,
string.Join(",", _workContext.CurrentCustomer.GetCustomerGroupIds()),
_workContext.CurrentStore.Id);
model.ProductCollections = await _cacheBase.GetAsync(collectionsCacheKey, async () =>
model.ProductCollections = await _cacheBase.GetAsync(collectionsCacheKey, async () =>
{
var listCollection = new List<CollectionModel>();
foreach (var item in product.ProductCollections.OrderBy(x => x.DisplayOrder))
{
var listCollection = new List<CollectionModel>();
foreach (var item in product.ProductCollections.OrderBy(x=>x.DisplayOrder))
{
var collect = (await _collectionService.GetCollectionById(item.CollectionId)).ToModel(_workContext.WorkingLanguage);
listCollection.Add(collect);
}
return listCollection;
});
var collect = (await _collectionService.GetCollectionById(item.CollectionId)).ToModel(_workContext.WorkingLanguage);
listCollection.Add(collect);
}
return listCollection;
});

#endregion

Expand Down Expand Up @@ -363,8 +361,7 @@ private async Task<ProductDetailsModel> PrepareStandardProperties(Product produc

var warehouseId = updateCartItem != null ? updateCartItem.WarehouseId : _workContext.CurrentStore.DefaultWarehouseId;

var model = new ProductDetailsModel
{
var model = new ProductDetailsModel {
Id = product.Id,
ProductType = product.ProductTypeId,
Name = product.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id),
Expand Down Expand Up @@ -411,8 +408,7 @@ private async Task<ProductDetailsModel> PrepareStandardProperties(Product produc
foreach (var warehouse in await _warehouseService.GetAllWarehouses())
{
var productwarehouse = product.ProductWarehouseInventory.FirstOrDefault(x => x.WarehouseId == warehouse.Id);
model.ProductWarehouses.Add(new ProductDetailsModel.ProductWarehouseModel
{
model.ProductWarehouses.Add(new ProductDetailsModel.ProductWarehouseModel {
Use = productwarehouse != null,
StockQuantity = productwarehouse?.StockQuantity ?? 0,
ReservedQuantity = productwarehouse?.ReservedQuantity ?? 0,
Expand Down Expand Up @@ -447,7 +443,7 @@ private async Task<ProductDetailsModel> PrepareStandardProperties(Product produc

//store name
model.CurrentStoreName = _workContext.CurrentStore.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id);

return model;

#endregion
Expand All @@ -457,8 +453,7 @@ private async Task<ProductAskQuestionSimpleModel> PrepareProductAskQuestionSimpl
{
var customer = _workContext.CurrentCustomer;

var model = new ProductAskQuestionSimpleModel
{
var model = new ProductAskQuestionSimpleModel {
Id = product.Id,
AskQuestionEmail = customer.Email,
AskQuestionFullName = customer.GetFullName(),
Expand All @@ -475,8 +470,7 @@ private async Task<BrandBriefInfoModel> PrepareBrandBriefInfoModel(Product produ
var brand = await _brandService.GetBrandById(product.BrandId);
if (brand != null && brand.Published)
{
return new BrandBriefInfoModel
{
return new BrandBriefInfoModel {
Id = brand.Id,
Name = brand.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id),
SeName = brand.GetSeName(_workContext.WorkingLanguage.Id),
Expand All @@ -492,8 +486,7 @@ private async Task<VendorBriefInfoModel> PrepareVendorBriefInfoModel(Product pro
var vendor = await _vendorService.GetVendorById(product.VendorId);
if (vendor != null && !vendor.Deleted && vendor.Active)
{
return new VendorBriefInfoModel
{
return new VendorBriefInfoModel {
Id = vendor.Id,
Name = vendor.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id),
SeName = vendor.GetSeName(_workContext.WorkingLanguage.Id),
Expand All @@ -512,8 +505,7 @@ private async Task<ProductDetailsModel.ProductBreadcrumbModel> PrepareProductBre
_workContext.CurrentStore.Id);
return await _cacheBase.GetAsync(breadcrumbCacheKey, async () =>
{
var breadcrumbModel = new ProductDetailsModel.ProductBreadcrumbModel
{
var breadcrumbModel = new ProductDetailsModel.ProductBreadcrumbModel {
Enabled = _catalogSettings.CategoryBreadcrumbEnabled,
ProductId = product.Id,
Expand All @@ -528,8 +520,7 @@ private async Task<ProductDetailsModel.ProductBreadcrumbModel> PrepareProductBre
{
foreach (var catBr in await _categoryService.GetCategoryBreadCrumb(category))
{
breadcrumbModel.CategoryBreadcrumb.Add(new CategorySimpleModel
{
breadcrumbModel.CategoryBreadcrumb.Add(new CategorySimpleModel {
Id = catBr.Id,
Name = catBr.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id),
SeName = catBr.GetSeName(_workContext.WorkingLanguage.Id),
Expand All @@ -553,8 +544,7 @@ private async Task<IList<ProductTagModel>> PrepareProductTagModel(Product produc
var tag = await _productTagService.GetProductTagByName(item);
if (tag != null)
{
tags.Add(new ProductTagModel()
{
tags.Add(new ProductTagModel() {
Id = tag.Id,
Name = tag.GetTranslation(y => y.Name, _workContext.WorkingLanguage.Id),
SeName = tag.SeName,
Expand All @@ -573,20 +563,19 @@ private async Task<(PictureModel defaultPictureModel, List<PictureModel> picture
defaultPicture = new ProductPicture();

var picture = await _pictureService.GetPictureById(defaultPicture.PictureId);

var defaultPictureModel = new PictureModel
{

var defaultPictureModel = new PictureModel {
Id = defaultPicture.PictureId,
ImageUrl = await _pictureService.GetPictureUrl(defaultPicture.PictureId, defaultPictureSize, !isAssociatedProduct),
FullSizeImageUrl = await _pictureService.GetPictureUrl(defaultPicture.PictureId, 0, !isAssociatedProduct),
};
//"title" attribute
defaultPictureModel.Title = (picture != null && !string.IsNullOrEmpty(picture.TitleAttribute)) ?
picture.TitleAttribute :
defaultPictureModel.Title = (picture != null && !string.IsNullOrEmpty(picture.GetTranslation(x => x.TitleAttribute, _workContext.WorkingLanguage.Id))) ?
picture.GetTranslation(x => x.TitleAttribute, _workContext.WorkingLanguage.Id) :
string.Format(_translationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"), name);
//"alt" attribute
defaultPictureModel.AlternateText = (picture != null && !string.IsNullOrEmpty(picture.AltAttribute)) ?
picture.AltAttribute :
defaultPictureModel.AlternateText = (picture != null && !string.IsNullOrEmpty(picture.GetTranslation(x => x.AltAttribute, _workContext.WorkingLanguage.Id))) ?
picture.GetTranslation(x => x.AltAttribute, _workContext.WorkingLanguage.Id) :
string.Format(_translationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"), name);

//all pictures
Expand All @@ -596,22 +585,19 @@ private async Task<(PictureModel defaultPictureModel, List<PictureModel> picture
picture = await _pictureService.GetPictureById(productPicture.PictureId);
if (picture != null)
{
var pictureModel = new PictureModel
{
var pictureModel = new PictureModel {
Id = productPicture.PictureId,
ThumbImageUrl = await _pictureService.GetPictureUrl(productPicture.PictureId, _mediaSettings.ProductThumbPictureSizeOnProductDetailsPage),
ImageUrl = await _pictureService.GetPictureUrl(productPicture.PictureId, _mediaSettings.ProductDetailsPictureSize),
FullSizeImageUrl = await _pictureService.GetPictureUrl(productPicture.PictureId),
Title = string.Format(_translationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"), name),
AlternateText = string.Format(_translationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"), name),
};
//"title" attribute
pictureModel.Title = !string.IsNullOrEmpty(picture.TitleAttribute) ?
picture.TitleAttribute :
pictureModel.Title = !string.IsNullOrEmpty(picture.GetTranslation(x => x.TitleAttribute, _workContext.WorkingLanguage.Id)) ?
picture.GetTranslation(x => x.TitleAttribute, _workContext.WorkingLanguage.Id) :
string.Format(_translationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"), name);
//"alt" attribute
pictureModel.AlternateText = !string.IsNullOrEmpty(picture.AltAttribute) ?
picture.AltAttribute :
pictureModel.AlternateText = !string.IsNullOrEmpty(picture.GetTranslation(x => x.AltAttribute, _workContext.WorkingLanguage.Id)) ?
picture.GetTranslation(x => x.AltAttribute, _workContext.WorkingLanguage.Id) :
string.Format(_translationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"), name);

pictureModels.Add(pictureModel);
Expand Down Expand Up @@ -725,8 +711,7 @@ private async Task<ProductDetailsModel.AddToCartModel> PrepareAddToCartModel(Pro
var allowedQuantities = product.ParseAllowedQuantities();
foreach (var qty in allowedQuantities)
{
model.AllowedQuantities.Add(new SelectListItem
{
model.AllowedQuantities.Add(new SelectListItem {
Text = qty.ToString(),
Value = qty.ToString(),
Selected = updatecartitem != null && updatecartitem.Quantity == qty
Expand Down Expand Up @@ -812,8 +797,7 @@ private async Task<IList<ProductDetailsModel.ProductAttributeModel>> PrepareProd
if (productAttribute == null)
continue;

var attributeModel = new ProductDetailsModel.ProductAttributeModel
{
var attributeModel = new ProductDetailsModel.ProductAttributeModel {
Id = attribute.Id,
ProductId = product.Id,
ProductAttributeId = attribute.ProductAttributeId,
Expand Down Expand Up @@ -853,8 +837,7 @@ private async Task<IList<ProductDetailsModel.ProductAttributeModel>> PrepareProd
var customattributes = _productAttributeParser.AddProductAttribute(null, attribute, attributeValue.Id);
stockAvailability = _stockQuantityService.FormatStockMessage(product, string.Empty, customattributes);
}
var valueModel = new ProductDetailsModel.ProductAttributeValueModel
{
var valueModel = new ProductDetailsModel.ProductAttributeValueModel {
Id = attributeValue.Id,
Name = attributeValue.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id),
ColorSquaresRgb = attributeValue.ColorSquaresRgb, //used with "Color squares" attribute type
Expand Down Expand Up @@ -884,8 +867,7 @@ private async Task<IList<ProductDetailsModel.ProductAttributeModel>> PrepareProd
var pm = new PictureModel();
if (attributeValue.ImageSquaresPictureId != null)
{
pm = new PictureModel
{
pm = new PictureModel {
Id = attributeValue.ImageSquaresPictureId,
FullSizeImageUrl = await _pictureService.GetPictureUrl(attributeValue.ImageSquaresPictureId),
ImageUrl = await _pictureService.GetPictureUrl(attributeValue.ImageSquaresPictureId, _mediaSettings.ImageSquarePictureSize)
Expand All @@ -900,8 +882,7 @@ private async Task<IList<ProductDetailsModel.ProductAttributeModel>> PrepareProd
var pm = new PictureModel();
if (attributeValue.PictureId != null)
{
pm = new PictureModel
{
pm = new PictureModel {
Id = attributeValue.PictureId,
FullSizeImageUrl = await _pictureService.GetPictureUrl(attributeValue.PictureId),
ImageUrl = await _pictureService.GetPictureUrl(attributeValue.PictureId, defaultPictureSize)
Expand Down Expand Up @@ -1078,8 +1059,7 @@ private async Task<IList<ProductDetailsModel.ProductBundleModel>> PrepareProduct
var p1 = await _productService.GetProductById(bundle.ProductId);
if (p1 != null && p1.Published && p1.IsAvailable())
{
var bundleProduct = new ProductDetailsModel.ProductBundleModel()
{
var bundleProduct = new ProductDetailsModel.ProductBundleModel() {
ProductId = p1.Id,
Name = p1.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id),
ShortDescription = p1.GetTranslation(x => x.ShortDescription, _workContext.WorkingLanguage.Id),
Expand All @@ -1105,8 +1085,7 @@ private async Task<IList<ProductDetailsModel.ProductBundleModel>> PrepareProduct

var picture = await _pictureService.GetPictureById(productPicture.PictureId);

var pictureModel = new PictureModel
{
var pictureModel = new PictureModel {
Id = productPicture.PictureId,
ImageUrl = await _pictureService.GetPictureUrl(productPicture.PictureId, _mediaSettings.ProductBundlePictureSize),
FullSizeImageUrl = await _pictureService.GetPictureUrl(productPicture.PictureId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,12 @@ async Task<PictureModel> PreparePictureModel(ProductPicture productpicture)
FullSizeImageUrl = await _pictureService.GetPictureUrl(productpicture.PictureId)
};
//"title" attribute
pictureModel.Title = (picture != null && !string.IsNullOrEmpty(picture.TitleAttribute)) ?
picture.TitleAttribute :
pictureModel.Title = (picture != null && !string.IsNullOrEmpty(picture.GetTranslation(x => x.TitleAttribute, _workContext.WorkingLanguage.Id))) ?
picture.GetTranslation(x => x.TitleAttribute, _workContext.WorkingLanguage.Id) :
string.Format(res["Media.Product.ImageLinkTitleFormat"], name);
//"alt" attribute
pictureModel.AlternateText = (picture != null && !string.IsNullOrEmpty(picture.AltAttribute)) ?
picture.AltAttribute :
pictureModel.AlternateText = (picture != null && !string.IsNullOrEmpty(picture.GetTranslation(x => x.AltAttribute, _workContext.WorkingLanguage.Id))) ?
picture.GetTranslation(x => x.AltAttribute, _workContext.WorkingLanguage.Id) :
string.Format(res["Media.Product.ImageAlternateTextFormat"], name);

return pictureModel;
Expand Down

0 comments on commit 7574ca4

Please sign in to comment.