Ejemplo n.º 1
0
        public string Parse(HtmlHelper htmlHelper, string current)
        {
            if (current == null)
                return null;

            bool submitted = true.Equals(htmlHelper.ViewContext.TempData["form-submitted"]);
            var errors = htmlHelper.ViewContext.TempData["form-submitted-message"] as List<string>;
            var data = htmlHelper.ViewContext.TempData["form-data"] as NameValueCollection;
            var status = new FormSubmittedStatus(submitted, errors, data);
            current = OtherPageFormMatch.Replace(current,
                match =>
                {
                    int pageId = Convert.ToInt32(
                        match.Value.Replace("[", "").Replace("]", "").Split('-')[1]);
                    var document = _documentService.GetDocument<Webpage>(pageId);
                    return document == null
                        ? string.Empty
                        : _formRenderer.RenderForm(document, status);
                });

            current = ThisPageFormMatch.Replace(current,
                match => _formRenderer.RenderForm(CurrentRequestData.CurrentPage, status));

            return current;
        }
Ejemplo n.º 2
0
        public string GetForm(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (webpage == null)
            {
                return(string.Empty);
            }

            IList <FormProperty> formProperties = webpage.FormProperties;

            if (!formProperties.Any())
            {
                return(string.Empty);
            }

            TagBuilder form = GetForm(webpage);

            string formDesign = webpage.FormDesign;

            formDesign     = Regex.Replace(formDesign, "{label:([^}]+)}", AddLabel(formProperties));
            formDesign     = Regex.Replace(formDesign, "{input:([^}]+)}", AddElement(formProperties, submittedStatus));
            formDesign     = Regex.Replace(formDesign, "{validation:([^}]+)}", AddValidation(formProperties));
            formDesign     = Regex.Replace(formDesign, "{submitted-message}", AddSubmittedMessage(webpage, submittedStatus));
            form.InnerHtml = formDesign;

            if (_siteSettings.HasHoneyPot)
            {
                form.InnerHtml += _siteSettings.GetHoneypot();
            }

            return(form.ToString());
        }
Ejemplo n.º 3
0
        public string GetDefault(IHtmlHelper helper, Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (webpage == null)
            {
                return(string.Empty);
            }

            var formProperties = webpage.FormProperties.OrderBy(x => x.DisplayOrder);

            if (!formProperties.Any())
            {
                return(string.Empty);
            }

            var form = GetForm(webpage);

            foreach (var property in formProperties)
            {
                string elementHtml            = string.Empty;
                IFormElementRenderer renderer = _elementRendererManager.GetElementRenderer(property);
                elementHtml += _labelRenderer.AppendLabel(property);
                var existingValue = submittedStatus.Data[property.Name];
                elementHtml += renderer.AppendElement(property, existingValue, _siteSettings.FormRendererType)
                               .ToString(renderer.IsSelfClosing
                                                        ? TagRenderMode.SelfClosing
                                                        : TagRenderMode.Normal);
                elementHtml += _validationMessaageRenderer.AppendRequiredMessage(property);
                var elementContainer = _elementRendererManager.GetElementContainer(_siteSettings.FormRendererType, property);
                if (elementContainer != null)
                {
                    elementContainer.InnerHtml += elementHtml;
                    form.InnerHtml             += elementContainer;
                }
                else
                {
                    form.InnerHtml += elementHtml;
                }
            }

            form.InnerHtml += helper.RenderRecaptcha().ToString();

            var div = new TagBuilder("div");

            div.InnerHtml  += GetSubmitButton(webpage).ToString(TagRenderMode.SelfClosing);
            form.InnerHtml += div;

            if (submittedStatus.Submitted)
            {
                form.InnerHtml += new TagBuilder("br");
                form.InnerHtml += _submittedMessageRenderer.AppendSubmittedMessage(webpage, submittedStatus);
            }

            if (_siteSettings.HasHoneyPot)
            {
                form.InnerHtml += _siteSettings.GetHoneypot();
            }

            return(form.ToString());
        }
