Exemplo n.º 1
0
        public static bool SubmitForm(ParameterList parameters, string captchaText)
        {
            try
            {
                _compiler.SaveControlProperties();
            }
            catch (Exception)
            { }
            webUiControl.InitializeViewState();

            Dictionary <string, string> errorMessages = formHelper.BindingsToObject(bindings, newData);

            DataTypeDescriptor dataTypeDescriptor = DynamicTypeManager.GetDataTypeDescriptor(newData.DataSourceId.InterfaceType);

            foreach (var property in newData.DataSourceId.InterfaceType.GetProperties())
            {
                if (property.PropertyType == typeof(string) && (string)property.GetValue(newData, null) == string.Empty)
                {
                    property.SetValue(newData, null, null);
                }
            }


            ValidationResults validationResults = ValidationFacade.Validate(newData.DataSourceId.InterfaceType, newData);

            var isValid    = true;
            var useCaptcha = parameters.GetParameter <bool>("UseCaptcha");

            if (useCaptcha)
            {
                var Session = HttpContext.Current.Session;
                if (Session["FormsRendererCaptcha"] == null || !Captcha.IsValid(captchaText, Session["FormsRendererCaptcha"].ToString()))
                {
                    ErrorSummary.AddError(StringResourceSystemFacade.GetString("Composite.Forms.Renderer", "Composite.Forms.Captcha.CaptchaText.error"));
                    isValid = false;
                }
            }

            if (validationResults.IsValid == false)
            {
                isValid = false;

                Dictionary <string, string> errorSummary = new Dictionary <string, string>();

                foreach (ValidationResult result in validationResults)
                {
                    var label = result.Key;
                    var help  = result.Message;

                    try
                    {
                        label = dataTypeDescriptor.Fields[result.Key].FormRenderingProfile.Label;

                        help = dataTypeDescriptor.Fields[result.Key].FormRenderingProfile.HelpText;

                        //if no HelpText specified - use standard C1 error
                        if (help == string.Empty)
                        {
                            help = result.Message;
                        }

                        string error = GetLocalized(label) + ": " + GetLocalized(help);

                        if (!errorSummary.ContainsValue(error))
                        {
                            errorSummary.Add(errorSummary.Count().ToString(), error);
                        }
                    }
                    catch { }
                }

                // add errors to ErrorSummary
                foreach (var dict in errorSummary)
                {
                    ErrorSummary.AddError(dict.Value);
                }
            }
            //TODO: Looks like rudimentary code related to old C1 errros with binding?
            if (errorMessages != null)
            {
                isValid = false;
                foreach (var kvp in errorMessages)
                {
                    var label = kvp.Key;
                    try
                    {
                        label = dataTypeDescriptor.Fields[kvp.Key].FormRenderingProfile.Label;
                    }
                    catch { }
                    ErrorSummary.AddError(GetLocalized(label) + ": " + GetLocalized(kvp.Value));
                }
            }

            if (isValid)
            {
                using (new DataScope(DataScopeIdentifier.Administrated))
                {
                    IPublishControlled publishControlled = newData as IPublishControlled;
                    if (publishControlled != null)
                    {
                        publishControlled.PublicationStatus = GenericPublishProcessController.Draft;
                    }
                    DataFacade.AddNew(newData);

                    using (var datascope = new FormsRendererDataScope(newData))
                    {
                        var formEmailHeaders = parameters.GetParameter("Email") as IEnumerable <FormEmail>;

                        if (formEmailHeaders != null)
                        {
                            foreach (var formEmail in formEmailHeaders)
                            {
                                ParameterFacade.ResolveProperties(formEmail);
                                var inputXml = GetXElement(newData);
                                var body     = new XhtmlDocument();

                                body.AppendDocument(formEmail.Body);

                                if (formEmail.AppendFormData)
                                {
                                    var formData     = new XDocument();
                                    var xslTransform = new XslCompiledTransform();

                                    xslTransform.LoadFromPath(FormsRendererLocalPath + "Xslt/MailBody.xslt");

                                    using (var writer = formData.CreateWriter())
                                    {
                                        xslTransform.Transform(inputXml.CreateReader(), writer);
                                    }

                                    body.AppendDocument(formData);
                                }

                                Reflection.CallStaticMethod <object>("Composite.Core.WebClient.Renderings.Page.PageRenderer", "NormalizeXhtmlDocument", body);

                                var mailMessage = new MailMessage();
                                try
                                {
                                    mailMessage.From = new MailAddress(formEmail.From);
                                }
                                catch (Exception e)
                                {
                                    LoggingService.LogError(string.Format("Mail sending(From: '{0}')", formEmail.From), e.Message);
                                    continue;
                                }
                                try
                                {
                                    mailMessage.To.Add(formEmail.To);
                                }
                                catch (Exception e)
                                {
                                    LoggingService.LogError(string.Format("Mail sending(To: '{0}')", formEmail.To), e.Message);
                                    continue;
                                }
                                if (!string.IsNullOrEmpty(formEmail.Cc))
                                {
                                    try
                                    {
                                        mailMessage.CC.Add(formEmail.Cc);
                                    }
                                    catch (Exception e)
                                    {
                                        LoggingService.LogError(string.Format("Mail sending(Cc: '{0}')", formEmail.Cc), e.Message);
                                    }
                                }

                                try
                                {
                                    mailMessage.Subject    = formEmail.Subject;
                                    mailMessage.IsBodyHtml = true;
                                    mailMessage.Body       = body.ToString();

                                    new SmtpClient().Send(mailMessage);
                                }
                                catch (Exception e)
                                {
                                    throw new InvalidOperationException("Unable to send mail. Please ensure that web.config has correct /configuration/system.net/mailSettings: " + e.Message);
                                }
                            }
                        }
                    }
                }
            }
            return(isValid);
        }