예제 #1
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);
            }
        }