ConfigEditor.cs

// Copyright � 2007 Jeffrey Bazinet, http://www.vwd-cms.com/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI; 
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Collections;
using System.Web.RegularExpressions;
using System.Text.RegularExpressions;
using System.Xml;
using VwdCms.Configuration;
using System.Drawing;
using System.Collections.Specialized;

namespace VwdCms.Admin
{
    public class ConfigEditor : Panel, INamingContainer, IPostBackDataHandler
    {
        //public event CommandEventHandler Errors;
        public event CommandEventHandler CmsSettingsChanged;

        //protected System.Web.UI.HtmlControls.HtmlInputHidden hdnScrollPos;
        protected System.Web.UI.WebControls.Table tblMain;
        protected System.Web.UI.WebControls.CheckBox chkSlowConn;
        protected System.Web.UI.WebControls.DropDownList ddlFontFamily;
        protected System.Web.UI.WebControls.DropDownList ddlFontSize;
        protected System.Web.UI.WebControls.CheckBox chkLineWrap;
        protected System.Web.UI.WebControls.CheckBox chkShowHelp;
        protected System.Web.UI.WebControls.CheckBox chkShowUnsavedChanges;
        protected System.Web.UI.WebControls.Button btnSave;

        private bool _loaded = false;

        protected override void OnPreRender(EventArgs e)
        {
            this.BuildCompositeControls();
            this.Page.RegisterRequiresPostBack(this);
            base.OnPreRender(e);
        }

        private CmsConfig CmsConfig
        {
            get    {return VwdCms.Configuration.CmsConfig.Current;}
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            CmsSettings settings = this.CmsConfig.CmsSettings;
            settings.SlowConnection = this.chkSlowConn.Checked;
            settings.EditorFontFamily = this.ddlFontFamily.SelectedItem.Text;
            settings.EditorFontSize = this.ddlFontSize.SelectedItem.Text;
            settings.EditorLineWrap = this.chkLineWrap.Checked;
            settings.ShowHelpAtStartup = this.chkShowHelp.Checked;
            settings.ShowUnsavedChangesMessage = this.chkShowUnsavedChanges.Checked;

            this.CmsConfig.Save();

            if ( this.CmsSettingsChanged !=  null )
            {
                this.CmsSettingsChanged(this, new CommandEventArgs("CmsSettingsChanged", settings));
            }
        }


        private void  CreateTableCell(ref TableCell tc, string ID, string cssClass, string text)
        {
            tc = new TableCell();
            if (!string.IsNullOrEmpty(ID))
            {
                tc.ID = ID;
            }
            if (!string.IsNullOrEmpty(cssClass))
            {
                tc.CssClass = cssClass;
            }
            if (text != null)
            {
                tc.Text = text;
            }
            tc.Height = new Unit("40px");
            tc.VerticalAlign = VerticalAlign.Middle;
            tc.Style.Add("border-bottom", "solid 1px silver");
        }

        private void CreateTableCell(ref TableCell tc, string ID, string cssClass, Control child)
        {
            tc = new TableCell();
            if (!string.IsNullOrEmpty(ID))
            {
                tc.ID = ID;
            }
            if (!string.IsNullOrEmpty(cssClass))
            {
                tc.CssClass = cssClass;
            }
            if (child != null)
            {
                tc.Controls.Add(child);
            }
            tc.Height = new Unit("40px");
            tc.VerticalAlign = VerticalAlign.Middle;
            tc.Style.Add("border-bottom", "solid 1px silver");

        }

        private TableRow AddTableRow(Table table, params TableCell[] cells)
        {
            TableRow tr = null;

            tr = new TableRow();

            foreach (TableCell tc in cells)
            {
                tr.Cells.Add(tc);
            }

            table.Rows.Add(tr);
            return tr;
        }

            
        private void CreateLinkButton(ref LinkButton lb, string ID, string cssClass,
            string title, string imageUrl, string imageCssClass, string text)
        {
            lb = new LinkButton();
            
            if (!string.IsNullOrEmpty(ID))
            {
                lb.ID = ID;
            }
            if (!string.IsNullOrEmpty(cssClass))
            {
                lb.CssClass = cssClass;
            }
            if (title != null)
            {
                lb.ToolTip = title;
            }

            if (this.CmsConfig.CmsSettings.SlowConnection)
            {
                if (text != null)
                {
                    lb.Text = text;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(imageUrl))
                {
                    HtmlImage img = new HtmlImage();
                    img.Src = imageUrl;
                    if (!string.IsNullOrEmpty(imageCssClass))
                    {
                        img.Attributes.Add("class", imageCssClass);
                    }
                    lb.Controls.Add(img);
                }
            }
        }

