public BlockActionResult SaveForm(Guid formGuid, FormSettingsViewModel formSettings) { if (formGuid == Guid.Empty || formSettings == null) { return(ActionBadRequest("Invalid parameters provided.")); } using (var rockContext = new RockContext()) { var formBuilderEntityTypeId = EntityTypeCache.Get(typeof(Rock.Workflow.Action.FormBuilder)).Id; var workflowType = new WorkflowTypeService(rockContext).Get(formGuid); if (workflowType == null || !workflowType.IsFormBuilder) { return(ActionBadRequest("Specified workflow type is not a form builder.")); } // Find the action type that represents the form. var actionForm = workflowType.ActivityTypes .SelectMany(a => a.ActionTypes) .Where(a => a.EntityTypeId == formBuilderEntityTypeId) .FirstOrDefault(); if (actionForm == null || actionForm.WorkflowForm == null) { return(ActionBadRequest("Specified workflow is not properly configured for use in form builder.")); } UpdateWorkflowType(formSettings, actionForm.WorkflowForm, workflowType, rockContext); rockContext.SaveChanges(); return(ActionOk()); } }
/// <summary> /// Updates a workflow type with values from the view model. /// </summary> /// <param name="formSettings">The view model that contains the settings to be updated.</param> /// <param name="actionForm">The <see cref="WorkflowActionForm"/> to be updated.</param> /// <param name="workflowType">The <see cref="WorkflowType"/> to be updated.</param> /// <param name="rockContext">The database context to use for data lookups.</param> private static void UpdateWorkflowType(FormSettingsViewModel formSettings, WorkflowActionForm actionForm, WorkflowType workflowType, RockContext rockContext) { actionForm.Header = formSettings.HeaderContent; actionForm.Footer = formSettings.FooterContent; actionForm.AllowPersonEntry = formSettings.AllowPersonEntry; if (formSettings.AllowPersonEntry) { actionForm.PersonEntryAddressEntryOption = formSettings.PersonEntry.Address.ToPersonEntryOption(); actionForm.PersonEntryGroupLocationTypeValueId = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueId(formSettings.PersonEntry.AddressType); actionForm.PersonEntryAutofillCurrentPerson = formSettings.PersonEntry.AutofillCurrentPerson; actionForm.PersonEntryBirthdateEntryOption = formSettings.PersonEntry.Birthdate.ToPersonEntryOption(); actionForm.PersonEntryCampusStatusValueId = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueId(formSettings.PersonEntry.CampusStatus); actionForm.PersonEntryCampusTypeValueId = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueId(formSettings.PersonEntry.CampusType); actionForm.PersonEntryConnectionStatusValueId = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueId(formSettings.PersonEntry.ConnectionStatus); actionForm.PersonEntryEmailEntryOption = formSettings.PersonEntry.Email.ToPersonEntryOption(); actionForm.PersonEntryGenderEntryOption = formSettings.PersonEntry.Gender.ToPersonEntryOption(); actionForm.PersonEntryHideIfCurrentPersonKnown = formSettings.PersonEntry.HideIfCurrentPersonKnown; actionForm.PersonEntryMaritalStatusEntryOption = formSettings.PersonEntry.MaritalStatus.ToPersonEntryOption(); actionForm.PersonEntryMobilePhoneEntryOption = formSettings.PersonEntry.MobilePhone.ToPersonEntryOption(); actionForm.PersonEntryRecordStatusValueId = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueId(formSettings.PersonEntry.RecordStatus); actionForm.PersonEntryCampusIsVisible = formSettings.PersonEntry.ShowCampus; actionForm.PersonEntrySpouseEntryOption = formSettings.PersonEntry.SpouseEntry.ToPersonEntryOption(); actionForm.PersonEntrySpouseLabel = formSettings.PersonEntry.SpouseLabel; } UpdateFormSections(formSettings, actionForm, workflowType, rockContext); // Update the workflow type properties. workflowType.Name = formSettings.General.Name.Trim(); workflowType.Description = formSettings.General.Description.Trim(); workflowType.FormStartDateTime = formSettings.General.EntryStarts?.DateTime; workflowType.FormEndDateTime = formSettings.General.EntryEnds?.DateTime; workflowType.IsLoginRequired = formSettings.General.IsLoginRequired; workflowType.CategoryId = CategoryCache.GetId(formSettings.General.Category.Value.AsGuid()).Value; // Update the workflow type form template. if (formSettings.General.Template.HasValue) { workflowType.FormBuilderTemplateId = new WorkflowFormBuilderTemplateService(rockContext) .GetId(formSettings.General.Template.Value); } else { workflowType.FormBuilderTemplateId = null; } // Update the settings stored in the JSON blob. var settings = workflowType.FormBuilderSettingsJson.FromJsonOrNull <Rock.Workflow.FormBuilder.FormSettings>() ?? new Rock.Workflow.FormBuilder.FormSettings(); settings.CompletionAction = formSettings.Completion.FromViewModel(); settings.ConfirmationEmail = formSettings.ConfirmationEmail.FromViewModel(rockContext); settings.NotificationEmail = formSettings.NotificationEmail.FromViewModel(rockContext); settings.CampusSetFrom = formSettings.CampusSetFrom?.FromViewModel(); workflowType.FormBuilderSettingsJson = settings.ToJson(); }
/// <summary> /// Gets the view model that represents a workflow form builder form in /// its entirety. /// </summary> /// <param name="actionForm">The <see cref="WorkflowActionForm"/> to be represented by the view model.</param> /// <param name="workflowType">The <see cref="WorkflowType"/> to be represented by the view model.</param> /// <param name="rockContext">The database context used for data lookup.</param> /// <returns>A view model that represents the form.</returns> private static FormSettingsViewModel GetFormSettingsViewModel(WorkflowActionForm actionForm, WorkflowType workflowType, RockContext rockContext) { var settings = workflowType.FormBuilderSettingsJson.FromJsonOrNull <Rock.Workflow.FormBuilder.FormSettings>(); var form = new FormSettingsViewModel { HeaderContent = actionForm.Header, FooterContent = actionForm.Footer, General = GetFormGeneralViewModel(workflowType), Sections = GetFormSectionViewModels(actionForm), ConfirmationEmail = settings?.ConfirmationEmail.ToViewModel(rockContext), NotificationEmail = settings?.NotificationEmail.ToViewModel(rockContext), Completion = settings?.CompletionAction.ToViewModel(), CampusSetFrom = settings?.CampusSetFrom?.ToViewModel() }; // Build the person entry settings. if (actionForm.AllowPersonEntry) { form.AllowPersonEntry = true; form.PersonEntry = new FormPersonEntryViewModel { Address = actionForm.PersonEntryAddressEntryOption.ToFormFieldVisibility(), AddressType = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueGuid(actionForm.PersonEntryGroupLocationTypeValueId), AutofillCurrentPerson = actionForm.PersonEntryAutofillCurrentPerson, Birthdate = actionForm.PersonEntryBirthdateEntryOption.ToFormFieldVisibility(), CampusStatus = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueGuid(actionForm.PersonEntryCampusStatusValueId), CampusType = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueGuid(actionForm.PersonEntryCampusTypeValueId), ConnectionStatus = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueGuid(actionForm.PersonEntryConnectionStatusValueId), Email = actionForm.PersonEntryEmailEntryOption.ToFormFieldVisibility(), Gender = actionForm.PersonEntryGenderEntryOption.ToFormFieldVisibility(), HideIfCurrentPersonKnown = actionForm.PersonEntryHideIfCurrentPersonKnown, MaritalStatus = actionForm.PersonEntryMaritalStatusEntryOption.ToFormFieldVisibility(), MobilePhone = actionForm.PersonEntryMobilePhoneEntryOption.ToFormFieldVisibility(), RecordStatus = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueGuid(actionForm.PersonEntryRecordStatusValueId), ShowCampus = actionForm.PersonEntryCampusIsVisible, SpouseEntry = actionForm.PersonEntrySpouseEntryOption.ToFormFieldVisibility(), SpouseLabel = actionForm.PersonEntrySpouseLabel }; } return(form); }
/// <summary> /// Handles logic for updating all the form sections as well as attributes. /// This will create any new items and delete any removed items as well. /// </summary> /// <param name="formSettings">The form settings that contain all the configuration information.</param> /// <param name="actionForm">The <see cref="WorkflowActionForm"/> entity being updated.</param> /// <param name="workflowType">The <see cref="WorkflowType"/> that is being updated.</param> /// <param name="rockContext">The database context to operate in.</param> private static void UpdateFormSections(FormSettingsViewModel formSettings, WorkflowActionForm actionForm, WorkflowType workflowType, RockContext rockContext) { var attributeService = new AttributeService(rockContext); var formAttributeService = new WorkflowActionFormAttributeService(rockContext); var formSectionService = new WorkflowActionFormSectionService(rockContext); var nextAttributeOrder = actionForm.FormAttributes != null && actionForm.FormAttributes.Any() ? actionForm.FormAttributes.Select(a => a.Order).Max() + 1 : 0; if (formSettings.Sections != null) { // Get all the section identifiers that are sticking around. var allValidSectionGuids = formSettings.Sections .Select(s => s.Guid) .ToList(); // Get all the attribute identifiers that are sticking around. var allValidAttributeGuids = formSettings.Sections .SelectMany(s => s.Fields) .Select(f => f.Guid) .ToList(); // Find all sections that no longer exist in this form. var sectionsToDelete = actionForm.FormSections .Where(s => !allValidSectionGuids.Contains(s.Guid)) .ToList(); // Find all form attributes that no longer exist in this form. var formAttributesToDelete = actionForm.FormAttributes .Where(a => !allValidAttributeGuids.Contains(a.Attribute.Guid)) .ToList(); var allFormFields = formSettings.Sections.SelectMany(s => s.Fields).ToList(); // Delete all sections that no longer exist in this form. sectionsToDelete.ForEach(s => { formSectionService.Delete(s); }); // Delete all form attributes that no longer exist in this form. formAttributesToDelete.ForEach(a => { if (a.Attribute.IsSystem) { attributeService.Delete(a.Attribute); } formAttributeService.Delete(a); }); // Loop through all sections that need to be either added or updated. for (int sectionOrder = 0; sectionOrder < formSettings.Sections.Count; sectionOrder++) { var section = formSettings.Sections[sectionOrder]; var formSection = AddOrUpdateFormSection(actionForm, workflowType, attributeService, formAttributeService, formSectionService, section, allFormFields, ref nextAttributeOrder); formSection.Order = sectionOrder; } } else { // Remove all form attributes and sections. var nonUserAttributes = actionForm.FormAttributes .Select(a => a.Attribute) .Where(a => a.IsSystem) .ToList(); attributeService.DeleteRange(nonUserAttributes); formAttributeService.DeleteRange(actionForm.FormAttributes); formSectionService.DeleteRange(actionForm.FormSections); } }