public ActionResult Create(WebsiteTDO tdo)
        {
            ValidateWebsiteTDO(tdo);

            if (ModelState.IsValid)
            {
                try {
                    var website = this.WebsiteTDO2Website(tdo);
                    if (fsWebService.CreateEventWebsite(website) != null)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        this.AddModelError(string.Format(Errors.ERROR_EVENTSPONSOR_CREATE_UNEXPECTED, website.WebsiteName), _logger);
                    }
                }
                catch (Exception ex) {
                    this.AddModelError(ex.Message, _logger, ex);
                }
            }
            else
            {
                this.AddModelError(ModelState.Values.SelectMany(v => v.Errors).ToList(), _logger);
            }

            return(View("Modify", tdo));
        }
        private WebsiteTDO Website2WebsiteTDO(Website website)
        {
            WebsiteTDO tdo = new WebsiteTDO {
                WebsiteId            = website.WebsiteId,
                WebsiteName          = website.WebsiteName,
                HeaderUrl            = website.HeaderUrl,
                FooterUrl            = website.FooterUrl,
                TopInfoBlockUrl      = website.TopInfoBlockUrl,
                BottomInfoBlockUrl   = website.BottomInfoBlockUrl,
                HeaderImage          = website.HeaderImage,
                FooterImage          = website.FooterImage,
                TopInfoBlockImage    = website.TopInfoBlockImage,
                BottomInfoBlockImage = website.BottomInfoBlockImage
            };

            return(tdo);
        }
        private void ValidateWebsiteTDO(WebsiteTDO tdo)
        {
            var imageTypes = ImageUtils.GetImageTypes();

            if (tdo.HeaderFile != null)
            {
                if (!imageTypes.Contains(tdo.HeaderFile.ContentType))
                {
                    ModelState.AddModelError("HeaderFile", "Please choose either a JPG or PNG image for the 'Header Image' field.");
                    return;
                }
            }

            if (tdo.FooterFile != null)
            {
                if (!imageTypes.Contains(tdo.FooterFile.ContentType))
                {
                    ModelState.AddModelError("FooterFile", "Please choose either a JPG or PNG image for the 'Footer Image' field.");
                    return;
                }
            }

            if (tdo.TopInfoBlockFile != null)
            {
                if (!imageTypes.Contains(tdo.TopInfoBlockFile.ContentType))
                {
                    ModelState.AddModelError("TopInfoBlockFile", "Please choose either a JPG or PNG image for the 'Top Block Image' field.");
                    return;
                }
            }

            if (tdo.BottomInfoBlockFile != null)
            {
                if (!imageTypes.Contains(tdo.BottomInfoBlockFile.ContentType))
                {
                    ModelState.AddModelError("BottomInfoBlockFile", "Please choose either a JPG or PNG image for the 'Bottom Block Image' field.");
                    return;
                }
            }
        }
        public ActionResult Edit(WebsiteTDO tdo)
        {
            ValidateWebsiteTDO(tdo);

            if (ModelState.IsValid)
            {
                try {
                    var website = this.WebsiteTDO2Website(tdo);
                    fsWebService.UpdateEventWebsite(website);
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex) {
                    this.AddModelError(ex.Message, _logger, ex);
                }
            }
            else
            {
                this.AddModelError(ModelState.Values.SelectMany(v => v.Errors).ToList(), _logger);
            }

            return(View("Modify", tdo));
        }
        private Website WebsiteTDO2Website(WebsiteTDO tdo)
        {
            Website website = new Website {
                WebsiteId            = tdo.WebsiteId,
                WebsiteName          = tdo.WebsiteName,
                HeaderUrl            = tdo.HeaderUrl,
                FooterUrl            = tdo.FooterUrl,
                TopInfoBlockUrl      = tdo.TopInfoBlockUrl,
                BottomInfoBlockUrl   = tdo.BottomInfoBlockUrl,
                HeaderImage          = tdo.HeaderImage,
                FooterImage          = tdo.FooterImage,
                TopInfoBlockImage    = tdo.TopInfoBlockImage,
                BottomInfoBlockImage = tdo.BottomInfoBlockImage
            };

            var           uploadDir  = "~/uploads";
            var           uploadPath = Server.MapPath(uploadDir);
            var           imagePath  = "";
            DirectoryInfo dirInfo    = new DirectoryInfo(uploadPath);

            if (!dirInfo.Exists)
            {
                Directory.CreateDirectory(uploadPath);
            }

            if (tdo.HeaderFile != null)
            {
                var websiteHeaderImage = Path.Combine(uploadDir, tdo.HeaderFile.FileName).Replace('\\', '/');
                if (websiteHeaderImage != website.HeaderImage)
                {
                    website.HeaderImage = websiteHeaderImage;
                    imagePath           = Path.Combine(uploadPath, tdo.HeaderFile.FileName);
                    tdo.HeaderFile.SaveAs(imagePath);
                }
            }

            if (tdo.FooterFile != null)
            {
                var websiteFooterImage = Path.Combine(uploadDir, tdo.FooterFile.FileName).Replace('\\', '/');
                if (websiteFooterImage != website.FooterImage)
                {
                    website.FooterImage = websiteFooterImage;
                    imagePath           = Path.Combine(uploadPath, tdo.FooterFile.FileName);
                    tdo.FooterFile.SaveAs(imagePath);
                }
            }

            if (tdo.TopInfoBlockFile != null)
            {
                var websiteTopInfoBlockImage = Path.Combine(uploadDir, tdo.TopInfoBlockFile.FileName).Replace('\\', '/');
                if (websiteTopInfoBlockImage != website.TopInfoBlockImage)
                {
                    website.TopInfoBlockImage = websiteTopInfoBlockImage;
                    imagePath = Path.Combine(uploadPath, tdo.TopInfoBlockFile.FileName);
                    tdo.TopInfoBlockFile.SaveAs(imagePath);
                }
            }

            if (tdo.BottomInfoBlockFile != null)
            {
                var websiteBottomInfoBlockImage = Path.Combine(uploadDir, tdo.BottomInfoBlockFile.FileName).Replace('\\', '/');
                if (websiteBottomInfoBlockImage != website.BottomInfoBlockImage)
                {
                    website.BottomInfoBlockImage = websiteBottomInfoBlockImage;
                    imagePath = Path.Combine(uploadPath, tdo.BottomInfoBlockFile.FileName);
                    tdo.BottomInfoBlockFile.SaveAs(imagePath);
                }
            }

            return(website);
        }