public static string deletePost(int id) { var post = PostPublicController.GetByID(id); string result = ""; if (post != null) { // Delete image gallery var postImage = PostPublicImageController.GetByPostID(post.ID); if (postImage.Count > 0) { foreach (var img in postImage) { if (!string.IsNullOrEmpty(img.Image)) { // Delete in database string deletePostImage = PostPublicImageController.Delete(img.ID); } } } string deletePost = PostPublicController.Delete(id); if (!string.IsNullOrEmpty(deletePost)) { result = "success"; } else { result = "failed"; } // Delete clone var postClone = PostCloneController.GetAll(id); if (postClone.Count > 0) { foreach (var item in postClone) { PostCloneController.Delete(item.ID); } } } else { result = "notfound"; } return(result); }
protected void btnSubmit_Click(object sender, EventArgs e) { string username = Request.Cookies["usernameLoginSystem"].Value; var acc = AccountController.GetByUsername(username); DateTime currentDate = DateTime.Now; int PostID = ViewState["ID"].ToString().ToInt(0); var post = PostPublicController.GetByID(PostID); if (post != null) { int CategoryID = hdfParentID.Value.ToInt(); var category = PostPublicCategoryController.GetByID(CategoryID); string CategorySlug = category.Slug; string Title = txtTitle.Text.Trim(); string Slugs = Slug.ConvertToSlug(txtSlug.Text.Trim()); string Link = txtLink.Text.Trim(); string Content = pContent.Content.ToString(); string Summary = HttpUtility.HtmlDecode(pSummary.Content.ToString()); string Action = ddlAction.SelectedValue.ToString(); string ActionValue = ""; if (Action == "view_more") { ActionValue = Slugs; } else if (Action == "show_web") { ActionValue = Link; } bool AtHome = ddlAtHome.SelectedValue.ToBool(); bool IsPolicy = ddlIsPolicy.SelectedValue.ToBool(); //Phần thêm ảnh đại diện sản phẩm string path = "/uploads/images/posts/"; string Thumbnail = ListPostPublicThumbnail.Value; if (PostPublicThumbnailImage.UploadedFiles.Count > 0) { foreach (UploadedFile f in PostPublicThumbnailImage.UploadedFiles) { var o = path + "post-app-" + PostID + '-' + Slug.ConvertToSlug(Path.GetFileName(f.FileName), isFile: true); try { f.SaveAs(Server.MapPath(o)); Thumbnail = o; } catch { } } } // Delete Image Gallery string deleteImageGallery = hdfDeleteImageGallery.Value; if (deleteImageGallery != "") { string[] deletelist = deleteImageGallery.Split(','); for (int i = 0; i < deletelist.Length - 1; i++) { var img = PostPublicImageController.GetByID(Convert.ToInt32(deletelist[i])); if (img != null) { string delete = PostPublicImageController.Delete(img.ID); } } } // Update post var oldPostPublic = new PostPublic() { ID = PostID, CategoryID = CategoryID, CategorySlug = CategorySlug, Title = Title, Thumbnail = Thumbnail, Summary = Summary, Content = Content, Action = Action, ActionValue = ActionValue, AtHome = AtHome, IsPolicy = IsPolicy, CreatedDate = post.CreatedDate, CreatedBy = acc.Username, ModifiedDate = currentDate, ModifiedBy = acc.Username }; var updatePost = PostPublicController.Update(oldPostPublic); if (updatePost != null) { // Cập nhật thư viện ảnh cho bài viết if (UploadImages.HasFiles) { foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles) { var o = path + "post-app-" + PostID + '-' + Slug.ConvertToSlug(Path.GetFileName(uploadedFile.FileName), isFile: true); uploadedFile.SaveAs(Server.MapPath(o)); PostPublicImageController.Insert(PostID, o, username, DateTime.Now); } } // tạo phiên bản cho wordpress if (!String.IsNullOrEmpty(hdfPostVariants.Value)) { JavaScriptSerializer serializer = new JavaScriptSerializer(); var variants = serializer.Deserialize <List <PostClone> >(hdfPostVariants.Value); if (variants != null) { foreach (var item in variants) { var getpostClone = PostCloneController.Get(updatePost.ID, item.Web); if (getpostClone == null) { continue; } var oldPostClone = new PostClone() { ID = getpostClone.ID, PostPublicID = updatePost.ID, Web = getpostClone.Web, PostWebID = getpostClone.PostWebID, CategoryName = category.Name, CategoryID = updatePost.CategoryID, Title = !String.IsNullOrEmpty(item.Title) ? item.Title : updatePost.Title, Summary = updatePost.Summary, Content = updatePost.Content, Thumbnail = updatePost.Thumbnail, CreatedBy = getpostClone.CreatedBy, CreatedDate = getpostClone.CreatedDate, ModifiedDate = currentDate, ModifiedBy = acc.Username }; PostCloneController.Update(oldPostClone); } } } PJUtils.ShowMessageBoxSwAlertCallFunction("Cập nhật bài viết thành công", "s", true, "redirectTo(" + updatePost.ID.ToString() + ")", Page); } } }