Пример #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);
                }
            }
        }
Пример #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();
        }
        /// <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;
        }