Ejemplo n.º 1
0
        public ActionResult CreateEdit(NccPost model, long ParentId, long[] SelecetdCategories, string SelectedTags, string SubmitType)
        {
            var oldModel = NccHelper.DeepClone(model);

            ViewBag.MessageType = "ErrorMessage";
            ViewBag.Message     = "Error occoured. Please fill up all field correctly.";

            if (ModelState.IsValid)
            {
                bool isSuccess = true;

                #region For default language
                var defaultPostDetails = model.PostDetails.Where(x => x.Language == GlobalContext.WebSite.Language).FirstOrDefault();
                if (defaultPostDetails == null)
                {
                    isSuccess       = false;
                    ViewBag.Message = "Default language Post can't be null";
                }
                else
                {
                    //title empty validation
                    if (string.IsNullOrEmpty(defaultPostDetails.Title))
                    {
                        isSuccess       = false;
                        ViewBag.Message = "Default language Title can't be null";
                    }
                    //slug empty validation
                    if (isSuccess && string.IsNullOrEmpty(defaultPostDetails.Slug))
                    {
                        isSuccess       = false;
                        ViewBag.Message = "Default language Slug can't be null";
                    }
                    //slug duplicate validation
                    if (isSuccess)
                    {
                        defaultPostDetails.Title = defaultPostDetails.Title.Trim();
                        defaultPostDetails.Slug  = defaultPostDetails.Slug.Trim();
                        if (defaultPostDetails.MetaDescription?.Trim() == "")
                        {
                            defaultPostDetails.MetaDescription = Regex.Replace(defaultPostDetails.Content, "<.*?>", String.Empty);
                            if (defaultPostDetails.MetaDescription.Length > 161)
                            {
                                defaultPostDetails.MetaDescription = defaultPostDetails.MetaDescription.Substring(0, 160);
                            }
                        }
                        model.Name = defaultPostDetails.Slug;

                        var temp = _nccPostDetailsService.Get(defaultPostDetails.Slug, defaultPostDetails.Language);
                        if (temp != null && temp.Id != defaultPostDetails.Id)
                        {
                            isSuccess       = false;
                            ViewBag.Message = "Duplicate Slug found for language " + defaultPostDetails.Language;
                        }
                    }
                }
                #endregion

                #region Check validation for other languages
                List <NccPostDetails> deletedList = new List <NccPostDetails>();
                foreach (var item in model.PostDetails.Where(x => x.Language != GlobalContext.WebSite.Language).ToList())
                {
                    if (item.Id == 0 && string.IsNullOrEmpty(item.Title) && string.IsNullOrEmpty(item.Slug) && string.IsNullOrEmpty(item.Content) && string.IsNullOrEmpty(item.MetaKeyword) && string.IsNullOrEmpty(item.MetaDescription))
                    {
                        deletedList.Add(item);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(item.Title))
                        {
                            isSuccess       = false;
                            ViewBag.Message = "Title can't be null for language " + item.Language;
                        }
                        //slug empty validation
                        if (isSuccess && string.IsNullOrEmpty(item.Slug))
                        {
                            isSuccess       = false;
                            ViewBag.Message = "Slug can't be null for language " + item.Language;
                        }
                        //slug duplicate validation
                        if (isSuccess)
                        {
                            item.Title = item.Title.Trim();
                            item.Slug  = item.Slug.Trim();
                            if (item.MetaDescription == null || item.MetaDescription.Trim() == "")
                            {
                                item.MetaDescription = Regex.Replace(item.Content, "<.*?>", String.Empty);
                                if (item.MetaDescription.Length > 161)
                                {
                                    item.MetaDescription = item.MetaDescription.Substring(0, 160);
                                }
                            }
                            var temp = _nccPostDetailsService.Get(item.Slug, item.Language);
                            if (temp != null && temp.Id != item.Id)
                            {
                                isSuccess       = false;
                                ViewBag.Message = "Duplicate Slug found for language " + item.Language;
                            }
                        }
                    }
                }

                //Remove empty
                if (isSuccess)
                {
                    foreach (var item in deletedList)
                    {
                        model.PostDetails.Remove(item);
                    }
                }
                #endregion

                #region Operation
                if (isSuccess)
                {
                    //Parent assign
                    try
                    {
                        var parrent = _nccPostService.Get(ParentId);
                        model.Parent = parrent;
                    }
                    catch (Exception) { }

                    //Categories Assign
                    try
                    {
                        if (SelecetdCategories.Count() > 0)
                        {
                            model.Categories = new List <NccPostCategory>();
                            foreach (var item in SelecetdCategories)
                            {
                                model.Categories.Add(new NccPostCategory()
                                {
                                    Post = model, CategoryId = item
                                });
                            }
                        }
                    }
                    catch (Exception) { }

                    //Tags Assign
                    try
                    {
                        if (!string.IsNullOrEmpty(SelectedTags))
                        {
                            model.Tags = new List <NccPostTag>();
                            string[] tagList = SelectedTags.Trim().Split(',');
                            foreach (var item in tagList)
                            {
                                if (!string.IsNullOrEmpty(item))
                                {
                                    NccPostTag postTag = new NccPostTag();
                                    NccTag     nccTag  = _nccTagService.LoadAll(false, -1, item.Trim()).FirstOrDefault();
                                    if (nccTag == null)
                                    {
                                        nccTag      = new NccTag();
                                        nccTag.Name = item.Trim();
                                        _nccTagService.Save(nccTag);
                                    }
                                    model.Tags.Add(new NccPostTag()
                                    {
                                        Post = model, Tag = nccTag
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception) { }

                    if (model.Id > 0)
                    {
                        try
                        {
                            _nccPostService.Update(model);
                            model = _mediator.FirePostEvent(oldModel, PostEvent.Edit, model, _logger);
                            ViewBag.MessageType = "SuccessMessage";
                            ViewBag.Message     = "Page updated successful";
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError("Page create error.", ex.ToString());
                        }
                    }
                    else
                    {
                        try
                        {
                            _nccPostService.Save(model);
                            model = _mediator.FirePostEvent(model, PostEvent.Create);
                            ViewBag.MessageType = "SuccessMessage";
                            ViewBag.Message     = "Page save successful";
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError("Page create error.", ex.ToString());
                        }
                    }
                }
                #endregion
            }
            else
            {
                ViewBag.Message = string.Join("; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
            }

            if (ViewBag.MessageType == "SuccessMessage" && SubmitType.ToLower() == "publish")
            {
                return(RedirectToAction("Manage"));
            }

            if (ViewBag.MessageType == "SuccessMessage")
            {
                return(RedirectToAction("CreateEdit", new { Id = oldModel.Id }));
            }

            SetPostViewData(model);
            return(View(model));
        }
Ejemplo n.º 2
0
        public UserViewModel(NccUser user)
        {
            _nccUser = user;
            Id       = user.Id;
            Email    = user.Email;
            FullName = user.FullName;
            Mobile   = user.Mobile;
            UserName = user.UserName;

            DenyModules  = new List <ModuleViewModel>();
            AllowModules = new List <ModuleViewModel>();

            foreach (var item in GlobalContext.GetActiveModules())
            {
                var module = new ModuleViewModel();
                module.ModuleName = item.ModuleName;
                module.Name       = item.ModuleTitle;

                var menus      = item.Menus;
                var adminMenus = menus
                                 .Where(x => x.Type == Menu.MenuType.Admin)
                                 .GroupBy(y => y.DisplayName,
                                          (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(x => x.MenuItems).ToList() }
                                          ).ToList();

                var siteMenus = menus.Where(x => x.Type == Menu.MenuType.WebSite)
                                .GroupBy(y => y.DisplayName,
                                         (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(z => z.MenuItems).ToList() }
                                         ).ToList();

                foreach (var adminMenu in adminMenus)
                {
                    var menu = new MenuViewModel()
                    {
                        Type      = "Admin",
                        Name      = adminMenu.MenuName,
                        Order     = adminMenu.Menu.Order,
                        MenuItems = GetMenuItems(adminMenu.Items, item.ModuleName),
                    };
                    module.AdminMenus.Add(menu);
                }

                foreach (var webSiteMenu in siteMenus)
                {
                    var menu = new MenuViewModel()
                    {
                        Type      = "WebSite",
                        Name      = webSiteMenu.MenuName,
                        Order     = webSiteMenu.Menu.Order,
                        MenuItems = GetMenuItems(webSiteMenu.Items, item.ModuleName),
                    };
                    module.SiteMenus.Add(menu);
                }

                AllowModules.Add(module);
                DenyModules.Add(NccHelper.DeepClone(module));
            }

            foreach (var module in AllowModules)
            {
                foreach (var adminMenu in module.AdminMenus)
                {
                    foreach (var adminMenuItem in adminMenu.MenuItems)
                    {
                        var pd = user.ExtraPermissions.Where(
                            x => x.ModuleName == module.ModuleName &&
                            x.AllowUser != null &&
                            x.ExtraAllowUserId == user.Id &&
                            x.Action == adminMenuItem.Action &&
                            x.Controller == adminMenuItem.Controller
                            ).FirstOrDefault();
                        if (pd != null)
                        {
                            module.IsChecked        = true;
                            adminMenu.IsChecked     = true;
                            adminMenuItem.Id        = pd.Id;
                            adminMenuItem.Name      = pd.Name;
                            adminMenuItem.IsChecked = true;
                        }
                        else
                        {
                            adminMenuItem.IsChecked = false;
                        }
                    }
                }

                foreach (var siteMenu in module.SiteMenus)
                {
                    foreach (var siteMenuItem in siteMenu.MenuItems)
                    {
                        var pd = user.ExtraPermissions.Where(
                            x => x.ModuleName == module.ModuleName &&
                            x.AllowUser != null &&
                            x.ExtraAllowUserId == user.Id &&
                            x.Action == siteMenuItem.Action &&
                            x.Controller == siteMenuItem.Controller
                            ).FirstOrDefault();
                        if (pd != null)
                        {
                            module.IsChecked       = true;
                            siteMenu.IsChecked     = true;
                            siteMenuItem.Id        = pd.Id;
                            siteMenuItem.Name      = pd.Name;
                            siteMenuItem.IsChecked = true;
                        }
                        else
                        {
                            siteMenuItem.IsChecked = false;
                        }
                    }
                }
            }

            foreach (var module in DenyModules)
            {
                foreach (var adminMenu in module.AdminMenus)
                {
                    foreach (var adminMenuItem in adminMenu.MenuItems)
                    {
                        var pd = user.ExtraDenies.Where(
                            x => x.ModuleName == module.ModuleName &&
                            x.DenyUser != null &&
                            x.ExtraDenyUserId == user.Id &&
                            x.Action == adminMenuItem.Action &&
                            x.Controller == adminMenuItem.Controller
                            ).FirstOrDefault();

                        if (pd != null)
                        {
                            module.IsChecked        = true;
                            adminMenu.IsChecked     = true;
                            adminMenuItem.Id        = pd.Id;
                            adminMenuItem.Name      = pd.Name;
                            adminMenuItem.IsChecked = true;
                        }
                        else
                        {
                            adminMenuItem.IsChecked = false;
                        }
                    }
                }

                foreach (var siteMenu in module.SiteMenus)
                {
                    foreach (var siteMenuItem in siteMenu.MenuItems)
                    {
                        var pd = user.ExtraDenies.Where(
                            x => x.ModuleName == module.ModuleName &&
                            x.DenyUser != null &&
                            x.ExtraDenyUserId == user.Id &&
                            x.Action == siteMenuItem.Action &&
                            x.Controller == siteMenuItem.Controller
                            ).FirstOrDefault();

                        if (pd != null)
                        {
                            module.IsChecked       = true;
                            siteMenu.IsChecked     = true;
                            siteMenuItem.Id        = pd.Id;
                            siteMenuItem.Name      = pd.Name;
                            siteMenuItem.IsChecked = true;
                        }
                        else
                        {
                            siteMenuItem.IsChecked = false;
                        }
                    }
                }
            }
        }