|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using Newtonsoft.Json.Linq; |
| 6 | +using Umbraco.Cms.Core; |
| 7 | +using Umbraco.Cms.Core.Configuration.Grid; |
| 8 | +using Umbraco.Cms.Core.Models; |
| 9 | +using Umbraco.Cms.Core.PropertyEditors; |
| 10 | +using Umbraco.Cms.Core.Serialization; |
| 11 | +using Umbraco.Cms.Core.Services; |
| 12 | +using Umbraco.Cms.Core.Strings; |
| 13 | +using Umbraco.Deploy.Infrastructure.Artifacts; |
| 14 | +using Umbraco.Deploy.Infrastructure.Migrators; |
| 15 | +using Umbraco.Extensions; |
| 16 | +using static Umbraco.Cms.Core.PropertyEditors.BlockGridConfiguration; |
| 17 | + |
| 18 | +namespace Umbraco.Deploy.Contrib.Migrators; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Migrates the <see cref="DataTypeArtifact" /> to replace the legacy/obsoleted <see cref="Constants.PropertyEditors.Aliases.Grid" /> editor with <see cref="Constants.PropertyEditors.Aliases.BlockGrid" /> and support DTGE grid editors. |
| 22 | +/// </summary> |
| 23 | +public class ReplaceDocTypeGridEditorDataTypeArtifactMigrator : ReplaceGridDataTypeArtifactMigrator |
| 24 | +{ |
| 25 | + private readonly IContentTypeService _contentTypeService; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Gets or sets a value indicating whether to add the default DTGE grid editor (if not configured in the grid.editors.config.js files). |
| 29 | + /// </summary> |
| 30 | + /// <value> |
| 31 | + /// <c>true</c> if the default DTGE grid editor is added; otherwise, <c>false</c>. |
| 32 | + /// </value> |
| 33 | + /// <remarks> |
| 34 | + /// Defaults to <c>true</c>. |
| 35 | + /// </remarks> |
| 36 | + protected bool AddDefaultDocTypeGridEditor { get; init; } = true; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes a new instance of the <see cref="ReplaceDocTypeGridEditorDataTypeArtifactMigrator" /> class. |
| 40 | + /// </summary> |
| 41 | + /// <param name="propertyEditors">The property editors.</param> |
| 42 | + /// <param name="configurationEditorJsonSerializer">The configuration editor JSON serializer.</param> |
| 43 | + /// <param name="contentTypeService">The content type service.</param> |
| 44 | + /// <param name="dataTypeService">The data type service.</param> |
| 45 | + /// <param name="shortStringHelper">The short string helper.</param> |
| 46 | + /// <param name="gridConfig">The grid configuration.</param> |
| 47 | + public ReplaceDocTypeGridEditorDataTypeArtifactMigrator(PropertyEditorCollection propertyEditors, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer, IContentTypeService contentTypeService, IDataTypeService dataTypeService, IShortStringHelper shortStringHelper, IGridConfig gridConfig) |
| 48 | + : base(propertyEditors, configurationEditorJsonSerializer, contentTypeService, dataTypeService, shortStringHelper, gridConfig) |
| 49 | + => _contentTypeService = contentTypeService; |
| 50 | + |
| 51 | + /// <inheritdoc /> |
| 52 | + protected override IEnumerable<(string, BlockGridBlockConfiguration)> MigrateGridEditors(IEnumerable<GridConfigurationLayout> gridLayouts) |
| 53 | + { |
| 54 | + // Migrate regular grid editors (DTGE editors are skipped) |
| 55 | + foreach ((string alias, BlockGridBlockConfiguration blockGridBlockConfiguration) in base.MigrateGridEditors(gridLayouts)) |
| 56 | + { |
| 57 | + yield return (alias, blockGridBlockConfiguration); |
| 58 | + } |
| 59 | + |
| 60 | + // Migrate DTGE editors |
| 61 | + var allElementTypes = GetAllElementTypes().ToList(); |
| 62 | + var migratedContentTypeKeys = new HashSet<Guid>(); |
| 63 | + foreach (IGridEditorConfig gridEditor in GetGridEditors(gridLayouts).Where(IsDocTypeGridEditor)) |
| 64 | + { |
| 65 | + foreach (Guid contentElementTypeKey in MigrateDocTypeGridEditor(gridEditor, allElementTypes)) |
| 66 | + { |
| 67 | + // Avoid DocTypeGridEditors returning duplicate block configurations for the same content element type |
| 68 | + if (migratedContentTypeKeys.Add(contentElementTypeKey)) |
| 69 | + { |
| 70 | + yield return (gridEditor.Alias, new BlockGridBlockConfiguration() |
| 71 | + { |
| 72 | + ContentElementTypeKey = contentElementTypeKey, |
| 73 | + Label = gridEditor.Config.TryGetValue("nameTemplate", out var nameTemplateConfig) && nameTemplateConfig is string nameTemplate && !string.IsNullOrEmpty(nameTemplate) |
| 74 | + ? nameTemplate |
| 75 | + : gridEditor.NameTemplate, |
| 76 | + EditorSize = gridEditor.Config.TryGetValue("overlaySize", out var overviewSizeConfig) && overviewSizeConfig is string overviewSize && !string.IsNullOrEmpty(overviewSize) |
| 77 | + ? overviewSize |
| 78 | + : null, |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <inheritdoc /> |
| 86 | + protected override IEnumerable<IGridEditorConfig> GetGridEditors() |
| 87 | + { |
| 88 | + foreach (IGridEditorConfig gridEditor in base.GetGridEditors()) |
| 89 | + { |
| 90 | + yield return gridEditor; |
| 91 | + } |
| 92 | + |
| 93 | + if (AddDefaultDocTypeGridEditor) |
| 94 | + { |
| 95 | + yield return new GridEditor() |
| 96 | + { |
| 97 | + Name = "Doc Type", |
| 98 | + Alias = "docType", |
| 99 | + View = "/App_Plugins/DocTypeGridEditor/Views/doctypegrideditor.html", |
| 100 | + Render = "/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditor.cshtml", |
| 101 | + Icon = "icon-item-arrangement", |
| 102 | + }; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <inheritdoc /> |
| 107 | + protected override Guid? MigrateGridEditor(IGridEditorConfig gridEditor) |
| 108 | + // Skip migrating DocTypeGridEditor using base implementation (as that creates a new element type) |
| 109 | + => IsDocTypeGridEditor(gridEditor) is false ? base.MigrateGridEditor(gridEditor) : null; |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Migrates the DocTypeGridEditor. |
| 113 | + /// </summary> |
| 114 | + /// <param name="gridEditor">The grid editor.</param> |
| 115 | + /// <param name="allElementTypes">All element types.</param> |
| 116 | + /// <returns> |
| 117 | + /// The keys of the content element types of the migrated grid editor. |
| 118 | + /// </returns> |
| 119 | + protected virtual IEnumerable<Guid> MigrateDocTypeGridEditor(IGridEditorConfig gridEditor, IEnumerable<IContentType> allElementTypes) |
| 120 | + { |
| 121 | + if (gridEditor.Config.TryGetValue("allowedDocTypes", out var allowedDocTypesConfig) && |
| 122 | + allowedDocTypesConfig is JArray allowedDocTypes && |
| 123 | + allowedDocTypes.Values<string>().WhereNotNull().ToArray() is string[] docTypes && |
| 124 | + docTypes.Length > 0) |
| 125 | + { |
| 126 | + // Use regex matching |
| 127 | + return allElementTypes.Where(x => docTypes.Any(y => Regex.IsMatch(x.Alias, y))).Select(x => x.Key); |
| 128 | + } |
| 129 | + |
| 130 | + // Return all |
| 131 | + return allElementTypes.Select(x => x.Key); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Gets all element types. |
| 136 | + /// </summary> |
| 137 | + /// <returns> |
| 138 | + /// Returns all element types. |
| 139 | + /// </returns> |
| 140 | + /// <remarks> |
| 141 | + /// The default implementation excludes element types with aliases that start with <c>gridLayout_</c>, <c>gridRow_</c>, <c>gridEditor_</c> or <c>gridSettings_</c>. |
| 142 | + /// </remarks> |
| 143 | + protected virtual IEnumerable<IContentType> GetAllElementTypes() |
| 144 | + { |
| 145 | + static bool IsAllowedElementType(string alias) => !alias.StartsWith("gridLayout_") && !alias.StartsWith("gridRow_") && !alias.StartsWith("gridEditor_") && !alias.StartsWith("gridSettings_"); |
| 146 | + |
| 147 | + return _contentTypeService.GetAllElementTypes().Where(x => IsAllowedElementType(x.Alias)); |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Determines whether the grid editor is the DocTypeGridEditor. |
| 152 | + /// </summary> |
| 153 | + /// <param name="gridEditor">The grid editor.</param> |
| 154 | + /// <returns> |
| 155 | + /// <c>true</c> if the grid editor is the DocTypeGridEditor; otherwise, <c>false</c>. |
| 156 | + /// </returns> |
| 157 | + protected static bool IsDocTypeGridEditor(IGridEditorConfig gridEditor) |
| 158 | + => gridEditor.View?.Contains("doctypegrideditor", StringComparison.OrdinalIgnoreCase) is true; |
| 159 | +} |
0 commit comments