|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using Umbraco.Core; |
| 6 | +using Umbraco.Core.Deploy; |
| 7 | +using Umbraco.Core.Models; |
| 8 | +using Umbraco.Core.Services; |
| 9 | + |
| 10 | +namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Represents a value connector for the Cogworks.Meganav property editor. |
| 14 | + /// </summary> |
| 15 | + /// <seealso cref="Umbraco.Core.Deploy.IValueConnector" /> |
| 16 | + public class MeganavValueConnector : IValueConnector |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// The entity service. |
| 20 | + /// </summary> |
| 21 | + private readonly IEntityService _entityService; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets the property editor aliases that the value converter supports by default. |
| 25 | + /// </summary> |
| 26 | + public IEnumerable<string> PropertyEditorAliases => new[] { "Cogworks.Meganav" }; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <see cref="MeganavValueConnector"/> class. |
| 30 | + /// </summary> |
| 31 | + /// <param name="entityService">The entity service.</param> |
| 32 | + /// <exception cref="ArgumentNullException">entityService</exception> |
| 33 | + public MeganavValueConnector(IEntityService entityService) |
| 34 | + { |
| 35 | + _entityService = entityService ?? throw new ArgumentNullException("entityService"); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Gets the deploy property corresponding to a content property. |
| 40 | + /// </summary> |
| 41 | + /// <param name="property">The content property.</param> |
| 42 | + /// <param name="dependencies">The content dependencies.</param> |
| 43 | + /// <returns> |
| 44 | + /// The deploy property value. |
| 45 | + /// </returns> |
| 46 | + public string GetValue(Property property, ICollection<ArtifactDependency> dependencies) |
| 47 | + { |
| 48 | + var value = property.Value as string; |
| 49 | + if (String.IsNullOrWhiteSpace(value) || !StringExtensions.DetectIsJson(value)) |
| 50 | + { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + // Parse links and convert ID to UDI |
| 55 | + var rootLinks = ParseLinks(JArray.Parse(value), dependencies, Direction.ToArtifact); |
| 56 | + |
| 57 | + return rootLinks.ToString(Formatting.None); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Sets a content property value using a deploy property. |
| 62 | + /// </summary> |
| 63 | + /// <param name="content">The content item.</param> |
| 64 | + /// <param name="alias">The property alias.</param> |
| 65 | + /// <param name="value">The deploy property value.</param> |
| 66 | + public void SetValue(IContentBase content, string alias, string value) |
| 67 | + { |
| 68 | + if (String.IsNullOrWhiteSpace(value)) |
| 69 | + { |
| 70 | + // Save empty value |
| 71 | + content.SetValue(alias, value); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (!StringExtensions.DetectIsJson(value)) |
| 76 | + { |
| 77 | + // Skip invalid values |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Parse links and convert UDI back to local ID |
| 82 | + var rootLinks = ParseLinks(JArray.Parse(value), null, Direction.FromArtifact); |
| 83 | + |
| 84 | + content.SetValue(alias, rootLinks.ToString(Formatting.None)); |
| 85 | + } |
| 86 | + |
| 87 | + private JArray ParseLinks(JArray links, ICollection<ArtifactDependency> dependencies, Direction direction) |
| 88 | + { |
| 89 | + foreach (var link in links) |
| 90 | + { |
| 91 | + if (direction == Direction.ToArtifact) |
| 92 | + { |
| 93 | + GuidUdi guidUdi = null; |
| 94 | + var id = link.Value<int>("id"); |
| 95 | + if (id != 0) |
| 96 | + { |
| 97 | + var keyForId = _entityService.GetKeyForId(id, UmbracoObjectTypes.Document); |
| 98 | + if (keyForId.Success) |
| 99 | + { |
| 100 | + guidUdi = new GuidUdi(Constants.UdiEntityType.Document, keyForId.Result); |
| 101 | + dependencies.Add(new ArtifactDependency(guidUdi, false, ArtifactDependencyMode.Exist)); |
| 102 | + } |
| 103 | + } |
| 104 | + link["id"] = guidUdi?.ToString(); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + var id = 0; |
| 109 | + if (GuidUdi.TryParse(link.Value<string>("id"), out GuidUdi guidUdi)) |
| 110 | + { |
| 111 | + var idForUdi = _entityService.GetIdForUdi(guidUdi); |
| 112 | + if (idForUdi.Success) |
| 113 | + { |
| 114 | + id = idForUdi.Result; |
| 115 | + } |
| 116 | + } |
| 117 | + link["id"] = id; |
| 118 | + } |
| 119 | + |
| 120 | + // Parse children |
| 121 | + var children = link.Value<JArray>("children"); |
| 122 | + if (children != null) |
| 123 | + { |
| 124 | + link["children"] = ParseLinks(children, dependencies, direction); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return links; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments