/CMSModules/Forums/Controls/Posts/PostListing.ascx.cs

https://bitbucket.org/kudutest2/kenticogit · C# · 373 lines · 272 code · 60 blank · 41 comment · 37 complexity · dd31ced897627f5e59ca35cd298467d1 MD5 · raw file

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using CMS.GlobalHelper;
  13. using CMS.UIControls;
  14. using CMS.SettingsProvider;
  15. using CMS.WorkflowEngine;
  16. using CMS.SiteProvider;
  17. using CMS.TreeEngine;
  18. using CMS.Forums;
  19. using CMS.CMSHelper;
  20. using TimeZoneInfo = CMS.SiteProvider.TimeZoneInfo;
  21. public partial class CMSModules_Forums_Controls_Posts_PostListing : CMSAdminEditControl
  22. {
  23. #region "Variables & enums"
  24. private int mPostId = 0;
  25. private int mForumId = 0;
  26. private int mCommunityGroupID = 0;
  27. private ForumPostInfo mPostInfo = null;
  28. private CurrentUserInfo currentUserInfo = null;
  29. private SiteInfo currentSiteInfo = null;
  30. protected enum Action
  31. {
  32. Approve = 0,
  33. ApproveSubTree = 1,
  34. Reject = 2,
  35. RejectSubTree = 3,
  36. Delete = 4
  37. }
  38. protected enum What
  39. {
  40. Selected = 0,
  41. All = 1
  42. }
  43. #endregion
  44. #region "Properties"
  45. /// <summary>
  46. /// Gets or sets ID of the parent post, its child-posts are displayed.
  47. /// </summary>
  48. public int PostId
  49. {
  50. get
  51. {
  52. return mPostId;
  53. }
  54. set
  55. {
  56. mPostId = value;
  57. mPostInfo = null;
  58. }
  59. }
  60. /// <summary>
  61. /// Gets or sets forums id. Is is used when postid is 0 = forum thread should be displayed.
  62. /// </summary>
  63. public int ForumId
  64. {
  65. get
  66. {
  67. return mForumId;
  68. }
  69. set
  70. {
  71. mForumId = value;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets community group ID.
  76. /// </summary>
  77. private int CommunityGroupID
  78. {
  79. get
  80. {
  81. // Community group id is not set
  82. if ((mCommunityGroupID == 0) && (ForumId != 0))
  83. {
  84. ForumInfo fi = ForumInfoProvider.GetForumInfo(ForumId);
  85. if (fi != null)
  86. {
  87. ForumGroupInfo fgi = ForumGroupInfoProvider.GetForumGroupInfo(fi.ForumGroupID);
  88. if (fgi != null)
  89. {
  90. mCommunityGroupID = fgi.GroupGroupID;
  91. }
  92. }
  93. }
  94. return mCommunityGroupID;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets or sets post info.
  99. /// </summary>
  100. public ForumPostInfo PostInfo
  101. {
  102. get
  103. {
  104. if ((mPostInfo == null) && (this.PostId > 0))
  105. {
  106. mPostInfo = ForumPostInfoProvider.GetForumPostInfo(this.PostId);
  107. // Update post id
  108. if (mPostInfo != null)
  109. {
  110. mPostId = mPostInfo.PostId;
  111. }
  112. }
  113. return mPostInfo;
  114. }
  115. set
  116. {
  117. mPostInfo = value;
  118. // Update post id
  119. if (mPostInfo != null)
  120. {
  121. mPostId = mPostInfo.PostId;
  122. }
  123. }
  124. }
  125. #endregion
  126. #region "Methods"
  127. protected void Page_Load(object sender, EventArgs e)
  128. {
  129. bool reloadTree = QueryHelper.GetBoolean("reloadtree", true);
  130. ScriptHelper.RegisterClientScriptBlock(this.Page, typeof(string), "PostListingScripts",
  131. ScriptHelper.GetScript(
  132. "function ViewPost(postId) { \n" +
  133. " location.href = 'ForumPost_View.aspx?postid=' + postId+'&listingpost=" + this.PostId + ";" + this.ForumId + "'; \n" +
  134. "} \n" +
  135. "function SelectPost(postId, forumId) { \n" +
  136. " location.href = 'ForumPost_Listing.aspx?postid=' + postId + ';' + forumId; \n" +
  137. "} \n" +
  138. "function SelectInTree(postId, force) { \n" +
  139. " var treeFrame = parent.frames['posts_tree']; \n" +
  140. " if (treeFrame != null) { \n" +
  141. " // Refresh tree if necessary \n" +
  142. " if ((treeFrame.selectedPostId != postId) || force) { \n" +
  143. " treeFrame.RefreshTree(postId); \n" +
  144. " } \n" +
  145. " } \n" +
  146. "} \n" +
  147. "SelectInTree(" + this.PostId + ", false); \n"
  148. ));
  149. if (!RequestHelper.IsPostBack())
  150. {
  151. DataHelper.FillListControlWithEnum(typeof(Action), drpAction, "Forums.ListingActions.", null);
  152. drpAction.Items.Insert(0, new ListItem(GetString("general.SelectAction"), "-1"));
  153. //DataHelper.FillListControlWithEnum(typeof(What), drpWhat, "Forums.ListingWhat.", null);
  154. }
  155. string where = (this.PostId > 0) ? "PostParentID = " + this.PostId : "PostParentID IS NULL AND PostForumID=" + this.ForumId;
  156. gridPosts.WhereCondition = where;
  157. gridPosts.ZeroRowsText = GetString("forums.listing.nochildposts");
  158. gridPosts.FilteredZeroRowsText = GetString("forums.listing.nochildpostssearch");
  159. //gridPosts.OnDataReload += gridPosts_OnDataReload;
  160. gridPosts.OnExternalDataBound += gridPosts_OnExternalDataBound;
  161. gridPosts.OnAction += gridPosts_OnAction;
  162. btnOk.Click += btnOk_Click;
  163. currentUserInfo = CMSContext.CurrentUser;
  164. currentSiteInfo = CMSContext.CurrentSite;
  165. }
  166. /// <summary>
  167. /// OnPreRender.
  168. /// </summary>
  169. protected override void OnPreRender(EventArgs e)
  170. {
  171. // Hide action panel if no data
  172. pnlFooter.Visible = !DataHelper.DataSourceIsEmpty(gridPosts.GridView.DataSource);
  173. }
  174. #endregion
  175. #region "Grid events"
  176. /// <summary>
  177. /// External data binding handler.
  178. /// </summary>
  179. protected object gridPosts_OnExternalDataBound(object sender, string sourceName, object parameter)
  180. {
  181. sourceName = sourceName.ToLower();
  182. switch (sourceName)
  183. {
  184. case "subposts":
  185. DataRowView data = ((GridViewRow)parameter).DataItem as DataRowView;
  186. int subposts = ValidationHelper.GetInteger(data["PostThreadPostsAbsolute"], 0);
  187. int postid = ValidationHelper.GetInteger(data["PostId"], 0);
  188. int postForumId = ValidationHelper.GetInteger(data["PostForumId"], 0);
  189. int postLevel = ValidationHelper.GetInteger(data["PostLevel"], 0);
  190. ImageButton btnSubPosts = sender as ImageButton;
  191. // Hide button if post has no child, thread counts itself = 1 means no subposts
  192. if ((subposts == 0) || ((subposts == 1) && (postLevel == 0)))
  193. {
  194. btnSubPosts.Visible = false;
  195. }
  196. else
  197. {
  198. btnSubPosts.ImageUrl = GetImageUrl("/CMSModules/CMS_Forums/subposts.png");
  199. btnSubPosts.OnClientClick = "SelectPost(" + postid + "," + postForumId + ");return false;";
  200. }
  201. break;
  202. case "postapproved":
  203. return UniGridFunctions.ColoredSpanYesNo(parameter);
  204. }
  205. return parameter;
  206. }
  207. protected void gridPosts_OnAction(string actionName, object actionArgument)
  208. {
  209. int postId = ValidationHelper.GetInteger(actionArgument, 0);
  210. if (actionName.ToLower() == "delete")
  211. {
  212. if (CommunityGroupID == 0)
  213. {
  214. // Check forums modify permissions
  215. if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.forums", CMSAdminControl.PERMISSION_MODIFY))
  216. {
  217. RedirectToAccessDenied("cms.forums", CMSAdminControl.PERMISSION_MODIFY);
  218. }
  219. }
  220. else
  221. {
  222. // Check group permissions
  223. CMSGroupPage.CheckGroupPermissions(CommunityGroupID, CMSAdminControl.PERMISSION_MANAGE);
  224. }
  225. ForumPostInfoProvider.DeleteForumPostInfo(postId);
  226. }
  227. }
  228. #endregion
  229. #region "Multiple action handling"
  230. protected void btnOk_Click(object sender, EventArgs e)
  231. {
  232. if (CommunityGroupID == 0)
  233. {
  234. // Check forums modify permissions
  235. if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.forums", CMSAdminControl.PERMISSION_MODIFY))
  236. {
  237. RedirectToAccessDenied("cms.forums", CMSAdminControl.PERMISSION_MODIFY);
  238. }
  239. }
  240. else
  241. {
  242. // Check group permissions
  243. CMSGroupPage.CheckGroupPermissions(CommunityGroupID, CMSAdminControl.PERMISSION_MANAGE);
  244. }
  245. Action action = (Action)ValidationHelper.GetInteger(drpAction.SelectedValue, 0);
  246. What what = What.Selected; //(What)ValidationHelper.GetInteger(drpWhat.SelectedValue, 0);
  247. List<int> items = new List<int>();
  248. switch (what)
  249. {
  250. // Only selected posts
  251. case What.Selected:
  252. foreach (object item in gridPosts.SelectedItems)
  253. {
  254. items.Add(ValidationHelper.GetInteger(item, 0));
  255. }
  256. break;
  257. // On posts in unigrid
  258. case What.All:
  259. DataSet ds = gridPosts.GridView.DataSource as DataSet;
  260. if (!DataHelper.DataSourceIsEmpty(ds))
  261. {
  262. foreach (DataRow dr in ds.Tables[0].Rows)
  263. {
  264. int postId = ValidationHelper.GetInteger(dr["PostId"], 0);
  265. items.Add(postId);
  266. }
  267. }
  268. break;
  269. }
  270. // For all specified forum posts
  271. foreach (int postId in items)
  272. {
  273. ForumPostInfo fpi = ForumPostInfoProvider.GetForumPostInfo(postId);
  274. if (fpi != null)
  275. {
  276. switch (action)
  277. {
  278. // Approve post
  279. case Action.Approve:
  280. fpi.Approve();
  281. break;
  282. // Approve subtree
  283. case Action.ApproveSubTree:
  284. fpi.Approve(CMSContext.CurrentUser.UserID, true);
  285. break;
  286. // Reject post
  287. case Action.Reject:
  288. fpi.Reject();
  289. break;
  290. // Reject subtree
  291. case Action.RejectSubTree:
  292. fpi.Reject(true);
  293. break;
  294. // Delete post
  295. case Action.Delete:
  296. ForumPostInfoProvider.DeleteForumPostInfo(fpi);
  297. break;
  298. }
  299. }
  300. }
  301. // If something happened
  302. if (items.Count > 0)
  303. {
  304. // Get rid of selection
  305. gridPosts.ResetSelection();
  306. // Reload unigrid to see changes
  307. gridPosts.ReloadData();
  308. // Force refresh post tree
  309. ScriptHelper.RegisterClientScriptBlock(this.Page, typeof(string), "RefreshPostTree", ScriptHelper.GetScript("SelectInTree(" + this.PostId + ", true);"));
  310. }
  311. }
  312. #endregion
  313. }