Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created March 17, 2023 15:46
Show Gist options
  • Select an option

  • Save KevinJump/2684b0d40cef1bd5c727ca818c6e27ca to your computer and use it in GitHub Desktop.

Select an option

Save KevinJump/2684b0d40cef1bd5c727ca818c6e27ca to your computer and use it in GitHub Desktop.

Revisions

  1. KevinJump renamed this gist Mar 17, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. KevinJump created this gist Mar 17, 2023.
    168 changes: 168 additions & 0 deletions UmbNavValueMapper.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,168 @@
    using System.Linq;

    using Jumoo.TranslationManager.Core;
    using Jumoo.TranslationManager.Core.Models;
    using Jumoo.TranslationManager.Core.ValueMappers;

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;

    using Umbraco.Core;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Services;

    namespace TMMultiSetLanguages.App_Code
    {
    public class UmbNavValueMapper : BaseValueMapper, IValueMapper, IPropertyValueMapper
    {
    public UmbNavValueMapper(
    IContentService contentService,
    IDataTypeService dataTypeService,
    IContentTypeService contentTypeService,
    ILogger logger) : base(contentService, dataTypeService, contentTypeService, logger)
    { }

    public string Name => "AaronSadler.UmbNav";

    public override string[] Editors => new[] { "AaronSadler.UmbNav" };

    public TranslationValue GetSourceValue(string displayName, string propertyTypeAlias, object value, CultureInfoView culture)
    => GetSourceValue(displayName, propertyTypeAlias, "", value, culture);

    public TranslationValue GetSourceValue(string displayName, string propertyTypeAlias, string path, object value, CultureInfoView culture)
    {
    var array = GetValueAsArray(value);
    if (array == null || array.Count == 0) return null;

    var translationValue = new TranslationValue($"{displayName}", propertyTypeAlias, $"{path}/UmbNav");

    for (int i = 0; i < array.Count; i++)
    {
    var entry = GetSourceEntry($"{displayName} [{i}]", propertyTypeAlias, path, array[i]);
    if (entry != null)
    {
    translationValue.InnerValues.Add($"{i}", entry);
    }
    }

    return translationValue;
    }

    public TranslationValue GetSourceEntry(string displayName, string propertyTypeAlias, string path, JToken entry)
    {
    if (entry == null) return null;

    var translationValue = new TranslationValue(displayName, propertyTypeAlias, path);

    // link name
    var linkNameValue = new TranslationValue($"{displayName} Link", Constants.PropertyEditors.Aliases.TextBox, path + "/Name")
    {
    Value = entry["name"] == null ? "" : entry.Value<string>("name")
    };
    translationValue.InnerValues.Add("name", linkNameValue);

    var titleValue = new TranslationValue($"{displayName} Title", Constants.PropertyEditors.Aliases.TextBox, path + "/Title")
    {
    Value = entry["title"] == null ? "" : entry.Value<string>("title")
    };

    translationValue.InnerValues.Add("title", titleValue);

    if (entry["children"] != null)
    {
    var children = entry.Value<JArray>("children");
    for (int c = 0; c < children.Count; c++)
    {
    var childValue = GetSourceEntry($"{displayName} child [{c}]", propertyTypeAlias, path + "/child", children[c]);
    if (childValue != null)
    {
    translationValue.InnerValues.Add($"Child-{c}", childValue);
    }
    }
    }

    return translationValue;
    }


    public object GetTargetValue(string propertyTypeAlias, object sourceValue, TranslationValue values, CultureInfoView sourceCulture, CultureInfoView targetCulture)
    {
    var array = GetValueAsArray(sourceValue);

    for (int i = 0; i < array.Count; i++)
    {
    var translatedItem = values.GetInnerValue($"{i}");
    if (translatedItem != null)
    {
    var result = GetTargetEntry(propertyTypeAlias, array[i], translatedItem);
    if (result != null)
    {
    array[i] = result;
    }
    }
    }

    return JsonConvert.SerializeObject(array);
    }

    public JToken GetTargetEntry(string propertyTypeAlias, JToken entry, TranslationValue values)
    {
    var linkValue = values.GetInnerValue("name");
    if (linkValue != null)
    {
    entry["name"] = linkValue.Value;
    }

    var titleValue = values.GetInnerValue("title");
    if (titleValue != null)
    {
    entry["title"] = titleValue.Value;
    }

    var children = entry.Value<JArray>("children");

    for (int c = 0; c < children.Count; c++)
    {
    var childValue = values.GetInnerValue($"Child-{c}");
    if (childValue != null)
    {
    var result = GetTargetEntry(propertyTypeAlias, children[c], childValue);
    children[c] = result as JObject;
    }
    }

    return entry;
    }

    /// <summary>
    /// Helper to convert the source into a JArray
    /// </summary>
    /// <returns>JArray or null if cannot convert.</returns>
    private JArray GetValueAsArray(object value)
    {
    if (value == null)
    return null;

    var attempt = value.TryConvertTo<string>();
    if (!attempt.Success) return null;

    var stringValue = attempt.Result;
    if (stringValue.IsNullOrWhiteSpace())
    return null;

    try
    {
    var array = JsonConvert.DeserializeObject<JArray>(stringValue);
    if (array == null || !array.Any())
    return null;

    return array;
    }
    catch
    {
    return null;
    }

    }
    }
    }