public ActionResult Compose(string id) { DraftPost post = new DraftPost(); if (!string.IsNullOrEmpty(id)) { post = DraftComp.Load(id); this.Session["Post"] = post; } else { if (this.Session["Post"] == null) { TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(SettingsComp.GetSettings().Timezone); post.Time = LocalTime.GetCurrentTime(tzi); post.Type = PostType.New; this.Session["Post"] = post; } else { post = this.Session["Post"] as DraftPost; } } ComposePostModel model = new ComposePostModel(); model.Title = post.Title; model.PublishDate = post.Time.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture); List<string> catList = new List<string>(); if (!string.IsNullOrEmpty(post.CatID)) { string[] catIDs = post.CatID.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string catID in catIDs) { catList.Add(catID); } } model.CatID = catList; model.Content = post.Contents; model.DraftID = post.DraftID; model.Post = post; this.Response.Cache.SetCacheability(HttpCacheability.NoCache); this.ViewData.Model = model; return this.View(); }
/// <summary> /// The publish. /// </summary> /// <param name="post"> /// The post. /// </param> /// <returns> /// The System.Boolean. /// </returns> public static bool Publish(DraftPost post) { TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(SettingsComp.GetSettings().Timezone); if (post.Time > LocalTime.GetCurrentTime(tzi)) { ConfigHelper.DataContext.DraftData.AddSchedule(post.DraftID, post.Time); return false; } IDraftPostData postData = ConfigHelper.DataContext.DraftPostData; post.FileID = postData.Publish(post); UpdatePost(post); UpdateCategory(post.OldCatID, post.CatID); if (post.Type == PostType.New) { UpdateMonth(post.Time); } IDraftData draftData = ConfigHelper.DataContext.DraftData; draftData.Delete(post.DraftID); return true; }
/// <summary> /// Creates a draft post /// Called when creating a new post /// </summary> /// <param name="title"> /// The Title /// </param> /// <param name="author"> /// The Author /// </param> /// <param name="time"> /// The Time /// </param> /// <param name="catID"> /// Category ID /// </param> /// <returns> /// Draft Post /// </returns> public DraftPost Create(string title, string author, DateTime time, string catID) { DraftPost post = new DraftPost(); post.FileID = string.Empty; DateTime now = LocalTime.GetCurrentTime(SettingsData.TimeZoneInfo); post.DraftID = DraftPost.GetNewDraftID(this.GetUniqueFileID(title), now); post.Time = time; post.CatID = catID; post.Title = title; post.Author = author; post.Type = PostType.New; string xmlTime = post.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture); string template = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; template += "<Post Title=\"{0}\" FileID=\"{1}\" Author=\"{2}\" Time=\"{3}\" CatID=\"{4}\" Comments=\"0\" Type=\"New\">"; template += "<Documents></Documents>"; template += "<Content></Content>"; template += "<Comments></Comments></Post>"; string xml = string.Format(template, title, post.FileID, author, xmlTime, catID); XElement postElem = XElement.Parse(xml); string path = this.GetFilePath(post.DraftID); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; XmlWriter writer = XmlWriter.Create(path, settings); postElem.Save(writer); writer.Close(); DraftData draft = new DraftData(); draft.Insert(post.DraftID, title); return post; }
/// <summary> /// Save draft post /// Called while creating / editing post /// </summary> /// <param name="post"> /// Draft Post /// </param> public void Save(DraftPost post) { XElement root = null; string path = this.GetFilePath(post.DraftID); string template = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; template += "<Post Title=\"{0}\" FileID=\"{1}\" Author=\"{2}\" Time=\"{3}\" CatID=\"{4}\" Comments=\"{5}\" Type=\"{6}\">"; template += "<Documents></Documents>"; template += "<Content></Content>"; template += "<Comments></Comments></Post>"; template = string.Format( template, post.Title, post.FileID, post.Author, post.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture), post.CatID, post.Comments.Count, post.Type.ToString()); try { root = XElement.Parse(template); } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } XElement postElem = root; try { XElement docsElem = postElem.Element("Documents"); foreach (Document document in post.Documents) { XElement docElem = new XElement("Document", new XAttribute("Path", document.Path)); docsElem.Add(docElem); } // string contents2 = HttpContext.Current.Server.HtmlEncode(post.Contents); // postElem.Element("Content").Value = contents2; postElem.Element("Content").Value = post.Contents; XElement commentsElem = postElem.Element("Comments"); foreach (Comment comment in post.Comments) { XElement commentElem = new XElement( "Comment", new XAttribute("ID", comment.ID), new XAttribute("Name", comment.Name), new XAttribute("Url", comment.Url), new XAttribute( "Time", comment.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture)), new XAttribute("Ip", comment.Ip)); // commentElem.Value = HttpContext.Current.Server.HtmlEncode(comment.Text); commentElem.Value = comment.Text; commentsElem.Add(commentElem); } if (post.Type == PostType.Draft) { // Save the old CatID attribute postElem.SetAttributeValue("OldCatID", post.OldCatID); } if (!File.Exists(path)) { FileStream fs2 = File.Create(path); fs2.Close(); } XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; using (FileStream fs = File.Open(path, FileMode.Truncate, FileAccess.Write, FileShare.None)) { using (XmlWriter writer = XmlWriter.Create(fs, settings)) { root.Save(writer); } } } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } }
/// <summary> /// Publish the post /// Draft posts will merge the comments /// New posts will get a unique file id /// OldCatID, DraftType etc will be removed /// </summary> /// <param name="post"> /// Draft Post /// </param> /// <returns> /// Post ID /// </returns> public string Publish(DraftPost post) { if (post.Type == PostType.Draft) { string draftPath = this.GetFilePath(post.DraftID); XElement root = null; root = XElement.Load(draftPath); XAttribute attr = root.Attribute("OldCatID"); if (attr != null) { attr.Remove(); } XAttribute attr2 = root.Attribute("Type"); if (attr2 != null) { attr2.Remove(); } // Get a copy of the post to merge comments PostData data = new PostData(); Post existPost = data.Load(post.FileID); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; string publishPath = this.GetFilePath(post.FileID); XmlWriter writer = XmlWriter.Create(publishPath, settings); root.Save(writer); writer.Close(); // Merge comments Post publishPost = data.Load(post.FileID); publishPost.Comments = existPost.Comments; data.Save(publishPost); System.IO.File.Delete(draftPath); } if (post.Type == PostType.New) { string filePath = this.GetFilePath(post.DraftID); XElement root = null; root = XElement.Load(filePath); XAttribute attr2 = root.Attribute("Type"); if (attr2 != null) { attr2.Remove(); } post.FileID = this.GetUniqueFileID(post.Title); root.Attribute("FileID").Value = post.FileID; string publishPath = this.GetFilePath(post.FileID); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; XmlWriter writer = XmlWriter.Create(publishPath, settings); root.Save(writer); writer.Close(); System.IO.File.Delete(filePath); } // Newly published posts will get their file ID here. return post.FileID; }
/// <summary> /// Loads draft /// Called when creating a post /// </summary> /// <param name="draftID"> /// Draft ID /// </param> /// <returns> /// Draft Post /// </returns> public DraftPost Load(string draftID) { string path = this.GetFilePath(draftID); XElement root = null; try { root = XElement.Load(path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } XElement postElem = root; DraftPost post = new DraftPost(); try { post.FileID = postElem.Attribute("FileID").Value; post.DraftID = draftID; post.Title = postElem.Attribute("Title").Value; post.Author = postElem.Attribute("Author").Value; post.CatID = postElem.Attribute("CatID").Value; XAttribute attr = postElem.Attribute("Type"); if (attr != null) { post.Type = (PostType)Enum.Parse(typeof(PostType), attr.Value, true); } // Load draft data, if we are loading a draft if (post.Type == PostType.Draft) { if (postElem.Attribute("OldCatID") != null) { post.OldCatID = postElem.Attribute("OldCatID").Value; } } try { post.Time = DateTime.ParseExact( postElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); } catch (Exception ex) { string msg = string.Format(DATE_FORMAT_ERROR, draftID); Logger.Log(msg, ex); post.Time = DateTime.MinValue; } XElement docsElem = postElem.Element("Documents"); if (docsElem != null) { foreach (XElement docElem in docsElem.Elements("Document")) { Document document = new Document(); document.Path = docElem.Attribute("Path").Value; post.Documents.Add(document); } } // string contents = HttpContext.Current.Server.HtmlDecode(postElem.Element("Content").Value); string contents = HttpContext.Current.Server.HtmlDecode(GetInnerXml(postElem.Element("Content"))); post.Contents = contents; XElement commentsElem = postElem.Element("Comments"); if (commentsElem != null) { foreach (XElement commentElem in commentsElem.Elements("Comment")) { Comment comment = new Comment(); comment.FileID = post.FileID; comment.ID = commentElem.Attribute("ID").Value; comment.Name = commentElem.Attribute("Name").Value; comment.Text = HttpContext.Current.Server.HtmlDecode(GetInnerXml(commentElem)); comment.Url = commentElem.Attribute("Url").Value; comment.Time = DateTime.ParseExact( commentElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); comment.Ip = commentElem.Attribute("Ip").Value; post.Comments.Add(comment); } } } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } return post; }
/// <summary> /// Creates draft post /// Called when editing a published post /// </summary> /// <param name="fileID"> /// Post ID /// </param> /// <returns> /// Draft Post /// </returns> public DraftPost Create(string fileID) { PostData data = new PostData(); Post post = data.Load(fileID); DraftPost draft = new DraftPost(post); DateTime now = LocalTime.GetCurrentTime(SettingsData.TimeZoneInfo); draft.DraftID = DraftPost.GetNewDraftID(fileID, now); this.Save(draft); DraftData data2 = new DraftData(); data2.Insert(draft.DraftID, draft.Title); return draft; }
/// <summary> /// Strange method, got to look into it /// </summary> /// <param name="model"> /// The model /// </param> private void UpdatePost(ComposePostModel model) { DraftPost post = new DraftPost(); if (this.Session["Post"] != null) { post = this.Session["Post"] as DraftPost; } model.Title = model.Title ?? string.Empty; post.Title = model.Title; post.CatID = string.Empty; if (model.CatID != null) { foreach (string catID in model.CatID) { post.CatID += catID + ","; } post.CatID = post.CatID.Substring(0, post.CatID.Length - 1); } if (post.Type == PostType.New) { try { if (!string.IsNullOrEmpty(model.PublishDate)) { post.Time = DateTime.ParseExact(model.PublishDate, "MM/dd/yyyy", CultureInfo.InvariantCulture); TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(SettingsComp.GetSettings().Timezone); DateTime now = LocalTime.GetCurrentTime(tzi); post.Time = post.Time.AddHours(now.Hour); post.Time = post.Time.AddMinutes(now.Minute); } } catch { } } // To avoid exception in syntax highlighter post.Contents = model.Content ?? string.Empty; post.Author = this.User.Identity.Name; model.Post = post; this.Session["Post"] = post; }
/// <summary> /// The save post. /// </summary> /// <param name="post"> /// The post. /// </param> /// <returns> /// The LiteBlog.Common.DraftPost. /// </returns> private DraftPost SavePost(DraftPost post) { if (string.IsNullOrEmpty(post.DraftID)) { if (post.Type == PostType.New) { post.DraftID = DraftComp.Create(post.Title, post.Time, post.CatID); } else { post.DraftID = DraftComp.Create(post.FileID); } } // post.Contents = post.Contents.Replace(" ", " "); DraftComp.Save(post); this.Session["Post"] = post; return post; }
/// <summary> /// The update post. /// </summary> /// <param name="post"> /// The post. /// </param> private static void UpdatePost(DraftPost post) { if (post.Type == PostType.New) { PostInfo postInfo = new PostInfo(); postInfo.FileID = post.FileID; postInfo.Author = post.Author; postInfo.Title = post.Title; postInfo.CatID = post.CatID; postInfo.Time = post.Time; BlogComp.Create(postInfo); } else { BlogComp.Update(post.FileID, post.Title, post.CatID); } }
/// <summary> /// The save. /// </summary> /// <param name="post"> /// The post. /// </param> public static void Save(DraftPost post) { IDraftPostData data = ConfigHelper.DataContext.DraftPostData; data.Save(post); }
/// <summary> /// The load. /// </summary> /// <param name="draftID"> /// The draft id. /// </param> /// <returns> /// The LiteBlog.Common.DraftPost. /// </returns> public static DraftPost Load(string draftID) { IDraftPostData data = ConfigHelper.DataContext.DraftPostData; DraftPost post; if (DraftPost.IsDraft(draftID)) { post = data.Load(draftID); } else { Post post2 = PostComp.Load(draftID); post = new DraftPost(post2); // post.GetCode(); // post.RestoreCode(); } //// for earlier versions! // post.GetCode(); // post.RestoreCode(); return post; }