Ejemplo n.º 4
0
 public string RenderForm(Webpage webpage, FormSubmittedStatus submitted)
 {
     if (webpage == null)
         return string.Empty;
     return string.IsNullOrWhiteSpace(webpage.FormDesign)
                ? _defaultFormRenderer.GetDefault(webpage, submitted)
                : _customFormRenderer.GetForm(webpage, submitted);
 }
Ejemplo n.º 5
0
        private string AddSubmittedMessage(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (!submittedStatus.Submitted)
            {
                return(string.Empty);
            }

            return(_submittedMessageRenderer.AppendSubmittedMessage(webpage, submittedStatus).ToString());
        }
Ejemplo n.º 6
0
 public string RenderForm(Webpage webpage, FormSubmittedStatus submitted)
 {
     if (webpage == null)
     {
         return(string.Empty);
     }
     return(string.IsNullOrWhiteSpace(webpage.FormDesign)
                ? _defaultFormRenderer.GetDefault(webpage, submitted)
                : _customFormRenderer.GetForm(webpage, submitted));
 }
Ejemplo n.º 7
0
        public void FormRenderer_RenderForm_IfFormDesignHasValueReturnResultCustomRendererGetForm()
        {
            var stubWebpage = new StubWebpage {FormDesign = "form-design-data"};
            var formSubmittedStatus = new FormSubmittedStatus(false, null, null);
            A.CallTo(() => _customFormRenderer.GetForm(stubWebpage, formSubmittedStatus)).Returns("custom-form");

            var renderForm = _formRenderingManager.RenderForm(stubWebpage, formSubmittedStatus);

            renderForm.Should().Be("custom-form");
        }
Ejemplo n.º 8
0
        public void FormRenderer_RenderForm_WhenFormDesignIsEmptyReturnsResultOfIDefaultFormRenderer()
        {
            var stubWebpage = new StubWebpage();
            var formSubmittedStatus = new FormSubmittedStatus(false, null, null);
            A.CallTo(() => _defaultFormRenderer.GetDefault(stubWebpage, formSubmittedStatus)).Returns("test-default");

            var renderForm = _formRenderingManager.RenderForm(stubWebpage, formSubmittedStatus);

            renderForm.Should().Be("test-default");
        }
Ejemplo n.º 9
0
        public TagBuilder AppendSubmittedMessage(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            var message = new TagBuilder("div");
            message.AddCssClass("alert");
            message.AddCssClass(submittedStatus.Success ? "alert-success" : "alert-danger");
            message.InnerHtml +=
                "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>" +
                (submittedStatus.Errors.Any()
                     ? RenderErrors(submittedStatus.Errors)
                     : (!string.IsNullOrWhiteSpace(webpage.FormSubmittedMessage)
                            ? webpage.FormSubmittedMessage
                            : "Form submitted"));

            return message;
        }
Ejemplo n.º 10
0
        public TagBuilder AppendSubmittedMessage(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            TagBuilder message = new TagBuilder("div");

            message.AddCssClass("alert");
            message.AddCssClass(submittedStatus.Success ? "alert-success" : "alert-danger");
            message.InnerHtml +=
                "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>" +
                (submittedStatus.Errors.Any()
                     ? RenderErrors(submittedStatus.Errors)
                     : (!string.IsNullOrWhiteSpace(webpage.FormSubmittedMessage)
                            ? webpage.FormSubmittedMessage
                            : "Form submitted"));

            return(message);
        }
