コード例 #1
0
ファイル: FormServiceTests.cs プロジェクト: neozhu/MrCMS
        public void FormAdminService_ExportFormData_ShouldReturnByteArray()
        {
            var webpage = new StubWebpage();
            var posting = new FormPosting
            {
                IsDeleted = false,
                Webpage = webpage,
                FormValues = new List<FormValue>
                {
                    new FormValue
                    {
                        IsDeleted = false,
                        IsFile = false,
                        Key = "Name",
                        Value = "MrCMS"
                    }
                }
            };

            webpage.FormPostings = new List<FormPosting> {posting};

            byte[] result = _formAdminService.ExportFormData(webpage);

            result.Should().BeOfType<byte[]>();
        }
コード例 #2
0
ファイル: FormServiceTests.cs プロジェクト: neozhu/MrCMS
        public void FormAdminService_ClearFormData_ShouldDeleteFormPosting()
        {
            var webpage = new StubWebpage();
            var posting = new FormPosting
            {
                IsDeleted = false,
                Webpage = webpage,
                FormValues = new List<FormValue>
                {
                    new FormValue
                    {
                        IsDeleted = false,
                        IsFile = false,
                        Key = "Name",
                        Value = "MrCMS"
                    }
                }
            };

            webpage.FormPostings = new List<FormPosting> {posting};

            Session.Transact(session => session.Save(posting));

            _formAdminService.ClearFormData(webpage);

            Session.QueryOver<FormPosting>().RowCount().Should().Be(0);
        }
コード例 #3
0
ファイル: FormController.cs プロジェクト: neozhu/MrCMS
 public ActionResult ViewPosting(FormPosting formPosting)
 {
     return PartialView(formPosting);
 }
コード例 #4
0
ファイル: FormController.cs プロジェクト: neozhu/MrCMS
 public ActionResult DeleteEntry_POST(FormPosting posting)
 {
     _formAdminService.DeletePosting(posting);
     return RedirectToAction("Edit", "Webpage", new { id = posting.Webpage.Id });
 }
コード例 #5
0
ファイル: FormController.cs プロジェクト: neozhu/MrCMS
 public ViewResult DeleteEntry(FormPosting posting)
 {
     return View(posting);
 }
コード例 #6
0
ファイル: FormControllerTests.cs プロジェクト: neozhu/MrCMS
 public void FormController_ViewPosting_ReturnsTheResultOfTheCallToGetFormPostingAsTheModel()
 {
     var formPosting = new FormPosting();
     _formController.ViewPosting(formPosting).As<PartialViewResult>().Model.Should().Be(formPosting);
 }
コード例 #7
0
ファイル: FormPostingHandler.cs プロジェクト: neozhu/MrCMS
        public List<string> SaveFormData(Webpage webpage, HttpRequestBase request)
        {
            var formProperties = webpage.FormProperties;

            var formPosting = new FormPosting { Webpage = webpage };
            _session.Transact(session =>
            {
                webpage.FormPostings.Add(formPosting);
                session.SaveOrUpdate(formPosting);
            });
            var errors = new List<string>();
            _session.Transact(session =>
            {
                foreach (var formProperty in formProperties)
                {
                    try
                    {
                        if (formProperty is FileUpload)
                        {
                            var file = request.Files[formProperty.Name];

                            if (file == null && formProperty.Required)
                                throw new RequiredFieldException("No file was attached to the " +
                                                                 formProperty.Name + " field");

                            if (file != null && !string.IsNullOrWhiteSpace(file.FileName))
                            {
                                var value = SaveFile(webpage, formPosting, file);

                                formPosting.FormValues.Add(new FormValue
                                {
                                    Key = formProperty.Name,
                                    Value = value,
                                    IsFile = true,
                                    FormPosting = formPosting
                                });
                            }
                        }
                        else
                        {
                            var value = SanitizeValue(formProperty, request.Form[formProperty.Name]);

                            if (string.IsNullOrWhiteSpace(value) && formProperty.Required)
                                throw new RequiredFieldException("No value was posted for the " +
                                                                 formProperty.Name + " field");

                            formPosting.FormValues.Add(new FormValue
                            {
                                Key = formProperty.Name,
                                Value = value,
                                FormPosting = formPosting
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        errors.Add(ex.Message);
                    }
                }

                if (errors.Any())
                {
                    session.Delete(formPosting);
                }
                else
                {
                    formPosting.FormValues.ForEach(value => session.Save(value));

                    SendFormMessages(webpage, formPosting);
                }
            });
            return errors;
        }
コード例 #8
0
ファイル: FormPostingHandler.cs プロジェクト: neozhu/MrCMS
        private void SendFormMessages(Webpage webpage, FormPosting formPosting)
        {
            if (webpage.SendFormTo == null) return;

            var sendTo = webpage.SendFormTo.Split(',');
            if (sendTo.Any())
            {
                _session.Transact(session =>
                {
                    foreach (var email in sendTo)
                    {
                        var formMessage = ParseFormMessage(webpage.FormMessage, webpage,
                            formPosting);
                        var formTitle = ParseFormMessage(webpage.FormEmailTitle, webpage,
                            formPosting);

                        session.SaveOrUpdate(new QueuedMessage
                        {
                            Subject = formTitle,
                            Body = formMessage,
                            FromAddress = _mailSettings.SystemEmailAddress,
                            ToAddress = email,
                            IsHtml = true
                        });
                    }
                });
            }
        }
コード例 #9
0
ファイル: FormPostingHandler.cs プロジェクト: neozhu/MrCMS
        private string SaveFile(Webpage webpage, FormPosting formPosting, HttpPostedFileBase file)
        {
            var mediaCategory = _documentService.GetDocumentByUrl<MediaCategory>("file-uploads") ??
                                CreateFileUploadMediaCategory();

            var result = _fileService.AddFile(file.InputStream, webpage.Id + "-" + formPosting.Id + "-" + file.FileName, file.ContentType, file.ContentLength, mediaCategory);

            return result.FileUrl;
        }
コード例 #10
0
ファイル: FormPostingHandler.cs プロジェクト: neozhu/MrCMS
        private static string ParseFormMessage(string formMessage, Webpage webpage, FormPosting formPosting)
        {
            var formRegex = new Regex(@"\[form\]");
            var pageRegex = new Regex(@"{{page.(.*)}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            var messageRegex = new Regex(@"{{(.*)}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            formMessage = formRegex.Replace(formMessage, match =>
            {
                var list = new TagBuilder("ul");

                foreach (var formValue in formPosting.FormValues)
                {
                    var listItem = new TagBuilder("li");

                    var title = new TagBuilder("b");
                    title.InnerHtml += formValue.Key + ":";
                    listItem.InnerHtml += title.ToString() + " " +
                                          formValue.GetMessageValue();

                    list.InnerHtml += listItem.ToString();
                }

                return list.ToString();
            });

            formMessage = pageRegex.Replace(formMessage, match =>
            {
                var propertyInfo =
                    typeof(Webpage).GetProperties().FirstOrDefault(
                        info =>
                            info.Name.Equals(match.Value.Replace("{", "").Replace("}", "").Replace("page.", ""),
                                StringComparison.OrdinalIgnoreCase));

                return propertyInfo == null
                    ? string.Empty
                    : propertyInfo.GetValue(webpage,
                        null).
                        ToString();
            });
            return messageRegex.Replace(formMessage, match =>
            {
                var formValue =
                    formPosting.FormValues.FirstOrDefault(
                        value =>
                            value.Key.Equals(
                                match.Value.Replace("{", "").Replace("}", ""),
                                StringComparison.
                                    OrdinalIgnoreCase));
                return formValue == null
                    ? string.Empty
                    : formValue.GetMessageValue();
            });
        }
コード例 #11
0
ファイル: FormAdminService.cs プロジェクト: neozhu/MrCMS
 public void DeletePosting(FormPosting posting)
 {
     posting.Webpage.FormPostings.Remove(posting);
     _session.Transact(session => session.Delete(posting));
 }