Created
June 17, 2015 11:44
-
-
Save TimGeyssens/a0a9988a68705cf66800 to your computer and use it in GitHub Desktop.
Contour import form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Form ImportFormFromXml(XmlDocument xd, bool insert, string name= "") | |
{ | |
Form f = null; | |
XmlSerializer deserializer = new XmlSerializer(typeof(Form), "UmbracoContour"); | |
f = (Form)deserializer.Deserialize(new XmlNodeReader(xd.DocumentElement)); | |
var formOrig = (Form)deserializer.Deserialize(new XmlNodeReader(xd.DocumentElement)); ; | |
foreach (Field fld in f.AllFields) | |
{ | |
fld.Id = Guid.Empty; | |
// set fieldtypes | |
fld.FieldType = FieldTypeProviderCollection.Instance.GetProvider(fld.FieldTypeId); | |
//set prevaluetype | |
if (fld.FieldType.SupportsPrevalues) | |
fld.PreValueSource = FieldPreValueSource.GetDefaultProvider(); | |
} | |
if (insert) | |
{ | |
FormStorage fs = new FormStorage(); | |
f.Id = Guid.NewGuid(); | |
fs.InsertForm(f,true); | |
var mappings = new Dictionary<Guid, Guid>(); | |
for (int i = 0; i < f.AllFields.Count; i++ ) | |
{ | |
if (formOrig.AllFields[i].Id != Guid.Empty && !mappings.ContainsKey(formOrig.AllFields[i].Id)) | |
mappings.Add(formOrig.AllFields[i].Id, f.AllFields[i].Id); | |
} | |
foreach(var field in f.AllFields) | |
{ | |
if (field.Condition.Rules.Any()) | |
{ | |
foreach (var condi in field.Condition.Rules) | |
condi.Field = mappings[condi.Field]; | |
} | |
} | |
fs.UpdateForm(f); | |
fs.Dispose(); | |
//Set form security | |
umbraco.BusinessLogic.User cur = umbraco.BusinessLogic.User.GetCurrent(); | |
if (cur != null && !cur.IsAdmin()) | |
{ | |
UserFormSecurity ufs = new UserFormSecurity(); | |
ufs.Form = f.Id; | |
ufs.User = cur.Id; | |
ufs.HasAccess = true; | |
ufs.AllowInEditor = true; | |
ufs.SecurityType = Umbraco.Forms.Core.Enums.FormSecurityType.Full; | |
UserFormSecurityStorage ufss = new UserFormSecurityStorage(); | |
ufss.InsertUserFormSecurity(ufs); | |
ufss.Dispose(); | |
} | |
} | |
return f; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment