2018-07-21 15:58:49 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2018-07-24 13:32:29 +02:00
|
|
|
|
using Umbraco.Core;
|
2018-07-21 09:41:07 +02:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2018-07-24 13:32:29 +02:00
|
|
|
|
using Umbraco.Core.Services;
|
2018-05-02 13:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.PublishedContent
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides a default implementation for <see cref="IPublishedValueFallback"/>.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PublishedValueFallback : IPublishedValueFallback
|
|
|
|
|
|
{
|
2018-07-24 13:32:29 +02:00
|
|
|
|
private readonly ILocalizationService _localizationService;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="PublishedValueFallback"/> class.
|
|
|
|
|
|
/// </summary>
|
2018-07-28 07:58:22 +02:00
|
|
|
|
/// <param name="serviceContext"></param>
|
|
|
|
|
|
public PublishedValueFallback(ServiceContext serviceContext)
|
2018-07-24 13:32:29 +02:00
|
|
|
|
{
|
2018-07-28 07:58:22 +02:00
|
|
|
|
_localizationService = serviceContext.LocalizationService;
|
2018-07-24 13:32:29 +02:00
|
|
|
|
}
|
2018-05-02 13:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-10-03 10:31:35 +02:00
|
|
|
|
public bool TryGetValue(IPublishedProperty property, string culture, string segment, Fallback fallback, object defaultValue, out object value)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
return TryGetValue<object>(property, culture, segment, fallback, defaultValue, out value);
|
2018-05-02 13:38:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-10-03 10:31:35 +02:00
|
|
|
|
public bool TryGetValue<T>(IPublishedProperty property, string culture, string segment, Fallback fallback, T defaultValue, out T value)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
foreach (var f in fallback)
|
2018-07-28 08:39:02 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
switch (f)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Fallback.None:
|
|
|
|
|
|
continue;
|
|
|
|
|
|
case Fallback.DefaultValue:
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
case Fallback.Language:
|
|
|
|
|
|
if (TryGetValueWithLanguageFallback(property, culture, segment, defaultValue, out value))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw NotSupportedFallbackMethod(f, "property");
|
|
|
|
|
|
}
|
2018-07-28 08:39:02 +02:00
|
|
|
|
}
|
2018-10-03 10:31:35 +02:00
|
|
|
|
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
return false;
|
2018-05-02 13:38:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-10-03 10:31:35 +02:00
|
|
|
|
public bool TryGetValue(IPublishedElement content, string alias, string culture, string segment, Fallback fallback, object defaultValue, out object value)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
return TryGetValue<object>(content, alias, culture, segment, fallback, defaultValue, out value);
|
2018-05-02 13:38:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-10-03 10:31:35 +02:00
|
|
|
|
public bool TryGetValue<T>(IPublishedElement content, string alias, string culture, string segment, Fallback fallback, T defaultValue, out T value)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
foreach (var f in fallback)
|
2018-07-28 08:39:02 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
switch (f)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Fallback.None:
|
|
|
|
|
|
continue;
|
|
|
|
|
|
case Fallback.DefaultValue:
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
case Fallback.Language:
|
|
|
|
|
|
if (TryGetValueWithLanguageFallback(content, alias, culture, segment, defaultValue, out value))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw NotSupportedFallbackMethod(f, "element");
|
|
|
|
|
|
}
|
2018-07-28 08:39:02 +02:00
|
|
|
|
}
|
2018-10-03 10:31:35 +02:00
|
|
|
|
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
return false;
|
2018-05-02 13:38:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-10-03 10:31:35 +02:00
|
|
|
|
public bool TryGetValue(IPublishedContent content, string alias, string culture, string segment, Fallback fallback, object defaultValue, out object value)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
// is that ok?
|
2018-10-03 10:31:35 +02:00
|
|
|
|
return TryGetValue<object>(content, alias, culture, segment, fallback, defaultValue, out value);
|
2018-05-02 13:38:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-10-03 10:31:35 +02:00
|
|
|
|
public virtual bool TryGetValue<T>(IPublishedContent content, string alias, string culture, string segment, Fallback fallback, T defaultValue, out T value)
|
2018-07-21 15:58:49 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
// note: we don't support "recurse & language" which would walk up the tree,
|
|
|
|
|
|
// looking at languages at each level - should someone need it... they'll have
|
|
|
|
|
|
// to implement it.
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var f in fallback)
|
2018-07-21 15:58:49 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
switch (f)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Fallback.None:
|
|
|
|
|
|
continue;
|
|
|
|
|
|
case Fallback.DefaultValue:
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
case Fallback.Language:
|
|
|
|
|
|
if (TryGetValueWithLanguageFallback(content, alias, culture, segment, defaultValue, out value))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Fallback.Ancestors:
|
|
|
|
|
|
if (TryGetValueWithRecursiveFallback(content, alias, culture, segment, defaultValue, out value))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw NotSupportedFallbackMethod(f, "content");
|
|
|
|
|
|
}
|
2018-07-21 15:58:49 +02:00
|
|
|
|
}
|
2018-10-03 10:31:35 +02:00
|
|
|
|
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
return false;
|
2018-07-21 15:58:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-03 10:31:35 +02:00
|
|
|
|
private NotSupportedException NotSupportedFallbackMethod(int fallback, string level)
|
2018-07-28 08:39:02 +02:00
|
|
|
|
{
|
2018-10-03 10:31:35 +02:00
|
|
|
|
return new NotSupportedException($"Fallback {GetType().Name} does not support fallback code '{fallback}' at {level} level.");
|
2018-07-28 08:39:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-24 13:32:29 +02:00
|
|
|
|
// tries to get a value, recursing the tree
|
2018-07-28 08:39:02 +02:00
|
|
|
|
private static bool TryGetValueWithRecursiveFallback<T>(IPublishedContent content, string alias, string culture, string segment, T defaultValue, out T value)
|
2018-07-21 15:58:49 +02:00
|
|
|
|
{
|
2018-05-02 13:38:45 +02:00
|
|
|
|
IPublishedProperty property = null; // if we are here, content's property has no value
|
|
|
|
|
|
IPublishedProperty noValueProperty = null;
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
|
|
|
|
|
content = content.Parent;
|
|
|
|
|
|
property = content?.GetProperty(alias);
|
2018-07-21 15:58:49 +02:00
|
|
|
|
if (property != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
noValueProperty = property;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
while (content != null && (property == null || property.HasValue(culture, segment) == false));
|
2018-05-02 13:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
// if we found a content with the property having a value, return that property value
|
|
|
|
|
|
if (property != null && property.HasValue(culture, segment))
|
2018-07-21 15:58:49 +02:00
|
|
|
|
{
|
|
|
|
|
|
value = property.Value<T>(culture, segment);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-05-02 13:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
// if we found a property, even though with no value, return that property value
|
|
|
|
|
|
// because the converter may want to handle the missing value. ie if defaultValue is default,
|
|
|
|
|
|
// either specified or by default, the converter may want to substitute something else.
|
|
|
|
|
|
if (noValueProperty != null)
|
2018-07-21 15:58:49 +02:00
|
|
|
|
{
|
|
|
|
|
|
value = noValueProperty.Value<T>(culture, segment, defaultValue: defaultValue);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-05-02 13:38:45 +02:00
|
|
|
|
|
2018-07-21 15:58:49 +02:00
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
return false;
|
2018-05-02 13:38:45 +02:00
|
|
|
|
}
|
2018-07-24 13:32:29 +02:00
|
|
|
|
|
2018-07-28 08:39:02 +02:00
|
|
|
|
// tries to get a value, falling back onto other languages
|
|
|
|
|
|
private bool TryGetValueWithLanguageFallback<T>(IPublishedProperty property, string culture, string segment, T defaultValue, out T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
|
|
|
|
|
|
if (culture.IsNullOrWhiteSpace()) return false;
|
|
|
|
|
|
|
|
|
|
|
|
var visited = new HashSet<int>();
|
|
|
|
|
|
|
|
|
|
|
|
var language = _localizationService.GetLanguageByIsoCode(culture);
|
|
|
|
|
|
if (language == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (language.FallbackLanguageId == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
var language2Id = language.FallbackLanguageId.Value;
|
|
|
|
|
|
if (visited.Contains(language2Id)) return false;
|
|
|
|
|
|
visited.Add(language2Id);
|
|
|
|
|
|
|
|
|
|
|
|
var language2 = _localizationService.GetLanguageById(language2Id);
|
|
|
|
|
|
if (language2 == null) return false;
|
|
|
|
|
|
var culture2 = language2.IsoCode;
|
|
|
|
|
|
|
|
|
|
|
|
if (property.HasValue(culture2, segment))
|
|
|
|
|
|
{
|
|
|
|
|
|
value = property.Value<T>(culture2, segment);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
language = language2;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// tries to get a value, falling back onto other languages
|
|
|
|
|
|
private bool TryGetValueWithLanguageFallback<T>(IPublishedElement content, string alias, string culture, string segment, T defaultValue, out T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
|
|
|
|
|
|
if (culture.IsNullOrWhiteSpace()) return false;
|
|
|
|
|
|
|
|
|
|
|
|
var visited = new HashSet<int>();
|
|
|
|
|
|
|
|
|
|
|
|
var language = _localizationService.GetLanguageByIsoCode(culture);
|
|
|
|
|
|
if (language == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (language.FallbackLanguageId == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
var language2Id = language.FallbackLanguageId.Value;
|
|
|
|
|
|
if (visited.Contains(language2Id)) return false;
|
|
|
|
|
|
visited.Add(language2Id);
|
|
|
|
|
|
|
|
|
|
|
|
var language2 = _localizationService.GetLanguageById(language2Id);
|
|
|
|
|
|
if (language2 == null) return false;
|
|
|
|
|
|
var culture2 = language2.IsoCode;
|
|
|
|
|
|
|
|
|
|
|
|
if (content.HasValue(alias, culture2, segment))
|
|
|
|
|
|
{
|
|
|
|
|
|
value = content.Value<T>(alias, culture2, segment);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
language = language2;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-24 13:32:29 +02:00
|
|
|
|
// tries to get a value, falling back onto other languages
|
|
|
|
|
|
private bool TryGetValueWithLanguageFallback<T>(IPublishedContent content, string alias, string culture, string segment, T defaultValue, out T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
|
|
|
|
|
|
|
if (culture.IsNullOrWhiteSpace()) return false;
|
|
|
|
|
|
|
|
|
|
|
|
var visited = new HashSet<int>();
|
|
|
|
|
|
|
|
|
|
|
|
// fixme
|
|
|
|
|
|
// _localizationService.GetXxx() is expensive, it deep clones objects
|
|
|
|
|
|
// we want _localizationService.GetReadOnlyXxx() returning IReadOnlyLanguage which cannot be saved back = no need to clone
|
|
|
|
|
|
|
|
|
|
|
|
var language = _localizationService.GetLanguageByIsoCode(culture);
|
|
|
|
|
|
if (language == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (language.FallbackLanguageId == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
var language2Id = language.FallbackLanguageId.Value;
|
|
|
|
|
|
if (visited.Contains(language2Id)) return false;
|
|
|
|
|
|
visited.Add(language2Id);
|
|
|
|
|
|
|
|
|
|
|
|
var language2 = _localizationService.GetLanguageById(language2Id);
|
|
|
|
|
|
if (language2 == null) return false;
|
|
|
|
|
|
var culture2 = language2.IsoCode;
|
|
|
|
|
|
|
|
|
|
|
|
if (content.HasValue(alias, culture2, segment))
|
|
|
|
|
|
{
|
|
|
|
|
|
value = content.Value<T>(alias, culture2, segment);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
language = language2;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-05-02 13:38:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|