CmsHost.cs

// Copyright © 2007 Jeffrey Bazinet, http://www.vwd-cms.com/
using System;
using System.Configuration;
using System.Web;
using System.Web.Util;
using System.Web.Configuration;
using System.Collections.Generic;

namespace VwdCms.Configuration
{
    public class CmsHost 
    {
        private string _key = null;
        private string _domain = null;
        private string _applicationPath = null;
        private bool _useSSL = false;
        private string _path = string.Empty;
        private string _cmsMode = "normal"; // normal, demo
        private string _applicationId = null;
        private string _exceptionNotify = null;
        private string _demoActivityNotify = null;
        private bool _showAdminBanner = false;
        private string _loginUrl = null;
        private string _membersUrl = null;
        private bool _aspNetMembershipInstalled = false;
        private bool _persistent = false;
        private bool _analyticsOn = false;
        private string _analyticsId = string.Empty;

        public CmsConfig CmsConfig = null;
        private Dictionarystring, HostComponent _components = new Dictionarystring, HostComponent();

        public Dictionarystring, HostComponent Components
        {
            get { return _components; }
        }

        public CmsHost(CmsConfig cmsconfig, string key)
        {
            this.CmsConfig = cmsconfig;
            this.Key = key;
        }

        public CmsHost(CmsConfig cmsconfig, System.Xml.XmlNode node)
        {
            this.CmsConfig = cmsconfig;
            _persistent = true;

            this.Key = CmsConfig.GetChildNodeInnerText(node, "key", cmsconfig).ToLower();
            this.UseSSL = Convert.ToBoolean(CmsConfig.GetChildNodeInnerText(node, "usessl", cmsconfig));
            this.Path = CmsConfig.GetChildNodeInnerText(node, "path", cmsconfig);
            this.CmsMode = CmsConfig.GetChildNodeInnerText(node, "cmsmode", cmsconfig);
            this.ApplicationId = CmsConfig.GetChildNodeInnerText(node, "applicationid", cmsconfig);
            this.ExceptionNotify = CmsConfig.GetChildNodeInnerText(node, "exceptionnotify", cmsconfig);
            this.DemoActivityNotify = CmsConfig.GetChildNodeInnerText(node, "demoactivitynotify", cmsconfig);
            this.ShowAdminBanner = Convert.ToBoolean(CmsConfig.GetChildNodeInnerText(node, "showadminbanner", cmsconfig));
            this.LoginUrl = CmsConfig.GetChildNodeInnerText(node, "loginurl", cmsconfig);
            this.MembersUrl = CmsConfig.GetChildNodeInnerText(node, "membersurl", cmsconfig);
            this.AspNetMembershipInstalled = Convert.ToBoolean(CmsConfig.GetChildNodeInnerText(node, "aspnetmembershipinstalled", cmsconfig));
            this.AnalyticsOn = Convert.ToBoolean(CmsConfig.GetChildNodeInnerText(node, "analyticson", cmsconfig));
            this.AnalyticsId = CmsConfig.GetChildNodeInnerText(node, "analyticsid", cmsconfig);

            string name = "./";
            string key = null;

            //<components>
            this.Components.Clear();
            System.Xml.XmlNode colnode = node.SelectSingleNode(name + "components");
            HostComponent hc = null;
            if (colnode != null  colnode.ChildNodes.Count  0)
            {
                foreach (System.Xml.XmlNode n in colnode.ChildNodes)
                {
                    if (n != null  n.NodeType == System.Xml.XmlNodeType.Element)
                    {
                        hc = new HostComponent(this, n);
                        key = hc.Key;
                        if (this.Components.ContainsKey(key))
                        {
                            CmsConfig.DuplicateItem(cmsconfig, "HostComponent", key);
                        }
                        else
                        {
                            this.Components.Add(key, hc);
                        }
                    }
                }
            }
        }

        [NotPersisted]
        internal bool Persistent
        {
            get { return _persistent; }
        }

        public string Key
        {
            get { return _key; }
            set
            {
                _key = value;
                this.Domain = null;
                this.ApplicationPath = null;
                if (!string.IsNullOrEmpty(_key))
                {
                    _key = _key.ToLower();

                    int index = _key.IndexOf("/");
                    if ( index == -1 )
                    {
                        this.Domain = _key;
                        _key = _key + "/";
                        this.ApplicationPath = string.Empty;
                    }
                    else
                    {
                        this.Domain = _key.Substring(0, index);
                        this.ApplicationPath = _key.Substring(index);
                    }
                }
            }
        }

        [NotPersisted]
        public string Domain
        {
            get { return _domain; }
            set { _domain = value; }
        }

        [NotPersisted]
        public string ApplicationPath
        {
            get { return _applicationPath; }
            set 
            { 
                _applicationPath = value; 
            }
        }

        public bool UseSSL
        {
            get { return _useSSL; }
            set { _useSSL = value; }
        }

        public bool ShowAdminBanner
        {
            get { return _showAdminBanner; }
            set { _showAdminBanner = value; }
        }

        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        [NotPersisted]
        public string SitePath
        {
            get 
            {
                string path = this.ApplicationPath;
                if (!path.StartsWith("/"))
                {
                    path = "/" + path;
                }
                if (!path.EndsWith("/"))
                {
                    path += "/";
                }
                path += _path;

                if (!path.EndsWith("/"))
                {
                    path += "/";
                }
                
                return path;
            }
        }

        public string CmsMode
        {
            get { return _cmsMode; }
            set { _cmsMode = value; }
        }

        [NotPersisted]
        public bool DemoMode
        {
            get { return (this.CmsMode == "demo"); }
        }

        public string ApplicationId
        {
            get { return _applicationId; }
            set { _applicationId = value; }
        }

        public string ExceptionNotify
        {
            get { return _exceptionNotify; }
            set { _exceptionNotify = value; }
        }
        public string DemoActivityNotify
        {
            get { return _demoActivityNotify; }
            set { _demoActivityNotify = value; }
        }

        [NotUpdated]
        public string LoginUrl
        {
            get { return _loginUrl; }
            set 
            {
                if (string.IsNullOrEmpty(value))
                {
                    _loginUrl = VwdCms.Configuration.Utilities.GetSecureUrl(this);
                }
                else
                {
                    _loginUrl = value;
                    if (!_loginUrl.EndsWith("/"))
                    {
                        _loginUrl += "/";
                    }
                }
            }
        }
        [NotUpdated]
        public string MembersUrl
        {
            get { return _membersUrl; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    _membersUrl = VwdCms.Configuration.Utilities.GetSiteUrl(this) + "Members/";
                }
                else
                {
                    _membersUrl = value;
                    if (!_membersUrl.EndsWith("/"))
                    {
                        _membersUrl += "/";
                    }
                }
            }
        }

        public bool AspNetMembershipInstalled
        {
            get { return _aspNetMembershipInstalled; }
            set { _aspNetMembershipInstalled = value; }
        }

        public bool IsComponentInstalled(string componentKey)
        {
            bool installed = false;
            if (!string.IsNullOrEmpty(componentKey))
            {
                if (this.Components != null  this.Components.Count  0)
                {
                    if (this.Components.ContainsKey(componentKey.ToLower()))
                    {
                        HostComponent comp = this.Components[componentKey.ToLower()];
                        installed = (comp.Status.ToLower() == "installed");
                    }
                }
            }
            return installed;
        }

        public string AnalyticsId
        {
            get { return _analyticsId; }
            set { _analyticsId = value; }
        }

        public bool AnalyticsOn
        {
            get { return _analyticsOn; }
            set { _analyticsOn = value; }
        }

    }
}