Merge branch 'bug/handling-theme-dependencies' into dev

This commit is contained in:
Lombiq
2015-12-03 21:43:34 +01:00
5 changed files with 116 additions and 6 deletions

View File

@@ -35,6 +35,8 @@ namespace Orchard.Themes.Controllers {
private readonly IThemeService _themeService;
private readonly ShellSettings _shellSettings;
private const string AlreadyEnabledFeatures = "Orchard.Themes.AlreadyEnabledFeatures";
public AdminController(
IEnumerable<IExtensionDisplayEventHandler> extensionDisplayEventHandlers,
IOrchardServices services,
@@ -132,8 +134,11 @@ namespace Orchard.Themes.Controllers {
Services.Notifier.Error(T("Theme {0} was not found", themeId));
} else {
var alreadyEnabledFeatures = GetEnabledFeatures();
_themeService.EnableThemeFeatures(themeId);
_previewTheme.SetPreviewTheme(themeId);
alreadyEnabledFeatures.Except(new[] { themeId });
TempData[AlreadyEnabledFeatures] = alreadyEnabledFeatures;
}
return this.RedirectLocal(returnUrl, "~/");
@@ -161,6 +166,18 @@ namespace Orchard.Themes.Controllers {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't preview the current theme")))
return new HttpUnauthorizedResult();
if (TempData.ContainsKey(AlreadyEnabledFeatures)) {
var alreadyEnabledFeatures = TempData[AlreadyEnabledFeatures] as IEnumerable<string>;
if (alreadyEnabledFeatures != null) {
var afterEnabledFeatures = GetEnabledFeatures();
if (afterEnabledFeatures.Count() > alreadyEnabledFeatures.Count()) {
var disableFeatures = afterEnabledFeatures.Except(alreadyEnabledFeatures);
_themeService.DisablePreviewFeatures(disableFeatures);
}
}
}
_previewTheme.SetPreviewTheme(null);
return RedirectToAction("Index");
@@ -252,5 +269,9 @@ namespace Orchard.Themes.Controllers {
return string.IsNullOrEmpty(value);
}
}
public IEnumerable<string> GetEnabledFeatures() {
return _featureManager.GetEnabledFeatures().Select(f => f.Id);
}
}
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Environment;
using Orchard.Environment.Extensions.Models;
using Orchard.Environment.Features;
using Orchard.Localization;
using Orchard.Themes.Services;
using Orchard.UI.Notify;
namespace Orchard.Themes.Events {
public class ThemeDisableEventHandler : IFeatureEventHandler {
private readonly IFeatureManager _featureManager;
private readonly ISiteThemeService _siteThemeService;
private readonly INotifier _notifier;
public ThemeDisableEventHandler(
IFeatureManager featureManager,
ISiteThemeService siteThemeService,
INotifier notifier) {
_featureManager = featureManager;
_siteThemeService = siteThemeService;
_notifier = notifier;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Installing(Feature feature) {
}
public void Installed(Feature feature) {
}
public void Enabling(Feature feature) {
}
public void Enabled(Feature feature) {
}
public void Disabling(Feature feature) {
}
public void Disabled(Feature feature) {
var currentTheme = _siteThemeService.GetCurrentThemeName();
if (feature.Descriptor.Name == currentTheme) {
_siteThemeService.SetSiteTheme(null);
// Notifications don't work in feature events. See: https://github.com/OrchardCMS/Orchard/issues/6106
_notifier.Warning(T("The current theme was disabled, because one of its dependencies was disabled."));
}
}
public void Uninstalling(Feature feature) {
}
public void Uninstalled(Feature feature) {
}
}
}

View File

@@ -68,6 +68,7 @@
<Compile Include="AdminMenu.cs" />
<Compile Include="Commands\ThemeCommands.cs" />
<Compile Include="Drivers\DisableThemePartDriver.cs" />
<Compile Include="Events\ThemeDisableEventHandler.cs" />
<Compile Include="Events\IExtensionDisplayEventHandler.cs" />
<Compile Include="Migrations.cs" />
<Compile Include="Models\ThemeEntry.cs" />

View File

@@ -1,9 +1,11 @@
using Orchard.Environment.Extensions.Models;
using System.Collections.Generic;
using Orchard.Environment.Extensions.Models;
namespace Orchard.Themes.Services {
public interface IThemeService : IDependency {
void DisableThemeFeatures(string themeName);
void EnableThemeFeatures(string themeName);
bool IsRecentlyInstalled(ExtensionDescriptor module);
void DisablePreviewFeatures(IEnumerable<string> features);
}
}

View File

@@ -18,6 +18,7 @@ namespace Orchard.Themes.Services {
private readonly IEnumerable<IThemeSelector> _themeSelectors;
private readonly IVirtualPathProvider _virtualPathProvider;
private readonly ICacheManager _cacheManager;
private readonly ISiteThemeService _siteThemeService;
public ThemeService(
IOrchardServices orchardServices,
@@ -25,7 +26,8 @@ namespace Orchard.Themes.Services {
IFeatureManager featureManager,
IEnumerable<IThemeSelector> themeSelectors,
IVirtualPathProvider virtualPathProvider,
ICacheManager cacheManager) {
ICacheManager cacheManager,
ISiteThemeService siteThemeService) {
Services = orchardServices;
@@ -34,6 +36,7 @@ namespace Orchard.Themes.Services {
_themeSelectors = themeSelectors;
_virtualPathProvider = virtualPathProvider;
_cacheManager = cacheManager;
_siteThemeService = siteThemeService;
if (_featureManager.FeatureDependencyNotification == null) {
_featureManager.FeatureDependencyNotification = GenerateWarning;
@@ -62,8 +65,16 @@ namespace Orchard.Themes.Services {
: null;
}
while (themes.Count > 0)
_featureManager.DisableFeatures(new[] { themes.Dequeue() });
var currentTheme = _siteThemeService.GetCurrentThemeName();
while (themes.Count > 0) {
var themeId = themes.Dequeue();
// Not disabling base theme if it's the current theme.
if (themeId != currentTheme) {
_featureManager.DisableFeatures(new[] { themeId });
}
}
}
public void EnableThemeFeatures(string themeName) {
@@ -79,8 +90,15 @@ namespace Orchard.Themes.Services {
: null;
}
while (themes.Count > 0)
_featureManager.EnableFeatures(new[] {themes.Pop()});
while (themes.Count > 0) {
var themeId = themes.Pop();
foreach (var featureId in _featureManager.EnableFeatures(new[] { themeId }, true)) {
if (themeId != featureId) {
var featureName = _featureManager.GetAvailableFeatures().First(f => f.Id.Equals(featureId, StringComparison.OrdinalIgnoreCase)).Name;
Services.Notifier.Information(T("{0} was enabled", featureName));
}
}
}
}
public ExtensionDescriptor GetRequestTheme(RequestContext requestContext) {
@@ -172,5 +190,12 @@ namespace Orchard.Themes.Services {
: "{0}, "), fn).ToString()).ToArray())
: featuresInQuestion.First()));
}
public void DisablePreviewFeatures(IEnumerable<string> features) {
foreach (var featureId in _featureManager.DisableFeatures(features,true)) {
var featureName = _featureManager.GetAvailableFeatures().First(f => f.Id.Equals(featureId, StringComparison.OrdinalIgnoreCase)).Name;
Services.Notifier.Information(T("{0} was disabled", featureName));
}
}
}
}