|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using Newtonsoft.Json.Linq; |
| 7 | +using Umbraco.Cms.Core; |
| 8 | +using Umbraco.Cms.Core.Configuration.Grid; |
| 9 | +using Umbraco.Cms.Core.Deploy; |
| 10 | +using Umbraco.Cms.Core.Models; |
| 11 | +using Umbraco.Cms.Core.PropertyEditors; |
| 12 | +using Umbraco.Cms.Core.Serialization; |
| 13 | +using Umbraco.Cms.Core.Services; |
| 14 | +using Umbraco.Deploy.Core; |
| 15 | +using Umbraco.Deploy.Infrastructure.Connectors.DataTypeConfigurationConnectors; |
| 16 | +using Umbraco.Extensions; |
| 17 | + |
| 18 | +namespace Umbraco.Deploy.Contrib.DataTypeConfigurationConnectors |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Implements a Grid layout data type configuration connector supporting DocTypeGridEditor. |
| 22 | + /// </summary> |
| 23 | + public class DocTypeGridEditorDataTypeConfigurationConnector : DataTypeConfigurationConnectorBase2 |
| 24 | + { |
| 25 | + private readonly IGridConfig _gridConfig; |
| 26 | + private readonly IContentTypeService _contentTypeService; |
| 27 | + |
| 28 | + /// <inheritdoc /> |
| 29 | + public override IEnumerable<string> PropertyEditorAliases { get; } = new[] |
| 30 | + { |
| 31 | + Constants.PropertyEditors.Aliases.Grid |
| 32 | + }; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes a new instance of the <see cref="DocTypeGridEditorDataTypeConfigurationConnector" /> class. |
| 36 | + /// </summary> |
| 37 | + /// <param name="gridConfig">The grid configuration.</param> |
| 38 | + /// <param name="contentTypeService">The content type service.</param> |
| 39 | + /// <param name="configurationEditorJsonSerializer">The configuration editor JSON serializer.</param> |
| 40 | + public DocTypeGridEditorDataTypeConfigurationConnector(IGridConfig gridConfig, IContentTypeService contentTypeService, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer) |
| 41 | + : base(configurationEditorJsonSerializer) |
| 42 | + { |
| 43 | + _gridConfig = gridConfig; |
| 44 | + _contentTypeService = contentTypeService; |
| 45 | + } |
| 46 | + |
| 47 | + /// <inheritdoc /> |
| 48 | + public override string? ToArtifact(IDataType dataType, ICollection<ArtifactDependency> dependencies, IContextCache contextCache) |
| 49 | + { |
| 50 | + if (dataType.ConfigurationAs<GridConfiguration>() is GridConfiguration gridConfiguration && |
| 51 | + gridConfiguration.Items?.ToObject<GridConfigurationItems>() is GridConfigurationItems gridConfigurationItems) |
| 52 | + { |
| 53 | + // Get all element types (when needed) |
| 54 | + var allElementTypes = new Lazy<IEnumerable<IContentType>>(() => _contentTypeService.GetAll().Where(x => x.IsElement).ToList()); |
| 55 | + |
| 56 | + // Process DTGE editors |
| 57 | + foreach (var gridEditor in GetGridEditors(gridConfigurationItems).Where(IsDocTypeGridEditor)) |
| 58 | + { |
| 59 | + if (gridEditor.Config.TryGetValue("allowedDocTypes", out var allowedDocTypesConfig) && |
| 60 | + allowedDocTypesConfig is JArray allowedDocTypes && |
| 61 | + allowedDocTypes.Count > 0) |
| 62 | + { |
| 63 | + string[] docTypes = allowedDocTypes.Values<string>().WhereNotNull().ToArray(); |
| 64 | + |
| 65 | + // Use regex matching |
| 66 | + AddDependencies(dependencies, allElementTypes.Value.Where(x => docTypes.Any(y => Regex.IsMatch(x.Alias, y)))); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + // Add all element types as dependencies and stop processing |
| 71 | + AddDependencies(dependencies, allElementTypes.Value); |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return base.ToArtifact(dataType, dependencies, contextCache); |
| 78 | + } |
| 79 | + |
| 80 | + private static void AddDependencies(ICollection<ArtifactDependency> dependencies, IEnumerable<IContentType> contentTypes) |
| 81 | + { |
| 82 | + foreach (var contentType in contentTypes) |
| 83 | + { |
| 84 | + dependencies.Add(new ArtifactDependency(contentType.GetUdi(), true, ArtifactDependencyMode.Exist)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Gets the grid editors used within the grid configuration. |
| 90 | + /// </summary> |
| 91 | + /// <param name="gridConfigurationItems">The grid configuration items.</param> |
| 92 | + /// <returns> |
| 93 | + /// The used grid editors (returns all editors if any of the areas allows all editors). |
| 94 | + /// </returns> |
| 95 | + protected virtual IEnumerable<IGridEditorConfig> GetGridEditors(GridConfigurationItems gridConfigurationItems) |
| 96 | + { |
| 97 | + foreach (var gridEditor in _gridConfig.EditorsConfig.Editors) |
| 98 | + { |
| 99 | + if (IsAllowedGridEditor(gridConfigurationItems, gridEditor.Alias)) |
| 100 | + { |
| 101 | + yield return gridEditor; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Determines whether the grid editor alias is allowed in the specified grid configuration. |
| 108 | + /// </summary> |
| 109 | + /// <param name="gridConfigurationItems">The grid configuration items.</param> |
| 110 | + /// <param name="alias">The alias.</param> |
| 111 | + /// <returns> |
| 112 | + /// <c>true</c> if the grid editor alias is allowed in the specified grid configuration; otherwise, <c>false</c>. |
| 113 | + /// </returns> |
| 114 | + protected static bool IsAllowedGridEditor(GridConfigurationItems gridConfigurationItems, string alias) |
| 115 | + => gridConfigurationItems.Layouts.Any(x => x.Areas.Any(y => y.AllowAll || y.Allowed.Contains(alias))); |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Determines whether the grid editor is the DTGE. |
| 119 | + /// </summary> |
| 120 | + /// <param name="gridEditor">The grid editor.</param> |
| 121 | + /// <returns> |
| 122 | + /// <c>true</c> if the grid editor is the DTGE; otherwise, <c>false</c>. |
| 123 | + /// </returns> |
| 124 | + protected static bool IsDocTypeGridEditor(IGridEditorConfig gridEditor) |
| 125 | + => !string.IsNullOrEmpty(gridEditor.View) && gridEditor.View.Contains("doctypegrideditor"); |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// The grid configuration items. |
| 129 | + /// </summary> |
| 130 | + protected sealed class GridConfigurationItems |
| 131 | + { |
| 132 | + /// <summary> |
| 133 | + /// Gets or sets the row configurations. |
| 134 | + /// </summary> |
| 135 | + /// <value> |
| 136 | + /// The row configurations. |
| 137 | + /// </value> |
| 138 | + [JsonProperty("layouts")] |
| 139 | + public GridConfigurationLayout[] Layouts { get; set; } = Array.Empty<GridConfigurationLayout>(); |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// The grid row configuration. |
| 144 | + /// </summary> |
| 145 | + protected sealed class GridConfigurationLayout |
| 146 | + { |
| 147 | + /// <summary> |
| 148 | + /// Gets or sets the areas. |
| 149 | + /// </summary> |
| 150 | + /// <value> |
| 151 | + /// The areas. |
| 152 | + /// </value> |
| 153 | + [JsonProperty("areas")] |
| 154 | + public GridConfigurationLayoutArea[] Areas { get; set; } = Array.Empty<GridConfigurationLayoutArea>(); |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// The grid row configuration area. |
| 159 | + /// </summary> |
| 160 | + protected sealed class GridConfigurationLayoutArea |
| 161 | + { |
| 162 | + /// <summary> |
| 163 | + /// Gets or sets a value indicating whether all grid editors are allowed. |
| 164 | + /// </summary> |
| 165 | + /// <value> |
| 166 | + /// <c>true</c> if all grid editors are allowed; otherwise, <c>false</c>. |
| 167 | + /// </value> |
| 168 | + /// <remarks> |
| 169 | + /// Defaults to <c>true</c>. |
| 170 | + /// </remarks> |
| 171 | + [JsonProperty("allowAll")] |
| 172 | + public bool AllowAll { get; set; } = true; |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// Gets or sets the allowed grid editor aliases. |
| 176 | + /// </summary> |
| 177 | + /// <value> |
| 178 | + /// The allowed grid editor aliases. |
| 179 | + /// </value> |
| 180 | + [JsonProperty("allowed")] |
| 181 | + public string[] Allowed { get; set; } = Array.Empty<string>(); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments