Exemple #1
0
        /// <summary>
        /// Handles the Delete event of the gFormTemplates control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gFormTemplates_Delete(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                int templateId      = ( int )e.RowKeyValue;
                var workflowService = new WorkflowService(rockContext);
                var hasTemplates    = workflowService.Queryable().Any(wf => wf.WorkflowType.FormBuilderTemplateId == templateId);
                if (hasTemplates)
                {
                    ShowAlert("This template has workflows assigned to it", ModalAlertType.Warning);
                }
                else
                {
                    var formTemplateService = new WorkflowFormBuilderTemplateService(rockContext);
                    var template            = formTemplateService.Get(templateId);
                    formTemplateService.Delete(template);
                    rockContext.SaveChanges();
                }
            }

            BindGrid();
        }
        public BlockActionResult SaveTemplate(Guid guid, TemplateEditDetailViewModel template)
        {
            using (var rockContext = new RockContext())
            {
                var isNew           = false;
                var templateService = new WorkflowFormBuilderTemplateService(rockContext);
                WorkflowFormBuilderTemplate formTemplate;

                // If we are provided an empty guid (not null) then we treat that
                // to mean create a new template.
                if (guid == Guid.Empty)
                {
                    formTemplate = new WorkflowFormBuilderTemplate();
                    templateService.Add(formTemplate);
                    isNew = true;
                }
                else
                {
                    formTemplate = templateService.Get(guid);
                }

                // Perform a check to see if we now have a valid template object
                // and that the user has permission to edit/create it.
                if (formTemplate == null || !formTemplate.IsAuthorized(Authorization.EDIT, RequestContext.CurrentPerson))
                {
                    return(ActionBadRequest("Invalid template."));
                }

                // Store all the template details.
                formTemplate.Name                    = template.Name.Trim();
                formTemplate.Description             = template.Description?.Trim();
                formTemplate.IsActive                = template.IsActive;
                formTemplate.IsLoginRequired         = template.IsLoginRequired;
                formTemplate.FormHeader              = template.FormHeader?.Trim();
                formTemplate.FormFooter              = template.FormFooter?.Trim();
                formTemplate.AllowPersonEntry        = template.AllowPersonEntry;
                formTemplate.PersonEntrySettingsJson = template.PersonEntry?.FromViewModel().ToJson();
                formTemplate.CompletionSettingsJson  = template.CompletionAction?.FromViewModel().ToJson();

                var confirmationEmail = template.ConfirmationEmail?.FromViewModel(rockContext);

                // Special check for template logic. We don't have attributes yet
                // so check if the confirmation e-mail uses the special values to
                // indicate which attribute to be used at runtime. If we find one
                // of those special values then translate it to the enum.
                if (confirmationEmail != null)
                {
                    if (confirmationEmail.RecipientAttributeGuid == RecipientPersonGuid)
                    {
                        confirmationEmail.Destination            = Rock.Workflow.FormBuilder.FormConfirmationEmailDestination.Person;
                        confirmationEmail.RecipientAttributeGuid = null;
                    }
                    else if (confirmationEmail.RecipientAttributeGuid == RecipientSpouseGuid)
                    {
                        confirmationEmail.Destination            = Rock.Workflow.FormBuilder.FormConfirmationEmailDestination.Spouse;
                        confirmationEmail.RecipientAttributeGuid = null;
                    }
                }

                formTemplate.ConfirmationEmailSettingsJson = confirmationEmail?.ToJson();

                rockContext.SaveChanges();

                if (isNew)
                {
                    return(ActionContent(System.Net.HttpStatusCode.Created, this.GetCurrentPageUrl(new Dictionary <string, string>
                    {
                        ["FormTemplateId"] = formTemplate.Id.ToString()
                    })));
                }

                // Ensure navigation properties will work now.
                formTemplate = templateService.Get(formTemplate.Id);

                return(ActionOk(GetTemplateDetailViewModel(formTemplate, rockContext)));
            }
        }