public override Item HandlePack(ItemIdentifier id)
        {
            var fs   = new Umbraco.Forms.Data.Storage.FormStorage();
            var form = fs.GetForm(Guid.Parse(id.Id));

            if (form == null)
            {
                return(null);
            }

            var item = new UmbracoForm();

            item.ItemId = id;
            item.Name   = form.Name;

            //Handle forms and fields
            var formPath = Umbraco.Forms.Core.Configuration.ContourFolder + "/data/forms/" + id.Id + ".json";

            item.Resources.Add(formPath);

            //handle submit to node id
            if (form.GoToPageOnSubmit > 0)
            {
                item.GoToPageOnSubmitId = ExecutionContext.DatabasePersistence.GetUniqueId(form.GoToPageOnSubmit);
                item.Dependencies.Add(item.GoToPageOnSubmitId.ToString(), ItemProviders.ProviderIDCollection.documentItemProviderGuid);
            }
            //Parse field settings and field prevalue source settings
            foreach (var field in form.AllFields)
            {
                var parsed = ParseSettings(field.FieldType.Settings(), field.Settings, item);
                if (parsed.Count > 0)
                {
                    item.FieldSettings.Add(field.Id, parsed);
                }

                if (field.PreValueSourceId != Guid.Empty && !item.PrevalueSourceSettings.ContainsKey(field.PreValueSourceId))
                {
                    var prvParsed = ParseSettings(field.PreValueSource.Type.Settings(), field.PreValueSource.Settings, item);
                    if (prvParsed.Count > 0)
                    {
                        item.PrevalueSourceSettings.Add(field.PreValueSourceId, prvParsed);
                    }

                    item.Resources.Add(Umbraco.Forms.Core.Configuration.ContourFolder + "/data/prevalueSources/" + field.PreValueSourceId + ".json");
                }
            }



            //parse data source settings
            if (form.HasDataSource())
            {
                using (var dss = new Umbraco.Forms.Data.Storage.DataSourceStorage())
                {
                    var dataSource = dss.GetDataSource(form.DataSource.Id);

                    if (dataSource != null)
                    {
                        var dsParsed = ParseSettings(dataSource.Type.Settings(), dataSource.Settings, item);

                        if (dsParsed.Count > 0)
                        {
                            item.DataSourceSettings = dsParsed;
                        }

                        //add as file to item
                        item.Resources.Add(Umbraco.Forms.Core.Configuration.ContourFolder + "/data/datasources/" + form.DataSource.Id.ToString() + ".json");
                    }
                }
            }

            //Parse workflow settings
            if (form.WorkflowIds.Count > 0)
            {
                using (var wfs = new Umbraco.Forms.Data.Storage.WorkflowStorage())
                {
                    foreach (var wfId in form.WorkflowIds)
                    {
                        var workflow = wfs.GetWorkflow(wfId);
                        if (workflow != null)
                        {
                            var wfParsed = ParseSettings(workflow.Type.Settings(), workflow.Settings, item);
                            if (wfParsed.Count > 0)
                            {
                                item.WorkflowSettings.Add(wfId, wfParsed);
                            }

                            //add file to item
                            item.Resources.Add(Umbraco.Forms.Core.Configuration.ContourFolder + "/data/workflows/" + wfId.ToString() + ".json");
                        }
                    }
                }
            }

            return(item);
        }
        public override Item HandleExtract(Item item)
        {
            var umbracoform = (UmbracoForm)item;

            //migrate workflowsettings
            if (umbracoform.WorkflowSettings.Count > 0)
            {
                using (var wfs = new Umbraco.Forms.Data.Storage.WorkflowStorage())
                {
                    foreach (var workflowSetting in umbracoform.WorkflowSettings)
                    {
                        var workflow = wfs.GetWorkflow(workflowSetting.Key);
                        if (workflow != null)
                        {
                            workflow.Settings = ReplaceSettings(workflow.Settings, workflowSetting.Value) as Dictionary <string, string>;
                            wfs.UpdateWorkflow(workflow);
                        }
                    }
                }
            }


            //migrate datasource settings
            if (umbracoform.DataSourceId != Guid.Empty)
            {
                using (var dss = new Umbraco.Forms.Data.Storage.DataSourceStorage())
                {
                    var datasource = dss.GetDataSource(umbracoform.DataSourceId);
                    if (datasource != null)
                    {
                        datasource.Settings = ReplaceSettings(datasource.Settings, umbracoform.DataSourceSettings) as Dictionary <string, string>;
                        dss.UpdateDataSource(datasource);
                    }
                }
            }


            //migrate prevaluesource settings
            if (umbracoform.PrevalueSourceSettings.Count > 0)
            {
                using (var pvs = new Umbraco.Forms.Data.Storage.PrevalueSourceStorage())
                {
                    foreach (var prevalueSettings in umbracoform.PrevalueSourceSettings)
                    {
                        var prevalueSource = pvs.GetPrevalueSource(prevalueSettings.Key);
                        if (prevalueSource != null)
                        {
                            prevalueSource.Settings = ReplaceSettings(prevalueSource.Settings, prevalueSettings.Value) as Dictionary <string, string>;
                            pvs.UpdatePreValueSource(prevalueSource);
                        }
                    }
                }
            }


            //migrate field and form settings
            using (var fs = new Umbraco.Forms.Data.Storage.FormStorage())
            {
                var form = fs.GetForm(Guid.Parse(umbracoform.ItemId.Id));
                if (form != null)
                {
                    //handle submit to node id
                    if (umbracoform.GoToPageOnSubmitId != Guid.Empty)
                    {
                        form.GoToPageOnSubmit = ExecutionContext.DatabasePersistence.GetNodeId(umbracoform.GoToPageOnSubmitId);
                    }

                    if (umbracoform.FieldSettings.Count > 0)
                    {
                        foreach (var fieldSetting in umbracoform.FieldSettings)
                        {
                            var field = form.AllFields.Where(x => x.Id == fieldSetting.Key).FirstOrDefault();
                            if (field != null)
                            {
                                field.Settings = ReplaceSettings(field.Settings, fieldSetting.Value) as Dictionary <string, string>;
                            }
                        }


                        fs.UpdateForm(form);
                    }
                }
            }

            return(item);
        }
