using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Collections; using System.Collections.Specialized; using System.Globalization; using ClientDependency.Core; using umbraco.IO; namespace umbraco.controls { /// /// Summary description for datePicker. /// [DefaultProperty("Text"), ToolboxData("<{0}:datePicker runat=server>")] [ClientDependency(200, ClientDependencyType.Javascript, "datepicker/cal_s.js", "UmbracoClient")] [ClientDependency(201, ClientDependencyType.Javascript, "datepicker/cal_set_s.js", "UmbracoClient")] [ClientDependency(202, ClientDependencyType.Javascript, "datepicker/lang/calendar-en.js", "UmbracoClient")] [ClientDependency(ClientDependencyType.Css, "datepicker/aqua/theme.css", "UmbracoClient")] public class datePicker : TextBox { private DateTime _datetime = DateTime.Now; private bool _showTime = false; private bool _emptyDateAsDefault = false; private int _yearsBack = 100; private string[] _minutes = { "00", "15", "30", "45" }; private string[] _hours = { "--", "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }; private string[] _days = { "--", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }; private ArrayList _months = new ArrayList(); private ArrayList _years = new ArrayList(); private string _globalAlias = "Da-dk"; private string dateFormatJs = "%Y-%m-%d"; private string dateFormatNet = "yyyy-MM-dd"; private string dateFormatJsSelect = ""; private string dateTimeVisibleValue = ""; public string CustomMinutes { set { _minutes = value.Split(", ".ToCharArray()); } } public bool EmptyDateAsDefault { set { _emptyDateAsDefault = value; } } public bool ShowTime { set { _showTime = value; } get { return _showTime; } } public string GlobalizationAlias { set { _globalAlias = value; } get { return _globalAlias; } } public int YearsBack { set { _yearsBack = value; } get { return _yearsBack; } } [Bindable(true), Category("Appearance"), DefaultValue("")] public DateTime DateTime { get { return _datetime; } set { try { _datetime = value; this.Text = _datetime.ToString(); } catch { } } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (Page.IsPostBack) { try { string dateVal = this.Text; DateTime tempDate; tempDate = DateTime.Parse(dateVal); if (_showTime) { tempDate = new DateTime( int.Parse(dateVal.Substring(0, 4)), int.Parse(dateVal.Substring(5, 2)), int.Parse(dateVal.Substring(8, 2)), int.Parse(dateVal.Substring(11, 2)), int.Parse(dateVal.Substring(14, 2)), 0); } else tempDate = new DateTime( int.Parse(dateVal.Substring(0, 4)), int.Parse(dateVal.Substring(5, 2)), int.Parse(dateVal.Substring(8, 2)), 0, 0, 0); _datetime = tempDate; } catch { _datetime = new DateTime(1, 1, 1, 0, 0, 0); } } else { if (this.Text != "") _datetime = DateTime.Parse(this.Text); } } protected override void OnInit(EventArgs e) { if (this.ShowTime) { dateFormatJs += " %H:%M"; dateFormatNet += " HH:mm"; dateFormatJsSelect = ",\n" + " showsTime : true,\n" + " timeFormat : 24 \n"; } base.OnInit(e); } protected override void OnPreRender(EventArgs e) { if (!_emptyDateAsDefault) { if (_datetime.Year != 1 || _datetime.Month != 1 || _datetime.Day != 1) { this.Text = _datetime.ToString(dateFormatNet); dateTimeVisibleValue = _datetime.ToString(dateFormatNet); } else { this.Text = ""; dateTimeVisibleValue = ui.Text("noDate"); } } base.OnPreRender(e); } /// /// Render this control to the output parameter specified. /// /// The HTML writer to write out to /// protected override void Render(HtmlTextWriter output) { // DateTimeFormatInfo dtInfo = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat; base.Render(output); //output.WriteLine(""); output.WriteLine("" + dateTimeVisibleValue + " "); output.WriteLine(""); output.WriteLine("" + ui.Text("removeDate") + ""); string strSetup = " Calendar.setup({" + " inputField : \"" + this.ClientID + "\",\n" + " ifFormat : \"" + dateFormatJs + "\",\n" + " displayArea : \"" + this.ClientID + "_show_e\",\n" + " daFormat : \"" + dateFormatJs + "\",\n" + " button : \"" + this.ClientID + "_f_trigger_e\",\n" + " singleClick : true\n" + dateFormatJsSelect + " });"; try { if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack) ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Calendar.Setup_" + this.ClientID, strSetup, true); else Page.ClientScript.RegisterStartupScript(this.GetType(), "Calendar.Setup_" + this.ClientID, strSetup, true); } catch { Page.ClientScript.RegisterStartupScript(this.GetType(), "Calendar.Setup_" + this.ClientID, strSetup, true); } } private string markMinute(int minute) { int _currentDiff = 100; int _currentMinute = 0; System.Collections.ArrayList _localMinutes = new ArrayList(); foreach (string s in _minutes) { _localMinutes.Add(s); } _localMinutes.Add("60"); foreach (string s in _localMinutes) { if (s.Trim() != "") { if (_currentDiff > Math.Abs(int.Parse(s) - minute)) { _currentMinute = int.Parse(s); _currentDiff = Math.Abs(int.Parse(s) - minute); } } } if (_currentMinute == 60) return "00"; else { if (_currentMinute.ToString().Length == 1) return "0" + _currentMinute.ToString(); else return _currentMinute.ToString(); } } } }