Ejemplo n.º 11
0
        public string GetDefault(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (webpage == null)
                return string.Empty;

            var formProperties = webpage.FormProperties.OrderBy(x => x.DisplayOrder);
            if (!formProperties.Any())
                return string.Empty;

            var form = GetForm(webpage);
            foreach (var property in formProperties)
            {
                string elementHtml = string.Empty;
                IFormElementRenderer renderer = _elementRendererManager.GetElementRenderer(property);
                elementHtml+= _labelRenderer.AppendLabel(property);
                var existingValue = submittedStatus.Data[property.Name];
                elementHtml += renderer.AppendElement(property, existingValue, _siteSettings.FormRendererType)
                                          .ToString(renderer.IsSelfClosing
                                                        ? TagRenderMode.SelfClosing
                                                        : TagRenderMode.Normal);
                elementHtml += _validationMessaageRenderer.AppendRequiredMessage(property);
                var elementContainer = _elementRendererManager.GetElementContainer(_siteSettings.FormRendererType, property);
                if (elementContainer != null)
                {
                    elementContainer.InnerHtml += elementHtml;
                    form.InnerHtml += elementContainer;
                }
                else
                    form.InnerHtml += elementHtml;
            }

            var div = new TagBuilder("div");
            div.InnerHtml += GetSubmitButton(webpage).ToString(TagRenderMode.SelfClosing);
            form.InnerHtml += div;

            if (submittedStatus.Submitted)
            {
                form.InnerHtml += new TagBuilder("br");
                form.InnerHtml += _submittedMessageRenderer.AppendSubmittedMessage(webpage, submittedStatus);
            }

            if (_siteSettings.HasHoneyPot)
                form.InnerHtml += _siteSettings.GetHoneypot();

            return form.ToString();
        }
Ejemplo n.º 12
0
        private MatchEvaluator AddElement(IList<FormProperty> formProperties, FormSubmittedStatus submittedStatus)
        {
            return match =>
                       {

                           var formProperty =
                               formProperties.FirstOrDefault(
                                   property => property.Name.Equals(match.Groups[1].Value, StringComparison.OrdinalIgnoreCase));
                           if (formProperty == null)
                               return string.Empty;
                           var existingValue = submittedStatus.Data[formProperty.Name];

                           var renderer = _elementRendererManager.GetElementRenderer(formProperty);

                           return
                               renderer.AppendElement(formProperty, existingValue, _siteSettings.FormRendererType)
                                       .ToString(renderer.IsSelfClosing
                                                     ? TagRenderMode.SelfClosing
                                                     : TagRenderMode.Normal);
                       };
        }
Ejemplo n.º 13
0
        private MatchEvaluator AddElement(IList <FormProperty> formProperties, FormSubmittedStatus submittedStatus)
        {
            return(match =>
            {
                FormProperty formProperty =
                    formProperties.FirstOrDefault(
                        property => property.Name.Equals(match.Groups[1].Value, StringComparison.OrdinalIgnoreCase));
                if (formProperty == null)
                {
                    return string.Empty;
                }
                string existingValue = submittedStatus.Data[formProperty.Name];

                IFormElementRenderer renderer = _elementRendererManager.GetElementRenderer(formProperty);

                return
                renderer.AppendElement(formProperty, existingValue, _siteSettings.FormRendererType)
                .ToString(renderer.IsSelfClosing
                                                     ? TagRenderMode.SelfClosing
                                                     : TagRenderMode.Normal);
            });
        }
Ejemplo n.º 14
0
        public string GetForm(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (webpage == null)
                return string.Empty;

            var formProperties = webpage.FormProperties;
            if (!formProperties.Any())
                return string.Empty;

            var form = GetForm(webpage);

            var formDesign = webpage.FormDesign;
            formDesign = Regex.Replace(formDesign, "{label:([^}]+)}", AddLabel(formProperties));
            formDesign = Regex.Replace(formDesign, "{input:([^}]+)}", AddElement(formProperties, submittedStatus));
            formDesign = Regex.Replace(formDesign, "{validation:([^}]+)}", AddValidation(formProperties));
            formDesign = Regex.Replace(formDesign, "{submitted-message}", AddSubmittedMessage(webpage, submittedStatus));
            form.InnerHtml = formDesign;

            if (_siteSettings.HasHoneyPot)
                form.InnerHtml += _siteSettings.GetHoneypot();

            return form.ToString();
        }
Ejemplo n.º 15
0
        private string AddSubmittedMessage(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (!submittedStatus.Submitted) return string.Empty;

            return _submittedMessageRenderer.AppendSubmittedMessage(webpage, submittedStatus).ToString();
        }