예제 #1
0
        public virtual async Task <IActionResult> PopupInteractiveForm(IFormCollection formCollection,
                                                                       [FromServices] IInteractiveFormService interactiveFormService, [FromServices] IQueuedEmailService queuedEmailService,
                                                                       [FromServices] IEmailAccountService emailAccountService)
        {
            var formid = formCollection["Id"];
            var form   = await interactiveFormService.GetFormById(formid);

            if (form == null)
            {
                return(Content(""));
            }
            string enquiry = "";

            foreach (var item in form.FormAttributes)
            {
                enquiry += string.Format("{0}: {1} <br />", item.Name, formCollection[item.SystemName]);

                if (!string.IsNullOrEmpty(item.RegexValidation))
                {
                    var   valuesStr = formCollection[item.SystemName];
                    Regex regex     = new Regex(item.RegexValidation);
                    Match match     = regex.Match(valuesStr);
                    if (!match.Success)
                    {
                        ModelState.AddModelError("", string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.Regex"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id)));
                    }
                }
                if (item.IsRequired)
                {
                    var valuesStr = formCollection[item.SystemName];
                    if (string.IsNullOrEmpty(valuesStr))
                    {
                        ModelState.AddModelError("", string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.IsRequired"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id)));
                    }
                }
                if (item.ValidationMinLength.HasValue)
                {
                    if (item.AttributeControlType == FormControlType.TextBox ||
                        item.AttributeControlType == FormControlType.MultilineTextbox)
                    {
                        var valuesStr         = formCollection[item.SystemName].ToString();
                        int enteredTextLength = String.IsNullOrEmpty(valuesStr) ? 0 : valuesStr.Length;
                        if (item.ValidationMinLength.Value > enteredTextLength)
                        {
                            ModelState.AddModelError("", string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.TextboxMinimumLength"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id), item.ValidationMinLength.Value));
                        }
                    }
                }
                if (item.ValidationMaxLength.HasValue)
                {
                    if (item.AttributeControlType == FormControlType.TextBox ||
                        item.AttributeControlType == FormControlType.MultilineTextbox)
                    {
                        var valuesStr         = formCollection[item.SystemName].ToString();
                        int enteredTextLength = String.IsNullOrEmpty(valuesStr) ? 0 : valuesStr.Length;
                        if (item.ValidationMaxLength.Value < enteredTextLength)
                        {
                            ModelState.AddModelError("", string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.TextboxMaximumLength"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id), item.ValidationMaxLength.Value));
                        }
                    }
                }
            }

            if (ModelState.Keys.Count() == 0)
            {
                var emailAccount = await emailAccountService.GetEmailAccountById(form.EmailAccountId);

                if (emailAccount == null)
                {
                    emailAccount = (await emailAccountService.GetAllEmailAccounts()).FirstOrDefault();
                }
                if (emailAccount == null)
                {
                    throw new Exception("No email account could be loaded");
                }

                string from;
                string fromName;
                string subject = string.Format(_localizationService.GetResource("PopupInteractiveForm.EmailForm"), form.Name);
                from     = emailAccount.Email;
                fromName = emailAccount.DisplayName;

                await queuedEmailService.InsertQueuedEmail(new QueuedEmail {
                    From           = from,
                    FromName       = fromName,
                    To             = emailAccount.Email,
                    ToName         = emailAccount.DisplayName,
                    Priority       = QueuedEmailPriority.High,
                    Subject        = subject,
                    Body           = enquiry,
                    CreatedOnUtc   = DateTime.UtcNow,
                    EmailAccountId = emailAccount.Id
                });

                //activity log
                await _customerActivityService.InsertActivity("PublicStore.InteractiveForm", form.Id, string.Format(_localizationService.GetResource("ActivityLog.PublicStore.InteractiveForm"), form.Name));
            }

            return(Json(new
            {
                success = ModelState.Keys.Count() == 0,
                errors = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                         .Select(m => m.ErrorMessage).ToArray()
            }));
        }
        public IActionResult Edit(string id)
        {
            var form = _interactiveFormService.GetFormById(id);

            if (form == null)
            {
                return(RedirectToAction("List"));
            }

            var model = form.ToModel();

            AddLocales(_languageService, model.Locales, (locale, languageId) =>
            {
                locale.Name = form.GetLocalized(x => x.Name, languageId, false, false);
                locale.Body = form.GetLocalized(x => x.Body, languageId, false, false);
            });
            //available email accounts
            foreach (var ea in _emailAccountService.GetAllEmailAccounts())
            {
                model.AvailableEmailAccounts.Add(ea.ToModel());
            }

            //available tokens
            model.AvailableTokens = FormatTokens(form.FormAttributes.Select(x => "%" + x.SystemName + "%").ToArray());
            return(View(model));
        }
예제 #3
0
        public async Task <IList <string> > Handle(PopupInteractiveCommand request, CancellationToken cancellationToken)
        {
            var errors = new List <string>();
            var formid = request.Form["Id"];
            var form   = await _interactiveFormService.GetFormById(formid);

            if (form == null)
            {
                return(errors);
            }

            string enquiry = "";

            foreach (var item in form.FormAttributes)
            {
                enquiry += string.Format("{0}: {1} <br />", item.Name, request.Form[item.SystemName]);

                if (!string.IsNullOrEmpty(item.RegexValidation))
                {
                    var   valuesStr = request.Form[item.SystemName];
                    Regex regex     = new Regex(item.RegexValidation);
                    Match match     = regex.Match(valuesStr);
                    if (!match.Success)
                    {
                        errors.Add(string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.Regex"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id)));
                    }
                }
                if (item.IsRequired)
                {
                    var valuesStr = request.Form[item.SystemName];
                    if (string.IsNullOrEmpty(valuesStr))
                    {
                        errors.Add(string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.IsRequired"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id)));
                    }
                }
                if (item.ValidationMinLength.HasValue)
                {
                    if (item.AttributeControlType == FormControlType.TextBox ||
                        item.AttributeControlType == FormControlType.MultilineTextbox)
                    {
                        var valuesStr         = request.Form[item.SystemName].ToString();
                        int enteredTextLength = String.IsNullOrEmpty(valuesStr) ? 0 : valuesStr.Length;
                        if (item.ValidationMinLength.Value > enteredTextLength)
                        {
                            errors.Add(string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.TextboxMinimumLength"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id), item.ValidationMinLength.Value));
                        }
                    }
                }
                if (item.ValidationMaxLength.HasValue)
                {
                    if (item.AttributeControlType == FormControlType.TextBox ||
                        item.AttributeControlType == FormControlType.MultilineTextbox)
                    {
                        var valuesStr         = request.Form[item.SystemName].ToString();
                        int enteredTextLength = String.IsNullOrEmpty(valuesStr) ? 0 : valuesStr.Length;
                        if (item.ValidationMaxLength.Value < enteredTextLength)
                        {
                            errors.Add(string.Format(_localizationService.GetResource("PopupInteractiveForm.Fields.TextboxMaximumLength"), item.GetLocalized(a => a.Name, _workContext.WorkingLanguage.Id), item.ValidationMaxLength.Value));
                        }
                    }
                }
            }

            if (!errors.Any())
            {
                var emailAccount = await _emailAccountService.GetEmailAccountById(form.EmailAccountId);

                if (emailAccount == null)
                {
                    emailAccount = (await _emailAccountService.GetAllEmailAccounts()).FirstOrDefault();
                }
                if (emailAccount == null)
                {
                    throw new Exception("No email account could be loaded");
                }

                string from;
                string fromName;
                string subject = string.Format(_localizationService.GetResource("PopupInteractiveForm.EmailForm"), form.Name);
                from     = emailAccount.Email;
                fromName = emailAccount.DisplayName;

                await _queuedEmailService.InsertQueuedEmail(new QueuedEmail {
                    From           = from,
                    FromName       = fromName,
                    To             = emailAccount.Email,
                    ToName         = emailAccount.DisplayName,
                    Priority       = QueuedEmailPriority.High,
                    Subject        = subject,
                    Body           = enquiry,
                    CreatedOnUtc   = DateTime.UtcNow,
                    EmailAccountId = emailAccount.Id
                });

                //activity log
                await _customerActivityService.InsertActivity("PublicStore.InteractiveForm", form.Id, string.Format(_localizationService.GetResource("ActivityLog.PublicStore.InteractiveForm"), form.Name));
            }

            return(errors);
        }