From 551e8120c926b38366b36e8ae36f97ffd09c6c3b Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 12 Sep 2018 12:08:51 +0100 Subject: [PATCH] Adds state property to ContentItemBase (Not sure if this will blow things up or if I need to use a diff model for ListView children now) Adds/copies the same logic from ContentSavedStateResolver.cs --- .../Models/ContentEditing/ContentItemBasic.cs | 5 ++ .../Models/Mapping/ContentMapperProfile.cs | 48 ++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs b/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs index 427dadb2c9..df23b1fc72 100644 --- a/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs +++ b/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs @@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Runtime.Serialization; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using Umbraco.Core.Models; using Umbraco.Core.Models.Validation; using Umbraco.Web.WebApi; @@ -44,6 +45,10 @@ namespace Umbraco.Web.Models.ContentEditing [DataMember(Name = "sortOrder")] public int SortOrder { get; set; } + [DataMember(Name = "state")] + [JsonConverter(typeof(StringEnumConverter))] + public ContentSavedState State { get; set; } + protected bool Equals(ContentItemBasic other) { return Id == other.Id; diff --git a/src/Umbraco.Web/Models/Mapping/ContentMapperProfile.cs b/src/Umbraco.Web/Models/Mapping/ContentMapperProfile.cs index d7dc5cf46a..6ae36a1f19 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentMapperProfile.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentMapperProfile.cs @@ -83,7 +83,8 @@ namespace Umbraco.Web.Models.Mapping .ForMember(dest => dest.AdditionalData, opt => opt.Ignore()) .ForMember(dest => dest.UpdateDate, opt => opt.ResolveUsing()) .ForMember(dest => dest.Published, opt => opt.ResolveUsing()) - .ForMember(dest => dest.Name, opt => opt.ResolveUsing()); + .ForMember(dest => dest.Name, opt => opt.ResolveUsing()) + .ForMember(dest => dest.State, opt => opt.ResolveUsing()); //FROM IContent TO ContentPropertyCollectionDto //NOTE: the property mapping for cultures relies on a culture being set in the mapping context @@ -91,6 +92,51 @@ namespace Umbraco.Web.Models.Mapping } } + internal class CultureStateResolver : IValueResolver, ContentSavedState> + { + //WB: Note this is same logic as ContentSavedStateResolver.cs + //But this is for ContentItemBasic instead of ContentVariantDisplay + public ContentSavedState Resolve(IContent source, ContentItemBasic destination, ContentSavedState destMember, ResolutionContext context) + { + PublishedState publishedState; + bool isEdited; + + if (source.ContentType.VariesByCulture()) + { + //Get the culture from the context which will be set during the mapping operation for each variant + var culture = context.GetCulture(); + + //a culture needs to be in the context for a variant content item + if (culture == null) + throw new InvalidOperationException($"No culture found in mapping operation when one is required for a culture variant"); + + publishedState = source.PublishedState == PublishedState.Unpublished //if the entire document is unpublished, then flag every variant as unpublished + ? PublishedState.Unpublished + : source.IsCulturePublished(culture) + ? PublishedState.Published + : PublishedState.Unpublished; + + isEdited = source.IsCultureEdited(culture); + } + else + { + publishedState = source.PublishedState == PublishedState.Unpublished + ? PublishedState.Unpublished + : PublishedState.Published; + + isEdited = source.Edited; + } + + if (publishedState == PublishedState.Unpublished) + return isEdited && source.Id > 0 ? ContentSavedState.Draft : ContentSavedState.NotCreated; + + if (publishedState == PublishedState.Published) + return isEdited ? ContentSavedState.PublishedPendingChanges : ContentSavedState.Published; + + throw new NotSupportedException($"PublishedState {publishedState} is not supported."); + } + } + internal class CultureUpdateDateResolver : IValueResolver, DateTime> { public DateTime Resolve(IContent source, ContentItemBasic destination, DateTime destMember, ResolutionContext context)