예제 #1
0
        /// <summary>
        /// Load embedded script
        /// </summary>
        /// <param name="formId"></param>
        /// <returns></returns>
        public string LoadEmbeddedScript(int formId)
        {
            var form = GetById(formId);

            if (form != null)
            {
                var formRenderModel = new FormWidget(form);

                var formBuilderSetting = _siteSettingService.LoadSetting <FormBuilderSetting>();
                var cacheName          = SettingNames.FormBuilderSetting.GetTemplateCacheName(formBuilderSetting.EmbeddedTemplate);

                return(RazorEngineHelper.CompileAndRun(formBuilderSetting.EmbeddedTemplate, formRenderModel, null,
                                                       cacheName));
            }
            return(HtmlUtilities.BuildScript(ScriptAction.Alert, T("Form_Message_InvalidFormId")));
        }
예제 #2
0
        /// <summary>
        /// Reformat form content
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public string ParseForm(string name, string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                return(content);
            }

            content = content.ParseProperties(typeof(FormElementsModel), true);
            var cacheName         = name.GetTemplateCacheName(content);
            var formElementsModel = new FormElementsModel();

            content = RazorEngineHelper.CompileAndRun(content, formElementsModel, null, cacheName);

            // Remove hidden html
            content = content.Replace(EzCMSContants.AddElementClass, string.Empty);
            return(content.RemoveElementsHasClass(EzCMSContants.RemoveElementClass));
        }
예제 #3
0
        /// <summary>
        /// Render widget
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public override string Render(string[] parameters)
        {
            ParseParams(parameters);

            var form = _formService.GetById(FormId);

            if (form == null)
            {
                return(_localizedResourceService.T("Widget_Form_Message_InvalidFormId"));
            }

            var formRenderModel = new FormWidget(form);

            var formBuilderSetting = _siteSettingService.LoadSetting <FormBuilderSetting>();
            var cacheName          = SettingNames.FormBuilderSetting.GetTemplateCacheName(formBuilderSetting.EmbeddedTemplate);

            return(RazorEngineHelper.CompileAndRun(formBuilderSetting.EmbeddedTemplate, formRenderModel, null, cacheName));
        }
예제 #4
0
        /// <summary>
        /// Submit form
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="contact"></param>
        /// <param name="communication"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        public ResponseModel SubmitForm(string formId, Contact contact, ContactCommunication communication,
                                        NameValueCollection collection)
        {
            try
            {
                var id = PasswordUtilities.ComplexDecrypt(formId).ToInt();

                var form = GetById(id);

                if (form != null)
                {
                    var formData = collection.AllKeys.SelectMany(collection.GetValues, (k, v) => new ContactInformation
                    {
                        Key   = k,
                        Value = v
                    }).ToList();

                    //Save contact and communication
                    contact = _contactService.SaveForm(contact, communication, formData);

                    #region Form Data

                    var formEmailModel =
                        collection.AllKeys.SelectMany(collection.GetValues, (k, v) => new ContactInformation
                    {
                        Key   = k.CamelFriendly(),
                        Value = v
                    }).ToList();

                    var formBuilderSetting = _siteSettingService.LoadSetting <FormBuilderSetting>();

                    var cacheName =
                        SettingNames.FormBuilderSetting.GetTemplateCacheName(formBuilderSetting.SubmitFormBodyTemplate);

                    var formDataBody = RazorEngineHelper.CompileAndRun(formBuilderSetting.SubmitFormBodyTemplate,
                                                                       formEmailModel, null, cacheName);

                    #endregion

                    if (form.SendSubmitFormEmail && !string.IsNullOrEmpty(form.EmailTo))
                    {
                        var email = new EmailLog
                        {
                            From     = form.FromEmail,
                            FromName = form.FromName,
                            To       = form.EmailTo,
                            ToName   = form.EmailTo,
                            Subject  = formBuilderSetting.SubmitFormSubject,
                            Body     = formDataBody
                        };

                        _emailLogService.CreateEmail(email, true);
                    }

                    if (form.SendNotificationEmail && !string.IsNullOrEmpty(form.NotificationEmailTo))
                    {
                        var notificationBodyStringBuilder = new StringBuilder();
                        notificationBodyStringBuilder.AppendLine(form.NotificationBody);
                        notificationBodyStringBuilder.AppendLine(formDataBody);

                        var email = new EmailLog
                        {
                            From     = form.FromEmail,
                            FromName = form.FromName,
                            To       = form.NotificationEmailTo,
                            ToName   = form.NotificationEmailTo,
                            Subject  = form.NotificationSubject,
                            Body     = notificationBodyStringBuilder.ToString()
                        };

                        _emailLogService.CreateEmail(email, true);
                    }

                    if (form.SendAutoResponse)
                    {
                        // Get email from form data
                        var emailAddress =
                            formData.FirstOrDefault(
                                f => f.Key.Contains("Email", StringComparison.CurrentCultureIgnoreCase));

                        if (emailAddress != null && !string.IsNullOrEmpty(emailAddress.Value))
                        {
                            var toName = contact.FullName;
                            var email  = new EmailLog
                            {
                                From     = form.FromEmail,
                                FromName = form.FromName,
                                To       = emailAddress.Value,
                                ToName   = string.IsNullOrWhiteSpace(toName) ? emailAddress.Value : toName,
                                Subject  = form.AutoResponseSubject,
                                Body     = form.AutoResponseBody
                            };

                            _emailLogService.CreateEmail(email, true);
                        }
                    }

                    return(new ResponseModel
                    {
                        Success = true,
                        Message = form.ThankyouMessage
                    });
                }
            }
            catch (Exception)
            {
                // Form parameters invalid
            }

            return(new ResponseModel
            {
                Success = false,
                Message = T("Form_Message_InvalidFormId")
            });
        }