PageRenderTime 67ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/OpenQuarters.WebQuarters/OpenQuarters.WebQuarters.Core/CMSPageVersion.cs

#
C# | 266 lines | 227 code | 33 blank | 6 comment | 33 complexity | df00e650470224a175d93c4f3c125bb3 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using OpenQuarters.WebQuarters.Utils;
  6. using System.Xml;
  7. using System.Data.Linq;
  8. namespace OpenQuarters.WebQuarters.Core
  9. {
  10. public partial class CMSPageVersion : CMSEntity<CMSPageVersion, long>
  11. {
  12. [Newtonsoft.Json.JsonProperty]
  13. public long json_PageVersionId
  14. {
  15. get { return this.PageVersionId; }
  16. set { this.PageVersionId = value; }
  17. }
  18. [Newtonsoft.Json.JsonProperty]
  19. public long json_PageId
  20. {
  21. get { return this.PageId; }
  22. set { this.PageId = value; }
  23. }
  24. [Newtonsoft.Json.JsonProperty]
  25. public string json_Template
  26. {
  27. get { return this.Template; }
  28. set { this.Template = value; }
  29. }
  30. [Newtonsoft.Json.JsonProperty]
  31. public string json_Title
  32. {
  33. get { return this.Title; }
  34. set { this.Title = value; }
  35. }
  36. [Newtonsoft.Json.JsonProperty]
  37. public string json_Keywords
  38. {
  39. get { return this.Keywords; }
  40. set { this.Keywords = value; }
  41. }
  42. [Newtonsoft.Json.JsonProperty]
  43. public string json_Description
  44. {
  45. get { return this.Description; }
  46. set { this.Description = value; }
  47. }
  48. [Newtonsoft.Json.JsonProperty]
  49. public string json_Tags
  50. {
  51. get { return this.Tags; }
  52. set { this.Tags = value; }
  53. }
  54. [Newtonsoft.Json.JsonProperty]
  55. public string json_UserId
  56. {
  57. get { return this.UserId; }
  58. set { this.UserId = value; }
  59. }
  60. [Newtonsoft.Json.JsonProperty]
  61. public bool json_IsLive
  62. {
  63. get { return this.IsLive; }
  64. set { this.IsLive = value; }
  65. }
  66. [Newtonsoft.Json.JsonProperty]
  67. public bool json_NoFollow
  68. {
  69. get { return this.NoFollow; }
  70. set { this.NoFollow = value; }
  71. }
  72. [Newtonsoft.Json.JsonProperty]
  73. public int json_Order
  74. {
  75. get { return this.Order; }
  76. set { this.Order = value; }
  77. }
  78. [Newtonsoft.Json.JsonProperty]
  79. public string json_DateCreated
  80. {
  81. get { return this.DateCreated.ToString("U"); }
  82. set
  83. {
  84. try { this.DateCreated = DateTime.Parse(value); }
  85. catch { this.DateCreated = DateTime.UtcNow; }
  86. }
  87. }
  88. partial void OnTemplateChanging(string value)
  89. {
  90. if (String.IsNullOrEmpty(value))
  91. {
  92. string sError = "Template cannot be blank";
  93. throw new Exception(sError);
  94. }
  95. }
  96. public string[] GetTags()
  97. {
  98. if (string.IsNullOrEmpty(this.Tags))
  99. return new string[]{};
  100. else
  101. return this.Tags.Replace(" ,", "").Replace(", ", "").Replace(" ", " ").Split(',');
  102. }
  103. partial void OnCreated()
  104. {
  105. this.IsLive = false;
  106. this.DateCreated = DateTime.UtcNow;
  107. this.LanguageId = Language.Current.LanguageId;
  108. this.UserId = null;
  109. this.Title = "";
  110. this.Template = "Default";
  111. }
  112. private ContentDefinition contentDefinition = null;
  113. public ContentDefinition ContentDefinition
  114. {
  115. get
  116. {
  117. if (contentDefinition == null)
  118. {
  119. contentDefinition = ContentDefinition.FromJSON(this.Definition);
  120. }
  121. return contentDefinition;
  122. }
  123. set
  124. {
  125. contentDefinition = value;
  126. }
  127. }
  128. public override void OnSaving()
  129. {
  130. base.OnSaving();
  131. this.Definition = this.ContentDefinition.ToJSON();
  132. }
  133. public override void OnSaved()
  134. {
  135. base.OnSaved();
  136. if (this.Title != null && this.Title.Length > 0 && Url.Table.FirstOrDefault(e => e.PageId == this.PageId && e.LanguageId == this.LanguageId) == null)
  137. {
  138. Url url = new Url()
  139. {
  140. PageId = this.PageId,
  141. LanguageId = this.LanguageId,
  142. PageUrl = this.GenerateUrl()
  143. }.Save();
  144. }
  145. }
  146. public string GenerateUrl()
  147. {
  148. string url = "";
  149. var p = this;
  150. url = p.Title.ToUrlSafeString();
  151. while (p != null)
  152. {
  153. p = p.CMSPage.ParentId == null ? null : p.CMSPage.ParentPage.CurrentVersion;
  154. if (p != null && p.Title != null && p.Title.Length > 0 && p.CMSPage.ParentId != null)
  155. {
  156. url = p.Title.ToUrlSafeString() + "/" + url;
  157. }
  158. }
  159. return url;
  160. }
  161. public override System.Linq.Expressions.Expression<Func<CMSPageVersion, bool>> GetIDSelector(long ID)
  162. {
  163. return e => e.PageVersionId == ID;
  164. }
  165. public override long GetKey(CMSPageVersion Entity)
  166. {
  167. return Entity.PageVersionId;
  168. }
  169. public override DataLoadOptions GetLoadOptions()
  170. {
  171. var r = new DataLoadOptions();
  172. r.LoadWith<CMSPageVersion>(e => e.CMSPage);
  173. return r;
  174. }
  175. public class JSON
  176. {
  177. public CMSPageVersion PageVersion = null;
  178. public string Definition = "{}";
  179. public JSON(CMSPageVersion PageVersion)
  180. {
  181. this.PageVersion = PageVersion;
  182. this.Definition = PageVersion.Definition;
  183. }
  184. public JSON()
  185. {
  186. }
  187. }
  188. public void MakeLive()
  189. {
  190. Table.Where(e => e.IsLive && e.PageId == this.PageId).ToList().ForEach(e => e.Delete());
  191. var v = CMSPageVersion.Load(this.PageVersionId);
  192. v.IsLive = true;
  193. v.UserId = null;
  194. v = v.Save();
  195. EventLog.Add(EventLogPriority.L, "CMSPageVersion.MakeLive", this.PageId.ToString(), new CMSPageVersion.JSON(v).ToJSON());
  196. }
  197. //public static List<CMSPageVersion> GetVersionsForUser(long PageId)
  198. //{
  199. // var versions = CMSPageVersion.Table
  200. // .Where(v => v.PageId == PageId && v.IsLive == false)
  201. // return versions.OrderByDescending(v => v.DateCreated).ToList();
  202. //}
  203. public static List<CMSPageVersion> GetVersionsForUser(long PageId)
  204. {
  205. var versions = new List<CMSPageVersion>();
  206. var events = EventLog.Table
  207. .Where(e => e.Category == "CMSPageVersion.MakeLive" && e.Target == PageId.ToString())
  208. .ToList();
  209. foreach (var evt in events)
  210. {
  211. var pageJSON = GeneralUtils.FromJSON<CMSPageVersion.JSON>(evt.Message);
  212. pageJSON.PageVersion.Definition = pageJSON.Definition;
  213. versions.Add(pageJSON.PageVersion);
  214. }
  215. return versions.OrderByDescending(e => e.DateCreated).ToList();
  216. }
  217. public static CMSPageVersion GetVersionsFromArchive(long PageId, long PageVersionId)
  218. {
  219. var versions = new List<CMSPageVersion>();
  220. var events = EventLog.Table
  221. .Where(e => e.Category == "CMSPageVersion.MakeLive" && e.Target == PageId.ToString())
  222. .ToList();
  223. foreach (var evt in events)
  224. {
  225. var pageJSON = GeneralUtils.FromJSON<CMSPageVersion.JSON>(evt.Message);
  226. pageJSON.PageVersion.Definition = pageJSON.Definition;
  227. if (pageJSON.PageVersion.PageVersionId == PageVersionId)
  228. return pageJSON.PageVersion;
  229. }
  230. return null;
  231. }
  232. }
  233. }