示例#1
0
        /// <summary>
        /// Gets the list of view models that represent all the sections and fields
        /// of a form.
        /// </summary>
        /// <param name="actionForm">The <see cref="WorkflowActionForm"/> to be represented.</param>
        /// <returns>A collection of view models that represent that sections that will be edited.</returns>
        private static List <FormSectionViewModel> GetFormSectionViewModels(WorkflowActionForm actionForm)
        {
            var sectionViewModels = new List <FormSectionViewModel>();

            foreach (var workflowFormSection in actionForm.FormSections.OrderBy(s => s.Order))
            {
                // Create the basic section view model.
                var sectionViewModel = new FormSectionViewModel
                {
                    Guid                 = workflowFormSection.Guid,
                    Description          = workflowFormSection.Description,
                    Fields               = new List <FormFieldViewModel>(),
                    ShowHeadingSeparator = workflowFormSection.ShowHeadingSeparator,
                    Title                = workflowFormSection.Title,
                    Type                 = Rock.Blocks.WorkFlow.FormBuilder.Utility.GetDefinedValueGuid(workflowFormSection.SectionTypeValueId),
                    VisibilityRule       = workflowFormSection.SectionVisibilityRules?.ToViewModel()
                };

                // Get all the form attributes for this section.
                var formAttributes = actionForm.FormAttributes
                                     .Where(a => a.ActionFormSectionId == workflowFormSection.Id)
                                     .ToList();

                // Loop through each form attribute and make a view model out of it.
                foreach (var formAttribute in formAttributes.OrderBy(a => a.Order))
                {
                    var attribute = AttributeCache.Get(formAttribute.AttributeId);
                    var fieldType = FieldTypeCache.Get(attribute.FieldTypeId);

                    if (fieldType == null || fieldType.Field == null)
                    {
                        continue;
                    }

                    sectionViewModel.Fields.Add(new FormFieldViewModel
                    {
                        ConfigurationValues = fieldType.Field.GetPublicConfigurationValues(attribute.ConfigurationValues, Field.ConfigurationValueUsage.Configure, null),
                        DefaultValue        = fieldType.Field.GetPublicEditValue(attribute.DefaultValue, attribute.ConfigurationValues),
                        Description         = attribute.Description,
                        FieldTypeGuid       = fieldType.Guid,
                        Guid           = attribute.Guid,
                        IsHideLabel    = formAttribute.HideLabel,
                        IsRequired     = attribute.IsRequired,
                        IsShowOnGrid   = attribute.IsGridColumn,
                        Key            = attribute.Key,
                        Name           = attribute.Name,
                        Size           = formAttribute.ColumnSize ?? 12,
                        VisibilityRule = formAttribute.FieldVisibilityRules?.ToViewModel()
                    });
                }

                sectionViewModels.Add(sectionViewModel);
            }

            return(sectionViewModels);
        }
示例#2
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);
        }