From eaacdc051470b3427a6e2b87f3899bfa30a65f96 Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 28 Aug 2018 15:00:45 +0200 Subject: [PATCH] Deal with fixmes in IContent IsCultureSomething --- src/Umbraco.Core/Models/Content.cs | 18 ++++++++++------- src/Umbraco.Core/Models/IContent.cs | 7 +++++-- .../Implement/DocumentRepository.cs | 5 ++++- src/Umbraco.Web/Editors/ContentController.cs | 2 +- .../Mapping/ContentSavedStateResolver.cs | 20 +++++++++---------- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/Umbraco.Core/Models/Content.cs b/src/Umbraco.Core/Models/Content.cs index caa7a2a3e4..dd379e02f8 100644 --- a/src/Umbraco.Core/Models/Content.cs +++ b/src/Umbraco.Core/Models/Content.cs @@ -32,6 +32,7 @@ namespace Umbraco.Core.Models /// Name of the content /// Parent object /// ContentType for the current Content object + /// An optional culture. public Content(string name, IContent parent, IContentType contentType, string culture = null) : this(name, parent, contentType, new PropertyCollection(), culture) { } @@ -43,6 +44,7 @@ namespace Umbraco.Core.Models /// Parent object /// ContentType for the current Content object /// Collection of properties + /// An optional culture. public Content(string name, IContent parent, IContentType contentType, PropertyCollection properties, string culture = null) : base(name, parent, contentType, properties, culture) { @@ -57,6 +59,7 @@ namespace Umbraco.Core.Models /// Name of the content /// Id of the Parent content /// ContentType for the current Content object + /// An optional culture. public Content(string name, int parentId, IContentType contentType, string culture = null) : this(name, parentId, contentType, new PropertyCollection(), culture) { } @@ -68,6 +71,7 @@ namespace Umbraco.Core.Models /// Id of the Parent content /// ContentType for the current Content object /// Collection of properties + /// An optional culture. public Content(string name, int parentId, IContentType contentType, PropertyCollection properties, string culture = null) : base(name, parentId, contentType, properties, culture) { @@ -213,23 +217,23 @@ namespace Umbraco.Core.Models [IgnoreDataMember] public IEnumerable PublishedCultures => _publishInfos?.Keys ?? Enumerable.Empty(); - //fixme should this return false if ID == 0? - //fixme should this return false if IsCultureAvailable(culture) is false? /// public bool IsCulturePublished(string culture) + // just check _publishInfos + // a non-available culture could not become published anyways => _publishInfos != null && _publishInfos.ContainsKey(culture); - //fixme should this return false if ID == 0? - //fixme should this return false if IsCultureAvailable(culture) is false? /// public bool WasCulturePublished(string culture) + // just check _publishInfosOrig - a copy of _publishInfos + // a non-available culture could not become published anyways => _publishInfosOrig != null && _publishInfosOrig.ContainsKey(culture); - //fixme should this return false if ID == 0? - //fixme should this return false if IsCultureAvailable(culture) is false? /// public bool IsCultureEdited(string culture) - => !IsCulturePublished(culture) || (_editedCultures != null && _editedCultures.Contains(culture)); + => IsCultureAvailable(culture) && // is available, and + (!IsCulturePublished(culture) || // is not published, or + (_editedCultures != null && _editedCultures.Contains(culture))); // is edited /// [IgnoreDataMember] diff --git a/src/Umbraco.Core/Models/IContent.cs b/src/Umbraco.Core/Models/IContent.cs index 9e79a75e25..5910d01d34 100644 --- a/src/Umbraco.Core/Models/IContent.cs +++ b/src/Umbraco.Core/Models/IContent.cs @@ -87,6 +87,8 @@ namespace Umbraco.Core.Models /// A culture becomes published whenever values for this culture are published, /// and the content published name for this culture is non-null. It becomes non-published /// whenever values for this culture are unpublished. + /// A culture becomes published as soon as PublishCulture has been invoked, + /// even though the document might now have been saved yet (and can have no identity). /// bool IsCulturePublished(string culture); @@ -107,8 +109,9 @@ namespace Umbraco.Core.Models /// Gets a value indicated whether a given culture is edited. /// /// - /// A culture is edited when it is not published, or when it is published but - /// it has changes. + /// A culture is edited when it is available, and not published or published but + /// with changes. + /// A culture can be edited even though the document might now have been saved yet (and can have no identity). /// bool IsCultureEdited(string culture); diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs index 093723cea5..28c0b0dec6 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs @@ -1073,7 +1073,10 @@ namespace Umbraco.Core.Persistence.Repositories.Implement NodeId = content.Id, LanguageId = LanguageRepository.GetIdByIsoCode(culture) ?? throw new InvalidOperationException("Not a valid culture."), Culture = culture, - Edited = !content.IsCulturePublished(culture) || (editedCultures != null && editedCultures.Contains(culture)) // if not published, always edited + + // if not published, always edited + // no need to check for availability: it *is* available since it is in content.CultureNames + Edited = !content.IsCulturePublished(culture) || (editedCultures != null && editedCultures.Contains(culture)) }; } diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index f1fbfe7940..8c3cc5f557 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -745,7 +745,7 @@ namespace Umbraco.Web.Editors //Check if a mandatory language is missing from being published var variant = cultureVariants.First(x => x.Culture == lang.IsoCode); - var isPublished = contentItem.PersistedContent.IsCultureAvailable(lang.IsoCode) && contentItem.PersistedContent.IsCulturePublished(lang.IsoCode); + var isPublished = contentItem.PersistedContent.IsCulturePublished(lang.IsoCode); var isPublishing = variant.Publish; if (!isPublished && !isPublishing) diff --git a/src/Umbraco.Web/Models/Mapping/ContentSavedStateResolver.cs b/src/Umbraco.Web/Models/Mapping/ContentSavedStateResolver.cs index 1c9af34388..4fb4d3370a 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentSavedStateResolver.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentSavedStateResolver.cs @@ -28,8 +28,7 @@ namespace Umbraco.Web.Models.Mapping ? PublishedState.Published : PublishedState.Unpublished; - //it can only be 'edited' if the content item is persisted and if the variant has a name and it's flagged as edited - isEdited = source.Id > 0 && source.IsCultureAvailable(culture) && source.IsCultureEdited(culture); + isEdited = source.IsCultureEdited(culture); } else { @@ -37,17 +36,16 @@ namespace Umbraco.Web.Models.Mapping ? PublishedState.Unpublished : PublishedState.Published; - isEdited = source.Id > 0 && source.Edited; + isEdited = source.Edited; } - //now we can calculate the content state - if (!isEdited && publishedState == PublishedState.Unpublished) - return ContentSavedState.NotCreated; - if (isEdited && publishedState == PublishedState.Unpublished) - return ContentSavedState.Draft; - if (!isEdited && publishedState == PublishedState.Published) - return ContentSavedState.Published; - return ContentSavedState.PublishedPendingChanges; + 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."); } } }