예제 #1
0
        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);
        }
예제 #2
0
        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
                        });
                    }
                });
            }
        }
예제 #3
0
        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[]>();
        }
예제 #4
0
        private static Dictionary <int, List <string> > GetFormDataForExport(Webpage webpage)
        {
            var items = new Dictionary <int, List <string> >();

            for (int i = 0; i < webpage.FormPostings.Count; i++)
            {
                FormPosting posting = webpage.FormPostings[i];
                items.Add(i, new List <string>());
                foreach (
                    FormValue value in
                    GetHeadersForExport(webpage)
                    .SelectMany(header => posting.FormValues.Where(x => x.Key == header)))
                {
                    if (!value.IsFile)
                    {
                        items[i].Add(value.Value);
                    }
                    else
                    {
                        items[i].Add("http://" + CurrentRequestData.CurrentSite.BaseUrl + value.Value);
                    }
                }
            }
            return(items.OrderByDescending(x => x.Value.Count).ToDictionary(pair => pair.Key, pair => pair.Value));
        }
예제 #5
0
        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);
        }
예제 #6
0
        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();
            }));
        }
예제 #7
0
        public PostingsModel GetFormPostings(Webpage webpage, int page, string search)
        {
            FormPosting posting = null;
            var         query   =
                _session.QueryOver(() => posting).Where(() => posting.Webpage.Id == webpage.Id);

            if (!string.IsNullOrWhiteSpace(search))
            {
                query =
                    query.WithSubquery.WhereExists(
                        QueryOver.Of <FormValue>()
                        .Where(
                            value =>
                            value.FormPosting.Id == posting.Id &&
                            value.Value.IsInsensitiveLike(search, MatchMode.Anywhere)).Select(value => value.Id));
            }

            IPagedList <FormPosting> formPostings = query.OrderBy(() => posting.CreatedOn).Desc.Paged(page);

            return(new PostingsModel(formPostings, webpage.Id));
        }
예제 #8
0
        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 = _saveFormFileUpload.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);
        }
예제 #9
0
        public void FormController_ViewPosting_ReturnsTheResultOfTheCallToGetFormPostingAsTheModel()
        {
            var formPosting = new FormPosting();

            _formController.ViewPosting(formPosting).As <PartialViewResult>().Model.Should().Be(formPosting);
        }
예제 #10
0
 public void DeletePosting(FormPosting posting)
 {
     posting.Webpage.FormPostings.Remove(posting);
     _session.Transact(session => session.Delete(posting));
 }
예제 #11
0
 public ActionResult ViewPosting(FormPosting formPosting)
 {
     return(PartialView(formPosting));
 }
예제 #12
0
 public ActionResult DeleteEntry_POST(FormPosting posting)
 {
     _formAdminService.DeletePosting(posting);
     return(RedirectToAction("Edit", "Webpage", new { id = posting.Webpage.Id }));
 }
예제 #13
0
 public ViewResult DeleteEntry(FormPosting posting)
 {
     return(View(posting));
 }