private List <FilterCriteria> ProductFilterableSpecAttributes(FilterProductContext context, string attributeName = null) { var query = ProductFilter(context); var attributes = from p in query from sa in p.ProductSpecificationAttributes where sa.AllowFiltering orderby sa.DisplayOrder select sa.SpecificationAttributeOption; if (attributeName.HasValue()) { attributes = attributes.Where(a => a.SpecificationAttribute.Name == attributeName); } var grouped = from a in attributes group a by new { a.SpecificationAttributeId, a.Id } into g select new FilterCriteria { Name = g.FirstOrDefault().SpecificationAttribute.Name, Value = g.FirstOrDefault().Name, ID = g.Key.Id, ParentId = g.FirstOrDefault().SpecificationAttribute.Id, MatchCount = g.Count() }; var lst = grouped.OrderByDescending(a => a.MatchCount).ToList(); int languageId = _commonServices.WorkContext.WorkingLanguage.Id; lst.ForEach(c => { c.Entity = ShortcutSpecAttribute; c.IsInactive = true; if (c.ParentId != 0) { c.NameLocalized = _localizedEntityService.GetLocalizedValue(languageId, c.ParentId, "SpecificationAttribute", "Name"); } if (c.ID.HasValue) { c.ValueLocalized = _localizedEntityService.GetLocalizedValue(languageId, c.ID.Value, "SpecificationAttributeOption", "Name"); } }); return(lst); }
public IActionResult GetLocalizedProperties(int languageId, int entityId, string localeKeyGroup, string localeKey) { var value = _localizedEntityService.GetLocalizedValue(languageId, entityId, localeKeyGroup, localeKey); //var model = entities.ToModels(); return(Ok(value)); }
/// <summary> /// Get localized property of an entity /// </summary> /// <typeparam name="TEntity">Entity type</typeparam> /// <typeparam name="TPropType">Property type</typeparam> /// <param name="entity">Entity</param> /// <param name="keySelector">Key selector</param> /// <param name="languageId">Language identifier; pass null to use the current working language; pass 0 to get standard language value</param> /// <param name="returnDefaultValue">A value indicating whether to return default value (if localized is not found)</param> /// <param name="ensureTwoPublishedLanguages">A value indicating whether to ensure that we have at least two published languages; otherwise, load only default value</param> /// <returns>Localized property</returns> public virtual TPropType GetLocalized <TEntity, TPropType>(TEntity entity, Expression <Func <TEntity, TPropType> > keySelector, string languageId, bool returnDefaultValue = true, bool ensureTwoPublishedLanguages = true) where TEntity : BaseEntity, ILocalizedEntity { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (!(keySelector.Body is MemberExpression member)) { throw new ArgumentException($"Expression '{keySelector}' refers to a method, not a property."); } if (!(member.Member is PropertyInfo propInfo)) { throw new ArgumentException($"Expression '{keySelector}' refers to a field, not a property."); } var result = default(TPropType); var resultStr = string.Empty; var localeKeyGroup = entity.GetType().Name; var localeKey = propInfo.Name; if (!string.IsNullOrWhiteSpace(languageId)) { //ensure that we have at least two published languages var loadLocalizedValue = true; if (ensureTwoPublishedLanguages) { var totalPublishedLanguages = _languageService.GetAllLanguages().Count; loadLocalizedValue = totalPublishedLanguages >= 2; } //localized value if (loadLocalizedValue) { resultStr = _localizedEntityService .GetLocalizedValue(languageId, entity.Id, localeKeyGroup, localeKey); if (!string.IsNullOrEmpty(resultStr)) { result = CommonHelper.To <TPropType>(resultStr); } } } //set default value if required if (!string.IsNullOrEmpty(resultStr) || !returnDefaultValue) { return(result); } var localizer = keySelector.Compile(); result = localizer(entity); return(result); }
/// <summary> /// Delete slide from database /// </summary> /// <param name="slide">Deleting slide</param> public virtual void DeleteSlide(Slide slide) { var allLanguages = _languageService.GetAllLanguages(true); //delete slide localized pictures and values foreach (var language in allLanguages) { var pictureIdLocalizaedValue = _localizedEntityService.GetLocalizedValue(language.Id, slide.Id, "Slide", "PictureId"); if (!string.IsNullOrEmpty(pictureIdLocalizaedValue) && int.TryParse(pictureIdLocalizaedValue, out int pictureId)) { //delete localized values _localizedEntityService.SaveLocalizedValue(slide, x => x.PictureId, null, language.Id); _localizedEntityService.SaveLocalizedValue(slide, x => x.HyperlinkAddress, null, language.Id); _localizedEntityService.SaveLocalizedValue(slide, x => x.Description, null, language.Id); var localizedPicture = _pictureService.GetPictureById(pictureId); //go to next picture if current picture aren't exist if (localizedPicture == null) { continue; } _pictureService.DeletePicture(localizedPicture); } } //delete slide base picture var picture = _pictureService.GetPictureById(slide.PictureId.GetValueOrDefault(0)); if (picture != null) { _pictureService.DeletePicture(picture); } //publish event _eventPublisher.EntityDeleted(slide); //delete slide entity _slideRepository.Delete(slide); }
/// <summary> /// Find localized value /// </summary> /// <typeparam name="T">The entity type</typeparam> /// <typeparam name="TPropType">The property type of the key</typeparam> /// <param name="localizedEntityService">the localized entity service</param> /// <param name="entityId">The entity id</param> /// <param name="keySelector">The key selector to get a localized value for</param> /// <param name="languageId">The language to look for</param> /// <returns></returns> public static string GetLocalizedValue <T, TPropType>(this ILocalizedEntityService localizedEntityService, ObjectId entityId, Expression <Func <T, TPropType> > keySelector, ObjectId?languageId = null) { var member = keySelector.Body as MemberExpression; if (member == null) { throw new ArgumentException(string.Format( "Expression '{0}' refers to a method, not a property.", keySelector)); } var propInfo = member.Member as PropertyInfo; if (propInfo == null) { throw new ArgumentException(string.Format( "Expression '{0}' refers to a field, not a property.", keySelector)); } return(localizedEntityService.GetLocalizedValue(entityId, typeof(T).Name, propInfo.Name, languageId)); }
/// <summary> /// Delete slide localized resources include slide pictures for special language /// </summary> /// <param name="slide">Slide entitiy</param> /// <param name="languageId">Language entitiy unique id number</param> public virtual void DeleteSlideLocalizedValues(Slide slide, int languageId) { var pictureIdLocalizaedValue = _localizedEntityService.GetLocalizedValue(languageId, slide.Id, "Slide", "PictureId"); var isPictureValid = int.TryParse(pictureIdLocalizaedValue, out int localizePictureId); //delete localized values _localizedEntityService.SaveLocalizedValue(slide, x => x.PictureId, null, languageId); _localizedEntityService.SaveLocalizedValue(slide, x => x.HyperlinkAddress, null, languageId); _localizedEntityService.SaveLocalizedValue(slide, x => x.Description, null, languageId); //remove localized picture if (!string.IsNullOrEmpty(pictureIdLocalizaedValue) && isPictureValid) { var localizedPicture = _pictureService.GetPictureById(localizePictureId); //go to next picture if current picture aren't exist if (localizedPicture == null) { return; } _pictureService.DeletePicture(localizedPicture); } }
public LocalizedValue <TProp> GetLocalizedValue <T, TProp>(T entity, string localeKeyGroup, string localeKey, Func <T, TProp> fallback, object requestLanguageIdOrObj, // Id or Language bool returnDefaultValue = true, bool ensureTwoPublishedLanguages = true, bool detectEmptyHtml = false) where T : ILocalizedEntity { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } TProp result = default; var str = string.Empty; Language currentLanguage = null; Language requestLanguage = null; if (!(requestLanguageIdOrObj is Language)) { if (requestLanguageIdOrObj is int requestLanguageId) { requestLanguage = _languageService.GetLanguageById(requestLanguageId); } } else { requestLanguage = (Language)requestLanguageIdOrObj; } if (requestLanguage == null) { requestLanguage = _workContext.WorkingLanguage; } // Ensure that we have at least two published languages var loadLocalizedValue = true; if (ensureTwoPublishedLanguages) { loadLocalizedValue = _languageCount > 1; } // Localized value if (loadLocalizedValue) { str = _localizedEntityService.GetLocalizedValue(requestLanguage.Id, entity.Id, localeKeyGroup, localeKey); if (detectEmptyHtml && HtmlUtils.IsEmptyHtml(str)) { str = string.Empty; } if (str.HasValue()) { currentLanguage = requestLanguage; result = str.Convert <TProp>(CultureInfo.InvariantCulture); } } // Set default value if required if (returnDefaultValue && str.IsEmpty()) { currentLanguage = _defaultLanguage; result = fallback(entity); } if (currentLanguage == null) { currentLanguage = requestLanguage; } return(new LocalizedValue <TProp>(result, requestLanguage, currentLanguage)); }
/// <summary> /// Get localized property of an entity /// </summary> /// <typeparam name="T">Entity type</typeparam> /// <typeparam name="TPropType">Property type</typeparam> /// <param name="entity">Entity</param> /// <param name="keySelector">Key selector</param> /// <param name="languageId">Language identifier</param> /// <param name="returnDefaultValue">A value indicating whether to return default value (if localized is not found)</param> /// <param name="ensureTwoPublishedLanguages">A value indicating whether to ensure that we have at least two published languages; otherwise, load only default value</param> /// <returns>Localized property</returns> public static TPropType GetLocalized <T, TPropType>(this T entity, Expression <Func <T, TPropType> > keySelector, int languageId, ILanguageService languageService, ILocalizedEntityService localizedEntityService, bool returnDefaultValue = true, bool ensureTwoPublishedLanguages = true) where T : BaseEntity, ILocalizedEntity { if (entity == null) { throw new ArgumentNullException("entity"); } var member = keySelector.Body as MemberExpression; if (member == null) { throw new ArgumentException(string.Format( "Expression '{0}' refers to a method, not a property.", keySelector)); } var propInfo = member.Member as PropertyInfo; if (propInfo == null) { throw new ArgumentException(string.Format( "Expression '{0}' refers to a field, not a property.", keySelector)); } TPropType result = default(TPropType); string resultStr = string.Empty; //load localized value string localeKeyGroup = typeof(T).Name; string localeKey = propInfo.Name; if (languageId > 0) { //ensure that we have at least two published languages bool loadLocalizedValue = true; if (ensureTwoPublishedLanguages) { var totalPublishedLanguages = languageService.GetAllLanguages().Count; loadLocalizedValue = totalPublishedLanguages >= 2; } //localized value if (loadLocalizedValue) { resultStr = localizedEntityService.GetLocalizedValue(languageId, entity.Id, localeKeyGroup, localeKey); if (!String.IsNullOrEmpty(resultStr)) { result = CommonHelper.To <TPropType>(resultStr); } } } //set default value if required if (String.IsNullOrEmpty(resultStr) && returnDefaultValue) { var localizer = keySelector.Compile(); result = localizer(entity); } return(result); }
/// <summary> /// Find localized value /// </summary> /// <typeparam name="T">The entity type</typeparam> /// <param name="localizedEntityService">The localized entity service</param> /// <param name="entityId">The entity id</param> /// <param name="keySelector">The key selector to get a localized value for</param>? /// <param name="languageId">The language to look for</param> /// <returns></returns> public static string GetLocalizedValue <T>(this ILocalizedEntityService localizedEntityService, ObjectId entityId, Expression <Func <T, string> > keySelector, ObjectId?languageId = null) { return(localizedEntityService.GetLocalizedValue <T, string>(entityId, keySelector, languageId)); }
protected virtual string GetLocalized(int entityId, string localeKeyGroup, string localeKey, int languageId, string defaultValue) { return(_localizedEntityService.GetLocalizedValue(languageId, entityId, localeKeyGroup, localeKey).NullEmpty() ?? defaultValue.NullEmpty()); }
private List <FilterCriteria> ProductFilterableSpecAttributes(FilterProductContext context, string attributeName = null) { List <FilterCriteria> criterias = null; var languageId = _services.WorkContext.WorkingLanguage.Id; var query = ProductFilter(context); var attributes = from p in query from sa in p.ProductSpecificationAttributes where sa.AllowFiltering select sa.SpecificationAttributeOption; if (attributeName.HasValue()) { attributes = attributes.Where(a => a.SpecificationAttribute.Name == attributeName); } var grouped = from a in attributes group a by new { a.SpecificationAttributeId, a.Id } into g select new FilterCriteria { Name = g.FirstOrDefault().SpecificationAttribute.Name, Value = g.FirstOrDefault().Name, ID = g.Key.Id, PId = g.FirstOrDefault().SpecificationAttribute.Id, MatchCount = g.Count(), DisplayOrder = g.FirstOrDefault().SpecificationAttribute.DisplayOrder, DisplayOrderValues = g.FirstOrDefault().DisplayOrder }; if (_catalogSettings.SortFilterResultsByMatches) { criterias = grouped .OrderBy(a => a.DisplayOrder) .ThenByDescending(a => a.MatchCount) .ThenBy(a => a.DisplayOrderValues) .ToList(); } else { criterias = grouped .OrderBy(a => a.DisplayOrder) .ThenBy(a => a.DisplayOrderValues) .ToList(); } criterias.ForEach(c => { c.Entity = ShortcutSpecAttribute; c.IsInactive = true; if (c.PId.HasValue) { c.NameLocalized = _localizedEntityService.GetLocalizedValue(languageId, c.PId.Value, "SpecificationAttribute", "Name"); } if (c.ID.HasValue) { c.ValueLocalized = _localizedEntityService.GetLocalizedValue(languageId, c.ID.Value, "SpecificationAttributeOption", "Name"); } }); return(criterias); }
/// <summary> /// Find localized value /// </summary> /// <param name="languageId">Language identifier</param> /// <param name="entityId">Entity identifier</param> /// <param name="localeKeyGroup">Locale key group</param> /// <param name="localeKey">Locale key</param> /// <returns>Found localized value</returns> public string GetLocalizedValue(int languageId, int entityId, string localeKeyGroup, string localeKey) { return(_localizedEntityService.GetLocalizedValue(languageId, entityId, localeKeyGroup, localeKey)); }