Esempio n. 1
0
        public ActionResult Index(int NodeID)
        {
            var model = new USNContactFormViewModel();

            model.CurrentNodeID = NodeID;

            return(PartialView("USNForms/USN_ContactForm", model));
        }
        public ActionResult Index(int NodeID, IPublishedContent globalSettings)
        {
            var model = new USNContactFormViewModel();

            model.CurrentNodeID  = NodeID;
            model.GlobalSettings = globalSettings;

            return(PartialView("USNForms/USN_ContactForm", model));
        }
        public static bool SendContactFormMail(USNContactFormViewModel model, string mailTo, string websiteName, string pageName, out string lsErrorMessage)
        {
            lsErrorMessage = String.Empty;

            try
            {
                //Create MailDefinition
                MailDefinition md       = new MailDefinition();
                string         lsSendTo = String.Empty;

                //specify the location of template
                md.BodyFileName = "/usn/emailtemplates/contactform.htm";
                md.IsBodyHtml   = true;

                //Build replacement collection to replace fields in template
                System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
                replacements.Add("<% formFirstName %>", model.FirstName == null ? "" : model.FirstName);
                replacements.Add("<% formLastName %>", model.LastName == null ? "" : model.LastName);
                replacements.Add("<% formEmail %>", model.Email == null ? "" : model.Email);
                replacements.Add("<% formPhone %>", model.Telephone == null ? "" : model.Telephone);
                replacements.Add("<% formMessage %>", model.Message == null ? "" : umbraco.library.ReplaceLineBreaks(model.Message));
                replacements.Add("<% WebsitePage %>", pageName);
                replacements.Add("<% WebsiteName %>", websiteName);

                lsSendTo = mailTo;

                //now create mail message using the mail definition object
                System.Net.Mail.MailMessage msg = md.CreateMailMessage(lsSendTo, replacements, new System.Web.UI.Control());
                msg.ReplyToList.Add(model.Email);
                msg.Subject = websiteName + " Website: " + pageName + " Page Enquiry";

                //this uses SmtpClient in 2.0 to send email, this can be configured in web.config file.
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                smtp.Send(msg);

                return(true);
            }
            catch (Exception ex)
            {
                lsErrorMessage = ex.Message;
            }

            return(false);
        }
        public ActionResult Index(int GlobalSettingsID, string ViewPath, string DataSize, string FormColor, string FormButtonColor, USNHeading FormHeading,
                                  string FormSecondaryHeading, HtmlString FormIntroduction, string FormButtonText, string FormSubmissionMessage, string FormSubscriberListID, string ContactRecipientEmailAddress, string PageName)
        {
            var  model = new USNContactFormViewModel();
            Guid g     = Guid.NewGuid();

            model.UniqueID                     = Guid.NewGuid().ToString();
            model.GlobalSettingsID             = GlobalSettingsID;
            model.CaptchaDataSize              = DataSize;
            model.FormColor                    = FormColor;
            model.FormButtonColor              = FormButtonColor;
            model.FormHeading                  = FormHeading;
            model.FormSecondaryHeading         = FormSecondaryHeading;
            model.FormIntroduction             = FormIntroduction;
            model.FormButtonText               = FormButtonText;
            model.FormSubmissionMessage        = FormSubmissionMessage;
            model.FormSubscriberListID         = FormSubscriberListID;
            model.ContactRecipientEmailAddress = ContactRecipientEmailAddress;
            model.PageName                     = PageName;

            return(PartialView(ViewPath + "/USNForms/USN_ContactForm", model));
        }
        public ActionResult HandleContactSubmit(USNContactFormViewModel model)
        {
            System.Threading.Thread.Sleep(1000);

            string returnValue = String.Empty;

            if (!ModelState.IsValid)
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('{1}');", model.CurrentNodeID, HttpUtility.JavaScriptStringEncode(umbraco.library.GetDictionaryItem("USN Contact Form General Error")))));
            }

            //Need to get NodeID from hidden field. CurrentPage does not work with Ajax.BeginForm
            var contactFormNode = Umbraco.TypedContent(model.CurrentNodeID);
            var globalSettings  = Umbraco.TypedContent(model.GlobalSettingsID);

            if (globalSettings.HasValue("googleReCAPTCHASiteKey") && globalSettings.HasValue("googleReCAPTCHASecretKey"))
            {
                var    response  = Request["g-recaptcha-response"];
                string secretKey = globalSettings.GetPropertyValue <string>("googleReCAPTCHASecretKey");
                var    client    = new WebClient();
                var    result    = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secretKey, response));
                var    obj       = JObject.Parse(result);
                var    status    = (bool)obj.SelectToken("success");

                if (!status)
                {
                    return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('{1}');", model.CurrentNodeID, HttpUtility.JavaScriptStringEncode(umbraco.library.GetDictionaryItem("USN Form reCAPTCHA Error")))));
                }
            }

            string mailTo      = contactFormNode.GetPropertyValue <string>("contactRecipientEmailAddress");
            string websiteName = globalSettings.GetPropertyValue <string>("websiteName");
            string pageName    = contactFormNode.Parent.Parent.Name;

            string errorMessage = String.Empty;

            if (!SendContactFormMail(model, mailTo, websiteName, pageName, out errorMessage))
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('<div class=\"info\"><p>{1}</p><p>{2}</p></div>');", model.CurrentNodeID, HttpUtility.JavaScriptStringEncode(umbraco.library.GetDictionaryItem("USN Contact Form Mail Send Error")), HttpUtility.JavaScriptStringEncode(errorMessage))));
            }

            try
            {
                if (model.NewsletterSignup && globalSettings.HasValue("newsletterAPIKey") &&
                    (globalSettings.HasValue("defaultNewsletterSubscriberListID") || contactFormNode.HasValue("contactSubscriberListID")))
                {
                    if (globalSettings.GetPropertyValue <USNOptions>("emailMarketingPlatform") == USNOptions.Newsletter_CM)
                    {
                        AuthenticationDetails auth = new ApiKeyAuthenticationDetails(globalSettings.GetPropertyValue <string>("newsletterAPIKey"));

                        string subsciberListID = String.Empty;

                        if (contactFormNode.HasValue("newsletterSubscriberListID"))
                        {
                            subsciberListID = contactFormNode.GetPropertyValue <string>("contactSubscriberListID");
                        }
                        else
                        {
                            subsciberListID = globalSettings.GetPropertyValue <string>("defaultNewsletterSubscriberListID");
                        }

                        Subscriber loSubscriber = new Subscriber(auth, subsciberListID);

                        List <SubscriberCustomField> customFields = new List <SubscriberCustomField>();

                        string subscriberID = loSubscriber.Add(model.Email, model.FirstName + " " + model.LastName, customFields, false);
                    }
                    else if (globalSettings.GetPropertyValue <USNOptions>("emailMarketingPlatform") == USNOptions.Newsletter_Mailchimp)
                    {
                        var mc = new MailChimpManager(globalSettings.GetPropertyValue <string>("newsletterAPIKey"));

                        string subsciberListID = String.Empty;

                        if (contactFormNode.HasValue("contactSubscriberListID"))
                        {
                            subsciberListID = contactFormNode.GetPropertyValue <string>("contactSubscriberListID");
                        }
                        else
                        {
                            subsciberListID = globalSettings.GetPropertyValue <string>("defaultNewsletterSubscriberListID");
                        }

                        var email = new EmailParameter()
                        {
                            Email = model.Email
                        };

                        var myMergeVars = new MergeVar();
                        myMergeVars.Add("FNAME", model.FirstName);
                        myMergeVars.Add("LNAME", model.LastName);

                        EmailParameter results = mc.Subscribe(subsciberListID, email, myMergeVars, "html", false, true, false, false);
                    }
                }
            }
            catch (Exception ex)
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('<div class=\"info\"><p>{1}</p><p>{2}</p></div>');", model.CurrentNodeID, HttpUtility.JavaScriptStringEncode(umbraco.library.GetDictionaryItem("USN Contact Form Signup Error")), HttpUtility.JavaScriptStringEncode(ex.Message))));
            }

            returnValue = String.Format("<div class=\"spc alert alert-success alert-dismissible fade in\" role=\"alert\"><div class=\"info\">{0}</div></div>", contactFormNode.GetPropertyValue <string>("contactSubmissionMessage"));

            return(Content(returnValue));
        }
        public ActionResult HandleContactSubmit(USNContactFormViewModel model)
        {
            System.Threading.Thread.Sleep(1000);

            string returnValue = String.Empty;

            if (!ModelState.IsValid)
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('{1}');", model.CurrentNodeID, umbraco.library.GetDictionaryItem("USN Contact Form General Error"))));
            }

            //Need to get NodeID from hidden field. CurrentPage does not work with Ajax.BeginForm
            IPublishedContent contactFormNode = Umbraco.TypedContent(model.CurrentNodeID);

            IPublishedContent homeNode       = contactFormNode.Site();
            IPublishedContent globalSettings = homeNode.GetPropertyValue <IPublishedContent>("websiteConfigurationNode").Children.Where(x => x.IsDocumentType("USNGlobalSettings")).First();

            string mailTo      = contactFormNode.GetPropertyValue <string>("recipientEmailAddress");
            string websiteName = globalSettings.GetPropertyValue <string>("websiteName");
            string pageName    = contactFormNode.Parent.Parent.Name;

            string errorMessage = String.Empty;

            if (!SendContactFormMail(model, mailTo, websiteName, pageName, out errorMessage))
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('<div class=\"info\"><p>{1}</p><p>{2}</p></div>');", model.CurrentNodeID, umbraco.library.GetDictionaryItem("USN Contact Form Mail Send Error"), errorMessage)));
            }

            try
            {
                if (model.NewsletterSignup && globalSettings.HasValue("newsletterAPIKey") &&
                    (globalSettings.HasValue("defaultNewsletterSubscriberListID") || contactFormNode.HasValue("newsletterSubscriberListID")))
                {
                    if (globalSettings.GetPropertyValue <string>("emailMarketingPlatform") == "Campaign Monitor")
                    {
                        AuthenticationDetails auth = new ApiKeyAuthenticationDetails(globalSettings.GetProperty("newsletterAPIKey").Value.ToString());

                        string subsciberListID = String.Empty;

                        if (contactFormNode.HasValue("newsletterSubscriberListID"))
                        {
                            subsciberListID = contactFormNode.GetPropertyValue <string>("newsletterSubscriberListID");
                        }
                        else
                        {
                            subsciberListID = globalSettings.GetPropertyValue <string>("defaultNewsletterSubscriberListID");
                        }

                        Subscriber loSubscriber = new Subscriber(auth, subsciberListID);

                        List <SubscriberCustomField> customFields = new List <SubscriberCustomField>();

                        string subscriberID = loSubscriber.Add(model.Email, model.FirstName + " " + model.LastName, customFields, false);
                    }
                    else if (globalSettings.GetPropertyValue <string>("emailMarketingPlatform") == "MailChimp")
                    {
                        var mc = new MailChimpManager(globalSettings.GetPropertyValue <string>("newsletterAPIKey"));

                        string subsciberListID = String.Empty;

                        if (contactFormNode.HasValue("newsletterSubscriberListID"))
                        {
                            subsciberListID = contactFormNode.GetPropertyValue <string>("newsletterSubscriberListID");
                        }
                        else
                        {
                            subsciberListID = globalSettings.GetPropertyValue <string>("defaultNewsletterSubscriberListID");
                        }

                        var email = new EmailParameter()
                        {
                            Email = model.Email
                        };

                        var myMergeVars = new MergeVar();
                        myMergeVars.Add("FNAME", model.FirstName);
                        myMergeVars.Add("LNAME", model.LastName);

                        EmailParameter results = mc.Subscribe(subsciberListID, email, myMergeVars, "html", false, true, false, false);
                    }
                }
            }
            catch (Exception ex)
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('<div class=\"info\"><p>{1}</p><p>{2}</p></div>');", model.CurrentNodeID, umbraco.library.GetDictionaryItem("USN Contact Form Signup Error"), ex.Message.Replace("'", "\""))));
            }

            returnValue = String.Format("<div class=\"spc alert alert-success alert-dismissible fade in\" role=\"alert\"><div class=\"info\">{0}</div></div>", contactFormNode.GetPropertyValue <string>("submissionMessage"));

            return(Content(returnValue));
        }
