11using  System ; 
22using  System . Linq ; 
33using  System . Text ; 
4+ using  Newtonsoft . Json ; 
5+ using  Umbraco . Cms . Core ; 
6+ using  Umbraco . Extensions ; 
47using  System . Threading . Tasks ; 
58using  Umbraco . Cms . Core . Models ; 
9+ using  DataBlockConverter . Dtos ; 
610using  Umbraco . Cms . Core . Services ; 
711using  System . Collections . Generic ; 
812using  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} 
0 commit comments