|
10 | 10 | using Umbraco.Core.Models;
|
11 | 11 | using Umbraco.Core.Models.Editors;
|
12 | 12 | using Umbraco.Core.Models.PublishedContent;
|
| 13 | +using Umbraco.Core.Persistence; |
13 | 14 | using Umbraco.Core.PropertyEditors;
|
14 | 15 | using Umbraco.Core.Services;
|
15 | 16 | using Umbraco.Web;
|
@@ -149,6 +150,77 @@ private static string GetContentTypeAliasByGuid(Guid contentTypeGuid)
|
149 | 150 | string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypeAliasByGuid_", contentTypeGuid),
|
150 | 151 | () => Services.ContentTypeService.GetAliasByGuid(contentTypeGuid));
|
151 | 152 | }
|
| 153 | + |
| 154 | + public static void RemapDocTypeAlias(string oldAlias, string newAlias, Transaction transaction = null) |
| 155 | + { |
| 156 | + var db = ApplicationContext.Current.DatabaseContext.Database; |
| 157 | + |
| 158 | + // Update references in property data |
| 159 | + // We do 2 very similar replace statements, but one is without spaces in the JSON, the other is with spaces |
| 160 | + // as we can't guarantee what format it will actually get saved in |
| 161 | + var sql1 = string.Format(@"UPDATE cmsPropertyData |
| 162 | +SET dataNtext = CAST(REPLACE(REPLACE(CAST(dataNtext AS nvarchar(max)), '""dtgeContentTypeAlias"":""{0}""', '""dtgeContentTypeAlias"":""{1}""'), '""dtgeContentTypeAlias"": ""{0}""', '""dtgeContentTypeAlias"": ""{1}""') AS ntext) |
| 163 | +WHERE dataNtext LIKE '%""dtgeContentTypeAlias"":""{0}""%' OR dataNtext LIKE '%""dtgeContentTypeAlias"": ""{0}""%'", oldAlias, newAlias); |
| 164 | + |
| 165 | + if (transaction == null) |
| 166 | + { |
| 167 | + using (var tr = db.GetTransaction()) |
| 168 | + { |
| 169 | + db.Execute(sql1); |
| 170 | + tr.Complete(); |
| 171 | + } |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + db.Execute(sql1); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + public static void RemapPropertyAlias(string docTypeAlias, string oldAlias, string newAlias, Transaction transaction = null) |
| 180 | + { |
| 181 | + var db = ApplicationContext.Current.DatabaseContext.Database; |
| 182 | + |
| 183 | + // Update references in property data |
| 184 | + // We have to do it in code because there could be nested JSON so |
| 185 | + // we need to make sure it only replaces at the specific level only |
| 186 | + Action doQuery = () => |
| 187 | + { |
| 188 | + var rows = GetPropertyDataRows(docTypeAlias); |
| 189 | + foreach (var row in rows) |
| 190 | + { |
| 191 | + var tokens = row.Data.SelectTokens(string.Format("$..controls[?(@.value.dtgeContentTypeAlias == '{0}' && @.value.value.{1})].value", docTypeAlias, oldAlias)).ToList(); |
| 192 | + if (tokens.Any()) |
| 193 | + { |
| 194 | + foreach (var token in tokens) |
| 195 | + { |
| 196 | + token["value"][oldAlias].Rename(newAlias); |
| 197 | + } |
| 198 | + db.Execute("UPDATE [cmsPropertyData] SET [dataNtext] = @0 WHERE [id] = @1", row.RawData, row.Id); |
| 199 | + } |
| 200 | + } |
| 201 | + }; |
| 202 | + |
| 203 | + if (transaction == null) |
| 204 | + { |
| 205 | + using (var tr = db.GetTransaction()) |
| 206 | + { |
| 207 | + doQuery(); |
| 208 | + tr.Complete(); |
| 209 | + } |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + doQuery(); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + private static IEnumerable<JsonDbRow> GetPropertyDataRows(string docTypeAlias) |
| 218 | + { |
| 219 | + var db = ApplicationContext.Current.DatabaseContext.Database; |
| 220 | + return db.Query<JsonDbRow>(string.Format( |
| 221 | + @"SELECT [id], [dataNtext] as [rawdata] FROM cmsPropertyData WHERE dataNtext LIKE '%""dtgeContentTypeAlias"":""{0}""%' OR dataNtext LIKE '%""dtgeContentTypeAlias"": ""{0}""%'", |
| 222 | + docTypeAlias)).ToList(); |
| 223 | + } |
152 | 224 | }
|
153 | 225 |
|
154 | 226 | public class ContentTypeContainer
|
|
0 commit comments