2018-04-03 19:25:43 +10:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2018-04-21 09:57:28 +02:00
|
|
|
|
using Umbraco.Core;
|
2019-03-26 13:58:38 +01:00
|
|
|
|
using Umbraco.Core.Mapping;
|
2018-04-03 19:25:43 +10:00
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
|
|
|
|
|
using Language = Umbraco.Web.Models.ContentEditing.Language;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
2019-03-26 13:58:38 +01:00
|
|
|
|
internal class ContentVariantMapper
|
2018-04-03 19:25:43 +10:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILocalizationService _localizationService;
|
|
|
|
|
|
|
2019-03-26 13:58:38 +01:00
|
|
|
|
public ContentVariantMapper(ILocalizationService localizationService)
|
2018-04-03 19:25:43 +10:00
|
|
|
|
{
|
|
|
|
|
|
_localizationService = localizationService ?? throw new ArgumentNullException(nameof(localizationService));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-26 13:58:38 +01:00
|
|
|
|
public IEnumerable<ContentVariantDisplay> Map(IContent source, MapperContext context)
|
2018-04-03 19:25:43 +10:00
|
|
|
|
{
|
2018-07-13 12:45:04 +10:00
|
|
|
|
var result = new List<ContentVariantDisplay>();
|
2018-06-20 14:18:57 +02:00
|
|
|
|
if (!source.ContentType.VariesByCulture())
|
2018-04-03 19:25:43 +10:00
|
|
|
|
{
|
2018-07-13 12:45:04 +10:00
|
|
|
|
//this is invariant so just map the IContent instance to ContentVariationDisplay
|
2019-04-07 11:26:47 +02:00
|
|
|
|
result.Add(context.Map<ContentVariantDisplay>(source));
|
2018-07-13 12:45:04 +10:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var allLanguages = _localizationService.GetAllLanguages().OrderBy(x => x.Id).ToList();
|
|
|
|
|
|
if (allLanguages.Count == 0) return Enumerable.Empty<ContentVariantDisplay>(); //this should never happen
|
2018-04-03 19:25:43 +10:00
|
|
|
|
|
2019-04-08 16:38:18 +02:00
|
|
|
|
var langs = context.MapEnumerable<ILanguage, Language>(allLanguages).ToList();
|
2018-04-04 01:59:51 +10:00
|
|
|
|
|
2019-01-26 10:52:19 -05:00
|
|
|
|
//create a variant for each language, then we'll populate the values
|
2018-07-13 12:45:04 +10:00
|
|
|
|
var variants = langs.Select(x =>
|
2018-04-10 12:26:19 +10:00
|
|
|
|
{
|
2018-07-13 12:45:04 +10:00
|
|
|
|
//We need to set the culture in the mapping context since this is needed to ensure that the correct property values
|
|
|
|
|
|
//are resolved during the mapping
|
2019-03-26 13:58:38 +01:00
|
|
|
|
context.SetCulture(x.IsoCode);
|
2019-04-07 11:26:47 +02:00
|
|
|
|
return context.Map<ContentVariantDisplay>(source);
|
2018-07-13 12:45:04 +10:00
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < langs.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var x = langs[i];
|
|
|
|
|
|
var variant = variants[i];
|
|
|
|
|
|
|
|
|
|
|
|
variant.Language = x;
|
|
|
|
|
|
variant.Name = source.GetCultureName(x.IsoCode);
|
2018-04-10 12:26:19 +10:00
|
|
|
|
}
|
2018-04-03 19:25:43 +10:00
|
|
|
|
|
2018-10-23 16:03:49 +01:00
|
|
|
|
//Put the default language first in the list & then sort rest by a-z
|
|
|
|
|
|
var defaultLang = variants.SingleOrDefault(x => x.Language.IsDefault);
|
|
|
|
|
|
|
2019-01-26 10:52:19 -05:00
|
|
|
|
//Remove the default language from the list for now
|
2018-10-23 16:03:49 +01:00
|
|
|
|
variants.Remove(defaultLang);
|
|
|
|
|
|
|
|
|
|
|
|
//Sort the remaining languages a-z
|
2019-06-11 17:17:19 +10:00
|
|
|
|
variants = variants.OrderBy(x => x.Language.Name).ToList();
|
2018-10-23 16:03:49 +01:00
|
|
|
|
|
2019-01-26 10:52:19 -05:00
|
|
|
|
//Insert the default language as the first item
|
2018-10-23 16:03:49 +01:00
|
|
|
|
variants.Insert(0, defaultLang);
|
|
|
|
|
|
|
2018-07-13 12:45:04 +10:00
|
|
|
|
return variants;
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2018-04-03 19:25:43 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|