Пример #1
0
        public ActionResult Create(MenuLinkViewModel model, string returnUrl)
        {
            ActionResult action;

            try
            {
                if (!ModelState.IsValid)
                {
                    var messages = string.Join(Environment.NewLine, ModelState.Values.SelectMany(v => v.Errors)
                                               .Select(v => v.ErrorMessage + " " + v.Exception));
                    ModelState.AddModelError("", messages);
                    return(View(model));
                }

                var menuName   = model.MenuName.NonAccent();
                var menuExsist = _menuLinkService.GetListSeoUrl(menuName);
                model.SeoUrl = model.MenuName.NonAccent();

                if (menuExsist.Any(x => x.Id != model.Id))
                {
                    var menuLinkViewModel = model;
                    var seoUrl            = menuLinkViewModel.SeoUrl;
                    var num = menuExsist.Count();
                    menuLinkViewModel.SeoUrl = string.Concat(seoUrl, "-", num.ToString());
                }

                ImageHandler(model);

                var guid = Guid.NewGuid().ToString();
                if (model.ParentId.HasValue)
                {
                    model.CurrentVirtualId = guid;
                    var byId = _menuLinkService.GetMenu(model.ParentId.Value);
                    model.VirtualId     = $"{byId.VirtualId}/{guid}";
                    model.VirtualSeoUrl = $"{byId.SeoUrl}/{model.SeoUrl}";
                }
                else
                {
                    model.VirtualId        = guid;
                    model.CurrentVirtualId = guid;
                }

                var modelMap = Mapper.Map <MenuLinkViewModel, MenuLink>(model);
                _menuLinkService.Create(modelMap);

                //Update Localized
                foreach (var localized in model.Locales)
                {
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.MenuName, localized.MenuName, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.SeoUrl, localized.MenuName.NonAccent(), 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);
                }

                Response.Cookies.Add(new HttpCookie("system_message", string.Format(MessageUI.CreateSuccess, FormUI.MenuLink)));
                if (!Url.IsLocalUrl(returnUrl) || returnUrl.Length <= 1 || !returnUrl.StartsWith("/") || returnUrl.StartsWith("//") || returnUrl.StartsWith("/\\"))
                {
                    action = RedirectToAction("Index");
                }
                else
                {
                    action = Redirect(returnUrl);
                }
            }
            catch (Exception ex)
            {
                LogText.Log(string.Concat("MenuLink.Create: ", ex.Message));

                return(View(model));
            }
            return(action);
        }
Пример #2
0
        public ActionResult Create(MenuLinkViewModel model, string returnUrl)
        {
            ActionResult action;

            try
            {
                if (!ModelState.IsValid)
                {
                    var messages = String.Join(Environment.NewLine, ModelState.Values.SelectMany(v => v.Errors)
                                               .Select(v => v.ErrorMessage + " " + v.Exception));
                    ModelState.AddModelError("", messages);
                    return(View(model));
                }

                var menuName = model.MenuName.NonAccent();
                var bySeoUrl = _menuLinkService.GetListSeoUrl(menuName);
                model.SeoUrl = model.MenuName.NonAccent();

                if (bySeoUrl.Any(x => x.Id != model.Id))
                {
                    var menuLinkViewModel = model;
                    var seoUrl            = menuLinkViewModel.SeoUrl;
                    var num = bySeoUrl.Count();
                    menuLinkViewModel.SeoUrl = string.Concat(seoUrl, "-", num.ToString());
                }

                var folderName = Utils.FolderName(model.MenuName);
                if (model.ImageBigSizeFile != null && model.ImageBigSizeFile.ContentLength > 0)
                {
                    var fileName       = Path.GetFileNameWithoutExtension(model.ImageBigSizeFile.FileName);
                    var fileExtension  = Path.GetExtension(model.ImageBigSizeFile.FileName);
                    var fileNameFormat = fileName.FileNameFormat(fileExtension);

                    _imagePlugin.CropAndResizeImage(model.ImageBigSizeFile, $"{Contains.MenuFolder}{folderName}/", fileNameFormat, ImageSize.MenuWithBigSize, ImageSize.MenuHeightBigSize, true);
                    model.ImageBigSize = $"{Contains.MenuFolder}{folderName}/{fileNameFormat}";
                }

                if (model.ImageMediumSizeFile != null && model.ImageMediumSizeFile.ContentLength > 0)
                {
                    var fileName       = Path.GetFileNameWithoutExtension(model.ImageMediumSizeFile.FileName);
                    var fileExtension  = Path.GetExtension(model.ImageMediumSizeFile.FileName);
                    var fileNameFormat = fileName.FileNameFormat(fileExtension);

                    _imagePlugin.CropAndResizeImage(model.ImageMediumSizeFile, $"{Contains.MenuFolder}{folderName}/", fileNameFormat, ImageSize.MenuWithMediumSize, ImageSize.MenuHeightMediumSize, true);
                    model.ImageMediumSize = $"{Contains.MenuFolder}{folderName}/{fileNameFormat}";
                }

                if (model.ImageSmallSizeFile != null && model.ImageSmallSizeFile.ContentLength > 0)
                {
                    var fileName       = Path.GetFileNameWithoutExtension(model.ImageSmallSizeFile.FileName);
                    var fileExtension  = Path.GetExtension(model.ImageSmallSizeFile.FileName);
                    var fileNameFormat = fileName.FileNameFormat(fileExtension);

                    _imagePlugin.CropAndResizeImage(model.ImageSmallSizeFile, $"{Contains.MenuFolder}{folderName}/", fileNameFormat, ImageSize.MenuWithSmallSize, ImageSize.MenuHeightSmallSize, true);
                    model.ImageSmallSize = $"{Contains.MenuFolder}{folderName}/{fileNameFormat}";
                }

                var guid = Guid.NewGuid().ToString();
                if (model.ParentId.HasValue)
                {
                    model.CurrentVirtualId = guid;
                    var byId = _menuLinkService.GetById(model.ParentId.Value);
                    model.VirtualId     = $"{byId.VirtualId}/{guid}";
                    model.VirtualSeoUrl = $"{byId.SeoUrl}/{model.SeoUrl}";
                }
                else
                {
                    model.VirtualId        = guid;
                    model.CurrentVirtualId = guid;
                }

                var modelMap = Mapper.Map <MenuLinkViewModel, MenuLink>(model);
                _menuLinkService.Create(modelMap);

                //Update Localized
                foreach (var localized in model.Locales)
                {
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.MenuName, localized.MenuName, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.SeoUrl, localized.MenuName.NonAccent(), 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);
                }

                Response.Cookies.Add(new HttpCookie("system_message", string.Format(MessageUI.CreateSuccess, FormUI.MenuLink)));
                if (!Url.IsLocalUrl(returnUrl) || returnUrl.Length <= 1 || !returnUrl.StartsWith("/") || returnUrl.StartsWith("//") || returnUrl.StartsWith("/\\"))
                {
                    action = RedirectToAction("Index");
                }
                else
                {
                    action = Redirect(returnUrl);
                }
            }
            catch (Exception ex)
            {
                ExtentionUtils.Log(string.Concat("MenuLink.Create: ", ex.Message));

                return(View(model));
            }
            return(action);
        }