Пример #1
0
        /// <summary>
        /// Adds a new form section or updates an existing form section in the
        /// <paramref name="actionForm"/>. This also handles the creation of
        /// any new form attributes as needed.
        /// </summary>
        /// <param name="actionForm">The <see cref="WorkflowActionForm"/> being updated.</param>
        /// <param name="workflowType">The <see cref="WorkflowType"/> being updated.</param>
        /// <param name="attributeService">The database service that provides access to the attributes.</param>
        /// <param name="formAttributeService">The database service that provides access to the form attributes.</param>
        /// <param name="formSectionService">The database service that provides access to the form sections.</param>
        /// <param name="section">The section view model that will be used as the source of information.</param>
        /// <param name="nextAttributeOrder">The next attribute order number to use when creating a new attribute.</param>
        /// <returns>The <see cref="WorkflowActionFormSection"/> entity that was either updated or created.</returns>
        private static WorkflowActionFormSection AddOrUpdateFormSection(WorkflowActionForm actionForm, WorkflowType workflowType, AttributeService attributeService, WorkflowActionFormAttributeService formAttributeService, WorkflowActionFormSectionService formSectionService, FormSectionViewModel section, List <FormFieldViewModel> formFields, ref int nextAttributeOrder)
        {
            var formSection = actionForm.FormSections.FirstOrDefault(s => s.Guid == section.Guid);

            // If the section was not found then create a new one and add it
            // to the form.
            if (formSection == null)
            {
                formSection = new WorkflowActionFormSection
                {
                    Guid = section.Guid
                };
                actionForm.FormSections.Add(formSection);
                formSectionService.Add(formSection);
            }

            // Update the standard section properties from the view model.
            formSection.Description            = section.Description;
            formSection.Title                  = section.Title;
            formSection.ShowHeadingSeparator   = section.ShowHeadingSeparator;
            formSection.SectionTypeValueId     = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueId(section.Type);
            formSection.SectionVisibilityRules = section.VisibilityRule?.FromViewModel(formFields);

            // Loop through all fields that need to be either added or updated.
            for (int fieldOrder = 0; fieldOrder < section.Fields.Count; fieldOrder++)
            {
                var field = section.Fields[fieldOrder];

                var formField = AddOrUpdateFormField(actionForm, workflowType, attributeService, formAttributeService, formSection, field, formFields, ref nextAttributeOrder);
                formField.Order = fieldOrder;
            }

            return(formSection);
        }
Пример #2
0
        /// <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);
            }
        }