Files
Umbraco-CMS/src/Umbraco.Web/Models/Mapping/ContentItemDisplayVariationResolver.cs

71 lines
3.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
2018-04-21 09:57:28 +02:00
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;
using ContentVariation = Umbraco.Web.Models.ContentEditing.ContentVariation;
using Language = Umbraco.Web.Models.ContentEditing.Language;
namespace Umbraco.Web.Models.Mapping
{
/// <summary>
/// Used to map the <see cref="ContentItemDisplay"/> variations collection from an <see cref="IContent"/> instance
/// </summary>
internal class ContentItemDisplayVariationResolver : IValueResolver<IContent, ContentItemDisplay, IEnumerable<ContentVariation>>
{
private readonly ILocalizationService _localizationService;
public ContentItemDisplayVariationResolver(ILocalizationService localizationService)
{
_localizationService = localizationService ?? throw new ArgumentNullException(nameof(localizationService));
}
public IEnumerable<ContentVariation> Resolve(IContent source, ContentItemDisplay destination, IEnumerable<ContentVariation> destMember, ResolutionContext context)
{
if (!source.ContentType.Variations.Has(Core.Models.ContentVariation.CultureNeutral))
return Enumerable.Empty<ContentVariation>();
var allLanguages = _localizationService.GetAllLanguages().OrderBy(x => x.Id).ToList();
2018-04-12 22:53:04 +02:00
if (allLanguages.Count == 0) return Enumerable.Empty<ContentVariation>();
var langs = context.Mapper.Map<IEnumerable<ILanguage>, IEnumerable<Language>>(allLanguages, null, context);
var variants = langs.Select(x => new ContentVariation
{
Language = x,
Mandatory = x.Mandatory,
2018-04-21 09:57:28 +02:00
Name = source.GetName(x.IsoCode),
Exists = source.IsCultureAvailable(x.IsoCode), // segments ??
PublishedState = (source.PublishedState == PublishedState.Unpublished //if the entire document is unpublished, then flag every variant as unpublished
? PublishedState.Unpublished
: source.IsCulturePublished(x.IsoCode)
? PublishedState.Published
: PublishedState.Unpublished).ToString(),
IsEdited = source.IsCultureEdited(x.IsoCode)
//Segment = ?? We'll need to populate this one day when we support segments
}).ToList();
2018-04-21 09:57:28 +02:00
var culture = context.GetCulture();
//set the current variant being edited to the one found in the context or the default if nothing matches
var foundCurrent = false;
foreach (var variant in variants)
{
2018-04-21 09:57:28 +02:00
if (culture.InvariantEquals(variant.Language.IsoCode))
{
variant.IsCurrent = true;
foundCurrent = true;
break;
}
}
if (!foundCurrent)
variants.First(x => x.Language.IsDefaultVariantLanguage).IsCurrent = true;
return variants;
}
}
}