public static CMRegion GetByID(int CMRegionID, IEnumerable <string> includeList = null)
        {
            CMRegion obj = null;
            string   key = cacheKeyPrefix + CMRegionID + GetCacheIncludeText(includeList);

            CMRegion tmpClass = null;

            if (Cache.IsEnabled)
            {
                if (Cache.IsEmptyCacheItem(key))
                {
                    return(null);
                }
                tmpClass = Cache[key] as CMRegion;
            }

            if (tmpClass != null)
            {
                obj = tmpClass;
            }
            else
            {
                using (Entities entity = new Entities())
                {
                    IQueryable <CMRegion> itemQuery = AddIncludes(entity.CMRegion, includeList);
                    obj = itemQuery.FirstOrDefault(n => n.CMRegionID == CMRegionID);
                }
                Cache.Store(key, obj);
            }

            return(obj);
        }
示例#2
0
        /// <summary>
        /// This method checks the Content Manager page for form fields.
        /// If it finds form fields it will submit them to the form recipient.
        /// </summary>
        public static void ParseRequestForFormFields(string regionName)
        {
            if (String.IsNullOrEmpty(HttpContext.Current.Request.Form[DynamicSubmitName]))
            {
                return;
            }
            int?   micrositeID   = null;
            bool   globalContact = false;
            CMPage cmPage        = CMSHelpers.GetCurrentRequestCMSPage();

            // determine legit fields
            List <CMPageRegion> prs = new List <CMPageRegion>();
            int?userID = Helpers.GetCurrentUserID();

            if (userID == 0)
            {
                userID = null;
            }
            if (cmPage != null)
            {
                CMRegion cmRegion = CMRegion.CMRegionPage(0, 1, "", "", true, new CMRegion.Filters {
                    FilterCMRegionName = regionName
                }).FirstOrDefault();
                if (cmRegion != null)
                {
                    CMPageRegion currentRegion = CMPageRegion.LoadContentRegion(new CMPageRegion.Filters {
                        FilterCMPageRegionCMRegionID = cmRegion.CMRegionID.ToString(), FilterCMPageRegionUserID = userID.ToString(), FilterCMPageRegionCMPageID = cmPage.CMPageID.ToString(), FilterCMPageRegionNeedsApproval = false.ToString()
                    });
                    if (currentRegion != null)
                    {
                        prs.Add(currentRegion);
                    }
                }
            }

            //Also get Global areas that might contain forms
            List <CMPage> globalAreas = CMSHelpers.GetCachedCMPages().Where(c => !c.CMTemplateID.HasValue && c.FileName.Equals(regionName)).ToList();
            List <CMPage> temp        = new List <CMPage>();

            temp.AddRange(globalAreas);
            foreach (CMPage globalPage in temp)
            {
                CMRegion cmRegion = CMRegion.CMRegionGetByName(regionName).FirstOrDefault();
                if (cmRegion != null)
                {
                    CMPageRegion region = CMPageRegion.LoadContentRegion(new CMPageRegion.Filters {
                        FilterCMPageRegionCMRegionID = cmRegion.CMRegionID.ToString(), FilterCMPageRegionUserID = userID.ToString(), FilterCMPageRegionCMPageID = globalPage.CMPageID.ToString(), FilterCMPageRegionNeedsApproval = false.ToString()
                    });
                    if (region != null)
                    {
                        prs.Clear();
                        prs.Add(region);
                    }
                    else
                    {
                        globalAreas.Remove(globalPage);
                    }
                }
                else
                {
                    globalAreas.Remove(globalPage);
                }
            }
            if (prs.Count > 0)
            {
                bool hasFields = false;

                List <string> validFields = new List <string>();
                List <string> checkBoxes  = new List <string>();

                foreach (CMPageRegion pr in prs)
                {
                    MatchCollection ms = Regex.Matches(pr.Content, @"<(input|textarea|select) (.|\n)*?name=""?((\w|\d|\s|\-|\(|\))+)""?(.|\n)*?/?>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    if (ms.Count > 0 && globalAreas.Exists(c => c.CMPageID == pr.CMPageID))
                    {
                        cmPage        = globalAreas.Find(c => c.CMPageID == pr.CMPageID);
                        globalContact = true;
                    }
                    foreach (Match m in ms)
                    {
                        if (!m.ToString().Contains("type=\"radio\"") || !validFields.Contains(m.Groups[3].Value))
                        {
                            validFields.Add(m.Groups[3].Value);
                        }
                        if (m.ToString().Contains("type=\"checkbox\""))
                        {
                            checkBoxes.Add(m.Groups[3].Value);
                        }
                    }
                }

                validFields.Remove("dynamicsubmit");

                CMSubmittedForm newForm = new CMSubmittedForm();
                newForm.IsProcessed    = false;
                newForm.DateSubmitted  = DateTime.UtcNow;
                newForm.FormRecipient  = cmPage.FormRecipient;
                newForm.ResponsePageID = cmPage.ResponsePageID;
                newForm.CMMicrositeID  = cmPage.CMMicrositeID;

                if (HttpContext.Current.Request.Files.Count > 0)
                {
                    if (Regex.IsMatch(HttpContext.Current.Request.Files[0].FileName, "(\\.(doc)|(docx)|(pdf)|(jpg)|(jpeg)|(bmp)|(png)|(gif)|(ppt)|(pptx)|(xls)|(xlsx))$"))
                    {
                        HttpContext.Current.Request.Files[0].SaveAs(HttpContext.Current.Server.MapPath(UploadedFilesLocation + HttpContext.Current.Request.Files[0].FileName));
                        newForm.UploadedFile = HttpContext.Current.Request.Files[0].FileName;
                    }
                    else
                    {
                        Page page = (Page)HttpContext.Current.Handler;
                        page.ClientScript.RegisterStartupScript(page.GetType(), "InvalidFileExt", "alert('Invalid file extension.  Valid extensions are: doc,docx,pdf,jpg,jpeg,bmp,png,gif,ppt,pptx,xls,xlsx');", true);
                        return;
                    }
                }

                if (validFields.Count > 0)
                {
                    StringBuilder formData = new StringBuilder();
                    validFields.ForEach(s =>
                    {
                        if (HttpContext.Current.Request.Form[s] != null)
                        {
                            formData.Append(string.Format("<tr><td>{0}</td><td>{1}</td></tr>", s, HttpContext.Current.Request.Form[s].ToString()));
                            if (!hasFields)
                            {
                                hasFields = true;
                            }
                        }                                                                         // if the item is not posted, no harm, just dont include it
                        else if (checkBoxes.Contains(s))
                        {
                            formData.Append(string.Format("<tr><td>{0}</td><td>{1}</td></tr>", s, "off"));
                            if (!hasFields)
                            {
                                hasFields = true;
                            }
                        }
                    });
                    if (hasFields)
                    {
                        string body = EmailTemplateService.HtmlMessageBody(EmailTemplates.CMSFormPost, new { PageName = cmPage.FileName, FormFields = formData.ToString() });

                        newForm.FormHTML = body;
                        newForm.Save();
                        if (globalContact && !String.IsNullOrEmpty(Settings.GlobalContactEmailAddress))
                        {
                            MailMessage message = new MailMessage();
                            message.To.Add(Settings.GlobalContactEmailAddress);
                            message.IsBodyHtml = true;
                            message.Body       = body;
                            message.Subject    = Globals.Settings.SiteTitle + "- Form Submission From " + cmPage.FileName;
                            if (!String.IsNullOrEmpty(newForm.UploadedFile))
                            {
                                message.Attachments.Add(new Attachment(HttpContext.Current.Server.MapPath(UploadedFilesLocation + newForm.UploadedFile)));
                            }
                            SmtpClient client = new SmtpClient();
                            client.Send(message);
                        }
                        else if (!String.IsNullOrEmpty(cmPage.FormRecipient))
                        {
                            cmPage.FormRecipient.Split(',').ToList().ForEach(recipient =>
                            {
                                MailMessage message = new MailMessage();
                                message.To.Add(recipient);
                                message.IsBodyHtml = true;
                                message.Body       = body;
                                message.Subject    = Globals.Settings.SiteTitle + "- Form Submission From " + cmPage.FileName;
                                if (!String.IsNullOrEmpty(newForm.UploadedFile))
                                {
                                    message.Attachments.Add(new Attachment(HttpContext.Current.Server.MapPath(UploadedFilesLocation + newForm.UploadedFile)));
                                }
                                SmtpClient client = new SmtpClient();
                                client.Send(message);
                            });
                        }
                    }
                    if (hasFields)
                    {
                        if (globalContact)
                        {
                            Page page = (Page)HttpContext.Current.Handler;
                            page.ClientScript.RegisterStartupScript(page.GetType(), PopupScriptName, @"$(document).ready(function(){
	if ($('a#contactDummyLink').length == 0)
		$('div.contactSuccess').parent().parent().prepend('<a href=""#contactSuccess"" style=""display:none;"" id=""contactDummyLink"">success</a>');
	$('a#contactDummyLink').fancybox();
	$('a#contactDummyLink').trigger('click');
	setTimeout(function(){$.fancybox.close();}, 4000);
});", true);
                        }
                        else if (cmPage.ResponsePageID != null)
                        {
                            HttpContext.Current.Response.Redirect(CMSHelpers.GetCachedCMPages().Where(p => p.CMPageID == cmPage.ResponsePageID.Value).Single().FileName);
                        }
                        else
                        {
                            Page page = (Page)HttpContext.Current.Handler;
                            //If you change the key or type of the script below,
                            //you must also change it on the _RadEditor.cs file or your page will not load correctly
                            //in the if statement with Page.ClientScript.IsStartupScriptRegistered(Page.GetType(), "PopupScript")
                            page.ClientScript.RegisterStartupScript(page.GetType(), PopupScriptName, "alert('Thank you. Your form has been submitted successfully.');", true);
                        }
                    }
                }
            }
        }
 public CMRegion(CMRegion objectToCopy)
 {
     CMRegionID = objectToCopy.CMRegionID;
     Name       = objectToCopy.Name;
 }