/*
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;
}
}
}
}
}
}
|