示例#1
0
        private static string CreateUpdatePost(XmlDocument doc, IGraffitiUser user)
        {
            Post         post            = null;
            XmlAttribute postidAttribute = doc.SelectSingleNode("/post").Attributes["id"];

            if (postidAttribute == null)
            {
                post = new Post();
            }
            else
            {
                int pid = Int32.Parse(postidAttribute.Value);
                if (pid > 0)
                {
                    post = new Post(pid);
                }
                else
                {
                    post = new Post();
                }
            }
            XmlNode node = doc.SelectSingleNode("/post");



            if (GraffitiUsers.IsUserInRole(user.Name, GraffitiUsers.AdminRole))
            {
                XmlNode usernameNode = node.SelectSingleNode("author");
                if (usernameNode != null && !string.IsNullOrEmpty(usernameNode.Value))
                {
                    post.UserName = GraffitiUsers.GetUser(usernameNode.Value).Name;
                }
            }

            if (string.IsNullOrEmpty(post.UserName) && post.IsNew)
            {
                post.UserName = user.Name;
            }


            post.PostBody = GetNodeValue(node.SelectSingleNode("postBody"), null);
            if (string.IsNullOrEmpty(post.PostBody))
            {
                throw new RESTConflict("The Post body element is missing and is required");
            }


            post.CategoryId = GetNodeValue(node.SelectSingleNode("categoryId"), -1);
            if (post.CategoryId <= 0)
            {
                throw new RESTConflict("The category element is missing (or has an invalid value) and is required");
            }

            post.Title = GetNodeValue(node.SelectSingleNode("title"), null);
            if (string.IsNullOrEmpty(post.Title))
            {
                throw new RESTConflict("The title element is missing and is required");
            }

            post.ExtendedBody = GetNodeValue(node.SelectSingleNode("extendedBody"), null);

            XmlNode publishedDateNode = node.SelectSingleNode("publishedDate");

            if (publishedDateNode != null && !string.IsNullOrEmpty(publishedDateNode.InnerText) &&
                DateTime.Parse(publishedDateNode.InnerText) > new DateTime(2000, 1, 1))
            {
                post.Published = DateTime.Parse(publishedDateNode.InnerText);
            }
            else if (post.IsNew)
            {
                post.Published = SiteSettings.CurrentUserTime;
            }

            post.Name = GetNodeValue(node.SelectSingleNode("name"), post.Name);


            post.Status = GetNodeValue(node.SelectSingleNode("status"), post.IsNew ? (int)PostStatus.Draft : post.Status);

            post.TagList = GetNodeValue(node.SelectSingleNode("tags"), null);

            post.ContentType = GetNodeValue(node.SelectSingleNode("contenttype"), null);

            post.SortOrder = GetNodeValue(node.SelectSingleNode("sortOrder"), post.SortOrder);

            post.HomeSortOrder = GetNodeValue(node.SelectSingleNode("homeSortOrder"), post.HomeSortOrder);

            post.MetaDescription = GetNodeValue(node.SelectSingleNode("metaDescription"), post.MetaDescription);
            post.MetaKeywords    = GetNodeValue(node.SelectSingleNode("metaKeywords"), post.MetaKeywords);
            post.IsHome          = GetNodeValue(node.SelectSingleNode("isHome"), post.IsHome);
            post.EnableComments  = GetNodeValue(node.SelectSingleNode("enableComments"), post.EnableComments);

            XmlNodeList customFields = node.SelectNodes("customFields/customField");

            foreach (XmlNode cNode in customFields)
            {
                post[cNode.Attributes["key"].Value] = cNode.InnerText;
            }

            Permission perm = RolePermissionManager.GetPermissions(post.CategoryId, user);

            if (GraffitiUsers.IsAdmin(user) || perm.Publish)
            {
                post.IsDeleted = GetNodeValue(node.SelectSingleNode("isDeleted"), post.IsDeleted);
            }

            int id =
                PostRevisionManager.CommitPost(post, user, SiteSettings.Get().FeaturedId == post.Id,
                                               post.Category.FeaturedId == post.Id);

            return(string.Format("<result id=\"{0}\">true</result>", id));
        }
