PageRenderTime 36ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/2007/Projects/Eneta.Portal.Forum/Controls/ForumsMainViewControl.cs

#
C# | 275 lines | 239 code | 34 blank | 2 comment | 36 complexity | feb01c93a61e49dc3fa8829c71b908b2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web.UI;
  5. using System.Web.UI.HtmlControls;
  6. using System.Web.UI.WebControls;
  7. using Eneta.Portal.Common;
  8. using Eneta.Portal.Common.Dto;
  9. using Eneta.Portal.Common.Repository;
  10. using Microsoft.SharePoint;
  11. namespace Eneta.Portal.Forum.Controls
  12. {
  13. public class ForumsMainViewControl : UserControl
  14. {
  15. protected Repeater _categoriesRepeater;
  16. protected HtmlGenericControl _introBlock;
  17. protected HtmlAnchor _rssLink;
  18. protected HtmlAnchor _rssCommentsLink;
  19. protected HtmlAnchor _rssLinkBottom;
  20. protected HtmlAnchor _rssCommentsLinkBottom;
  21. private ForumRepository _forumRepository;
  22. private ForumRepository ForumRepository
  23. {
  24. get
  25. {
  26. if (_forumRepository != null)
  27. return _forumRepository;
  28. _forumRepository = new ForumRepository(SPContext.Current.Web);
  29. return _forumRepository;
  30. }
  31. }
  32. protected override void OnInit(EventArgs e)
  33. {
  34. base.OnInit(e);
  35. _categoriesRepeater.ItemDataBound += CategoriesRepeaterItemDataBound;
  36. }
  37. protected void CategoriesRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
  38. {
  39. if (e.Item.DataItem == null)
  40. return;
  41. var category = (ForumCategoryDto)e.Item.DataItem;
  42. var forumsRepeater = (Repeater) e.Item.FindControl("_forumsRepeater");
  43. if(forumsRepeater == null)
  44. {
  45. Logger.LogWarning("Cannot find _forumsRepeater", GetType().ToString());
  46. return;
  47. }
  48. forumsRepeater.ItemDataBound += ForumsRepeaterItemDataBound;
  49. forumsRepeater.DataSource = category.Forums;
  50. forumsRepeater.DataBind();
  51. }
  52. protected void ForumsRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
  53. {
  54. if (e.Item.DataItem == null)
  55. return;
  56. var forum = (ForumDto)e.Item.DataItem;
  57. var moderatorsRepeater = (Repeater) e.Item.FindControl("_moderatorsRepeater");
  58. moderatorsRepeater.DataSource = forum.Moderators;
  59. moderatorsRepeater.DataBind();
  60. }
  61. protected override void OnLoad(EventArgs e)
  62. {
  63. base.OnLoad(e);
  64. Page.Title = "Foorum | Microsoft Eneta";
  65. if (IsPostBack)
  66. return;
  67. try
  68. {
  69. if (string.IsNullOrEmpty(Request.QueryString["category"]))
  70. if (!string.IsNullOrEmpty(Request.QueryString["rss"]))
  71. {
  72. Response.Clear();
  73. Response.ContentType = "application/rss+xml";
  74. Response.Write(ForumRepository.GetCommonRss());
  75. Response.End();
  76. return;
  77. }
  78. else if (!string.IsNullOrEmpty(Request.QueryString["commentrss"]))
  79. {
  80. Response.Clear();
  81. Response.ContentType = "application/rss+xml";
  82. Trace.Write("ForumCommentsRss: start");
  83. Response.Write(ForumRepository.GetCommentsRss());
  84. Trace.Write("ForumCommentsRss: finish");
  85. Response.End();
  86. return;
  87. }
  88. }
  89. catch(Exception ex)
  90. {
  91. Logger.LogWarning("OnLoad(): Error creating forum rss", GetType().ToString());
  92. Logger.LogException(ex);
  93. Response.End();
  94. }
  95. int id;
  96. IList<ForumCategoryDto> categories;
  97. try
  98. {
  99. categories = ForumRepository.GetForumCategories();
  100. if (int.TryParse(Request.QueryString["category"], out id))
  101. {
  102. if (!string.IsNullOrEmpty(Request.QueryString["rss"]))
  103. {
  104. Response.Clear();
  105. Response.ContentType = "application/rss+xml";
  106. Response.Write(ForumRepository.GetForumCategoryRss(id));
  107. Response.End();
  108. return;
  109. }
  110. if (!string.IsNullOrEmpty(Request.QueryString["commentrss"]))
  111. {
  112. Response.Clear();
  113. Response.ContentType = "application/rss+xml";
  114. Response.Write(ForumRepository.GetForumCategoryCommentsRss(id));
  115. Response.End();
  116. return;
  117. }
  118. categories = (from c in categories
  119. where c.Id == id
  120. select c).ToList();
  121. _introBlock.Visible = false;
  122. var category = categories.FirstOrDefault();
  123. if (category == null || string.IsNullOrEmpty(category.FeedBurnerUrl))
  124. {
  125. _rssLink.HRef = Request.RawUrl + "&rss=1";
  126. _rssCommentsLink.HRef = string.Empty;
  127. //_rssCommentsLink.HRef = Request.RawUrl + "&commentrss=1";
  128. _rssCommentsLink.Visible = false;
  129. _rssLinkBottom.HRef = _rssLink.HRef;
  130. _rssCommentsLinkBottom.HRef = _rssCommentsLink.HRef;
  131. _rssCommentsLinkBottom.Visible = false;
  132. }
  133. else
  134. {
  135. _rssLink.HRef = category.FeedBurnerUrl;
  136. _rssLink.InnerText = category.Title;
  137. //_rssCommentsLink.HRef = Request.RawUrl + "&commentrss=1";
  138. _rssCommentsLink.HRef = string.Empty;
  139. _rssCommentsLink.Visible = false;
  140. _rssLinkBottom.HRef = _rssLink.HRef;
  141. _rssCommentsLinkBottom.HRef = _rssCommentsLink.HRef;
  142. _rssCommentsLinkBottom.Visible = false;
  143. Page.Title = "Foorum > " + category.Title + " | Microsoft Eneta";
  144. }
  145. }
  146. _categoriesRepeater.DataSource = categories;
  147. _categoriesRepeater.DataBind();
  148. }
  149. catch(Exception ex)
  150. {
  151. Response.Write("<!-- " + ex + " -->");
  152. return;
  153. }
  154. var master = Page.Master;
  155. if (master != null)
  156. {
  157. try
  158. {
  159. var link = (HtmlLink) master.FindControl("rssMetaUrl");
  160. if (!string.IsNullOrEmpty(Request.QueryString["category"]))
  161. {
  162. link.Visible = true;
  163. var cat = categories[0];
  164. if (!string.IsNullOrEmpty(cat.FeedBurnerUrl))
  165. link.Href = cat.FeedBurnerUrl;
  166. else
  167. link.Href = Request.RawUrl + "&rss=1";
  168. link.Attributes["title"] = "Eneta foorum: " + categories[0].Title;
  169. }
  170. else
  171. {
  172. link.Visible = true;
  173. link.Attributes["title"] = "Eneta foorum";
  174. }
  175. }
  176. catch(Exception ex)
  177. {
  178. Response.Write("<!-- " + ex + " -->");
  179. }
  180. try
  181. {
  182. var dto = new RssLinksDto();
  183. dto.RssTitle = _rssLink.InnerText;
  184. dto.RssUrl = _rssLink.HRef;
  185. dto.CommentsRssTitle = _rssCommentsLink.InnerText;
  186. dto.CommentsRssUrl = _rssCommentsLink.HRef;
  187. var typedMaster = master as MasterBaseWithDesignHack;
  188. if (typedMaster != null)
  189. {
  190. typedMaster.SetRssLinks(dto);
  191. }
  192. }
  193. catch(Exception ex)
  194. {
  195. Response.Write("<!-- ex: " + ex + " -->");
  196. }
  197. }
  198. }
  199. private UserProfileRepository _profileRepository;
  200. public UserProfileRepository ProfileRepository
  201. {
  202. get
  203. {
  204. if (_profileRepository != null)
  205. return _profileRepository;
  206. _profileRepository = new UserProfileRepository(SPContext.Current.Web);
  207. return _profileRepository;
  208. }
  209. }
  210. public string GetProfileImageUrl(int profileId)
  211. {
  212. var profile = ProfileRepository.GetUserProfileById(profileId);
  213. if (profile == null)
  214. return string.Empty;
  215. return profile.UserImage32x32Url;
  216. }
  217. public string GetShortTitle(string title)
  218. {
  219. if (string.IsNullOrEmpty(title))
  220. return title;
  221. if (title.Trim().Length == 0)
  222. return title;
  223. var len = 15;
  224. var suffix = string.Empty;
  225. if (title.Length < 15)
  226. len = title.Length;
  227. else if (title.Length > 15)
  228. suffix = "...";
  229. try
  230. {
  231. title = title.Substring(0, len).Trim() + suffix;
  232. }
  233. catch(Exception ex)
  234. {
  235. Logger.LogWarning("Exception in GetShortTitle()", string.Empty);
  236. Logger.LogException(ex);
  237. }
  238. return title;
  239. }
  240. }
  241. }