        private void BuildCompositeControls()
        {
            if (!_loaded)
            {
                _loaded = true;

                this.Controls.Clear();
                this.Style.Add("padding", "10px");

                ListItem itm = null;
                TableCell tcLeft = null;
                TableCell tcRight = null;

                // cms settings 
                CmsConfig cfg = CmsConfig.Current;
                CmsSettings settings = cfg.CmsSettings;

                // create tbMain which will hold all of the controls
                this.tblMain = new Table();
                this.tblMain.ID = "tblMain";
                this.tblMain.BorderStyle = BorderStyle.None;
                this.tblMain.GridLines = GridLines.None;
                this.tblMain.Font.Name = "Verdana";
                this.tblMain.Font.Size = new FontUnit("10pt");
                this.tblMain.ForeColor = Color.SteelBlue;
                this.tblMain.CellSpacing = 0;
                this.tblMain.CellPadding = 5;
                this.Controls.Add(this.tblMain);

                //*********************************************
                // header row
                CreateTableCell(ref tcLeft, null, null, "VWD-CMS Admin Configuration");
                tcLeft.Font.Bold = true;
                tcLeft.Font.Size = new FontUnit("12pt");
                tcLeft.ForeColor = Color.DarkGreen;
                tcLeft.ColumnSpan = 2;
                // add the row to the table
                AddTableRow(this.tblMain, tcLeft);

                //*********************************************
                // slow connection label
                CreateTableCell(ref tcLeft, null, null, "Text Only / No Images<br/><span style=\"font-weight:normal;\">(Improved performance for slow connections)</span>");
                tcLeft.Font.Bold = true;

                // slow connection checkbox
                this.chkSlowConn = new CheckBox();
                this.chkSlowConn.ID = "chkSlowConn";
                this.chkSlowConn.Checked = settings.SlowConnection;
                //this.chkSlowConn.Text = "Slow Connection";
                this.chkSlowConn.AutoPostBack = false;
                CreateTableCell(ref tcRight, null, null, this.chkSlowConn);

                // add the row to the table
                AddTableRow(this.tblMain, tcLeft, tcRight);

                //*********************************************
                // font family label
                CreateTableCell(ref tcLeft, null, null, "Editor Font Face<br/><span style=\"font-weight:normal;\">(In TextEdit &amp; TextArea elements)</span>");
                tcLeft.Font.Bold = true;
                
                //create dropdownlist for font-family                
                this.ddlFontFamily = new DropDownList();
                this.ddlFontFamily.ID = "ddlFontFamily";
                this.ddlFontFamily.Items.Add("Arial");
                this.ddlFontFamily.Items.Add("Courier");
                this.ddlFontFamily.Items.Add("Helvetica");
                this.ddlFontFamily.Items.Add("Times");
                this.ddlFontFamily.Items.Add("Verdana");
                itm = this.ddlFontFamily.Items.FindByText(settings.EditorFontFamily);
                this.ddlFontFamily.SelectedIndex = this.ddlFontFamily.Items.IndexOf(itm);
                this.ddlFontFamily.AutoPostBack = false;
                CreateTableCell(ref tcRight, null, null, this.ddlFontFamily);

                // add the row to the table
                AddTableRow(this.tblMain, tcLeft, tcRight);

                //*********************************************
                // font size label
                CreateTableCell(ref tcLeft, null, null, "Editor Font Size<br/><span style=\"font-weight:normal;\">(In TextEdit &amp; TextArea elements)</span>");
                tcLeft.Font.Bold = true;

                //create dropdownlist for font-size
                this.ddlFontSize = new DropDownList();
                this.ddlFontSize.ID = "ddlFontSize";
                this.ddlFontSize.Items.Add("6pt");
                this.ddlFontSize.Items.Add("8pt");
                this.ddlFontSize.Items.Add("10pt");
                this.ddlFontSize.Items.Add("12pt");
                this.ddlFontSize.Items.Add("14pt");
                itm = this.ddlFontSize.Items.FindByText(settings.EditorFontSize);
                this.ddlFontSize.SelectedIndex = this.ddlFontSize.Items.IndexOf(itm);
                this.ddlFontSize.AutoPostBack = false;
                CreateTableCell(ref tcRight, null, null, this.ddlFontSize);

                // add the row to the table
                AddTableRow(this.tblMain, tcLeft, tcRight);

                //*********************************************
                // line wrap label
                CreateTableCell(ref tcLeft, null, null, "Editor Line Wrapping<br/><span style=\"font-weight:normal;\">(In TextArea elements)</span>");
                tcLeft.Font.Bold = true;

                //create checkbox for line wrap 
                this.chkLineWrap = new CheckBox();
                this.chkLineWrap.ID = "chkLineWrap";
                this.chkLineWrap.Checked = settings.EditorLineWrap;
                //this.chkLineWrap.Text = "Editor Line Wrap";
                this.chkLineWrap.AutoPostBack = false;
                CreateTableCell(ref tcRight, null, null, this.chkLineWrap);

                // add the row to the table
                AddTableRow(this.tblMain, tcLeft, tcRight);

                //*********************************************
                // show help label
                CreateTableCell(ref tcLeft, null, null, "Show Help at Startup<br/><span style=\"font-weight:normal;\">(Displays the Getting Started page when no file is selected)</span>");
                tcLeft.Font.Bold = true;

                //create checkbox for show help at startup 
                this.chkShowHelp = new CheckBox();
                this.chkShowHelp.ID = "chkShowHelp";
                this.chkShowHelp.Checked = settings.ShowHelpAtStartup;
                //this.chkShowHelp.Text = "Show Help at Startup";
                this.chkShowHelp.AutoPostBack = false;
                CreateTableCell(ref tcRight, null, null, this.chkShowHelp);

                // add the row to the table
                AddTableRow(this.tblMain, tcLeft, tcRight);

                //*********************************************
                // show unsaved changes message label
                CreateTableCell(ref tcLeft, null, null, "Warn about Unsaved Changes before Changing Views<br/><span style=\"font-weight:normal;\">(Asks you if you want to save your work before it is lost.)</span>");
                tcLeft.Font.Bold = true;

                //create checkbox for show unsaved changes msg 
                this.chkShowUnsavedChanges = new CheckBox();
                this.chkShowUnsavedChanges.ID = "chkShowUnsavedChanges";
                this.chkShowUnsavedChanges.Checked = settings.ShowUnsavedChangesMessage;
                //this.chkShowUnsavedChanges.Text = "Show Unsaved Changes Message";
                this.chkShowUnsavedChanges.AutoPostBack = false;
                CreateTableCell(ref tcRight, null, null, this.chkShowUnsavedChanges);

                // add the row to the table
                AddTableRow(this.tblMain, tcLeft, tcRight);

                //*********************************************
                // emtpy cell for save button row
                CreateTableCell(ref tcLeft, null, null, "&nbsp;");
                tcLeft.Style.Remove("border-bottom");

                //create save button 
                this.btnSave = new Button();
                this.btnSave.ID = "btnSave";
                this.btnSave.Text = "Save";
                this.btnSave.Click += new EventHandler(btnSave_Click);
                this.btnSave.Font.Name = "Verdana";
                this.btnSave.Font.Size = new FontUnit("10pt");
                this.btnSave.Font.Bold = true;
                this.btnSave.ForeColor = Color.SteelBlue;
                this.btnSave.Width = new Unit("65px");
                this.btnSave.Height = new Unit("25px");
                if (cfg.CurrentHost.DemoMode)
                {
                    this.btnSave.Enabled = false;
                    this.btnSave.ToolTip = "Cannot change CMS Settings in Demo Mode.";
                }
                CreateTableCell(ref tcRight, null, null, this.btnSave);
                tcRight.HorizontalAlign = HorizontalAlign.Right;
                tcRight.Style.Remove("border-bottom");

                // add the row to the table
                AddTableRow(this.tblMain, tcLeft, tcRight);

                //*********************************************

            }
        }

        public void ApplyCmsSettings()
        {
            BuildCompositeControls();
        }

        // PostBack handling
        protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            if (this.Visible)
            {
                BuildCompositeControls();
                //this.hdnScrollPos.Value = postCollection[this.hdnScrollPos.UniqueID];
            }
            return true;
        }

        // IPostBackDataHandler Members

        bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            return this.LoadPostData(postDataKey, postCollection);
        }

        void IPostBackDataHandler.RaisePostDataChangedEvent()
        {
            //not implemented
        }
    }
}