Esempio n. 1
0
        public ActionResult Edit(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 menuExsist = _menuLinkService.GetMenu(model.Id);
                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());
                }

                ImageHandler(model);

                var parentId = model.ParentId;
                if (!parentId.HasValue)
                {
                    model.ParentId      = null;
                    model.VirtualId     = menuExsist.CurrentVirtualId;
                    model.VirtualSeoUrl = null;
                }
                else
                {
                    var byId1 = _menuLinkService.GetMenu(parentId.Value);
                    model.VirtualId     = $"{byId1.VirtualId}/{menuExsist.CurrentVirtualId}";
                    model.VirtualSeoUrl = $"{byId1.SeoUrl}/{model.SeoUrl}";
                }

                //Get PositionMenuLink selected
                var positionMenuLinks = new List <Domain.Menus.PositionMenuLink>();
                var positionIds       = new List <int>();

                var position = Request["Position"];
                if (!string.IsNullOrEmpty(position))
                {
                    foreach (var item in position.Split(',').ToList())
                    {
                        var positionId = int.Parse(item);
                        positionIds.Add(positionId);

                        positionMenuLinks.Add(_positionMenuLink.GetById(positionId, false));
                    }

                    if (positionIds.IsAny())
                    {
                        (from x in menuExsist.PositionMenuLinks
                         where !positionIds.Contains(x.Id)
                         select x).ToList()
                        .ForEach(m =>
                                 menuExsist.PositionMenuLinks.Remove(m)
                                 );
                    }
                }

                menuExsist.PositionMenuLinks = positionMenuLinks;

                var modelMap = Mapper.Map(model, menuExsist);
                _menuLinkService.Update(menuExsist);

                //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.UpdateSuccess, 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)
            {
                ModelState.AddModelError("", ex.Message);
                LogText.Log(string.Concat("MenuLink.Create: ", ex.Message));

                return(View(model));
            }

            return(action);
        }
Esempio n. 2
0
        public ActionResult Edit(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 byId     = _menuLinkService.GetById(model.Id);
                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 parentId = model.ParentId;
                if (!parentId.HasValue)
                {
                    model.ParentId      = null;
                    model.VirtualId     = byId.CurrentVirtualId;
                    model.VirtualSeoUrl = null;
                }
                else
                {
                    var byId1 = _menuLinkService.GetById(parentId.Value);
                    model.VirtualId     = $"{byId1.VirtualId}/{byId.CurrentVirtualId}";
                    model.VirtualSeoUrl = $"{byId1.SeoUrl}/{model.SeoUrl}";
                }

                var modelMap = Mapper.Map(model, byId);
                _menuLinkService.Update(byId);

                //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.UpdateSuccess, 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)
            {
                ModelState.AddModelError("", ex.Message);
                ExtentionUtils.Log(string.Concat("MenuLink.Create: ", ex.Message));

                return(View(model));
            }

            return(action);
        }