PositionHolder.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » MvcHelper » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » CmsServices » MvcHelper » PositionHolder.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI.WebControls;

using Everest.CmsServices.Models;
using Everest.CmsServices.Extension.Module;

using Everest.Library.Reflection;
using Everest.Library.ExtensionMethod;
using Everest.Library.Web;

namespace Everest.CmsServices.MvcHelper{

    public class PositionHolder : UserControl
    {
        public CmsViewPage ViewPage
        {
            get
            {
                return (CmsViewPage)this.Page;
            }
        }

        private aspnet_Applications Cms_Application
        {
            get
            {
                return ViewPage.Cms_Application;
            }
        }

        private Cms_Page Cms_Page
        {
            get
            {
                return ViewPage.Cms_Page;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            LoadCmsControls();
        }

        private void LoadCmsControls()
        {
            #region IsDesignMode
            if (System.Web.HttpContext.Current.GetCmsContext().IsDesignMode)
            {
                // drop container
                Panel dropCon = new Panel();
                dropCon.ID = "rid" + Guid.NewGuid().ToString().Substring(0, 6);
                dropCon.CssClass = "x-mydrop-target";
                this.Controls.Add(dropCon);

                // position name
                dropCon.Controls.Add(new Literal() { Text = this.ID });

                // scripts output
                StringBuilder output = new StringBuilder();
                output.Append(Environment.NewLine);
                output.Append("<script type=\"text/javascript\">").Append(Environment.NewLine);
                output.Append("if(!window.VisualDesignerData)").Append(Environment.NewLine);
                output.Append("    window.VisualDesignerData = {};").Append(Environment.NewLine);
                output.Append("if(!window.VisualDesignerData.drops)").Append(Environment.NewLine);
                output.Append("    window.VisualDesignerData.drops = [];").Append(Environment.NewLine);
                output.Append("window.VisualDesignerData.drops.push");
                output.Append("({");
                output.AppendFormat("clientId:'{0}', ", dropCon.ClientID);
                output.AppendFormat("positionName:'{0}'", this.ID);
                output.Append("});");
                output.Append(Environment.NewLine);
                output.Append("</script>");
                this.Controls.Add(new Literal() { Text = output.ToString() });

                // ret
                return;
            }

            #endregion


            var contentTemplates = CachedData.GetContentTemplateInPage(Cms_Page.PageId, this.ID);

            int itemCount = 0;
            foreach (var item in contentTemplates)
            {
                itemCount++;

                switch (item.ComponentType)
                {
                    case ComponentType.ContentTemplate:
                        AddContentTemplate(item);
                        break;
                    case ComponentType.Module:
                        AddModule(item);
                        break;
                    case ComponentType.WebForm:
                        AddWebForm(item.ComponentValue);
                        break;
                    default:
                        break;
                }

            }
        }

        /// <summary>
        /// Gets or sets the empty hiddens.
        /// </summary>
        /// <value>The empty hiddens.</value>
        public string EmptyHidden { get; set; }

        /// <summary>
        /// Gets or sets the contents hiddens.
        /// </summary>
        /// <value>The contents hiddens.</value>
        public string ContentHidden { get; set; }

        #region Add WebForm Internal
        /// <summary>
        /// Adds the web form.
        /// </summary>
        /// <param name="formId">The form id.</param>
        private Control AddWebForm(string formUUID)
        {
            var webFormControl = new WebFormControlWrapper();
            webFormControl.FormUUID = formUUID;
            this.Controls.Add(webFormControl);
            return webFormControl;
        }
        #endregion

        #region Add ContentTemplate Internal
        /// <summary>
        /// Adds the content template.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="controlId">The control id.</param>
        /// <returns></returns>
        private Control AddContentTemplate(QueryedComponent item)
        {
            var contentTemplateUUID = item.ContentTemplateUUID;
            var localizeTemplate = CachedData.GetContentTemplate(contentTemplateUUID);
            localizeTemplate = CachedData.GetContentTemplate(Cms_Application.ApplicationName, localizeTemplate.OriginalUUID);
            var virtualPath = localizeTemplate.GetVirtualPath();
            Control control = this.LoadControl(virtualPath);
            control.ID = CmsPageRouteData.GetControlId(item.DynamicControlId);
            if (control is CmsUserControl && localizeTemplate != null)
            {
                ((CmsUserControl)control).ContentTemplate = localizeTemplate;
            }
            //set property values
            foreach (var p in item.ParameterValues)
            {
                control.GetType().SetPropertyValue(p.Name, control, ColumnDataTypeHelper.GetValue(p.DataType, p.Value));
            }

            //control.ID = System.IO.Path.GetFileNameWithoutExtension(virtualPath);
            this.Controls.Add(control);

            return control;
        }
        #endregion

        #region Add Module Internal
        /// <summary>
        /// Adds the module.
        /// </summary>
        /// <param name="component">The component.</param>
        private Control AddModule(QueryedComponent component)
        {
            var moduleName = component.ComponentValue;
            var moduleSettings = new ModuleSettingCollection(component.ParameterValues.Select(p => new ModuleSetting() { Key = p.Name, Value = p.Value }).ToArray());
            ModuleInfo moduleInfo = ModuleManager.GetModule(moduleName, Cms_Application.ApplicationName);
            moduleInfo.ModuleSettings = moduleSettings;
            return AddModuleInternal(moduleInfo, component.DynamicControlId);
        }

        /// <summary>
        /// Adds the module internal.
        /// </summary>
        /// <param name="moduleInfo">The module info.</param>
        /// <param name="controlId">The control id.</param>
        /// <returns></returns>
        private Control AddModuleInternal(ModuleInfo moduleInfo, string controlId)
        {
            ModuleWrapperControl wrapperControl = new ModuleWrapperControl();
            wrapperControl.ID = CmsPageRouteData.GetControlId(controlId);

            //prepare parent page context
            ParentPageContext parentPageContext = new ParentPageContext()
            {
                AjaxHelper = this.ViewPage.Ajax,
                HtmlHelper = this.ViewPage.Html,
                RouteTable = RouteTable.Routes,
                TempData = this.ViewPage.TempData,
                UrlHelper = this.ViewPage.Url,
                ViewContext = this.ViewPage.ViewContext,
                ViewData = this.ViewPage.ViewData,
                ViewPage = this.ViewPage
            };
            var pageRouteData = ViewPage.CmsContext.PageRouteData;
            string moduleUrl = pageRouteData.GetModuleUrl(wrapperControl.ID);
            if (StringExtensions.IsNullOrEmptyTrim(moduleUrl))
            {
                var entryUrl = moduleInfo.ModuleSettings.EntryUrl;
                moduleUrl = entryUrl;
            }
            if (StringExtensions.IsNullOrEmptyTrim(moduleUrl))
            {
                moduleUrl = "~/";
            }
            wrapperControl.Initialize(moduleInfo, parentPageContext, moduleUrl, pageRouteData);

            this.Controls.Add(wrapperControl);

            return wrapperControl;
            //   IncludeModuleTheme(wrapperControl.ModuleInfo);
            //   IncludeJavascriptFiles(wrapperControl.ModuleInfo);
        }

        private void IncludeJavascriptFiles(ModuleInfo moduleInfo)
        {
            var javascriptFiles = moduleInfo.GetJavascriptFiles();
            if (javascriptFiles != null && javascriptFiles.Count() > 0)
            {
                if (ViewPage.Cms_Application.IsRelease)
                {
                    ViewPage.JavascriptFiles.Add(ViewPage.Url.Action("GetModuleJavascript", "Script", new { Application = moduleInfo.RuntimeApplication, ModuleName = moduleInfo.ModuleName }));
                }
                else
                {
                    if (javascriptFiles != null)
                    {
                        foreach (var path in javascriptFiles)
                        {
                            ViewPage.JavascriptFiles.Insert(0, ViewPage.Url.ResolveUrl(UrlConvertor.AbsolutePathToRelativeUrl(path)));
                        }
                    }
                }
            }
        }
        private void IncludeModuleTheme(ModuleInfo moduleInfo)
        {
            if (!StringExtensions.IsNullOrEmptyTrim(moduleInfo.ModuleSettings.Theme))
            {
                var themePath = moduleInfo.GetThemePath(moduleInfo.ModuleSettings.Theme);
                var themeRulesInterpretor = new ThemeRulesInterpretor(themePath, ViewPage.Url);
                ViewPage.ThemeRulesInterpretors.Insert(0, themeRulesInterpretor);
                List<string> styles = new List<string>();
                var stylesheets = moduleInfo.GetThemeStylesheets(moduleInfo.ModuleSettings.Theme);
                if (stylesheets != null)
                {
                    foreach (var path in stylesheets)
                    {
                        if (!themeRulesInterpretor.Files.Contains(path, StringComparer.InvariantCultureIgnoreCase))
                        {
                            styles.Add(path);
                        }
                    }
                }
                if (styles.Count > 0)
                {
                    if (ViewPage.Cms_Application.IsRelease)
                    {
                        ViewPage.StyleSheetFiles.Add(ViewPage.Url.Action("GetModuleTheme", "Script", new { Application = moduleInfo.RuntimeApplication, ModuleName = moduleInfo.ModuleName, Theme = moduleInfo.ModuleSettings.Theme }));
                    }
                    else
                    {
                        foreach (var path in styles)
                        {
                            ViewPage.StyleSheetFiles.Insert(0, ViewPage.Url.ResolveUrl(UrlConvertor.AbsolutePathToRelativeUrl(path)));
                        }
                    }
                }
            }
        }
        #endregion

        #region Public Add Module
        /// <summary>
        /// Adds the module.
        /// </summary>
        /// <param name="moduleName">Name of the module.</param>
        /// <param name="controlId">The control id.</param>
        public void AddModule(string moduleName, string controlId)
        {
            AddModule(moduleName, controlId, null);
        }
        /// <summary>
        /// Adds the module.
        /// </summary>
        /// <param name="moduleName">Name of the module.</param>
        /// <param name="controlId">The control id.</param>
        /// <param name="moduleSettings">The module settings.</param>
        public void AddModule(string moduleName, string controlId, object moduleSettings)
        {
            ModuleInfo moduleInfo = ModuleManager.GetModule(moduleName, Cms_Application.ApplicationName);
            ModuleSettingCollection moduleSettingCol = moduleInfo.ModuleSettings;
            if (moduleSettings != null)
            {
                Hashtable propertyHash = HtmlExtensionUtility.GetPropertyHash(moduleSettings);
                foreach (var item in propertyHash.Keys)
                {
                    var setting = moduleSettingCol[item.ToString()];
                    if (setting == null)
                    {
                        setting = new ModuleSetting() { Key = item.ToString() };
                        moduleSettingCol.Add(setting);
                    }
                    var value = propertyHash[item];
                    if (value != null)
                    {
                        setting.Value = value.ToString();
                    }
                    else
                    {
                        setting.Value = string.Empty;
                    }
                }
            }
            AddModuleInternal(moduleInfo, controlId);
        }
        #endregion

        #region Public AddContentTemplate
        /// <summary>
        /// Adds the content template.
        /// </summary>
        /// <param name="originalUUID">The original UUID.</param>
        public void AddContentTemplate(Guid originalUUID)
        {
            AddContentTemplate(originalUUID, null);
        }

        /// <summary>
        /// Adds the content template.
        /// </summary>
        /// <param name="originalUUID">The original UUID.</param>
        /// <param name="propertySettings">The property settings.</param>
        public void AddContentTemplate(Guid originalUUID, object propertySettings)
        {
            aspnet_Applications application = ViewPage.Cms_Application;
            var contentTemplate = CachedData.GetContentTemplate(application.ApplicationName, originalUUID);
            AddContentTemplate(contentTemplate, propertySettings);
        }

        /// <summary>
        /// Adds the content template.
        /// </summary>
        /// <param name="contentTemplateName">Name of the content template.</param>
        public void AddContentTemplate(string contentTemplateName)
        {
            AddContentTemplate(contentTemplateName, null);
        }

        /// <summary>
        /// Adds the content template.
        /// </summary>
        /// <param name="contentTemplateName">Name of the content template.</param>
        public void AddContentTemplate(string contentTemplateName, object propertySettings)
        {
            aspnet_Applications application = ViewPage.Cms_Application;
            var contentTemplate = CachedData.GetContentTemplate(application.ApplicationName, contentTemplateName);
            AddContentTemplate(contentTemplate, propertySettings);
        }

        private void AddContentTemplate(Cms_ContentTemplate contentTemplate, object propertySettings)
        {
            var virtualPath = contentTemplate.GetVirtualPath();
            Control control = this.LoadControl(virtualPath);

            if (control is CmsUserControl)
            {
                ((CmsUserControl)control).ContentTemplate = contentTemplate;
            }

            HtmlExtensionUtility.SetProperties(control, propertySettings);
            this.Controls.Add(control);
        }
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!System.Web.HttpContext.Current.GetCmsContext().IsDesignMode)
            {
                if (this.Controls.Count > 0)
                {
                    HiddenControls(this.ContentHidden);
                }
                else
                {
                    HiddenControls(this.EmptyHidden);
                }
            }
        }

        private void HiddenControls(string controls)
        {
            if (!string.IsNullOrEmpty(controls))
            {
                string[] hiddenArr = controls.Split(',');
                foreach (var id in hiddenArr)
                {
                    Control control = this.Page.FindControl(id);
                    if (control != null)
                    {
                        control.Visible = false;
                    }
                }
            }

        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.