Exemplo n.º 1
0
        public ActionResult Create(SystemSettingViewModel model, string returnUrl)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var messages = Join(Environment.NewLine, ModelState.Values.SelectMany(v => v.Errors)
                                        .Select(v => v.ErrorMessage + " " + v.Exception));
                    ModelState.AddModelError("", messages);
                    return(View(model));
                }

                if (model.Status == 1)
                {
                    var systemSettings = _systemSettingService.FindBy(x => x.Status == 1);
                    if (systemSettings.IsAny())
                    {
                        foreach (var item in systemSettings)
                        {
                            item.Status = 0;
                            _systemSettingService.Update(item);
                        }
                    }
                }

                if (model.Favicon != null && model.Favicon.ContentLength > 0)
                {
                    var extension = Path.GetExtension(model.Favicon.FileName);
                    var fileName  = Concat("favicon", extension);
                    var path      = Path.Combine(Server.MapPath(Concat("~/", Constant.SystemSettingFolder)), fileName);

                    //Check and delete logo exists
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }

                    model.Favicon.SaveAs(path);
                    model.FaviconImage = Concat(Constant.SystemSettingFolder, fileName);
                }

                if (model.Logo != null && model.Logo.ContentLength > 0)
                {
                    var extension = Path.GetExtension(model.Logo.FileName);
                    var fileName  = Concat("logo", extension);
                    var path      = Path.Combine(Server.MapPath(Concat("~/", Constant.SystemSettingFolder)), fileName);

                    //Check and delete logo exists
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }

                    model.Logo.SaveAs(path);
                    model.LogoImage = Concat(Constant.SystemSettingFolder, fileName);
                }

                if (model.LogoFooter != null && model.LogoFooter.ContentLength > 0)
                {
                    var extension = Path.GetExtension(model.LogoFooter.FileName);
                    var fileName  = Concat("logoFooter", extension);
                    var path      = Path.Combine(Server.MapPath(Concat("~/", Constant.SystemSettingFolder)), fileName);

                    //Check and delete logo exists
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }

                    model.LogoFooter.SaveAs(path);
                    model.LogoFooterImage = Concat(Constant.SystemSettingFolder, fileName);
                }

                var modelMap = Mapper.Map <SystemSettingViewModel, SystemSetting>(model);
                _systemSettingService.Create(modelMap);

                //Update Localized
                foreach (var localized in model.Locales)
                {
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.Title, localized.Title, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.FooterContent, localized.FooterContent, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.Description, localized.Description, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.MetaTitle, localized.MetaTitle, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.MetaKeywords, localized.MetaKeywords, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.MetaDescription, localized.MetaDescription, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.TimeWork, localized.TimeWork, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.Slogan, localized.Slogan, localized.LanguageId);
                }

                Response.Cookies.Add(new HttpCookie("system_message", Format(MessageUI.CreateSuccess, FormUI.SystemSetting)));
                if (!Url.IsLocalUrl(returnUrl) || returnUrl.Length <= 1 || !returnUrl.StartsWith("/") || returnUrl.StartsWith("//") || returnUrl.StartsWith("/\\"))
                {
                    return(RedirectToAction("Index"));
                }

                return(Redirect(returnUrl));
            }
            catch (Exception ex)
            {
                LogText.Log(Concat("SystemSetting.Create: ", ex.Message));
                ModelState.AddModelError("", ex.Message);

                return(View(model));
            }
        }