Example #3
0
        public override Item HandlePack(ItemIdentifier id)
        {
            var fs = new Umbraco.Forms.Data.Storage.FormStorage();

            Forms.Core.Form form;

            var formsDataLocation = GetFormsDataLocation();

            try
            {
                // If there is no form on the target disk or cache this method raises a System.NullReferenceException
                // See https://github.com/umbraco/Umbraco.Courier.FormsProvider/issues/3
                form = fs.GetForm(Guid.Parse(id.Id));
            }
            catch (Exception ex)
            {
                return(null);
            }

            if (form == null)
            {
                return(null);
            }

            var item = new UmbracoForm();

            item.ItemId = id;
            item.Name   = form.Name;

            //Handle forms and fields
            var formPath = formsDataLocation + "/forms/" + id.Id + ".json";

            item.Resources.Add(formPath);

            //handle submit to node id
            if (form.GoToPageOnSubmit > 0)
            {
                item.GoToPageOnSubmitId = ExecutionContext.DatabasePersistence.GetUniqueId(form.GoToPageOnSubmit);
                item.Dependencies.Add(item.GoToPageOnSubmitId.ToString(), ItemProviders.ProviderIDCollection.documentItemProviderGuid);
            }
            //Parse field settings and field prevalue source settings
            foreach (var field in form.AllFields)
            {
                var parsed = ParseSettings(field.FieldType.Settings(), field.Settings, item);
                if (parsed.Count > 0)
                {
                    item.FieldSettings.Add(field.Id, parsed);
                }

                if (field.PreValueSourceId != Guid.Empty && !item.PrevalueSourceSettings.ContainsKey(field.PreValueSourceId))
                {
                    var prvParsed = ParseSettings(field.PreValueSource.Type.Settings(), field.PreValueSource.Settings, item);
                    if (prvParsed.Count > 0)
                    {
                        item.PrevalueSourceSettings.Add(field.PreValueSourceId, prvParsed);
                    }

                    item.Resources.Add(formsDataLocation + "/prevalueSources/" + field.PreValueSourceId + ".json");
                }
            }



            //parse data source settings
            if (form.HasDataSource())
            {
                using (var dss = new Umbraco.Forms.Data.Storage.DataSourceStorage())
                {
                    var dataSource = dss.GetDataSource(form.DataSource.Id);

                    if (dataSource != null)
                    {
                        var dsParsed = ParseSettings(dataSource.Type.Settings(), dataSource.Settings, item);

                        if (dsParsed.Count > 0)
                        {
                            item.DataSourceSettings = dsParsed;
                        }

                        //add as file to item
                        item.Resources.Add(formsDataLocation + "/datasources/" + form.DataSource.Id.ToString() + ".json");
                    }
                }
            }

            //Parse workflow settings
            if (form.WorkflowIds.Count > 0)
            {
                using (var wfs = new Umbraco.Forms.Data.Storage.WorkflowStorage())
                {
                    foreach (var wfId in form.WorkflowIds)
                    {
                        var workflow = wfs.GetWorkflow(wfId);
                        if (workflow != null)
                        {
                            var wfParsed = ParseSettings(workflow.Type.Settings(), workflow.Settings, item);
                            if (wfParsed.Count > 0)
                            {
                                item.WorkflowSettings.Add(wfId, wfParsed);
                            }

                            //add file to item
                            item.Resources.Add(formsDataLocation + "/workflows/" + wfId.ToString() + ".json");
                        }
                    }
                }
            }

            return(item);
        }