PageRenderTime 30ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSModules/Friends/Controls/FriendsToApprovalList.ascx.cs

https://bitbucket.org/kudutest/kenticogit
C# | 355 lines | 267 code | 49 blank | 39 comment | 26 complexity | e81881a6c31f881c99b757ae0dad8ce5 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using CMS.CMSHelper;
  7. using CMS.Community;
  8. using CMS.GlobalHelper;
  9. using CMS.SiteProvider;
  10. using CMS.UIControls;
  11. using CMS.SettingsProvider;
  12. using TimeZoneInfo = CMS.SiteProvider.TimeZoneInfo;
  13. public partial class CMSModules_Friends_Controls_FriendsToApprovalList : CMSAdminListControl
  14. {
  15. #region "Private variables"
  16. private bool mDisplayFilter = true;
  17. protected string dialogsUrl = "~/CMSModules/Friends/Dialogs/";
  18. private TimeZoneInfo usedTimeZone = null;
  19. private UserInfo currentUserInfo = null;
  20. private SiteInfo currentSiteInfo = null;
  21. private string mRejectDialogUrl = null;
  22. private string mApproveDialogUrl = null;
  23. #endregion
  24. #region "Public properties"
  25. /// <summary>
  26. /// Gets and sets User ID.
  27. /// </summary>
  28. public int UserID
  29. {
  30. get;
  31. set;
  32. }
  33. /// <summary>
  34. /// Gets and sets whether to show filter.
  35. /// </summary>
  36. public bool DisplayFilter
  37. {
  38. get
  39. {
  40. return mDisplayFilter;
  41. }
  42. set
  43. {
  44. mDisplayFilter = value;
  45. searchElem.Visible = value;
  46. }
  47. }
  48. /// <summary>
  49. /// Gets reject dialog url.
  50. /// </summary>
  51. protected string RejectDialogUrl
  52. {
  53. get
  54. {
  55. if (mRejectDialogUrl == null)
  56. {
  57. mRejectDialogUrl = CMSContext.ResolveDialogUrl(dialogsUrl + "Friends_Reject.aspx") + "?userid=" + UserID;
  58. }
  59. return mRejectDialogUrl;
  60. }
  61. }
  62. /// <summary>
  63. /// Gets approve dialog url.
  64. /// </summary>
  65. protected string ApproveDialogUrl
  66. {
  67. get
  68. {
  69. if (mApproveDialogUrl == null)
  70. {
  71. mApproveDialogUrl = CMSContext.ResolveDialogUrl(dialogsUrl + "Friends_Approve.aspx") + "?userid=" + UserID;
  72. }
  73. return mApproveDialogUrl;
  74. }
  75. }
  76. /// <summary>
  77. /// Zero rows text.
  78. /// </summary>
  79. public string ZeroRowsText
  80. {
  81. get
  82. {
  83. return gridElem.ZeroRowsText;
  84. }
  85. set
  86. {
  87. gridElem.ZeroRowsText = value;
  88. }
  89. }
  90. #endregion
  91. #region "Page events"
  92. protected void Page_Load(object sender, EventArgs e)
  93. {
  94. if (StopProcessing)
  95. {
  96. Visible = false;
  97. // Do not load data
  98. gridElem.StopProcessing = true;
  99. }
  100. else
  101. {
  102. RaiseOnCheckPermissions(PERMISSION_READ, this);
  103. Visible = true;
  104. if (IsLiveSite)
  105. {
  106. dialogsUrl = "~/CMSModules/Friends/CMSPages/";
  107. }
  108. // Register the dialog script
  109. ScriptHelper.RegisterDialogScript(this.Page);
  110. // Register the refreshing script
  111. string refreshScript = ScriptHelper.GetScript("function refreshList(){" + Page.ClientScript.GetPostBackEventReference(hdnRefresh, string.Empty) + "}");
  112. ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), "friendsListRefresh", refreshScript);
  113. // Setup grid
  114. gridElem.OnAfterDataReload += new OnAfterDataReload(gridElem_OnAfterDataReload);
  115. gridElem.OnExternalDataBound += gridElem_OnExternalDataBound;
  116. gridElem.OrderBy = "UserName";
  117. gridElem.IsLiveSite = IsLiveSite;
  118. // Default where condition
  119. string defaultWhere = "(FriendStatus = " + Convert.ToInt32(FriendshipStatusEnum.Waiting) + ") AND (FriendUserID <> " + UserID + ")";
  120. // Add search where condition
  121. gridElem.WhereCondition = SqlHelperClass.AddWhereCondition(defaultWhere, searchElem.WhereCondition);
  122. // Add parameter @UserID
  123. if (gridElem.QueryParameters == null)
  124. {
  125. gridElem.QueryParameters = new QueryDataParameters();
  126. }
  127. gridElem.QueryParameters.Add("@UserID", UserID);
  128. // Register the dialog script
  129. ScriptHelper.RegisterDialogScript(this.Page);
  130. // Register reject script
  131. StringBuilder rejectScript = new StringBuilder();
  132. rejectScript.AppendLine("function FM_Reject_" + ClientID + "(item, gridId, url)");
  133. rejectScript.AppendLine("{");
  134. rejectScript.AppendLine(" var items = '';");
  135. rejectScript.AppendLine(" if(item == null)");
  136. rejectScript.AppendLine(" {");
  137. rejectScript.AppendLine(" items = document.getElementById(gridId).value;");
  138. rejectScript.AppendLine(" }");
  139. rejectScript.AppendLine(" else");
  140. rejectScript.AppendLine(" {");
  141. rejectScript.AppendLine(" items = item;");
  142. rejectScript.AppendLine(" }");
  143. rejectScript.AppendLine(" if((url != null) && (items != '') && (items != '|'))");
  144. rejectScript.AppendLine(" {");
  145. rejectScript.AppendLine(" modalDialog(url + '&ids=' + items, 'rejectDialog', 480, 350);");
  146. rejectScript.AppendLine(" }");
  147. rejectScript.AppendLine(" else");
  148. rejectScript.AppendLine(" {");
  149. rejectScript.AppendLine(" FM_ShowLabel('" + lblInfo.ClientID + "');");
  150. rejectScript.AppendLine(" }");
  151. rejectScript.AppendLine(" return false;");
  152. rejectScript.AppendLine("}");
  153. ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "rejectScript_" + ClientID, ScriptHelper.GetScript(rejectScript.ToString()));
  154. // Register approve script
  155. StringBuilder approveScript = new StringBuilder();
  156. approveScript.AppendLine("function FM_Approve_" + ClientID + "(item, gridId, url)");
  157. approveScript.AppendLine("{");
  158. approveScript.AppendLine(" var items = '';");
  159. approveScript.AppendLine(" if(item == null)");
  160. approveScript.AppendLine(" {");
  161. approveScript.AppendLine(" items = document.getElementById(gridId).value;");
  162. approveScript.AppendLine(" }");
  163. approveScript.AppendLine(" else");
  164. approveScript.AppendLine(" {");
  165. approveScript.AppendLine(" items = item;");
  166. approveScript.AppendLine(" }");
  167. approveScript.AppendLine(" if((url != null) && (items != '') && (items != '|'))");
  168. approveScript.AppendLine(" {");
  169. approveScript.AppendLine(" modalDialog(url + '&ids=' + items, 'approveDialog', 480, 350);");
  170. approveScript.AppendLine(" }");
  171. approveScript.AppendLine(" else");
  172. approveScript.AppendLine(" {");
  173. approveScript.AppendLine(" FM_ShowLabel('" + lblInfo.ClientID + "');");
  174. approveScript.AppendLine(" }");
  175. approveScript.AppendLine(" return false;");
  176. approveScript.AppendLine("}");
  177. ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "approveScript_" + ClientID, ScriptHelper.GetScript(approveScript.ToString()));
  178. // Add actions to buttons
  179. btnApproveSelected.OnClientClick = "return FM_Approve_" + ClientID + "(null,'" + gridElem.SelectionHiddenField.ClientID + "','" + ApproveDialogUrl + "');";
  180. btnRejectSelected.OnClientClick = "return FM_Reject_" + ClientID + "(null,'" + gridElem.SelectionHiddenField.ClientID + "','" + RejectDialogUrl + "');";
  181. // Hide label
  182. lblInfo.Attributes["style"] = "display: none;";
  183. // Register script for displaying label
  184. StringBuilder showLabelScript = new StringBuilder();
  185. showLabelScript.AppendLine("function FM_ShowLabel(labelId)");
  186. showLabelScript.AppendLine("{");
  187. showLabelScript.AppendLine(" var label = document.getElementById(labelId);");
  188. showLabelScript.AppendLine(" if (label != null)");
  189. showLabelScript.AppendLine(" {");
  190. showLabelScript.AppendLine(" label.innerHTML = '" + GetString("friends.selectfriends") + "';");
  191. showLabelScript.AppendLine(" label.style['display'] = 'block';");
  192. showLabelScript.AppendLine(" }");
  193. showLabelScript.AppendLine("}");
  194. ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), "showLabel", ScriptHelper.GetScript(showLabelScript.ToString()));
  195. }
  196. }
  197. protected override void OnPreRender(EventArgs e)
  198. {
  199. if (!DisplayFilter)
  200. {
  201. plcNoData.Visible = HasData();
  202. }
  203. base.OnPreRender(e);
  204. // Reset grid selection after multiple action
  205. if (RequestHelper.IsPostBack())
  206. {
  207. string invokerName = Page.Request.Params.Get("__EVENTTARGET");
  208. Control invokeControl = !string.IsNullOrEmpty(invokerName) ? Page.FindControl(invokerName) : null;
  209. if (invokeControl == hdnRefresh)
  210. {
  211. gridElem.ResetSelection();
  212. }
  213. }
  214. }
  215. #endregion
  216. #region "Grid events"
  217. protected object gridElem_OnExternalDataBound(object sender, string sourceName, object parameter)
  218. {
  219. DataRowView drv = null;
  220. switch (sourceName)
  221. {
  222. case "reject":
  223. if (sender is ImageButton)
  224. {
  225. // Get action button
  226. ImageButton rejectBtn = (ImageButton)sender;
  227. // Get full row view
  228. drv = UniGridFunctions.GetDataRowView((DataControlFieldCell)rejectBtn.Parent);
  229. // Add custom reject action
  230. rejectBtn.Attributes["onclick"] = "return FM_Reject_" + ClientID + "('" + drv["FriendID"] + "',null,'" + RejectDialogUrl + "');";
  231. return rejectBtn;
  232. }
  233. else
  234. {
  235. return string.Empty;
  236. }
  237. case "approve":
  238. if (sender is ImageButton)
  239. {
  240. // Get action button
  241. ImageButton approveBtn = (ImageButton)sender;
  242. // Get full row view
  243. drv = UniGridFunctions.GetDataRowView((DataControlFieldCell)approveBtn.Parent);
  244. // Add custom reject action
  245. approveBtn.Attributes["onclick"] = "return FM_Approve_" + ClientID + "('" + drv["FriendID"] + "',null,'" + ApproveDialogUrl + "');";
  246. return approveBtn;
  247. }
  248. else
  249. {
  250. return string.Empty;
  251. }
  252. case "friendrequestedwhen":
  253. if (currentUserInfo == null)
  254. {
  255. currentUserInfo = CMSContext.CurrentUser;
  256. }
  257. if (currentSiteInfo == null)
  258. {
  259. currentSiteInfo = CMSContext.CurrentSite;
  260. }
  261. DateTime currentDateTime = ValidationHelper.GetDateTime(parameter, DateTimeHelper.ZERO_TIME);
  262. if (IsLiveSite)
  263. {
  264. return CMSContext.ConvertDateTime(currentDateTime, this);
  265. }
  266. else
  267. {
  268. return TimeZoneHelper.GetCurrentTimeZoneDateTimeString(currentDateTime, currentUserInfo, currentSiteInfo, out usedTimeZone);
  269. }
  270. case "formattedusername":
  271. return HTMLHelper.HTMLEncode(Functions.GetFormattedUserName(Convert.ToString(parameter), this.IsLiveSite));
  272. }
  273. return parameter;
  274. }
  275. protected void gridElem_OnAfterDataReload()
  276. {
  277. // Hide filter when not needed
  278. if (searchElem.FilterIsSet)
  279. {
  280. DisplayFilter = true;
  281. }
  282. else if (!DataHelper.DataSourceIsEmpty(gridElem.GridView.DataSource))
  283. {
  284. DataSet friendships = gridElem.GridView.DataSource as DataSet;
  285. if (friendships != null)
  286. {
  287. int rowsCount = friendships.Tables[0].Rows.Count;
  288. DisplayFilter = (gridElem.FilterLimit <= 0) || (rowsCount > gridElem.FilterLimit);
  289. }
  290. }
  291. else
  292. {
  293. DisplayFilter = false;
  294. }
  295. }
  296. #endregion
  297. #region "Methods"
  298. /// <summary>
  299. /// Indicates if there are some data.
  300. /// </summary>
  301. public bool HasData()
  302. {
  303. return (!DataHelper.DataSourceIsEmpty(gridElem.GridView.DataSource));
  304. }
  305. #endregion
  306. }