/CMSModules/Forums/Controls/ThreadMove.ascx.cs

https://bitbucket.org/chadjimylos/livepay · C# · 258 lines · 188 code · 42 blank · 28 comment · 39 complexity · c5a93910672093829a46c7a65c24ebb7 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.Forums;
  13. using CMS.GlobalHelper;
  14. using CMS.CMSHelper;
  15. public partial class CMSModules_Forums_Controls_ThreadMove : ForumViewer
  16. {
  17. #region "Public properties"
  18. /// <summary>
  19. /// Gets the current thread id if ForumCOntext.CurrentThread is not available e.g. in tree layout
  20. /// </summary>
  21. public int CurrentThread
  22. {
  23. get
  24. {
  25. return QueryHelper.GetInteger("moveto", this.SelectedThreadID);
  26. }
  27. }
  28. /// <summary>
  29. /// Gets or sets the thread id
  30. /// </summary>
  31. public int SelectedThreadID
  32. {
  33. get
  34. {
  35. return ValidationHelper.GetInteger(this.GetValue("SelectedThreadID"), 0);
  36. }
  37. set
  38. {
  39. this.SetValue("SelectedThreadID", value);
  40. }
  41. }
  42. /// <summary>
  43. /// Gets or sets the value that indicates whether controls is in administration mode
  44. /// </summary>
  45. public bool AdminMode
  46. {
  47. get
  48. {
  49. return ValidationHelper.GetBoolean(this.GetValue("AdminMode"), false);
  50. }
  51. set
  52. {
  53. this.SetValue("AdminMode", value);
  54. }
  55. }
  56. #endregion
  57. /// <summary>
  58. /// Occurs when thread is moved
  59. /// </summary>
  60. public event EventHandler TopicMoved;
  61. protected void Page_Load(object sender, EventArgs e)
  62. {
  63. if ((ForumContext.CurrentMode == ForumMode.TopicMove) || (this.AdminMode))
  64. {
  65. CopyValuesFromParent(this);
  66. this.btnMove.Click += new EventHandler(btnMove_Click);
  67. if (!RequestHelper.IsPostBack())
  68. {
  69. LoadMoveTopicDropdown();
  70. }
  71. else
  72. {
  73. // Display message if no forum exists
  74. if (drpMoveToForum.Items.Count == 0)
  75. {
  76. plcMoveInner.Visible = false;
  77. lblMoveInfo.Visible = true;
  78. lblMoveInfo.Text = ResHelper.GetString("Forums.NoForumToMoveIn");
  79. }
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Topic move action handler.
  85. /// </summary>
  86. protected void btnMove_Click(object sender, EventArgs e)
  87. {
  88. int forumId = ValidationHelper.GetInteger(this.drpMoveToForum.SelectedValue, 0);
  89. if (forumId > 0)
  90. {
  91. ForumPostInfo fpi = ForumContext.CurrentThread;
  92. if ((fpi == null) && (this.CurrentThread > 0))
  93. {
  94. fpi = ForumPostInfoProvider.GetForumPostInfo(this.CurrentThread);
  95. }
  96. if (fpi != null)
  97. {
  98. // Move the thread
  99. ForumPostInfoProvider.MoveThread(fpi, forumId);
  100. this.plcMoveInner.Visible = false;
  101. // Generate back button
  102. this.ltlMoveBack.Text = GetLink(null, ResHelper.GetString("general.back"), "ActionLink", ForumActionType.Thread);
  103. string targetForumName = this.drpMoveToForum.SelectedItem.Text.TrimStart(' ');
  104. ForumInfo fi = ForumInfoProvider.GetForumInfo(forumId);
  105. if (fi != null)
  106. {
  107. targetForumName = HTMLHelper.HTMLEncode(fi.ForumDisplayName);
  108. }
  109. // Display info
  110. this.lblMoveInfo.Text = String.Format(ResHelper.GetString("forum.thread.topicmoved"), targetForumName);
  111. this.lblMoveInfo.Visible = true;
  112. this.SetValue("TopicMoved", true);
  113. if (TopicMoved != null)
  114. {
  115. TopicMoved(this, null);
  116. }
  117. }
  118. }
  119. else
  120. {
  121. this.lblMoveError.Text = ResHelper.GetString("forum.thread.movetopic.selectforum");
  122. this.lblMoveError.Visible = true;
  123. }
  124. }
  125. /// <summary>
  126. /// Loads the forums dropdownlist for topic move.
  127. /// </summary>
  128. private void LoadMoveTopicDropdown()
  129. {
  130. if (this.drpMoveToForum.Items.Count > 0)
  131. {
  132. return;
  133. }
  134. lblMoveInfo.Visible = false;
  135. int currentForumId = 0;
  136. ForumPostInfo fpi = ForumContext.CurrentThread;
  137. if ((fpi == null) && (this.CurrentThread > 0))
  138. {
  139. fpi = ForumPostInfoProvider.GetForumPostInfo(this.CurrentThread);
  140. }
  141. if (fpi != null)
  142. {
  143. bool isOk = AdminMode ? true : ((ForumContext.CurrentForum != null) && (ForumContext.CurrentForum.ForumID == fpi.PostForumID));
  144. if (isOk)
  145. {
  146. currentForumId = fpi.PostForumID;
  147. ForumInfo fi = ForumInfoProvider.GetForumInfo(currentForumId);
  148. if (fi != null)
  149. {
  150. ForumGroupInfo fgi = ForumGroupInfoProvider.GetForumGroupInfo(fi.ForumGroupID);
  151. if (fgi != null)
  152. {
  153. string where = null;
  154. DataSet dsGroups = null;
  155. if (fgi.GroupGroupID > 0)
  156. {
  157. where = "GroupName NOT LIKE 'AdHoc%' AND GroupSiteID = " + CMSContext.CurrentSiteID + " AND GroupGroupID = " + fgi.GroupGroupID;
  158. }
  159. else
  160. {
  161. where = "GroupName NOT LIKE 'AdHoc%' AND GroupSiteID = " + CMSContext.CurrentSiteID + " AND GroupGroupID IS NULL";
  162. }
  163. dsGroups = ForumGroupInfoProvider.GetGroups(where, "GroupDisplayName", 0, "GroupID, GroupDisplayName");
  164. if (!DataHelper.DataSourceIsEmpty(dsGroups))
  165. {
  166. Hashtable forums = new Hashtable();
  167. // Get all forums for selected groups
  168. string groupWhere = "AND ForumGroupID IN (SELECT GroupID FROM Forums_FOrumGroup WHERE " + where + ") ";
  169. DataSet dsForums = ForumInfoProvider.GetForums(" ForumOpen = 1 " + " AND NOT ForumID = " + currentForumId + groupWhere, "ForumDisplayName", 0, "ForumID, ForumDisplayName, ForumGroupID");
  170. if (!DataHelper.DataSourceIsEmpty(dsForums))
  171. {
  172. // Load forums into hash table
  173. foreach (DataRow drForum in dsForums.Tables[0].Rows)
  174. {
  175. int groupId = Convert.ToInt32(drForum["ForumGroupID"]);
  176. List<string[]> forumNames = forums[groupId] as List<string[]>;
  177. if (forumNames == null)
  178. {
  179. forumNames = new List<string[]>();
  180. }
  181. forumNames.Add(new string[] { Convert.ToString(drForum["ForumDisplayName"]), Convert.ToString(drForum["ForumID"]) });
  182. forums[groupId] = forumNames;
  183. }
  184. }
  185. foreach (DataRow dr in dsGroups.Tables[0].Rows)
  186. {
  187. int groupId = Convert.ToInt32(dr["GroupId"]);
  188. List<string[]> forumNames = forums[groupId] as List<string[]>;
  189. if (forumNames != null)
  190. {
  191. // Add forum group item if some forum
  192. ListItem li = new ListItem(Convert.ToString(dr["GroupDisplayName"]), "0");
  193. li.Attributes.Add("disabled", "disabled");
  194. this.drpMoveToForum.Items.Add(li);
  195. // Add forum items
  196. foreach (string[] forum in forumNames)
  197. {
  198. // Add forum to DDL
  199. this.drpMoveToForum.Items.Add(new ListItem(" \xA0\xA0\xA0\xA0 " + forum[0], forum[1]));
  200. }
  201. }
  202. }
  203. // Display message if no forum exists
  204. if (drpMoveToForum.Items.Count == 0)
  205. {
  206. plcMoveInner.Visible = false;
  207. lblMoveInfo.Visible = true;
  208. lblMoveInfo.Text = ResHelper.GetString("Forums.NoForumToMoveIn");
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }