/*
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.Web;
using System.Linq;
using System.Web.UI;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI.WebControls;
using Everest.Library.ExtensionMethod;
using Everest.Library;
using Everest.CmsServices.MvcHelper;
using System.Collections.Generic;
using Everest.Library.Web;
namespace Everest.CmsServices.Extension.Module{
/// <summary>
///
/// </summary>
public class ModuleWrapperControl : Control
{
private string moduleUrl;
private IControllerFactory controllerFactory;
public IModuleUrlConvertor ModuleUrlConvertor
{
get;
set;
}
/// <summary>
/// Initializes the specified parent page context.
/// </summary>
/// <param name="moduleInfo">The module info.</param>
/// <param name="parentPageContext">The parent page context.</param>
/// <param name="moduleUrl">The module URL.</param>
/// <param name="pageRouteData">The page route data.</param>
public void Initialize(ModuleInfo moduleInfo, ParentPageContext parentPageContext, string moduleUrl, CmsPageRouteData pageRouteData)
{
this.ModuleInfo = moduleInfo;
this.moduleUrl = UrlConvertor.MakeRelativeUrl(moduleUrl);
this.PageRouteData = pageRouteData;
this.ParentPageContext = parentPageContext;
if (moduleInfo.HostInCms)
{
this.ModuleUrlConvertor = new ModuleOnCmsUrlConvertor();
}
else
{
this.ModuleUrlConvertor = new StandardModuleUrlConvertor();
}
}
/// <summary>
/// Gets the controller factory.
/// </summary>
/// <value>The controller factory.</value>
public IControllerFactory ControllerFactory
{
get
{
if (this.controllerFactory == null)
{
return new DefaultControllerFactory();
}
return this.controllerFactory;
}
set
{
this.controllerFactory = value;
}
}
/// <summary>
/// Gets or sets the page route data.
/// </summary>
/// <value>The page route data.</value>
public CmsPageRouteData PageRouteData { get; set; }
/// <summary>
/// Gets or sets the parent page context.
/// </summary>
/// <value>The parent page context.</value>
public ParentPageContext ParentPageContext { get; set; }
/// <summary>
/// Gets or sets the module controller.
/// </summary>
/// <value>The module controller.</value>
public ModuleController ModuleController { get; private set; }
/// <summary>
/// Gets or sets the module info.
/// </summary>
/// <value>The module info.</value>
public ModuleInfo ModuleInfo { get; private set; }
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
//protected override void OnPreRender(EventArgs e)
//{
// base.OnPreRender(e);
// ExecuteModule();
//}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ExecuteModule();
}
/// <summary>
/// Executes the module.
/// </summary>
private void ExecuteModule()
{
HttpContext context = HttpContext.Current;
var httpContext = new ModuleHttpContext(context
, new ModuleHttpReqeust(context.Request, this.moduleUrl), new ModuleHttpResponse(context.Response, this.ID));
var routeData = ModuleInfo.RouteTable.GetRouteData(httpContext);
var requestContext = new ModuleRequestContext(httpContext, routeData
, PageRouteData, this.ModuleUrlConvertor, ModuleInfo, ParentPageContext);
string controllerName = requestContext.RouteData.GetRequiredString("controller");
ModuleController = ControllerFactory.CreateController(requestContext, controllerName) as ModuleController;
if (ModuleController == null)
{
throw new Exception(string.Format(Resources.NotFoundModuleController, ModuleInfo.ModuleName, controllerName));
}
ModuleController.InvokeAction(requestContext);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (ModuleController.IncludeJavascript)
{
IncludeJavascriptFiles(ModuleInfo);
}
if (ModuleController.EnableTheming)
{
IncludeModuleTheme(ModuleInfo);
}
}
private void IncludeJavascriptFiles(ModuleInfo moduleInfo)
{
var javascriptFiles = moduleInfo.GetJavascriptFiles();
if (javascriptFiles != null && javascriptFiles.Count() > 0)
{
if (moduleInfo.HostInCms && ParentPageContext.ViewPage.Cms_Application.IsRelease)
{
ParentPageContext.ViewPage.JavascriptFiles.Insert(0, ParentPageContext.ViewPage.Url.Action("GetModuleJavascript", "Script", new { Application = moduleInfo.RuntimeApplication, ModuleName = moduleInfo.ModuleName }));
}
else
{
if (javascriptFiles != null)
{
foreach (var path in javascriptFiles.OrderByDescending(f => f))
{
ParentPageContext.ViewPage.JavascriptFiles.Insert(0, ParentPageContext.UrlHelper.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, ParentPageContext.UrlHelper);
ParentPageContext.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 (moduleInfo.HostInCms && ParentPageContext.ViewPage.Cms_Application.IsRelease)
{
ParentPageContext.ViewPage.StyleSheetFiles.Insert(0, ParentPageContext.ViewPage.Url.Action("GetModuleTheme", "Script", new { Application = moduleInfo.RuntimeApplication, ModuleName = moduleInfo.ModuleName, Theme = moduleInfo.ModuleSettings.Theme }));
}
else
{
foreach (var path in styles.OrderByDescending(f => f))
{
ParentPageContext.ViewPage.StyleSheetFiles.Insert(0, ParentPageContext.UrlHelper.ResolveUrl(UrlConvertor.AbsolutePathToRelativeUrl(path)));
}
}
}
}
}
/// <summary>
/// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
protected override void Render(HtmlTextWriter writer)
{
((ModuleHttpResponse)ModuleController.ControllerContext.RequestContext.HttpContext.Response).SetOutput(writer);
//store the base directory;
var baseDirectory = EverestContext.GetContext().BaseDirectory;
EverestContext.GetContext().BaseDirectory = ModuleController.ModuleInfo.ModuleBaseDirectory;
//execute output
ModuleController.ExecuteResult();
ControllerFactory.ReleaseController(ModuleController);
//restore the base directory;
EverestContext.GetContext().BaseDirectory = baseDirectory;
}
}
}
|