PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/CommonLibrary.CMS/src/lib/CommonLibrary.Web.Modules/Src/_Models/Post/PostExtensions.cs

#
C# | 308 lines | 158 code | 51 blank | 99 comment | 9 complexity | 3d6aa392f151de14d64d8cb4e050b180 MD5 | raw file
  1. /*
  2. * Author: Kishore Reddy
  3. * Url: http://commonlibrarynet.codeplex.com/
  4. * Title: CommonLibrary.NET
  5. * Copyright: ? 2009 Kishore Reddy
  6. * License: LGPL License
  7. * LicenseUrl: http://commonlibrarynet.codeplex.com/license
  8. * Description: A C# based .NET 3.5 Open-Source collection of reusable components.
  9. * Usage: Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Data;
  20. using ComLib.Extensions;
  21. using ComLib.Entities;
  22. using ComLib.Caching;
  23. using ComLib.LocationSupport;
  24. using ComLib.Data;
  25. using ComLib.Feeds;
  26. using ComLib.Web.Lib.Core;
  27. using ComLib.Configuration;
  28. using ComLib.ValidationSupport;
  29. using ComLib.Web.Modules.Tags;
  30. using ComLib.Web.Modules.Categorys;
  31. using ComLib.Web.Lib.Attributes;
  32. namespace ComLib.Web.Modules.Posts
  33. {
  34. /// <summary>
  35. /// Settings specific for blog posts.
  36. /// </summary>
  37. public class PostSettings : EntitySettings<Post>
  38. {
  39. /// <summary>
  40. /// Set default values.
  41. /// </summary>
  42. public PostSettings()
  43. {
  44. CacheTime = 300;
  45. CacheOnArchives = true;
  46. CacheOnRecent = true;
  47. IndexPageSize = 15;
  48. CommentPageSize = 20;
  49. }
  50. public bool CacheOnArchives { get; set; }
  51. public bool CacheOnRecent { get; set; }
  52. public int CacheTime { get; set; }
  53. public int NumberDaysCommentsEnabled { get; set; }
  54. public int IndexPageSize { get; set; }
  55. public int CommentPageSize { get; set; }
  56. }
  57. /// <summary>
  58. /// BlogPost entity.
  59. /// </summary>
  60. [Model(Id = 6, DisplayName = "Post", Description = "BlogPost", IsPagable = true,
  61. IsExportable = true, IsImportable = true, FormatsForExport = "xml,ini", FormatsForImport = "xml,ini",
  62. RolesForCreate = "${Admin}", RolesForView = "?", RolesForIndex = "?", RolesForManage = "${Admin}",
  63. RolesForDelete = "${Admin}", RolesForImport = "${Admin}", RolesForExport = "${Admin}")]
  64. public partial class Post : ActiveRecordBaseEntity<Post>, IEntity, IPublishable, IEntityActivatable, IEntityClonable, IEntityMediaSupport
  65. {
  66. /// <summary>
  67. /// Singleton settings.
  68. /// </summary>
  69. private static PostSettings _settings = new PostSettings();
  70. #region Properties
  71. /// <summary>
  72. /// Year that the blogpost was published.
  73. /// </summary>
  74. public int Year { get { return PublishDate.Year; } }
  75. /// <summary>
  76. /// Month that the blogpost was published.
  77. /// </summary>
  78. public int Month { get { return PublishDate.Month; } }
  79. /// <summary>
  80. /// Activate.
  81. /// </summary>
  82. public bool IsActive { get { return IsPublished; } set { IsPublished = value; } }
  83. /// <summary>
  84. /// Get / set the category name used for import.
  85. /// </summary>
  86. public string CategoryName { get; set; }
  87. public Category Category { get; set; }
  88. /// <summary>
  89. /// Url for the slug. e.g. /my-latest-blog-post-with-different-url-than-default-title-48
  90. /// </summary>
  91. public string SlugUrl
  92. {
  93. get
  94. {
  95. if (string.IsNullOrEmpty(Slug))
  96. return ComLib.Web.UrlSeoUtils.BuildValidUrl(Title) + "-" + Id;
  97. return ComLib.Web.UrlSeoUtils.BuildValidUrl(Slug) + "-" + Id;
  98. }
  99. }
  100. #endregion
  101. #region Validation
  102. /// <summary>
  103. /// Gets the validator to use for validating this entity.
  104. /// </summary>
  105. /// <returns></returns>
  106. protected override IValidator GetValidatorInternal()
  107. {
  108. var val = new EntityValidator((validationEvent) =>
  109. {
  110. int initialErrorCount = validationEvent.Results.Count;
  111. IValidationResults results = validationEvent.Results;
  112. Post entity = (Post)validationEvent.Target;
  113. Validation.IsStringLengthMatch(entity.Title, false, true, true, 1, 150, results, "Title");
  114. Validation.IsStringLengthMatch(entity.Description, true, false, true, -1, 200, results, "Description");
  115. Validation.IsStringLengthMatch(entity.Content, false, true, false, 1, -1, results, "Content");
  116. Validation.IsStringLengthMatch(entity.Tags, true, false, true, -1, 80, results, "Tags");
  117. Validation.IsStringLengthMatch(entity.Slug, true, false, true, -1, 150, results, "Slug");
  118. if (!IsPersistant())
  119. {
  120. Validation.IsDateWithinRange(entity.PublishDate, true, true, DateTime.Today, DateTime.Today.AddDays(180), results, "PublishDate");
  121. }
  122. return initialErrorCount == validationEvent.Results.Count;
  123. });
  124. return val;
  125. }
  126. #endregion
  127. #region IPublishable
  128. /// <summary>
  129. /// Gets the author.
  130. /// </summary>
  131. /// <value>The author.</value>
  132. public string Author { get { return UpdateUser; } }
  133. /// <summary>
  134. /// Gets the relative link.
  135. /// </summary>
  136. /// <value>The relative link.</value>
  137. public string UrlRelative
  138. {
  139. get
  140. {
  141. return SlugUrl;
  142. }
  143. }
  144. /// <summary>
  145. /// Gets the absolute link.
  146. /// </summary>
  147. /// <value>The relative link.</value>
  148. public string UrlAbsolute
  149. {
  150. get
  151. {
  152. return "/" + SlugUrl;
  153. }
  154. }
  155. /// <summary>
  156. /// Gets the GUID.
  157. /// </summary>
  158. /// <value>The GUID.</value>
  159. public string GuidId
  160. {
  161. get { return UrlAbsolute; }
  162. }
  163. #endregion
  164. #region Life-Cycle Events
  165. /// <summary>
  166. /// Handle life-cycle event.
  167. /// </summary>
  168. /// <param name="ctx"></param>
  169. /// <returns></returns>
  170. public override bool OnBeforeValidate(object ctx)
  171. {
  172. if (this.PublishDate == DateTime.MinValue)
  173. PublishDate = DateTime.Now;
  174. if (this.PublishDate.TimeOfDay.IsMidnightExactly())
  175. this.PublishDate = this.PublishDate.GetDateWithCurrentTime();
  176. return true;
  177. }
  178. /// <summary>
  179. /// On before save callback
  180. /// </summary>
  181. /// <param name="ctx"></param>
  182. /// <returns></returns>
  183. public override bool OnBeforeSave(object ctx)
  184. {
  185. // Set the category id.
  186. if (!string.IsNullOrEmpty(CategoryName))
  187. {
  188. var lookup = Categorys.Category.LookupFor<Post>();
  189. this.CategoryId = lookup != null && lookup.ContainsKey(CategoryName) ? lookup[CategoryName].Id : 0;
  190. }
  191. this.Description = this.Content.Truncate(200);
  192. return true;
  193. }
  194. /// <summary>
  195. /// After this is created/updated, handle the tags.
  196. /// </summary>
  197. public override void OnAfterSave()
  198. {
  199. // Queue up the processing of tags.
  200. int groupId = ModuleMap.Instance.GetId(typeof(Post));
  201. Queue.Queues.Enqueue<TagsEntry>(new TagsEntry() { GroupId = groupId, RefId = Id, Tags = Tags });
  202. }
  203. #endregion
  204. #region Static Methods
  205. /// <summary>
  206. /// Settings for this entity.
  207. /// </summary>
  208. public static new PostSettings Settings
  209. {
  210. get { return _settings; }
  211. }
  212. /// <summary>
  213. /// Get recent blogposts.
  214. /// </summary>
  215. /// <param name="?"></param>
  216. /// <returns></returns>
  217. public static IList<Post> GetRecent(int count)
  218. {
  219. var recents = Cacher.Get<IList<Post>>("Posts_Recent", Settings.CacheOnRecent, Settings.CacheTime.Seconds(), () =>
  220. Find(Query<Post>.New().Limit(count).Where(p => p.IsPublished).Is(true)
  221. .OrderByDescending(p => p.PublishDate)));
  222. return recents;
  223. }
  224. /// <summary>
  225. /// Return a datatable of the year/month count groupings.
  226. /// </summary>
  227. /// <example>
  228. /// e.g.
  229. /// 2009, January, 4
  230. /// 2009, February, 2
  231. /// 2009, May, 6
  232. /// </example>
  233. /// <returns></returns>
  234. public static DataTable GetArchives()
  235. {
  236. var archives = Cacher.Get<DataTable>("Posts_Archive", Settings.CacheOnArchives, Settings.CacheTime.Seconds(), () =>
  237. {
  238. var repo = Post.Repository;
  239. DataTable groups = repo.Group(Query<Post>.New().Where(p => p.IsPublished).Is(true)
  240. .OrderByDescending(p => p.Year).OrderBy(p => p.Month), "Year", "Month");
  241. return groups;
  242. });
  243. return archives;
  244. }
  245. /// <summary>
  246. /// Gets the by tags.
  247. /// </summary>
  248. /// <param name="tag">The tag.</param>
  249. /// <returns></returns>
  250. public static IList<Post> GetByTags(string tag)
  251. {
  252. var posts = TagHelper.GetByTags<Post>(tag, (ids) => Query<Post>.New().Where(p => p.Id).In<int>(ids).And(p => p.IsPublished).Is(true));
  253. return posts;
  254. }
  255. #endregion
  256. }
  257. }