|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using Newtonsoft.Json.Linq; |
| 6 | +using Umbraco.Core; |
| 7 | +using Umbraco.Core.Deploy; |
| 8 | +using Umbraco.Core.Models; |
| 9 | +using Umbraco.Core.Services; |
| 10 | + |
| 11 | +namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors |
| 12 | +{ |
| 13 | + /* |
| 14 | + [ |
| 15 | + { |
| 16 | + "caption": "Page B Internal Link made here", |
| 17 | + "link": 1059, |
| 18 | + "newWindow": false, |
| 19 | + "internal": 1059, |
| 20 | + "edit": false, |
| 21 | + "isInternal": true, |
| 22 | + "internalName": "Page B", |
| 23 | + "internalIcon": "icon-document", |
| 24 | + "type": "internal", |
| 25 | + "title": "Page B Internal Link made here" |
| 26 | + } |
| 27 | + ] |
| 28 | + */ |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Implements a value connector for the new related links editor (storing Udis). |
| 32 | + /// </summary> |
| 33 | + public class RelatedLinksValueConnector : IValueConnector |
| 34 | + { |
| 35 | + private readonly IEntityService _entityService; |
| 36 | + |
| 37 | + public RelatedLinksValueConnector(IEntityService entityService) |
| 38 | + { |
| 39 | + if (entityService == null) throw new ArgumentNullException(nameof(entityService)); |
| 40 | + _entityService = entityService; |
| 41 | + } |
| 42 | + |
| 43 | + public string GetValue(Property property, ICollection<ArtifactDependency> dependencies) |
| 44 | + { |
| 45 | + var value = property.Value as string; |
| 46 | + |
| 47 | + if (string.IsNullOrWhiteSpace(value)) |
| 48 | + return null; |
| 49 | + |
| 50 | + if (value.DetectIsJson() == false) |
| 51 | + return null; |
| 52 | + |
| 53 | + var relatedLinks = JsonConvert.DeserializeObject<JArray>(value); |
| 54 | + |
| 55 | + if (relatedLinks == null) |
| 56 | + return string.Empty; |
| 57 | + |
| 58 | + foreach (var relatedLink in relatedLinks) |
| 59 | + { |
| 60 | + //Get the value from the JSON object |
| 61 | + var isInternal = Convert.ToBoolean(relatedLink["isInternal"]); |
| 62 | + |
| 63 | + //We are only concerned about internal links |
| 64 | + if (!isInternal) |
| 65 | + continue; |
| 66 | + |
| 67 | + var linkIntId = Convert.ToInt32(relatedLink["link"]); |
| 68 | + |
| 69 | + //get the guid corresponding to the id |
| 70 | + //it *can* fail if eg the id points to a deleted content, |
| 71 | + //and then we use an empty guid |
| 72 | + var guidAttempt = _entityService.GetKeyForId(linkIntId, UmbracoObjectTypes.Document); |
| 73 | + if (guidAttempt.Success) |
| 74 | + { |
| 75 | + // replace the picked content id by the corresponding Udi as a dependancy |
| 76 | + var udi = new GuidUdi(Constants.UdiEntityType.Document, guidAttempt.Result); |
| 77 | + dependencies.Add(new ArtifactDependency(udi, false, ArtifactDependencyMode.Exist)); |
| 78 | + |
| 79 | + //Set the current relatedlink 'internal' & 'link' properties to UDIs & not int's |
| 80 | + relatedLink["link"] = udi.ToString(); |
| 81 | + relatedLink["internal"] = udi.ToString(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + //Serialise the new updated object with UDIs in it to JSON to transfer across the wire |
| 86 | + return JsonConvert.SerializeObject(relatedLinks); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + public void SetValue(IContentBase content, string alias, string value) |
| 91 | + { |
| 92 | + if (string.IsNullOrWhiteSpace(value)) |
| 93 | + { |
| 94 | + content.SetValue(alias, value); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + var relatedLinks = JsonConvert.DeserializeObject<JArray>(value); |
| 99 | + |
| 100 | + foreach (var relatedLink in relatedLinks) |
| 101 | + { |
| 102 | + //Get the value from the JSON object |
| 103 | + var isInternal = Convert.ToBoolean(relatedLink["isInternal"]); |
| 104 | + |
| 105 | + //We are only concerned about internal links |
| 106 | + if (!isInternal) |
| 107 | + continue; |
| 108 | + |
| 109 | + //Get the UDI value in the JSON |
| 110 | + var pickedUdi = GuidUdi.Parse(relatedLink["link"].ToString()); |
| 111 | + |
| 112 | + //Lets use entitiy sevice to get the int ID for this item on the new environment |
| 113 | + //Get the Id corresponding to the Guid |
| 114 | + //it *should* succeed when deploying, due to dependencies management |
| 115 | + //nevertheless, assume it can fail, and then create an invalid localLink |
| 116 | + var idAttempt = _entityService.GetIdForKey(pickedUdi.Guid, UmbracoObjectTypes.Document); |
| 117 | + |
| 118 | + //Update the JSON back to the int ids on this env |
| 119 | + relatedLink["link"] = idAttempt.Success ? idAttempt.Result : 0; |
| 120 | + relatedLink["internal"] = idAttempt.Success ? idAttempt.Result : 0; |
| 121 | + } |
| 122 | + |
| 123 | + //Save the updated JSON with replaced UDIs for int IDs |
| 124 | + content.SetValue(alias, JsonConvert.SerializeObject(relatedLinks)); |
| 125 | + } |
| 126 | + |
| 127 | + public virtual IEnumerable<string> PropertyEditorAliases => new[] { Constants.PropertyEditors.RelatedLinksAlias }; |
| 128 | + |
| 129 | + } |
| 130 | +} |
0 commit comments