PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSAdminControls/Calendar/Calendar.aspx.cs

https://bitbucket.org/kudutest/kenticogit
C# | 358 lines | 242 code | 59 blank | 57 comment | 27 complexity | 17756d3e73f31ee30446a4ea13c454a7 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using CMS.GlobalHelper;
  11. using CMS.CMSHelper;
  12. using CMS.UIControls;
  13. using CMS.SiteProvider;
  14. using TimeZoneInfo = CMS.SiteProvider.TimeZoneInfo;
  15. public partial class CMSAdminControls_Calendar_Calendar : CMSPage
  16. {
  17. #region "Variable"
  18. protected string pagetitle = null;
  19. protected string mCulture = "";
  20. protected bool editTime = false;
  21. protected string selDate = null;
  22. protected bool allowEmpty = false;
  23. protected string controlId = null;
  24. #endregion
  25. /// <summary>
  26. /// Constructor.
  27. /// </summary>
  28. public CMSAdminControls_Calendar_Calendar()
  29. {
  30. mCulture = Page.UICulture;
  31. // Get UI culture parameter from querystring
  32. string uiCulture = QueryHelper.GetText("UILang", "");
  33. if (uiCulture != "")
  34. {
  35. // If exists set this culture
  36. this.UICulture = uiCulture;
  37. mCulture = uiCulture;
  38. }
  39. else
  40. {
  41. if (CMSContext.CurrentDocument != null)
  42. {
  43. // If this parameter doesn't exist, set document culture
  44. mCulture = CMSContext.CurrentDocument.DocumentCulture;
  45. }
  46. else
  47. {
  48. // Or system UI culture
  49. mCulture = System.Globalization.CultureInfo.CurrentUICulture.IetfLanguageTag;
  50. }
  51. }
  52. // Set culture to current thread
  53. System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(mCulture);
  54. System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(mCulture);
  55. }
  56. protected void Page_Load(System.Object sender, System.EventArgs e)
  57. {
  58. // Get parameters
  59. selDate = QueryHelper.GetString("selDate", "");
  60. allowEmpty = QueryHelper.GetBoolean("allowempty", false);
  61. editTime = QueryHelper.GetBoolean("editTime", false);
  62. controlId = QueryHelper.GetText("controlid", "");
  63. SetBrowserClass();
  64. ScriptHelper.RegisterTooltip(this.Page);
  65. // Disable caching
  66. this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  67. pagetitle = GetString("Calendar.Title");
  68. // Fill in the dropdown values
  69. if (this.drpHours.Items.Count == 0)
  70. {
  71. FillNumbers(this.drpHours, 0, 23);
  72. }
  73. if (this.drpMinutes.Items.Count == 0)
  74. {
  75. FillNumbers(this.drpMinutes, 0, 59);
  76. }
  77. if (this.drpSeconds.Items.Count == 0)
  78. {
  79. FillNumbers(this.drpSeconds, 0, 59);
  80. }
  81. // Fill in the month selector
  82. if (this.drpMonth.Items.Count == 0)
  83. {
  84. FillMonths(this.drpMonth, mCulture);
  85. }
  86. // Setup the buttons
  87. this.btnCancel.Text = GetString("general.cancel");
  88. this.btnCancel.Attributes.Add("onclick", "window.close(); return false;");
  89. if (editTime)
  90. {
  91. this.btnNow.Text = GetString("Calendar.Now");
  92. }
  93. else
  94. {
  95. this.btnNow.Text = GetString("Calendar.Today");
  96. }
  97. this.btnOk.Text = GetString("general.ok");
  98. this.btnNA.Text = GetString("general.na");
  99. this.btnNA.Attributes.Add("onclick", "CloseWindow(" + ScriptHelper.GetString(controlId) + ", ''); return false;");
  100. // Set content direction according to current culture (LTR/RTL)
  101. if (CultureHelper.IsCultureRTL(mCulture))
  102. {
  103. tblLayout.Attributes.Remove("dir");
  104. tblLayout.Attributes.Add("dir", "rtl");
  105. }
  106. else
  107. {
  108. tblLayout.Attributes.Remove("dir");
  109. tblLayout.Attributes.Add("dir", "ltr");
  110. }
  111. if (!RequestHelper.IsPostBack())
  112. {
  113. DateTime now = TimeZoneHelper.GetUserDateTime(CMSContext.CurrentUser);
  114. // Selected date
  115. if (selDate == "")
  116. {
  117. SetTime(now, true);
  118. }
  119. else
  120. {
  121. SetTime(ValidationHelper.GetDateTime(selDate, now), true);
  122. }
  123. // Allow empty ?
  124. if (!allowEmpty)
  125. {
  126. btnNA.Visible = false;
  127. }
  128. else
  129. {
  130. btnNA.Visible = true;
  131. }
  132. // Edit time ?
  133. if (editTime)
  134. {
  135. pnlTime.Visible = true;
  136. }
  137. else
  138. {
  139. pnlTime.Visible = false;
  140. }
  141. }
  142. // Display time zone in form 'GMT+1:00'
  143. int timeZoneId = QueryHelper.GetInteger("timezoneid", 0);
  144. if (timeZoneId > 0)
  145. {
  146. TimeZoneInfo tzi = TimeZoneInfoProvider.GetTimeZoneInfo(timeZoneId);
  147. if (tzi != null)
  148. {
  149. lblGMTShift.Visible = true;
  150. lblGMTShift.Text = TimeZoneHelper.GetGMTStringOffset(tzi);
  151. ScriptHelper.AppendTooltip(lblGMTShift, TimeZoneHelper.GetGMTLongStringOffset(tzi), "help");
  152. }
  153. }
  154. // Next and previous month images
  155. calDate.NextMonthText = "<img src=\"" + GetImageUrl("Design/Controls/Calendar/next.png") + "\" />";
  156. calDate.PrevMonthText = "<img src=\"" + GetImageUrl("Design/Controls/Calendar/previous.png") + "\" />";
  157. RegisterModalPageScripts();
  158. RegisterEscScript();
  159. }
  160. /// <summary>
  161. /// Fills the dropdownlist with the numbers.
  162. /// </summary>
  163. /// <param name="drp">Dropdownlist to fill</param>
  164. /// <param name="fromnumber">Starting number</param>
  165. /// <param name="tonumber">Edning number</param>
  166. private void FillNumbers(DropDownList drp, int fromNumber, int toNumber)
  167. {
  168. FillNumbers(drp, fromNumber, toNumber, fromNumber);
  169. }
  170. /// <summary>
  171. /// Fills the dropdownlist with the numbers. Text and value of dropdownlist are
  172. /// filled separately.
  173. /// </summary>
  174. /// <param name="drp">Dropdownlist to fill</param>
  175. /// <param name="fromnumber">Starting number (text)</param>
  176. /// <param name="tonumber">Ending number (text)</param>
  177. /// <param name="valFromNumber">Starting number (value)</param>
  178. private void FillNumbers(DropDownList drp, int fromnumber, int tonumber, int valFromNumber)
  179. {
  180. drp.Items.Clear();
  181. // Get length of number
  182. int max = tonumber.ToString().Length;
  183. for (int i = fromnumber; i <= tonumber; i++)
  184. {
  185. // Format string number with nulls
  186. string item = i.ToString().PadLeft(max, '0');
  187. // Add this number to dropdown list
  188. drp.Items.Add(new ListItem(item, valFromNumber.ToString()));
  189. valFromNumber++;
  190. }
  191. }
  192. /// <summary>
  193. /// Fills the dropdownlist with the months values in corect culture format.
  194. /// </summary>
  195. /// <param name="drp">Dropdownlist to fill</param>
  196. /// <param name="culture">Culture</param>
  197. private void FillMonths(DropDownList drp, string culture)
  198. {
  199. drp.Items.Clear();
  200. // Get DateTime informations
  201. DateTime dt = DateTimeHelper.GetYearStart(DateTime.Now);
  202. IFormatProvider ifp = new System.Globalization.CultureInfo(mCulture);
  203. string item = null;
  204. for (int i = 1; i <= 12; i++)
  205. {
  206. item = i.ToString();
  207. if (i < 10)
  208. {
  209. item = "0" + item;
  210. }
  211. // Fill dropdown list
  212. drp.Items.Add(new ListItem(item, dt.ToString("MM", ifp)));
  213. dt = dt.AddMonths(1);
  214. }
  215. }
  216. /// <summary>
  217. /// Sets the calendar time to the given time.
  218. /// </summary>
  219. /// <param name="time">Time to set</param>
  220. private void SetTime(DateTime time, bool selectCalendar)
  221. {
  222. if (selectCalendar)
  223. {
  224. calDate.VisibleDate = time.Date;
  225. calDate.SelectedDate = time.Date;
  226. }
  227. // Get culture specific year
  228. string cultureYear = time.ToString("yyyy", new System.Globalization.CultureInfo(mCulture));
  229. int cultureYearInt = ValidationHelper.GetInteger(cultureYear, time.Year);
  230. FillNumbers(this.drpYear, cultureYearInt - 5, cultureYearInt + 5, time.Year - 5);
  231. // Set the new time
  232. this.drpHours.SelectedValue = time.Hour.ToString();
  233. this.drpMinutes.SelectedValue = time.Minute.ToString();
  234. this.drpSeconds.SelectedValue = time.Second.ToString();
  235. this.drpYear.SelectedValue = time.Year.ToString();
  236. this.drpMonth.SelectedValue = time.ToString("MM", new System.Globalization.CultureInfo(mCulture));
  237. }
  238. protected void btnNow_Click(System.Object sender, System.EventArgs e)
  239. {
  240. SetTime(TimeZoneHelper.GetUserDateTime(CMSContext.CurrentUser), true);
  241. //this.ltlScript.Text = ScriptHelper.GetScript("CloseWindow('" + Request.QueryString["controlid"] + "', '" + DateTime.Now.ToString() + "');");
  242. }
  243. /// <summary>
  244. /// Gets selected time from calendar
  245. /// </summary>
  246. /// <returns></returns>
  247. protected DateTime GetCurrentTime()
  248. {
  249. DateTime selTime = DateTime.MinValue;
  250. selTime = calDate.SelectedDate.Date;
  251. selTime = selTime.AddHours(drpHours.SelectedIndex);
  252. selTime = selTime.AddMinutes(drpMinutes.SelectedIndex);
  253. selTime = selTime.AddSeconds(drpSeconds.SelectedIndex);
  254. return selTime;
  255. }
  256. protected void btnOK_Click(System.Object sender, System.EventArgs e)
  257. {
  258. DateTime selTime = GetCurrentTime();
  259. // Add 'Close window' script
  260. if (editTime)
  261. {
  262. this.ltlScript.Text = ScriptHelper.GetScript("CloseWindow(" + ScriptHelper.GetString(controlId) + ", '" + selTime.ToString("d", new System.Globalization.CultureInfo(mCulture)) + selTime.ToString(" HH:mm:ss") + "');");
  263. }
  264. else
  265. {
  266. this.ltlScript.Text = ScriptHelper.GetScript("CloseWindow(" + ScriptHelper.GetString(controlId) + ", '" + selTime.ToString("d", new System.Globalization.CultureInfo(mCulture)) + "');");
  267. }
  268. }
  269. protected void calDate_SelectionChanged(object sender, EventArgs e)
  270. {
  271. SetTime(GetCurrentTime(), false);
  272. }
  273. protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
  274. {
  275. try
  276. {
  277. // Change DateTime to the new value
  278. DateTime curTime = GetCurrentTime();
  279. DateTime newTime = new DateTime(Convert.ToInt32(this.drpYear.SelectedValue), curTime.Month, curTime.Day, curTime.Hour, curTime.Minute, curTime.Second);
  280. SetTime(newTime, true);
  281. }
  282. catch
  283. {
  284. }
  285. }
  286. protected void drpMonth_SelectedIndexChanged(object sender, EventArgs e)
  287. {
  288. try
  289. {
  290. // Change DateTime to the new value
  291. DateTime curTime = GetCurrentTime();
  292. DateTime newTime = new DateTime(curTime.Year, Convert.ToInt32(this.drpMonth.SelectedValue), curTime.Day, curTime.Hour, curTime.Minute, curTime.Second);
  293. SetTime(newTime, true);
  294. }
  295. catch
  296. {
  297. }
  298. }
  299. }