Themes admin allows you to enable/disable themes separately from setting them to the default. DataMigrations for themes are done there too.

--HG--
branch : dev
This commit is contained in:
Dave Reed
2010-10-12 16:52:36 -07:00
parent ff858f1536
commit 30cb7dc75d
10 changed files with 159 additions and 30 deletions

View File

@@ -68,7 +68,7 @@ namespace Orchard.Tests.Modules.Themes.Services {
var context = new DynamicProxyContext();
var builder = new ContainerBuilder();
builder.RegisterModule(new SettingsModule());
builder.RegisterType<StubWorkContextAccessor>().As<IWorkContextAccessor>(); // test
builder.RegisterType<StubWorkContextAccessor>().As<IWorkContextAccessor>();
builder.RegisterType<ThemeService>().EnableDynamicProxy(context).As<IThemeService>();
builder.RegisterType<SettingsModuleInterceptor>().As<ISettingsModuleInterceptor>();
builder.RegisterType<SiteService>().As<ISiteService>();
@@ -86,7 +86,7 @@ namespace Orchard.Tests.Modules.Themes.Services {
builder.RegisterType<ThemeSiteSettingsPartHandler>().As<IContentHandler>();
builder.RegisterType<ModuleService>().As<IModuleService>();
builder.RegisterType<OrchardServices>().As<IOrchardServices>();
builder.RegisterType<StubShellDescriptorManager>().As<IShellDescriptorManager>();
builder.RegisterType<StubShellDescriptorManager>().As<IShellDescriptorManager>().InstancePerLifetimeScope();
builder.RegisterType<TransactionManager>().As<ITransactionManager>();
builder.RegisterType<Notifier>().As<INotifier>();
builder.RegisterType<StubAuthorizer>().As<IAuthorizer>();
@@ -136,6 +136,34 @@ namespace Orchard.Tests.Modules.Themes.Services {
Assert.That(siteTheme.ThemeName, Is.EqualTo("ThemeOne"));
}
[Test]
public void CanEnableAndDisableThemes() {
_themeService.EnableTheme("ThemeOne");
Assert.IsTrue(_themeService.GetThemeByName("ThemeOne").Enabled);
Assert.IsTrue(_container.Resolve<IShellDescriptorManager>().GetShellDescriptor().Features.Any(sf => sf.Name == "ThemeOne"));
_themeService.DisableTheme("ThemeOne");
Assert.IsFalse(_themeService.GetThemeByName("ThemeOne").Enabled);
Assert.IsFalse(_container.Resolve<IShellDescriptorManager>().GetShellDescriptor().Features.Any(sf => sf.Name == "ThemeOne"));
}
[Test]
public void ActivatingThemeEnablesIt() {
_themeService.SetSiteTheme("ThemeOne");
Assert.IsTrue(_themeService.GetThemeByName("ThemeOne").Enabled);
Assert.IsTrue(_container.Resolve<IShellDescriptorManager>().GetShellDescriptor().Features.Any(sf => sf.Name == "ThemeOne"));
}
[Test]
public void ActivatingThemeDoesNotDisableOldTheme() {
_themeService.SetSiteTheme("ThemeOne");
_themeService.SetSiteTheme("ThemeTwo");
Assert.IsTrue(_themeService.GetThemeByName("ThemeOne").Enabled);
Assert.IsTrue(_themeService.GetThemeByName("ThemeTwo").Enabled);
Assert.IsTrue(_container.Resolve<IShellDescriptorManager>().GetShellDescriptor().Features.Any(sf => sf.Name == "ThemeOne"));
Assert.IsTrue(_container.Resolve<IShellDescriptorManager>().GetShellDescriptor().Features.Any(sf => sf.Name == "ThemeTwo"));
}
#region Stubs
public class TestSessionLocator : ISessionLocator {

View File

@@ -46,6 +46,7 @@ namespace Orchard.Tests.DisplayManagement {
}
public class Theme : ITheme {
public bool Enabled { get; set; }
public string ThemeName { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Web;
using System.Web.Routing;
using Autofac;
using JetBrains.Annotations;
using Orchard.Commands;
using Orchard.Commands.Builtin;
using Orchard.ContentManagement;
@@ -91,6 +92,7 @@ namespace Orchard.Setup {
}
[UsedImplicitly]
class SafeModeText : IText {
public LocalizedString Get(string textHint, params object[] args) {
if (args == null || args.Length == 0) {
@@ -100,9 +102,11 @@ namespace Orchard.Setup {
}
}
[UsedImplicitly]
class SafeModeThemeService : IThemeService {
class SafeModeTheme : ITheme {
public ContentItem ContentItem { get; set; }
public bool Enabled { get; set; }
public string ThemeName { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }
@@ -115,6 +119,7 @@ namespace Orchard.Setup {
}
private readonly SafeModeTheme _theme = new SafeModeTheme {
Enabled = true,
ThemeName = "SafeMode",
DisplayName = "SafeMode",
};
@@ -126,8 +131,11 @@ namespace Orchard.Setup {
public IEnumerable<ITheme> GetInstalledThemes() { return new[] { _theme }; }
public void InstallTheme(HttpPostedFileBase file) { }
public void UninstallTheme(string themeName) { }
public void EnableTheme(string themeName) { }
public void DisableTheme(string themeName) { }
}
[UsedImplicitly]
class SafeModeSiteWorkContextProvider : IWorkContextStateProvider {
public T Get<T>(string name) {
if (name == "CurrentSite")
@@ -136,6 +144,7 @@ namespace Orchard.Setup {
}
}
[UsedImplicitly]
class SafeModeSiteService : ISiteService {
public ISite GetSiteSettings() {
var siteType = new ContentTypeDefinitionBuilder().Named("Site").Build();

View File

@@ -2,8 +2,11 @@
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Orchard.Data.Migration;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc.Results;
using Orchard.Reports.Services;
using Orchard.Security;
using Orchard.Themes.Preview;
using Orchard.Themes.ViewModels;
@@ -14,8 +17,12 @@ namespace Orchard.Themes.Controllers {
public class AdminController : Controller {
private readonly IThemeService _themeService;
private readonly IPreviewTheme _previewTheme;
private readonly IDataMigrationManager _dataMigrationManager;
private readonly IReportsCoordinator _reportsCoordinator;
public AdminController(
IDataMigrationManager dataMigraitonManager,
IReportsCoordinator reportsCoordinator,
IOrchardServices services,
IThemeService themeService,
IPreviewTheme previewTheme,
@@ -23,6 +30,8 @@ namespace Orchard.Themes.Controllers {
INotifier notifier,
IShapeHelperFactory shapeHelperFactory) {
Services = services;
_dataMigrationManager = dataMigraitonManager;
_reportsCoordinator = reportsCoordinator;
_themeService = themeService;
_previewTheme = previewTheme;
T = NullLocalizer.Instance;
@@ -36,7 +45,8 @@ namespace Orchard.Themes.Controllers {
try {
var themes = _themeService.GetInstalledThemes();
var currentTheme = _themeService.GetSiteTheme();
var model = new ThemesIndexViewModel { CurrentTheme = currentTheme, Themes = themes };
var featuresThatNeedUpdate = _dataMigrationManager.GetFeaturesThatNeedUpdate();
var model = new ThemesIndexViewModel { CurrentTheme = currentTheme, Themes = themes, FeaturesThatNeedUpdate = featuresThatNeedUpdate };
return View(model);
}
catch (Exception exception) {
@@ -66,12 +76,11 @@ namespace Orchard.Themes.Controllers {
return new HttpUnauthorizedResult();
_previewTheme.SetPreviewTheme(null);
_themeService.SetSiteTheme(themeName);
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error(T("Previewing theme failed: " + exception.Message));
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
[HttpPost, ActionName("Preview"), FormValueRequired("submit.Cancel")]
@@ -80,12 +89,37 @@ namespace Orchard.Themes.Controllers {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't preview the current theme")))
return new HttpUnauthorizedResult();
_previewTheme.SetPreviewTheme(null);
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error(T("Previewing theme failed: " + exception.Message));
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult Enable(string themeName) {
try {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't enable the theme")))
return new HttpUnauthorizedResult();
_themeService.EnableTheme(themeName);
}
catch (Exception exception) {
Services.Notifier.Error(T("Enabling theme failed: " + exception.Message));
}
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult Disable(string themeName) {
try {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't disable the current theme")))
return new HttpUnauthorizedResult();
_themeService.DisableTheme(themeName);
}
catch (Exception exception) {
Services.Notifier.Error(T("Disabling theme failed: " + exception.Message));
}
return RedirectToAction("Index");
}
[HttpPost]
@@ -94,12 +128,11 @@ namespace Orchard.Themes.Controllers {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't set the current theme")))
return new HttpUnauthorizedResult();
_themeService.SetSiteTheme(themeName);
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error(T("Activating theme failed: " + exception.Message));
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
public ActionResult Install() {
@@ -137,6 +170,26 @@ namespace Orchard.Themes.Controllers {
}
}
[HttpPost]
public ActionResult Update(string themeName) {
if (!Services.Authorizer.Authorize(Permissions.ManageThemes, T("Couldn't update theme")))
return new HttpUnauthorizedResult();
if (string.IsNullOrEmpty(themeName))
return new NotFoundResult();
try {
_reportsCoordinator.Register("Data Migration", "Upgrade " + themeName, "Orchard installation");
_dataMigrationManager.Update(themeName);
Services.Notifier.Information(T("The theme {0} was updated succesfuly", themeName));
}
catch (Exception ex) {
Services.Notifier.Error(T("An error occured while updating the theme {0}: {1}", themeName, ex.Message));
}
return RedirectToAction("Index");
}
class FormValueRequiredAttribute : ActionMethodSelectorAttribute {
private readonly string _submitButtonName;

View File

@@ -1,5 +1,6 @@
namespace Orchard.Themes.Models {
public class Theme : ITheme {
public bool Enabled { get; set; }
public string ThemeName { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Web;
using System.Web.Routing;
using JetBrains.Annotations;
using Orchard.Environment.Descriptor;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
using Orchard.Localization;
@@ -19,13 +20,16 @@ namespace Orchard.Themes.Services {
private readonly IExtensionManager _extensionManager;
private readonly IEnumerable<IThemeSelector> _themeSelectors;
private readonly IModuleService _moduleService;
private IWorkContextAccessor _workContextAccessor;
private readonly IWorkContextAccessor _workContextAccessor;
private readonly IShellDescriptorManager _shellDescriptorManager;
public ThemeService(
IShellDescriptorManager shellDescriptorManager,
IExtensionManager extensionManager,
IEnumerable<IThemeSelector> themeSelectors,
IModuleService moduleService,
IWorkContextAccessor workContextAccessor) {
_shellDescriptorManager = shellDescriptorManager;
_extensionManager = extensionManager;
_themeSelectors = themeSelectors;
_moduleService = moduleService;
@@ -49,26 +53,17 @@ namespace Orchard.Themes.Services {
}
public void SetSiteTheme(string themeName) {
if (string.IsNullOrWhiteSpace(themeName))
return;
if (DoEnableTheme(themeName)) {
CurrentSite.As<ThemeSiteSettingsPart>().Record.CurrentThemeName = themeName;
}
}
//todo: (heskew) need messages given in addition to all of these early returns so something meaningful can be presented to the user
var themeToSet = GetThemeByName(themeName);
if (themeToSet == null)
return;
public void EnableTheme(string themeName) {
DoEnableTheme(themeName);
}
// ensure all base themes down the line are present and accounted for
//todo: (heskew) dito on the need of a meaningful message
if (!AllBaseThemesAreInstalled(themeToSet.BaseTheme))
return;
// disable all theme features
DisableThemeFeatures(CurrentSite.As<ThemeSiteSettingsPart>().CurrentThemeName);
// enable all theme features
EnableThemeFeatures(themeToSet.ThemeName);
CurrentSite.As<ThemeSiteSettingsPart>().Record.CurrentThemeName = themeToSet.ThemeName;
public void DisableTheme(string themeName) {
DisableThemeFeatures(themeName);
}
private bool AllBaseThemesAreInstalled(string baseThemeName) {
@@ -93,12 +88,15 @@ namespace Orchard.Themes.Services {
while (themeName != null) {
if (themes.Contains(themeName))
throw new InvalidOperationException(T("The theme \"{0}\" is already in the stack of themes that need features disabled.", themeName).Text);
var theme = GetThemeByName(themeName);
if (theme == null)
break;
themes.Enqueue(themeName);
var theme = GetThemeByName(themeName);
themeName = !string.IsNullOrWhiteSpace(theme.BaseTheme)
? theme.BaseTheme
: null;
}
while (themes.Count > 0)
@@ -122,6 +120,25 @@ namespace Orchard.Themes.Services {
_moduleService.EnableFeatures(new[] {themes.Pop()});
}
private bool DoEnableTheme(string themeName) {
if (string.IsNullOrWhiteSpace(themeName))
return false;
//todo: (heskew) need messages given in addition to all of these early returns so something meaningful can be presented to the user
var themeToEnable = GetThemeByName(themeName);
if (themeToEnable == null)
return false;
// ensure all base themes down the line are present and accounted for
//todo: (heskew) dito on the need of a meaningful message
if (!AllBaseThemesAreInstalled(themeToEnable.BaseTheme))
return false;
// enable all theme features
EnableThemeFeatures(themeToEnable.ThemeName);
return true;
}
public ITheme GetRequestTheme(RequestContext requestContext) {
var requestTheme = _themeSelectors
.Select(x => x.GetTheme(requestContext))
@@ -188,6 +205,10 @@ namespace Orchard.Themes.Services {
return localized;
}
private bool IsThemeEnabled(ExtensionDescriptor descriptor) {
return _shellDescriptorManager.GetShellDescriptor().Features.Any(sf => sf.Name == descriptor.Name);
}
private ITheme CreateTheme(ExtensionDescriptor descriptor) {
var localizer = LocalizationUtilities.Resolve(_workContextAccessor.GetContext(), String.Concat(descriptor.Location, "/", descriptor.Name, "/Theme.txt"));
@@ -202,6 +223,7 @@ namespace Orchard.Themes.Services {
Tags = TryLocalize("Tags", descriptor.Tags, localizer) ?? "",
Zones = descriptor.Zones ?? "",
BaseTheme = descriptor.BaseTheme ?? "",
Enabled = IsThemeEnabled(descriptor)
};
}
}

View File

@@ -4,5 +4,6 @@ namespace Orchard.Themes.ViewModels {
public class ThemesIndexViewModel {
public ITheme CurrentTheme { get; set; }
public IEnumerable<ITheme> Themes { get; set; }
public IEnumerable<string> FeaturesThatNeedUpdate { get; set; }
}
}

View File

@@ -29,9 +29,13 @@
<div>
<h3>@theme.DisplayName</h3>
@Html.Image(Href(Html.ThemePath(theme, "/Theme.png")), Html.Encode(theme.DisplayName), null)
@using (Html.BeginFormAntiForgeryPost(Url.Action(theme.Enabled ? "Disable" : "Enable"), FormMethod.Post, new { @class = "inline" })) {
@Html.Hidden("themeName", theme.ThemeName)
<button type="submit" title="@T(theme.Enabled ? "Disable" : "Enable")">@T(theme.Enabled ? "Disable" : "Enable")</button>
}
@using (Html.BeginFormAntiForgeryPost(Url.Action("Activate"), FormMethod.Post, new { @class = "inline" })) {
@Html.Hidden("themeName", theme.ThemeName)
<button type="submit" title="@T("Activate")">@T("Activate")</button>
<button type="submit" title="@T("Activate")">@T("Set Current")</button>
}
@using (Html.BeginFormAntiForgeryPost(Url.Action("Preview"), FormMethod.Post, new { @class = "inline" })) {
@Html.Hidden("themeName", theme.ThemeName)
@@ -43,6 +47,12 @@
@theme.Description<br />
@theme.HomePage
</p>
@if(Model.FeaturesThatNeedUpdate.Contains(theme.ThemeName)){
using (Html.BeginFormAntiForgeryPost(Url.Action("Update"), FormMethod.Post, new { @class = "inline link" })) {
@Html.Hidden("themeName", theme.ThemeName)
<button type="submit" class="update">@T("Update")</button> <br/>
}
}
@using (Html.BeginFormAntiForgeryPost(Url.Action("Uninstall"), FormMethod.Post, new { @class = "inline link" })) {
@Html.Hidden("themeName", theme.ThemeName)
<button type="submit" class="uninstall" title="@T("Uninstall")">@T("Uninstall")</button>

View File

@@ -3,6 +3,7 @@
/// Interface provided by the "themes" model.
/// </summary>
public interface ITheme {
bool Enabled { get; set; }
string ThemeName { get; set; }
string DisplayName { get; set; }
string Description { get; set; }

View File

@@ -10,6 +10,9 @@ namespace Orchard.Themes {
void SetSiteTheme(string themeName);
ITheme GetRequestTheme(RequestContext requestContext);
void EnableTheme(string themeName);
void DisableTheme(string themeName);
IEnumerable<ITheme> GetInstalledThemes();
void InstallTheme(HttpPostedFileBase file);
void UninstallTheme(string themeName);