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 Language = Umbraco . Web . Models . ContentEditing . Language ;
namespace Umbraco.Web.Models.Mapping
{
2018-07-13 12:45:04 +10:00
internal class ContentVariantResolver : IValueResolver < IContent , ContentItemDisplay , IEnumerable < ContentVariantDisplay > >
2018-04-03 19:25:43 +10:00
{
private readonly ILocalizationService _localizationService ;
2018-07-13 12:45:04 +10:00
private readonly ILocalizedTextService _textService ;
2018-04-03 19:25:43 +10:00
2018-07-13 12:45:04 +10:00
public ContentVariantResolver ( ILocalizationService localizationService , ILocalizedTextService textService )
2018-04-03 19:25:43 +10:00
{
_localizationService = localizationService ? ? throw new ArgumentNullException ( nameof ( localizationService ) ) ;
2018-07-13 12:45:04 +10:00
_textService = textService ? ? throw new ArgumentNullException ( nameof ( textService ) ) ;
2018-04-03 19:25:43 +10:00
}
2018-07-13 12:45:04 +10:00
public IEnumerable < ContentVariantDisplay > Resolve ( IContent source , ContentItemDisplay destination , IEnumerable < ContentVariantDisplay > destMember , ResolutionContext 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
result . Add ( context . Mapper . Map < ContentVariantDisplay > ( source ) ) ;
}
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
2018-07-13 12:45:04 +10:00
var langs = context . Mapper . Map < IEnumerable < ILanguage > , IEnumerable < Language > > ( allLanguages , null , context ) . ToList ( ) ;
2018-04-04 01:59:51 +10:00
2018-07-13 12:45:04 +10:00
//create a variant for each lang, then we'll populate the values
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
2018-08-01 16:46:13 +10:00
context . Items [ ResolutionContextExtensions . CultureKey ] = x . IsoCode ;
2018-07-13 12:45:04 +10:00
return context . Mapper . Map < IContent , ContentVariantDisplay > ( source , null , context ) ;
} ) . 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 ) ;
variant . Exists = source . IsCultureAvailable ( x . IsoCode ) ; // segments ??
variant . 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 ( ) ;
variant . IsEdited = source . IsCultureEdited ( x . IsoCode ) ;
2018-04-10 12:26:19 +10:00
}
2018-04-03 19:25:43 +10:00
2018-07-13 12:45:04 +10:00
return variants ;
}
return result ;
2018-04-03 19:25:43 +10:00
}
}
2018-07-13 12:45:04 +10:00
2018-04-03 19:25:43 +10:00
}