Пример #1
0
        public ActionResult EditArticle(HelpArticleModel model, HttpPostedFileBase articleImage)
        {
            model.CategoryList = new SelectList(_helpCategoryService.Help_Categories_SelectTreeAll(), "Id", "Name", model.CategoryId);
            model.ListStatus   = new SelectList(new[]
            {
                new SelectListItem {
                    Text = "Hiển thị", Value = HelpArticleStatusConst.Active.ToString()
                },
                new SelectListItem {
                    Text = "Ẩn", Value = HelpArticleStatusConst.InActive.ToString()
                }
            }, "Value", "Text", model.Status);

            ViewData["ToolbarTitle"] = Title;
            string command = Request.Form["submit"].ToString();

            switch (command)
            {
            case "Save":
            case "SaveAndContinueEdit":
                if (ModelState.IsValid)
                {
                    try
                    {
                        var helpArticle = _helpArticleService.GetHelpArticleById(model.Id);
                        if (helpArticle == null)
                        {
                            ErrorNotification("Không tìm thấy trang tĩnh nào thỏa mãn");
                            return(RedirectToAction("HelpArticles"));
                        }

                        helpArticle.CategoryId = model.CategoryId;
                        helpArticle.Title      = model.Title;

                        if (articleImage != null)
                        {
                            helpArticle.Image = "~/Upload/" + articleImage.FileName;
                            _thriftClient.UploadFileByService(helpArticle.Image, articleImage.InputStream.ToBytes());
                            var thumbnail = new Thumbnail(_thumbnailSettingService, _thriftClient);
                            thumbnail.CreateAllThumbnails(articleImage.FileName);
                        }

                        helpArticle.Modified        = DateTime.Now;
                        helpArticle.Abstract        = model.Abstract;
                        helpArticle.Body            = model.Body;
                        helpArticle.IsFeatured      = model.IsFeatured;
                        helpArticle.Status          = model.Status;
                        helpArticle.AliasTitle      = Util.GetSEOAlias(model.Title);
                        helpArticle.MetaKeyword     = model.MetaKeyword;
                        helpArticle.MetaDescription = Util.SubString(helpArticle.Abstract, 169);
                        helpArticle.IsSpecial       = model.IsSpecial;

                        _helpArticleService.UpdateHelpArticle(helpArticle);
                        SuccessNotification("Cập nhật thông tin thành công");

                        if (command == "SaveAndContinueEdit")
                        {
                            Title = "Chỉnh sửa bài viết : " + helpArticle.Title;
                        }
                        else
                        {
                            return(RedirectToAction("HelpArticles"));
                        }
                    }
                    catch (Exception e)
                    {
                        Title = "Chỉnh sửa danh mục hỗ trợ";
                        ErrorNotification(e.ToString());
                    }
                }

                break;

            case "Delete":
                return(RedirectToAction("DeleteArticle", new { id = model.Id }));

            default:
                ErrorNotification("Không rõ phương thức submit dữ liệu");
                return(RedirectToAction("HelpArticles"));
            }


            return(View(model));
        }
Пример #2
0
        public ActionResult CreateArticle(HelpArticleModel model, HttpPostedFileBase articleImage)
        {
            model.CategoryList = new SelectList(_helpCategoryService.Help_Categories_SelectTreeAll(), "Id", "Name", model.CategoryId);
            model.ListStatus   = new SelectList(new[]
            {
                new SelectListItem {
                    Text = "Hiển thị", Value = HelpArticleStatusConst.Active.ToString()
                },
                new SelectListItem {
                    Text = "Ẩn", Value = HelpArticleStatusConst.InActive.ToString()
                }
            }, "Value", "Text", model.Status);

            Title = "Thêm mới danh mục";
            ViewData["ToolbarTitle"] = Title;
            if (ModelState.IsValid)
            {
                if (model.CategoryId > 0)
                {
                    var category = _helpCategoryService.GetHelpCategoryById(model.CategoryId);
                    if (category != null)
                    {
                        var childCates = _helpCategoryService.GetAllHelpCategoryByParentId(category.Id);
                        if (childCates != null && childCates.Count() > 0)
                        {
                            ErrorNotification("Không được tạo bài viết ở danh mục cha");
                            return(View(model));
                        }
                    }
                    else
                    {
                        ErrorNotification("Danh mục không tồn tại.");
                        return(View(model));
                    }
                }
                var now = DateTime.Now;
                try
                {
                    var helpArticle = model.ToEntity();
                    helpArticle.CategoryId = model.CategoryId;
                    helpArticle.Title      = model.Title;

                    if (articleImage != null)
                    {
                        string[] fileExtensions = { ".jpg", ".jpeg", ".gif", ".png" };
                        string   extension      = Path.GetExtension(articleImage.FileName).ToLower();
                        if (fileExtensions.Contains(extension))
                        {
                            string currentDir = _rootDir + now.Year + "/" + now.Month + "/" + now.Day + "/";
                            if (!_thriftClient.CheckFolderIsExist(currentDir))
                            {
                                _thriftClient.CreateFolderByService(currentDir);
                            }
                            string filename = currentDir + Util.ComputeMD5Hash(articleImage.FileName + now.Ticks) + extension;
                            helpArticle.Image = filename;
                            _thriftClient.UploadFileByService(filename, articleImage.InputStream.ToBytes());
                            var thumbnail = new Thumbnail(_thumbnailSettingService, _thriftClient);
                            thumbnail.CreateAllThumbnails(filename);
                        }
                    }

                    helpArticle.Created         = now;
                    helpArticle.Modified        = now;
                    helpArticle.Abstract        = model.Abstract;
                    helpArticle.Body            = model.Body;
                    helpArticle.Status          = model.Status;
                    helpArticle.IsFeatured      = model.IsFeatured;
                    helpArticle.AliasTitle      = Util.GetSEOAlias(model.Title);
                    helpArticle.MetaKeyword     = model.MetaKeyword;
                    helpArticle.MetaDescription = Util.SubString(helpArticle.Abstract, 169);
                    helpArticle.IsSpecial       = model.IsSpecial;

                    _helpArticleService.AddHelpArticle(helpArticle);

                    SuccessNotification("Thêm mới bài viết thành công!");
                    return(RedirectToAction("HelpArticles"));
                }
                catch (Exception e)
                {
                    ErrorNotification(e.ToString());
                }
            }
            else
            {
                AddModelStateErrors();
            }


            return(View(model));
        }