Further incremental work establishing a per-request theme-aware engine stack

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044110
This commit is contained in:
loudej
2009-12-15 19:50:37 +00:00
parent 341bb208b0
commit 7b777ef4f6
14 changed files with 201 additions and 20 deletions

View File

@@ -91,6 +91,7 @@
<Compile Include="Themes\Permissions.cs" />
<Compile Include="Themes\Records\ThemeRecord.cs" />
<Compile Include="Themes\Records\ThemeSiteSettingsRecord.cs" />
<Compile Include="Themes\Services\SiteThemeSelector.cs" />
<Compile Include="Themes\Services\ThemeService.cs" />
<Compile Include="Themes\ViewModels\ThemesIndexViewModel.cs" />
<Compile Include="XmlRpc\Controllers\HomeController.cs" />

View File

@@ -28,7 +28,7 @@ namespace Orchard.Core.Themes.Controllers {
public ActionResult Index() {
try {
var themes = _themeService.GetInstalledThemes();
var currentTheme = _themeService.GetCurrentTheme();
var currentTheme = _themeService.GetSiteTheme();
var model = new ThemesIndexViewModel { CurrentTheme = currentTheme, Themes = themes };
return View(model);
}
@@ -40,9 +40,9 @@ namespace Orchard.Core.Themes.Controllers {
public ActionResult Activate(string themeName) {
try {
if (!_authorizer.Authorize(Permissions.SetCurrentTheme, T("Couldn't set the current theme")))
if (!_authorizer.Authorize(Permissions.SetSiteTheme, T("Couldn't set the current theme")))
return new HttpUnauthorizedResult();
_themeService.SetCurrentTheme(themeName);
_themeService.SetSiteTheme(themeName);
return RedirectToAction("Index");
}
catch (Exception exception) {

View File

@@ -4,7 +4,7 @@ using Orchard.Security.Permissions;
namespace Orchard.Core.Themes {
public class Permissions : IPermissionProvider {
public static readonly Permission InstallUninstallTheme = new Permission { Description = "Installing or Uninstalling Themes", Name = "InstallUninstallTheme" };
public static readonly Permission SetCurrentTheme = new Permission { Description = "Setting the Current Theme", Name = "SetCurrentTheme" };
public static readonly Permission SetSiteTheme = new Permission { Description = "Setting the Current Theme", Name = "SetSiteTheme" };
public string PackageName {
get {
@@ -14,7 +14,7 @@ namespace Orchard.Core.Themes {
public IEnumerable<Permission> GetPermissions() {
return new List<Permission> {
SetCurrentTheme,
SetSiteTheme,
InstallUninstallTheme
};
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using Orchard.Core.Themes.Models;
using Orchard.Models;
using Orchard.Settings;
using Orchard.Themes;
namespace Orchard.Core.Themes.Services {
public class SiteThemeSelector : IThemeSelector {
private readonly IThemeService _themeService;
public SiteThemeSelector (IThemeService themeService) {
_themeService = themeService;
}
public ThemeSelectorResult GetTheme(RequestContext context) {
var theme = _themeService.GetSiteTheme();
if (theme == null) {
return null;
}
return new ThemeSelectorResult {Priority = -5, ThemeName = theme.ThemeName};
}
}
}

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using Orchard.Extensions;
using Orchard.Logging;
using Orchard.Models;
@@ -11,9 +13,13 @@ using Orchard.Core.Themes.Models;
namespace Orchard.Core.Themes.Services {
public class ThemeService : IThemeService {
private readonly IExtensionManager _extensionManager;
private readonly IEnumerable<IThemeSelector> _themeSelectors;
public ThemeService(IExtensionManager extensionManager) {
public ThemeService(
IExtensionManager extensionManager,
IEnumerable<IThemeSelector> themeSelectors) {
_extensionManager = extensionManager;
_themeSelectors = themeSelectors;
Logger = NullLogger.Instance;
}
@@ -22,7 +28,7 @@ namespace Orchard.Core.Themes.Services {
#region Implementation of IThemeService
public ITheme GetCurrentTheme() {
public ITheme GetSiteTheme() {
string currentThemeName = CurrentSite.As<ThemeSiteSettings>().Record.CurrentThemeName;
if (String.IsNullOrEmpty(currentThemeName)) {
@@ -32,6 +38,27 @@ namespace Orchard.Core.Themes.Services {
return GetThemeByName(currentThemeName);
}
public void SetSiteTheme(string themeName) {
if (GetThemeByName(themeName) != null) {
CurrentSite.As<ThemeSiteSettings>().Record.CurrentThemeName = themeName;
}
}
public ITheme GetRequestTheme(RequestContext requestContext) {
var requestTheme = _themeSelectors
.Select(x => x.GetTheme(requestContext))
.Where(x => x != null)
.OrderByDescending(x => x.Priority)
.FirstOrDefault();
if (requestTheme == null) {
return null;
}
return GetThemeByName(requestTheme.ThemeName);
}
public ITheme GetThemeByName(string name) {
foreach (var descriptor in _extensionManager.AvailableExtensions()) {
if (String.Equals(descriptor.Name, name, StringComparison.OrdinalIgnoreCase)) {
@@ -66,11 +93,6 @@ namespace Orchard.Core.Themes.Services {
return themes;
}
public void SetCurrentTheme(string themeName) {
if (GetThemeByName(themeName) != null) {
CurrentSite.As<ThemeSiteSettings>().Record.CurrentThemeName = themeName;
}
}
public void InstallTheme(HttpPostedFileBase file) {
_extensionManager.InstallExtension("Theme", file);