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;
|
|
|
|
|
|
using ValueFallback = Umbraco.Core.Constants.Content.ValueFallback;
|
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>
|
|
|
|
|
|
/// <param name="localizationService"></param>
|
|
|
|
|
|
public PublishedValueFallback(ILocalizationService localizationService)
|
|
|
|
|
|
{
|
|
|
|
|
|
_localizationService = localizationService;
|
|
|
|
|
|
}
|
2018-05-02 13:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-07-24 13:32:29 +02:00
|
|
|
|
public object GetValue(IPublishedProperty property, string culture, string segment, object defaultValue)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
// no fallback here
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-07-24 13:32:29 +02:00
|
|
|
|
public T GetValue<T>(IPublishedProperty property, string culture, string segment, T defaultValue)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
// no fallback here
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-07-24 13:32:29 +02:00
|
|
|
|
public object GetValue(IPublishedElement content, string alias, string culture, string segment, object defaultValue)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
// no fallback here
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-07-24 13:32:29 +02:00
|
|
|
|
public T GetValue<T>(IPublishedElement content, string alias, string culture, string segment, T defaultValue)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
// no fallback here
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-07-24 13:32:29 +02:00
|
|
|
|
public object GetValue(IPublishedContent content, string alias, string culture, string segment, object defaultValue, int fallback)
|
2018-05-02 13:38:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
// is that ok?
|
2018-07-24 13:32:29 +02:00
|
|
|
|
return GetValue<object>(content, alias, culture, segment, defaultValue, fallback);
|
2018-05-02 13:38:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-07-24 13:32:29 +02:00
|
|
|
|
public virtual T GetValue<T>(IPublishedContent content, string alias, string culture, string segment, T defaultValue, int fallback)
|
2018-07-21 15:58:49 +02:00
|
|
|
|
{
|
2018-07-24 13:32:29 +02:00
|
|
|
|
switch (fallback)
|
2018-07-21 15:58:49 +02:00
|
|
|
|
{
|
2018-07-24 13:32:29 +02:00
|
|
|
|
case ValueFallback.None:
|
|
|
|
|
|
case ValueFallback.Default:
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
|
case ValueFallback.Recurse:
|
|
|
|
|
|
return TryGetValueWithRecursiveFallback(content, alias, culture, segment, defaultValue, out var value1) ? value1 : defaultValue;
|
|
|
|
|
|
case ValueFallback.Language:
|
|
|
|
|
|
return TryGetValueWithLanguageFallback(content, alias, culture, segment, defaultValue, out var value2) ? value2 : defaultValue;
|
2018-07-21 15:58:49 +02:00
|
|
|
|
default:
|
2018-07-24 13:32:29 +02:00
|
|
|
|
throw new NotSupportedException($"Fallback {GetType().Name} does not support policy code '{fallback}'.");
|
2018-07-21 15:58:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-24 13:32:29 +02:00
|
|
|
|
// tries to get a value, recursing the tree
|
|
|
|
|
|
protected 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
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
}
|