2018-04-03 19:25:43 +10:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using AutoMapper;
|
2018-04-21 09:57:28 +02:00
|
|
|
|
using Umbraco.Core;
|
2018-04-03 19:25:43 +10:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2018-04-20 00:59:23 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used to map the <see cref="ContentItemDisplay"/> variations collection from an <see cref="IContent"/> instance
|
|
|
|
|
|
/// </summary>
|
2018-04-19 22:06:02 +10:00
|
|
|
|
internal class ContentItemDisplayVariationResolver : IValueResolver<IContent, ContentItemDisplay, IEnumerable<ContentVariation>>
|
2018-04-03 19:25:43 +10:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILocalizationService _localizationService;
|
|
|
|
|
|
|
2018-04-19 22:06:02 +10:00
|
|
|
|
public ContentItemDisplayVariationResolver(ILocalizationService localizationService)
|
2018-04-03 19:25:43 +10:00
|
|
|
|
{
|
|
|
|
|
|
_localizationService = localizationService ?? throw new ArgumentNullException(nameof(localizationService));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ContentVariation> Resolve(IContent source, ContentItemDisplay destination, IEnumerable<ContentVariation> destMember, ResolutionContext context)
|
|
|
|
|
|
{
|
2018-06-20 14:18:57 +02:00
|
|
|
|
if (!source.ContentType.VariesByCulture())
|
2018-05-08 01:16:32 +10:00
|
|
|
|
return Enumerable.Empty<ContentVariation>();
|
|
|
|
|
|
|
2018-04-03 19:25:43 +10:00
|
|
|
|
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>();
|
2018-04-03 19:25:43 +10:00
|
|
|
|
|
2018-04-04 01:59:51 +10:00
|
|
|
|
var langs = context.Mapper.Map<IEnumerable<ILanguage>, IEnumerable<Language>>(allLanguages, null, context);
|
2018-04-03 19:25:43 +10:00
|
|
|
|
var variants = langs.Select(x => new ContentVariation
|
|
|
|
|
|
{
|
|
|
|
|
|
Language = x,
|
2018-04-09 16:59:45 +10:00
|
|
|
|
Mandatory = x.Mandatory,
|
2018-06-20 14:18:57 +02:00
|
|
|
|
Name = source.GetCultureName(x.IsoCode),
|
2018-04-21 09:57:28 +02:00
|
|
|
|
Exists = source.IsCultureAvailable(x.IsoCode), // segments ??
|
2018-05-08 00:37:41 +10:00
|
|
|
|
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(),
|
2018-04-30 16:06:53 +02:00
|
|
|
|
IsEdited = source.IsCultureEdited(x.IsoCode)
|
2018-04-10 01:38:35 +10:00
|
|
|
|
//Segment = ?? We'll need to populate this one day when we support segments
|
2018-04-03 19:25:43 +10:00
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
2018-04-21 09:57:28 +02:00
|
|
|
|
var culture = context.GetCulture();
|
2018-04-04 01:59:51 +10:00
|
|
|
|
|
2018-04-10 12:26:19 +10:00
|
|
|
|
//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))
|
2018-04-10 12:26:19 +10:00
|
|
|
|
{
|
|
|
|
|
|
variant.IsCurrent = true;
|
|
|
|
|
|
foundCurrent = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!foundCurrent)
|
|
|
|
|
|
variants.First(x => x.Language.IsDefaultVariantLanguage).IsCurrent = true;
|
2018-04-03 19:25:43 +10:00
|
|
|
|
|
|
|
|
|
|
return variants;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|