Ejemplo n.º 1
0
        protected override ViewModel EnrichModel(ViewModel model)
        {
            BaseFormModel baseForm = base.EnrichModel(model) as BaseFormModel;

            if (baseForm != null)
            {
                if (Request.HttpMethod == "POST")
                {
                    FormModel form = baseForm as FormModel;

                    // MapRequestFormData validates model as well
                    MapRequestFormData(form);

                    if (ModelState.IsValid)
                    {
                        try
                        {
                            //Add logic to save form data here
                            foreach (var postAction in form.FormPostActions)
                            {
                                string formHandlerName = string.Empty;

                                try
                                {
                                    formHandlerName = postAction.FormHandler;

                                    var formHandler = FormHandlerRegistry.Get(formHandlerName);
                                    if (formHandler != null)
                                    {
                                        formHandler.ProcessForm(Request.Form, form.FormFields, postAction);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    if (!string.IsNullOrEmpty(form.ErrorRedirect.Url))
                                    {
                                        Log.Error(ex, "Error occured while processing form data with form handler: {0}. Error message: {1}", formHandlerName, ex.Message);
                                        return(new RedirectModel(form.ErrorRedirect.Url));
                                    }
                                    else
                                    {
                                        throw ex;
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(form.SuccessRedirect.Url))
                            {
                                return(new RedirectModel(form.SuccessRedirect.Url));
                            }
                            else
                            {
                                return(new RedirectModel(WebRequestContext.Localization.Path));
                            }
                        }
                        catch (Exception ex)
                        {
                            if (!string.IsNullOrEmpty(form.ErrorRedirect.Url))
                            {
                                Log.Error(ex, "Error occured while saving form data.");
                                return(new RedirectModel(form.ErrorRedirect.Url));
                            }
                            else
                            {
                                throw ex;
                            }
                        }
                    }
                }
                else
                {
                    // Here we can add logic to enrich our model

                    // Load options from category
                    foreach (var field in baseForm.FormFields.Where(f => !string.IsNullOrEmpty(f.OptionsCategory)))
                    {
                        if (field.FieldType == FieldType.DropDown || field.FieldType == FieldType.CheckBox || field.FieldType == FieldType.RadioButton)
                        {
                            List <OptionModel> options = TaxonomyProvider.LoadOptionsFromCategory(field.OptionsCategory, WebRequestContext.Localization);
                            field.OptionsCategoryList = options;
                        }
                    }
                }
            }

            return(baseForm);
        }
Ejemplo n.º 2
0
        protected override ViewModel EnrichModel(ViewModel model)
        {
            FormCollectionModel baseFormCollection = base.EnrichModel(model) as FormCollectionModel;

            if (baseFormCollection != null)
            {
                if (baseFormCollection.Forms != null)
                {
                    if (Request.HttpMethod == "POST")
                    {
                        bool status = true;

                        // MapRequestFormData validates model as well
                        MapRequestFormData(baseFormCollection.Forms);

                        if (string.Compare(baseFormCollection.EnableRecaptcha, "Enable") == 0)
                        {
                            var    response  = Request["g-recaptcha-response"];
                            string secretKey = WebRequestContext.Localization.GetConfigValue("core.recaptchaSecretKey");

                            var client = new WebClient();
                            var result = client.DownloadString(string.Format(WebRequestContext.Localization.GetConfigValue("core.recaptchaValidateUrl"), secretKey, response));
                            var obj    = JObject.Parse(result);
                            status          = (bool)obj.SelectToken("success");
                            ViewBag.Message = status ? "" : WebRequestContext.Localization.GetConfigValue("core.recaptchaErrorMessage");
                        }
                        if (ModelState.IsValid && status)
                        {
                            try
                            {
                                // Loading the OptionCategory list from TemData to avoid broker calls
                                foreach (var form in baseFormCollection.Forms)
                                {
                                    if (form.FormFields != null)
                                    {
                                        foreach (var field in form.FormFields.Where(f => !string.IsNullOrEmpty(f.OptionsCategory)))
                                        {
                                            if (field.FieldType == FieldType.DropDown || field.FieldType == FieldType.CheckBox || field.FieldType == FieldType.RadioButton)
                                            {
                                                field.OptionsCategoryList = TempData[field.Name] as List <OptionModel>;
                                            }
                                        }
                                    }
                                }
                                //Add logic to save form data here
                                foreach (var postAction in baseFormCollection.FormPostActions)
                                {
                                    string formHandlerName = string.Empty;

                                    try
                                    {
                                        formHandlerName = postAction.FormHandler;

                                        var formHandler = FormHandlerRegistry.Get(formHandlerName);
                                        if (formHandler != null)
                                        {
                                            formHandler.ProcessForm(Request.Form, baseFormCollection.Forms.Where(form => form.FormFields != null).SelectMany(d => d.FormFields).ToList(), postAction);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        if (!string.IsNullOrEmpty(baseFormCollection.ErrorRedirect.Url))
                                        {
                                            Log.Error(ex, "Error occured while processing form data with form handler: {0}. Error message: {1}", formHandlerName, ex.Message);
                                            return(new RedirectModel(baseFormCollection.ErrorRedirect.Url));
                                        }
                                        else
                                        {
                                            throw ex;
                                        }
                                    }
                                }

                                if (!string.IsNullOrEmpty(baseFormCollection.SuccessRedirect.Url))
                                {
                                    return(new RedirectModel(baseFormCollection.SuccessRedirect.Url));
                                }
                                else
                                {
                                    return(new RedirectModel(WebRequestContext.Localization.Path));
                                }
                            }
                            catch (Exception ex)
                            {
                                if (!string.IsNullOrEmpty(baseFormCollection.ErrorRedirect.Url))
                                {
                                    Log.Error(ex, "Error occured while saving form data.");
                                    return(new RedirectModel(baseFormCollection.ErrorRedirect.Url));
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                        }
                    }
                    else
                    {
                        // Load options from category
                        foreach (var form in baseFormCollection.Forms)
                        {
                            if (form.FormFields != null)
                            {
                                foreach (var field in form.FormFields.Where(f => !string.IsNullOrEmpty(f.OptionsCategory)))
                                {
                                    if (field.FieldType == FieldType.DropDown || field.FieldType == FieldType.CheckBox || field.FieldType == FieldType.RadioButton)
                                    {
                                        List <OptionModel> options = TaxonomyProvider.LoadOptionsFromCategory(field.OptionsCategory, WebRequestContext.Localization);
                                        field.OptionsCategoryList = options.OrderBy(x => x.Key).ThenBy(x => x.DisplayText).ToList();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(baseFormCollection);
        }
Ejemplo n.º 3
0
        public ActionResult ProcessAjaxForm(FormCollection formCollection)
        {
            // get form entity by id
            // <param name="id">The Entity Identifier in format ComponentID-TemplateID.</param>
            string formId = formCollection["formId"];

            if (string.IsNullOrEmpty(formId))
            {
                throw new Exception("FormdId was not found in form values");
            }


            EntityModel entity = this.ContentProvider.GetEntityModel(formId, WebRequestContext.Localization);

            AjaxFormModel form = entity as AjaxFormModel;

            //TODO: replace this code, get form from cache
            foreach (var field in form.FormFields.Where(f => !string.IsNullOrEmpty(f.OptionsCategory)))
            {
                if (field.FieldType == FieldType.DropDown || field.FieldType == FieldType.CheckBox || field.FieldType == FieldType.RadioButton)
                {
                    List <OptionModel> options = TaxonomyProvider.LoadOptionsFromCategory(field.OptionsCategory, WebRequestContext.Localization);
                    field.OptionsCategoryList = options;
                }
            }



            if (form != null)
            {
                MapRequestFormData(form);

                if (ModelState.IsValid)
                {
                    try {
                        //Add logic to save form data here
                        foreach (var postAction in form.FormPostActions)
                        {
                            string formHandlerName = string.Empty;
                            try
                            {
                                formHandlerName = postAction.FormHandler;

                                var formHandler = FormHandlerRegistry.Get(formHandlerName);
                                if (formHandler != null)
                                {
                                    formHandler.ProcessForm(formCollection, form.FormFields, postAction);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (form.ErrorRedirect != null)
                                {
                                    Log.Error(ex, "Error occured while processing form data with form handler: {0}. Error message: {1}", formHandlerName, ex.Message);
                                    return(PartialView("~/Areas/Forms/Views/Forms/ErrorResult.cshtml", form.ErrorRedirect));
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                        }

                        if (form.SuccessRedirect != null)
                        {
                            return(PartialView("~/Areas/Forms/Views/Forms/SuccessResult.cshtml", form.SuccessRedirect));
                        }
                        else
                        {
                            return(Redirect(WebRequestContext.Localization.Path));
                        }
                    }
                    catch (Exception ex)
                    {
                        if (form.ErrorRedirect != null)
                        {
                            Log.Error(ex, "Error occured while saving form data.");
                            return(PartialView("~/Areas/Forms/Views/Forms/ErrorResult.cshtml", form.ErrorRedirect));
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                }
            }

            return(View());
        }