示例#2
0
    protected void publish_return_click(object sender, EventArgs e)
    {
        try
        {
            if (!IsValid)
            {
                return;
            }

            IGraffitiUser user = GraffitiUsers.Current;

            ListItem catItem = CategoryList.SelectedItem;
            if (catItem.Value == "-1" && String.IsNullOrEmpty(newCategory.Text))
            {
                SetMessage("Please enter a name for the new Category.", StatusType.Error);
                return;
            }

            string extenedBody = txtContent_extend.Text;
            string postBody    = txtContent.Text;

            if (string.IsNullOrEmpty(postBody))
            {
                SetMessage("Please enter a post body.", StatusType.Warning);
                return;
            }

            Category c = new Category();

            if (catItem.Value == "-1")
            {
                try
                {
                    Category temp = new Category();
                    temp.Name = newCategory.Text;
                    temp.Save();

                    c = temp;

                    CategoryController.Reset();
                }
                catch (Exception ex)
                {
                    SetMessage("The category could not be created. Reason: " + ex.Message, StatusType.Error);
                }
            }
            else
            {
                c = new CategoryController().GetCachedCategory(Int32.Parse(catItem.Value), false);
            }

            string pid = Request.QueryString["id"];
            Post   p   = pid == null ? new Post() : new Post(pid);

            if (p.IsNew)
            {
                p["where"] = "web";

                p.UserName = user.Name;

                if (Request.Form["dateChangeFlag"] == "true")
                {
                    p.Published = PublishDate.DateTime;
                }
                else
                {
                    p.Published = DateTime.Now.AddHours(SiteSettings.Get().TimeZoneOffSet);
                }
            }
            else
            {
                p.Published = PublishDate.DateTime;
            }

            p.ModifiedOn = DateTime.Now.AddHours(SiteSettings.Get().TimeZoneOffSet);

            p.PostBody = postBody;
            if (string.IsNullOrEmpty(extenedBody) || extenedBody == "<p></p>" || extenedBody == "<p>&nbsp;</p>" ||
                extenedBody == "<br />\r\n")
            {
                p.ExtendedBody = null;
            }
            else
            {
                p.ExtendedBody = extenedBody;
            }

            p.Title           = Server.HtmlEncode(txtTitle.Text);
            p.EnableComments  = EnableComments.Checked;
            p.Name            = txtName.Text;
            p.TagList         = txtTags.Text.Trim();
            p.ContentType     = "text/html";
            p.CategoryId      = c.Id;
            p.Notes           = txtNotes.Text;
            p.ImageUrl        = postImage.Text;
            p.MetaKeywords    = Server.HtmlEncode(txtKeywords.Text.Trim());
            p.MetaDescription = Server.HtmlEncode(txtMetaScription.Text.Trim());
            p.IsHome          = HomeSortOverride.Checked;
            p.PostStatus      = (PostStatus)Enum.Parse(typeof(PostStatus), Request.Form[PublishStatus.UniqueID]);

            CustomFormSettings cfs = CustomFormSettings.Get(c);
            if (cfs.HasFields)
            {
                foreach (CustomField cf in cfs.Fields)
                {
                    if (cf.FieldType == FieldType.CheckBox && Request.Form[cf.Id.ToString()] == null)
                    {
                        p[cf.Name] = null;                         // false.ToString();
                    }
                    else if (cf.FieldType == FieldType.DateTime && Request.Form[cf.Id.ToString()].IndexOf("_") > -1)
                    {
                        p[cf.Name] = null;
                    }
                    else
                    {
                        p[cf.Name] = Request.Form[cf.Id.ToString()];
                    }
                }
            }

            if (HasDuplicateName(p))
            {
                SetMessage("A post in the selected category already exists with the same name.", StatusType.Error);
                return;
            }

            PostRevisionManager.CommitPost(p, user, FeaturedSite.Checked, FeaturedCategory.Checked);

            string CatQuery = (Request.QueryString["category"] == null)
                                                  ? null
                                                  : (p.Status == 1) ? "&category=" + p.CategoryId : "&category=" + Request.QueryString["category"];
            string AuthQuery = (Request.QueryString["author"] == null) ? null : "&author=" + Request.QueryString["author"];
            Response.Redirect("~/graffiti-admin/posts/" + "?id=" + p.Id + "&status=" + p.Status + CatQuery + AuthQuery);
        }
        catch (Exception ex)
        {
            SetMessage("Your post could not be saved. Reason: " + ex.Message, StatusType.Error);
        }
    }