Beispiel #1
0
 protected void Page_Load(object sender, EventArgs e)
 {
     //输出所有的专辑
     var mode = string.IsNullOrEmpty(Request.QueryString["mode"]) ? "new" : Request.QueryString["mode"].Trim().ToLower();
     if (mode != "new" && mode != "edit") mode = "new";
     if (mode == "edit")
     {
         int pid = 0;
         if (int.TryParse(Request.QueryString["postid"].Trim(), out pid) && pid > 0)
         {
             UpdatePosts = Service.PostsService.GetPostById(pid);
             if (UpdatePosts == null) Response.Redirect("Editor.aspx?mode=new");
             PostInCats = Service.PostInCategoriesService.GetList(pid, true);
             //获取标签
             postTags = Service.TagsService.GetTagsByPostId(UpdatePosts.ID); //postTags.AddRange(Service.TagsService.GetTagsByPostId(pid));
             isEdit = true;
         }
     }
     PostInCats = PostInCats ?? new List<PostInCategoriesModel>();
     Cats = Service.CategoriesService.GetList();
 }
Beispiel #2
0
        protected override void OnInit(EventArgs e)
        {
            if (Method == HttpMethod.HttpPost && ResponseType == ResponseDataType.Json)
            {
                Frm = _Context.Request.Form;
                string mode = string.IsNullOrEmpty(Frm["Mode"]) ? string.Empty : Frm["mode"].Trim();
                /// new 新增文章
                /// update 整体保存
                /// savePractice 保存成草稿
                /// send 发布
                /// toRecycle   放入回收站
                /// delFlag  删除表示
                /// delete   彻底删除
                /// cats        专辑修改
                /// newTag      新增标签
                /// delTag  删除标签
                /// pic     图片设置
                /// aprove  批准
                List<string> errors = new List<string>();
                //bool isOk = false;
                string[] modes = { "new", "edit", "rename", "practice", "delflag", "categories", "visible", "newtag", "deletetag", "delete", "pic", "approve" };
                if (modes.Contains(mode))
                {
                    string title = stringNull(Frm["Title"]) ? null : Frm["Title"].Trim();
                    string name = stringNull(Frm["Name"]) ? null : Frm["Name"].Trim();
                    string content = stringNull(Frm["Content"]) ? null : Frm["Content"].Trim();
                    bool approved = false;
                    bool _approvedSet = !stringNull(Frm["Approved"]) && bool.TryParse(Frm["Approved"].Trim(), out approved);
                    bool practice = true;
                    bool _practiceSet = !stringNull(Frm["Practice"]) && bool.TryParse(Frm["Practice"].Trim(), out practice);
                    bool delflag = false;
                    bool _delflagSet = !stringNull(Frm["DelFlag"]) && bool.TryParse(Frm["DelFlag"].Trim(), out delflag);
                    short visible = !stringNull(Frm["Visible"]) && short.TryParse(Frm["Visible"].Trim(), out visible) ? (visible < (short)1 || visible > (short)4 ? (short)1 : visible) : (short)1;
                    string[] categories = Frm.GetValues("Categories");
                    string[] tags = Frm.GetValues("Tags");
                    int tagId = !stringNull(Frm["TagID"]) && int.TryParse(Frm["TagID"].Trim(), out tagId) ? tagId : 0;

                    if (tags != null && tags.Length > 0)
                    {
                        var tagList = tags.ToList();
                        tagList.ForEach(p =>
                        {
                            p = p.Replace("/", "").Replace("\\", "").Replace("&", "").Replace("?", "").Replace(":", "").Replace("#", "").Replace("=", "").Trim();
                        });
                        tags = tagList.Distinct().ToArray();
                    }
                    string newTag = stringNull(Frm["NewTag"]) ? null : Frm["NewTag"].Trim();
                    if (newTag != null)
                    {
                        newTag = newTag.Replace("/", "").Replace("\\", "").Replace("&", "").Replace("?", "").Replace(":", "").Replace("#", "").Replace("=", "").Trim();
                        if (stringNull(newTag)) newTag = null;
                    }

                    string image = stringNull(Frm["Image"]) ? null : Frm["Image"].Trim();
                    int postId = 0;
                    bool _postId_set = !stringNull(Frm["PostID"]) && int.TryParse(Frm["PostID"].Trim(), out postId);

                    //返回类型
                    #region Add
                    if (mode == "new")
                    {
                        //新增
                        if (title != string.Empty)
                        {
                            PostsModel post = new PostsModel()
                            {
                                Title = title,
                                Content = content ?? "",
                                Approved = _approvedSet && approved,
                                AuthorID = OnlineUser.ID,
                                CanComment = true,
                                CreateTime = DateTime.Now,
                                DelFlag = false,
                                IP = _Context.Request.UserHostAddress,
                                Modified = false,
                                LastModifiedTime = DateTime.Now,
                                Name = Guid.NewGuid().ToString().Replace("-", ""),
                                NeadPassword = false,
                                Password = "",
                                SendTime = DateTime.Now,
                                Practice = _practiceSet ? _practiceSet && practice : true,
                                PostImage = image ?? "",
                                ThemeGuid = "",
                                UserAgent = _Context.Request.UserAgent,
                                VisibleState = visible,
                                PostType = false,
                            };

                            if (Service.PostsService.Add(post))
                            {
                                post = Service.PostsService.GetPostByName(post.Name);
                                //标签
                                AddTags(post.ID, tags, true);
                                post.Tags = tags != null && tags.Length > 0 ? Service.TagsService.GetTagsByPostId(post.ID) : new List<TagsModel>();

                                //插入分类
                                AddCategories(post.ID, categories, true);
                                post.Categories = Service.CategoriesService.CategoriesByPost(post.ID);

                                //设置结果
                                res = new { post_id = post.ID, post_name = post.Name, tags = post.Tags.Select(p => p.Title), cats = post.Categories.Select(p => p.ID) };
                                isOk = true;
                            }
                            else error = "on_new_error";
                        }
                        else error = "title_null";

                    }

                    #endregion

                    #region Edit

                    else if (mode == "edit")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                if (!stringNull(title))
                                {
                                    post.Title = title;
                                    post.Content = content;
                                    post.LastModifiedTime = DateTime.Now;
                                    post.Modified = true;
                                    //post.Name = name;
                                    post.PostImage = image ?? "";
                                    post.Practice = _practiceSet && practice;
                                    post.VisibleState = visible;
                                    post.Approved = _approvedSet && approved;
                                    //修改
                                    AddTags(postId, tags);
                                    AddCategories(postId, categories);
                                    if (Service.PostsService.Update(post))
                                    {
                                        post.Tags = Service.TagsService.GetTagsByPostId(postId);
                                        post.Categories = Service.CategoriesService.CategoriesByPost(postId);
                                        isOk = true;
                                        res = new { post_id = postId, post_name = post.Name, tags = post.Tags.Select(p => p.Title), cats = post.Categories.Select(p => p.ID) };
                                    }
                                    else error = "on_update_error";
                                }
                                else error = "title_null";
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    #region Rename

                    else if (mode == "rename")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                if (!stringNull(name))
                                {
                                    if (Regex.IsMatch(name, Validater.OtherName))
                                    {
                                        var nPost = Service.PostsService.GetPostByName(name);
                                        if (nPost == null || nPost.ID == post.ID)
                                        {
                                            if (post.Name == name)
                                                isOk = true;
                                            else
                                            {
                                                post.Name = name;
                                                if (Service.PostsService.Update(post, PostModified.Rename))
                                                    isOk = true;
                                                else error = "on_rename_error";
                                            }
                                        }
                                        else error = "has_name";
                                    }
                                    else error = "name_format";
                                }
                                else error = "name_null";
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    #region Practice

                    else if (mode == "practice")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                if (_practiceSet)
                                {
                                    post.Practice = practice;
                                    if (Service.PostsService.Update(post, PostModified.Practice)) isOk = true;
                                    else error = "on_practice_error";
                                }
                                else error = "data_null";
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    #region DelFlag

                    else if (mode == "delflag")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                if (_delflagSet)
                                {
                                    post.DelFlag = delflag;
                                    if (Service.PostsService.Update(post, PostModified.DelFlag))
                                    {
                                        isOk = true;
                                    }
                                    else error = "on_delflag_error";
                                }
                                else error = "data_null";
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    #region Categories

                    else if (mode == "categories")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                AddCategories(postId, categories);
                                post.Categories = Service.CategoriesService.CategoriesByPost(postId);
                                isOk = true;
                                res = new { cats = post.Categories.Select(p => p.ID) };
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    #region Visible

                    else if (mode == "visible")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                post.VisibleState = visible;
                                if (Service.PostsService.Update(post, PostModified.Visible))
                                {
                                    isOk = true;
                                }
                                else error = "on_visible_error";
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    #region NewTag

                    else if (mode == "newtag")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                //AddTags(postId, new string[] { newTag });
                                //post.Tags = Service.TagsService.GetTagsByPostId(postId);
                                int tagIdd = AddNewTag(postId, newTag);
                                isOk = true;
                                res = new { tag_id = tagIdd };
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    #region 删除标签

                    else if (mode == "deletetag")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                if (tagId > 0)
                                {
                                    Service.PostInTagsService.Delete(postId, tagId);
                                    isOk = true;
                                }
                                else error = "data_null";
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }
                    #endregion

                    #region 删除文章

                    else if (mode == "delete")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                if (Service.PostsService.Delete(postId))
                                {
                                    isOk = true;
                                }
                                else error = "on_delete_error";
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    #region 批准文章

                    else if (mode == "approve")
                    {
                        if (postId > 0)
                        {
                            var post = Service.PostsService.GetPostById(postId);
                            if (post != null)
                            {
                                if (_approvedSet)
                                {
                                    post.Approved = approved;
                                    if (Service.PostsService.Update(post, PostModified.Approve))
                                    {
                                        isOk = true;
                                    }
                                    else error = "on_approve_error";
                                }
                                else error = "data_null";
                            }
                            else error = "post_null";
                        }
                        else error = "post_null";
                    }

                    #endregion

                    //context.Response.Write(JsonConvert.SerializeObject(new { result = isOk ? "ok" : "no", errors = errors, res = retains }));
                    //return;
                    Result.Set(isOk, error, res);
                }
            }
            base.OnInit(e);
        }