Esempio n. 7
0
        public ActionResult HandleContactSubmit(USNContactFormViewModel model)
        {
            System.Threading.Thread.Sleep(1000);

            string returnValue = "";

            if (!ModelState.IsValid)
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('{1}');", model.CurrentNodeID, umbraco.library.GetDictionaryItem("USN Contact Form General Error"))));
            }

            //Need to get NodeID from hidden field. CurrentPage does not work with Ajax.BeginForm
            var currentNode = Umbraco.TypedContent(model.CurrentNodeID);

            IPublishedContent homeNode = currentNode.AncestorOrSelf(1);
            var settingsFolder         = Umbraco.TypedContent(homeNode.GetProperty("websiteConfigurationNode").Value);
            var globalSettings         = settingsFolder.Children.Where(x => x.DocumentTypeAlias == "USNGlobalSettings").First();

            string mailTo      = currentNode.GetProperty("recipientEmailAddress").Value.ToString();
            string websiteName = globalSettings.GetProperty("websiteName").Value.ToString();
            string pageName    = currentNode.Parent.Parent.Name;

            string errorMessage = "";

            if (!SendContactFormMail(model, mailTo, websiteName, pageName, out errorMessage))
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('<div class=\"info\"><p>{1}</p><p>{2}</p></div>');", model.CurrentNodeID, umbraco.library.GetDictionaryItem("USN Contact Form Mail Send Error"), errorMessage)));
            }

            try
            {
                if (model.NewsletterSignup && globalSettings.HasValue("campaignMonitorAPIKey") &&
                    (globalSettings.HasValue("defaultCampaignMonitorSubscriberListID") || currentNode.HasValue("campaignMonitorSubscriberListID")))
                {
                    string subsciberListID = "";

                    if (currentNode.GetProperty("campaignMonitorSubscriberListID").Value.ToString() != String.Empty)
                    {
                        subsciberListID = currentNode.GetProperty("campaignMonitorSubscriberListID").Value.ToString();
                    }
                    else
                    {
                        subsciberListID = globalSettings.GetProperty("defaultCampaignMonitorSubscriberListID").Value.ToString();
                    }

                    AuthenticationDetails auth = new ApiKeyAuthenticationDetails(globalSettings.GetProperty("campaignMonitorAPIKey").Value.ToString());

                    Subscriber loSubscriber = new Subscriber(auth, subsciberListID);

                    List <SubscriberCustomField> customFields = new List <SubscriberCustomField>();

                    string lsSubscriberID = loSubscriber.Add(model.Email, model.FirstName + " " + model.LastName, customFields, false);
                }
            }
            catch (Exception ex)
            {
                return(JavaScript(String.Format("$(ContactError{0}).show();$(ContactError{0}).html('<div class=\"info\"><p>{1}</p><p>{2}</p></div>');", model.CurrentNodeID, umbraco.library.GetDictionaryItem("USN Contact Form Signup Error"), ex.Message)));
            }

            returnValue = String.Format("<div class=\"page_component alert alert-success alert-dismissible fade in\" role=\"alert\"><div class=\"info\">{0}</div></div>", currentNode.GetProperty("submissionMessage").Value.ToString());

            return(Content(returnValue));
        }
        public ActionResult HandleContactSubmit(USNContactFormViewModel model)
        {
            System.Threading.Thread.Sleep(1000);

            //Need to get NodeID from hidden field. CurrentPage does not work with Ajax.BeginForm
            var globalSettings = Umbraco.Content(model.GlobalSettingsID);

            string returnValue    = String.Empty;
            string recaptchaReset = globalSettings.HasValue("googleReCAPTCHASiteKey") && globalSettings.HasValue("googleReCAPTCHASecretKey") ? "grecaptcha.reset();" : String.Empty;

            if (!ModelState.IsValid)
            {
                return(JavaScript(String.Format("{0}$(CE{1}).show();$(ContactError{1}).html('{2}');", recaptchaReset, model.UniqueID, HttpUtility.JavaScriptStringEncode(Umbraco.GetDictionaryValue("USN Form Required Field Error")))));
            }

            if (globalSettings.HasValue("googleReCAPTCHASiteKey") && globalSettings.HasValue("googleReCAPTCHASecretKey"))
            {
                var    response  = Request["g-recaptcha-response"];
                string secretKey = globalSettings.Value <string>("googleReCAPTCHASecretKey");
                var    client    = new WebClient();
                var    result    = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secretKey, response));
                var    obj       = JObject.Parse(result);
                var    status    = (bool)obj.SelectToken("success");

                if (!status)
                {
                    return(JavaScript(String.Format("{0}$(ContactError{1}).show();$(ContactError{1}).html('{2}');", recaptchaReset, model.UniqueID, HttpUtility.JavaScriptStringEncode(Umbraco.GetDictionaryValue("USN Form reCAPTCHA Error")))));
                }
            }

            string mailTo      = StringCipher.Decrypt(model.ContactRecipientEmailAddress);
            string websiteName = globalSettings.Value <string>("websiteName");
            string pageName    = model.PageName;

            string errorMessage = String.Empty;

            if (!SendContactFormMail(model, mailTo, websiteName, pageName, out errorMessage))
            {
                return(JavaScript(String.Format("{0}$(CE{1}).show();$(CE{1}).html('<div class=\"info\"><p>{2}</p><p>{3}</p></div>');", recaptchaReset, model.UniqueID, HttpUtility.JavaScriptStringEncode(Umbraco.GetDictionaryValue("USN Form Mail Send Error")), HttpUtility.JavaScriptStringEncode(errorMessage))));
            }

            try
            {
                if (model.NewsletterSignup && globalSettings.HasValue("newsletterAPIKey") &&
                    (globalSettings.HasValue("defaultNewsletterSubscriberListID") || model.FormSubscriberListID.HasValue()))
                {
                    if (globalSettings.Value <string>("emailMarketingPlatform") == "newsletterCM")
                    {
                        AuthenticationDetails auth = new ApiKeyAuthenticationDetails(globalSettings.Value <string>("newsletterAPIKey"));

                        string subsciberListID = String.Empty;

                        if (model.FormSubscriberListID.HasValue())
                        {
                            subsciberListID = model.FormSubscriberListID;
                        }
                        else
                        {
                            subsciberListID = globalSettings.Value <string>("defaultNewsletterSubscriberListID");
                        }

                        Subscriber loSubscriber = new Subscriber(auth, subsciberListID);

                        List <SubscriberCustomField> customFields = new List <SubscriberCustomField>();

                        string subscriberID = loSubscriber.Add(model.Email, model.FirstName + " " + model.LastName, customFields, false);
                    }
                    else if (globalSettings.Value <string>("emailMarketingPlatform") == "newsletterMC")
                    {
                        var mc = new MailChimpManager(globalSettings.Value <string>("newsletterAPIKey"));

                        string subsciberListID = String.Empty;

                        if (model.FormSubscriberListID.HasValue())
                        {
                            subsciberListID = model.FormSubscriberListID;
                        }
                        else
                        {
                            subsciberListID = globalSettings.Value <string>("defaultNewsletterSubscriberListID");
                        }

                        var email = new EmailParameter()
                        {
                            Email = model.Email
                        };

                        var myMergeVars = new MergeVar();
                        myMergeVars.Add("FNAME", model.FirstName);
                        myMergeVars.Add("LNAME", model.LastName);

                        EmailParameter results = mc.Subscribe(subsciberListID, email, myMergeVars, "html", false, true, false, false);
                    }
                }

                return(JavaScript(String.Format("$('#S_{0}').show();$('#Form_{0}').hide();", model.UniqueID)));
            }
            catch (Exception ex)
            {
                return(JavaScript(String.Format("{0}$(CE{1}).show();$(CE{1}).html('<div class=\"info\"><p>{2}</p><p>{3}</p></div>');", recaptchaReset, model.UniqueID, HttpUtility.JavaScriptStringEncode(Umbraco.GetDictionaryValue("USN Contact Form Signup Error")), HttpUtility.JavaScriptStringEncode(ex.Message))));
            }
        }