Skip to content

Commit 25512ec

Browse files
committed
Added content transfer
1 parent 2edde28 commit 25512ec

File tree

7 files changed

+116
-14
lines changed

7 files changed

+116
-14
lines changed

DataBlockConverter/ConvertApiController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public ActionResult ConvertNCInContentType(int id)
139139
PropertyEditorAlias = blockList.EditorAlias,
140140
ValueStorageType = dataType.DatabaseType,
141141
Name = propertyType.Name,
142-
Alias = propertyType.Alias + "BL",
142+
Alias = string.Format(_dataBlockConverterService.GetAliasFormatting(), propertyType.Alias),
143143
CreateDate = DateTime.Now,
144144
Description = propertyType.Description,
145145
Mandatory = propertyType.Mandatory,
@@ -165,5 +165,11 @@ public ActionResult ConvertNCInContentType(int id)
165165
return ValidationProblem("Failed to convert to block list.");
166166
}
167167
}
168+
169+
[HttpGet]
170+
public void TransferContent(int id)
171+
{
172+
_dataBlockConverterService.TransferContent(id);
173+
}
168174
}
169175
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using System.Collections.Generic;
6+
7+
namespace DataBlockConverter.Dtos
8+
{
9+
public class BlockList
10+
{
11+
public BlockListUdi layout { get; set; }
12+
public List<Dictionary<string, string>> contentData { get; set; }
13+
public List<Dictionary<string, string>> settingsData { get; set; }
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json;
2+
3+
namespace DataBlockConverter.Dtos
4+
{
5+
public class BlockListUdi
6+
{
7+
[JsonProperty("Umbraco.BlockList")]
8+
public List<Dictionary<string, string>> contentUdi { get; set; }
9+
10+
[JsonIgnore]
11+
public List<Dictionary<string, string>> settingsUdi { get; set; }
12+
public BlockListUdi(List<Dictionary<string, string>> items, List<Dictionary<string, string>> settings)
13+
{
14+
this.contentUdi = items;
15+
this.settingsUdi = settings;
16+
}
17+
}
18+
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
using System;
2-
using System.Linq;
3-
using System.Text;
4-
using System.Threading.Tasks;
5-
using System.Collections.Generic;
6-
7-
namespace DataBlockConverter.Core.Dtos
1+
namespace DataBlockConverter.Core.Dtos
82
{
93
public class DataBlockConverterSettings
104
{
115
public const string DataBlockConverter = "DataBlockConverter";
126
public string NameFormatting { get; set; } = "[Block list] - {0}";
7+
public string AliasFormatting { get; set; } = "{0}BL";
138
}
14-
}
9+
}

DataBlockConverter/Services/DataBlockConverterService.cs

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System;
22
using System.Linq;
33
using System.Text;
4+
using Newtonsoft.Json;
5+
using Umbraco.Cms.Core;
6+
using Umbraco.Extensions;
47
using System.Threading.Tasks;
58
using Umbraco.Cms.Core.Models;
9+
using DataBlockConverter.Dtos;
610
using Umbraco.Cms.Core.Services;
711
using System.Collections.Generic;
812
using DataBlockConverter.Core.Dtos;
@@ -19,21 +23,23 @@ namespace DataBlockConverter.Core.Services
1923
{
2024
public class DataBlockConverterService : IDataBlockConverterService
2125
{
26+
private readonly IContentService _contentService;
2227
private readonly IDataTypeService _dataTypeService;
2328
private readonly IContentTypeService _contentTypeService;
24-
2529
private readonly IDataValueEditorFactory _dataValueEditorFactory;
2630
private readonly PropertyEditorCollection _propertyEditorCollection;
2731
private readonly IOptions<DataBlockConverterSettings> _dataBlockConverterSettings;
2832
private readonly IConfigurationEditorJsonSerializer _configurationEditorJsonSerializer;
2933

30-
public DataBlockConverterService(IDataTypeService dataTypeService,
34+
public DataBlockConverterService(IContentService contentService,
35+
IDataTypeService dataTypeService,
3136
IContentTypeService contentTypeService,
3237
IDataValueEditorFactory dataValueEditorFactory,
3338
PropertyEditorCollection propertyEditorCollection,
3439
IOptions<DataBlockConverterSettings> dataBlockConverterSettings,
3540
IConfigurationEditorJsonSerializer configurationEditorJsonSerializer)
3641
{
42+
_contentService = contentService;
3743
_dataTypeService = dataTypeService;
3844
_contentTypeService = contentTypeService;
3945
_dataValueEditorFactory = dataValueEditorFactory;
@@ -94,9 +100,70 @@ public IEnumerable<CustomDisplayDataType> GetAllNCDataTypes()
94100
return dataTypes;
95101
}
96102

97-
public string GetNameFormatting()
103+
public void TransferContent(int id)
98104
{
99-
return _dataBlockConverterSettings.Value.NameFormatting;
105+
var node = _contentService.GetById(id);
106+
if (node == null)
107+
return;
108+
109+
var allNC = node.Properties.Where(x => x.PropertyType.PropertyEditorAlias == PropertyEditors.Aliases.NestedContent);
110+
111+
foreach (var nc in allNC)
112+
{
113+
var ncValues = JsonConvert.DeserializeObject<IEnumerable<Dictionary<string, string>>>(nc.GetValue().ToString());
114+
115+
116+
var contentData = new List<Dictionary<string, string>>();
117+
var contentUdiList = new List<Dictionary<string, string>>();
118+
119+
string[] defaultNC = new string[]
120+
{
121+
"name",
122+
"ncContentTypeAlias",
123+
"PropType",
124+
"key"
125+
};
126+
127+
foreach (var ncValue in ncValues)
128+
{
129+
var rawContentType = ncValue.FirstOrDefault(x => x.Key == "ncContentTypeAlias");
130+
var contentType = _contentTypeService.GetAllElementTypes().FirstOrDefault(x => x.Alias == rawContentType.Value);
131+
var contentUdi = new GuidUdi("element", System.Guid.NewGuid()).ToString();
132+
var values = ncValue.Where(x => !defaultNC.Contains(x.Key));
133+
134+
var content = new Dictionary<string, string>
135+
{
136+
{"contentTypeKey", contentType.Key.ToString() },
137+
{"udi", contentUdi },
138+
};
139+
140+
foreach (var value in values)
141+
content.Add(value.Key, value.Value);
142+
143+
144+
contentData.Add(content);
145+
146+
contentUdiList.Add(new Dictionary<string, string>
147+
{
148+
{"contentUdi", contentUdi },
149+
});
150+
}
151+
152+
var blockList = new BlockList()
153+
{
154+
layout = new BlockListUdi(contentUdiList, new List<Dictionary<string, string>>()),
155+
contentData = contentData,
156+
settingsData = new List<Dictionary<string, string>>()
157+
158+
};
159+
160+
node.SetValue(string.Format(GetAliasFormatting(), nc.Alias), JsonConvert.SerializeObject(blockList));
161+
}
162+
163+
_contentService.Save(node);
100164
}
165+
166+
public string GetNameFormatting() => _dataBlockConverterSettings.Value.NameFormatting;
167+
public string GetAliasFormatting() => _dataBlockConverterSettings.Value.AliasFormatting;
101168
}
102169
}

DataBlockConverter/Services/IDataBlockConverterService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace DataBlockConverter.Core.Services
1111
{
1212
public interface IDataBlockConverterService
1313
{
14+
void TransferContent(int id);
1415
string GetNameFormatting();
16+
string GetAliasFormatting();
1517
IDataType? CreateBLDataType(IDataType ncDataType);
1618
IEnumerable<CustomDisplayDataType> GetAllNCDataTypes();
1719
}

DataBlockConverter/wwwroot/backoffice/dataBlockConverter/overview.controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ angular.module("umbraco").controller("dataBlockConverter.overview.controller", f
2929
}
3030
});
3131

32-
3332
vm.convertDataType = function (dataType) {
3433
$http.get("/umbraco/api/ConvertApi/ConverNCDataType?id=" + dataType.id).then(function (response) {
3534
notificationsService.success(title, "");

0 commit comments

Comments
 (0)