Exemple #1
0
        /// <summary>
        /// Fills the form answers.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="model">The model.</param>
        /// <param name="formAnswer">The form answer.</param>
        private static void FillFormAnswers(MailTemplate email, FormBuilderWidget model, FormWidgetAnswer formAnswer)
        {
            email.AppendParam("Title", model.Title);
            email.AppendParam("Date", formAnswer.CreateDate.ToString());

            if (formAnswer.AnswerValues == null)
            {
                return;
            }

            var path = Path.Combine(FormsPlugin.Instance.PluginDirectory + FormsTemplatesDirectory, FormsAnswerValueTemplate);

            if (File.Exists(path))
            {
                using (var streamTemplate = new StreamReader(File.OpenRead(path), Encoding.UTF8))
                {
                    var template = streamTemplate.ReadToEnd();

                    //render form answer
                    var answer = new StringBuilder();
                    foreach (var item in formAnswer.AnswerValues)
                    {
                        answer.Append(template.Replace("<#AnswerValue#>", String.Format("{0}: {1}", item.Field, item.Value)));
                    }

                    email.AppendParam("Content", answer.ToString(), false);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Sends the form answer email.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="formAnswer">The form answer.</param>
        /// <returns></returns>
        public static bool SendFormAnswerEmail(FormBuilderWidget model, FormWidgetAnswer formAnswer)
        {
            var templatePath = Path.Combine(FormsPlugin.Instance.PluginDirectory + FormsTemplatesDirectory, FormsAnswerTemplate);
            var email        = new MailTemplate(templatePath)
            {
                FromEmail = EmailSettings.FromEmail,
                Subject   = HttpUtility.HtmlEncode(model.Title),
                ToEmail   = model.RecipientEmail
            };

            FillFormAnswers(email, model, formAnswer);
            return(email.Send());
        }
Exemple #3
0
        /// <summary>
        /// Saves the form builder widget.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static FormBuilderWidgetViewModel SaveFormBuilderWidget(FormBuilderWidgetViewModel model)
        {
            var widgetService = ServiceLocator.Current.GetInstance <IFormBuilderWidgetService>();
            var widget        = new FormBuilderWidget();

            if (model.Id > 0)
            {
                widget = widgetService.Find(model.Id);
            }

            var contentViewer = model.MapTo(widget);

            if (widget != null)
            {
                widgetService.Save(contentViewer);
            }

            return(new FormBuilderWidgetViewModel().MapFrom(contentViewer));
        }
        public virtual ActionResult EditWidget(ICoreWidgetInstance instance)
        {
            if (instance != null)
            {
                var widget = new FormBuilderWidget();

                if (instance.InstanceId != null)
                {
                    var widgetService  = ServiceLocator.Current.GetInstance <IFormBuilderWidgetService>();
                    var existingWidget = widgetService.Find((long)instance.InstanceId);

                    if (existingWidget != null)
                    {
                        widget = existingWidget;
                    }
                }

                return(PartialView(new FormBuilderWidgetViewModel().MapFrom(widget)));
            }

            return(Content(HttpContext.Translate("Messages.SetupForm", ResourceHelper.GetControllerScope(this))));
        }
Exemple #5
0
 /// <summary>
 /// Validates the form model.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="collection">The collection.</param>
 /// <param name="modelState">State of the model.</param>
 public static void Validate(FormBuilderWidget model, FormCollection collection, ModelStateDictionary modelState)
 {
     foreach (var item in model.Form.FormElements)
     {
         var elementName = String.Format("{0}_{1}", item.Type, item.Id);
         var value       = collection[elementName];
         if (value != null)
         {
             // check if the item is required
             if (item.IsRequired && String.IsNullOrEmpty(value))
             {
                 modelState.AddModelError(elementName, String.Format("The {0} field is required.", item.Title));
             }
             else if (!String.IsNullOrEmpty(value) && item.MaxLength != null)
             {
                 // check if the item has max length limitation
                 if (value.Length > item.MaxLength)
                 {
                     modelState.AddModelError(elementName, String.Format("The {0} field length is more then {1}.", item.Title, item.MaxLength));
                 }
             }
             // check if the item has regular expression validator
             if (!String.IsNullOrEmpty(value) && item.RegexTemplate != RegexTemplate.None && RegexTemplatesConfig.ValidationTemplates.ContainsKey(item.RegexTemplate))
             {
                 if (!Regex.IsMatch(value, RegexTemplatesConfig.ValidationTemplates[item.RegexTemplate]))
                 {
                     modelState.AddModelError(elementName, String.Format("The {0} field is not in valid format.", item.Title));
                 }
             }
             // validate if the item is Captcha
             if (item.Type == FormElementType.Captcha)
             {
                 ValidateCaptcha(collection, modelState, elementName, value);
             }
         }
     }
 }
Exemple #6
0
        /// <summary>
        /// Handles the form data.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="collection">The collection.</param>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static bool HandleFormData(FormBuilderWidget model, FormCollection collection, ICorePrincipal user)
        {
            bool result = true;

            if (model.SaveData || model.SendEmail)
            {
                //bind model

                var widgetAnswersService = ServiceLocator.Current.GetInstance <IFormWidgetAnswerService>();
                var answer = new FormWidgetAnswer
                {
                    CreateDate        = DateTime.Now,
                    FormBuilderWidget = model,
                    User = new BaseUser {
                        Id = user.PrincipalId
                    },
                    Title = model.Title
                };

                //save answer values
                foreach (FormElement item in model.Form.FormElements.ToList().OrderBy(val => val.OrderNumber))
                {
                    if (item.Type != FormElementType.Captcha)
                    {
                        String elementName = String.Format("{0}_{1}", item.Type, item.Id);
                        String value       = collection[elementName];
                        if (value != null)
                        {
                            if (item.Type == FormElementType.RadioButtons)
                            {
                                value = GetRadioButtonValue(item.ElementValues, elementName, value);
                            }

                            if (!String.IsNullOrEmpty(value))
                            {
                                ((HashedSet <FormWidgetAnswerValue>)answer.AnswerValues).Add(new FormWidgetAnswerValue
                                {
                                    Field  = item.Title,
                                    Value  = value,
                                    Answer = answer
                                });
                            }
                        }
                    }
                }

                //send email
                if (model.SendEmail)
                {
                    result = FormsMailer.SendFormAnswerEmail(model, answer);
                }

                //save answer
                if (model.SaveData)
                {
                    result = result && widgetAnswersService.Save(answer);
                }
            }

            return(result);
        }