2009-12-10 22:21:57 +00:00
|
|
|
|
using System;
|
2010-01-29 00:15:14 +00:00
|
|
|
|
using System.Reflection;
|
2009-12-11 19:49:53 +00:00
|
|
|
|
using System.Web;
|
2009-12-10 22:21:57 +00:00
|
|
|
|
using System.Web.Mvc;
|
2010-01-29 00:15:14 +00:00
|
|
|
|
using Orchard.Core.Themes.Preview;
|
2009-12-10 00:15:58 +00:00
|
|
|
|
using Orchard.Core.Themes.ViewModels;
|
|
|
|
|
|
using Orchard.Localization;
|
2009-12-11 20:02:51 +00:00
|
|
|
|
using Orchard.Security;
|
2009-12-10 00:15:58 +00:00
|
|
|
|
using Orchard.Themes;
|
|
|
|
|
|
using Orchard.UI.Notify;
|
2009-12-11 00:40:44 +00:00
|
|
|
|
using Orchard.Mvc.ViewModels;
|
2009-12-10 00:15:58 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Themes.Controllers {
|
|
|
|
|
|
[ValidateInput(false)]
|
|
|
|
|
|
public class AdminController : Controller {
|
|
|
|
|
|
private readonly IThemeService _themeService;
|
2010-01-29 00:15:14 +00:00
|
|
|
|
private readonly IPreviewTheme _previewTheme;
|
2009-12-10 00:15:58 +00:00
|
|
|
|
|
2010-02-25 21:59:17 -08:00
|
|
|
|
public AdminController(IOrchardServices services, IThemeService themeService, IPreviewTheme previewTheme, IAuthorizer authorizer, INotifier notifier) {
|
|
|
|
|
|
Services = services;
|
2009-12-10 00:15:58 +00:00
|
|
|
|
_themeService = themeService;
|
2010-01-29 00:15:14 +00:00
|
|
|
|
_previewTheme = previewTheme;
|
2009-12-10 00:15:58 +00:00
|
|
|
|
T = NullLocalizer.Instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-02-25 21:59:17 -08:00
|
|
|
|
public IOrchardServices Services{ get; set; }
|
2009-12-10 00:15:58 +00:00
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public ActionResult Index() {
|
2009-12-10 22:21:57 +00:00
|
|
|
|
try {
|
|
|
|
|
|
var themes = _themeService.GetInstalledThemes();
|
2009-12-15 19:50:37 +00:00
|
|
|
|
var currentTheme = _themeService.GetSiteTheme();
|
2009-12-10 22:21:57 +00:00
|
|
|
|
var model = new ThemesIndexViewModel { CurrentTheme = currentTheme, Themes = themes };
|
|
|
|
|
|
return View(model);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception) {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
Services.Notifier.Error(T("Listing themes failed: " + exception.Message));
|
2009-12-10 22:21:57 +00:00
|
|
|
|
return View(new ThemesIndexViewModel());
|
|
|
|
|
|
}
|
2009-12-10 00:15:58 +00:00
|
|
|
|
}
|
2009-12-11 00:01:32 +00:00
|
|
|
|
|
2010-01-29 00:15:14 +00:00
|
|
|
|
[HttpPost, FormValueAbsent("submit.Apply"), FormValueAbsent("submit.Cancel")]
|
|
|
|
|
|
public ActionResult Preview(string themeName, string returnUrl) {
|
|
|
|
|
|
try {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't preview the current theme")))
|
2010-01-29 00:15:14 +00:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
_previewTheme.SetPreviewTheme(themeName);
|
|
|
|
|
|
return Redirect(returnUrl ?? "~/");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception) {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
Services.Notifier.Error(T("Previewing theme failed: " + exception.Message));
|
2010-01-29 00:15:14 +00:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Preview"), FormValueRequired("submit.Apply")]
|
|
|
|
|
|
public ActionResult ApplyPreview(string themeName, string returnUrl) {
|
|
|
|
|
|
try {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't preview the current theme")))
|
2010-01-29 00:15:14 +00:00
|
|
|
|
return new HttpUnauthorizedResult();
|
2010-01-29 00:20:14 +00:00
|
|
|
|
_previewTheme.SetPreviewTheme(null);
|
|
|
|
|
|
_themeService.SetSiteTheme(themeName);
|
2010-01-29 21:09:14 +00:00
|
|
|
|
return RedirectToAction("Index");
|
2010-01-29 00:15:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception) {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
Services.Notifier.Error(T("Previewing theme failed: " + exception.Message));
|
2010-01-29 00:15:14 +00:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Preview"), FormValueRequired("submit.Cancel")]
|
|
|
|
|
|
public ActionResult CancelPreview(string returnUrl) {
|
|
|
|
|
|
try {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't preview the current theme")))
|
2010-01-29 00:15:14 +00:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
_previewTheme.SetPreviewTheme(null);
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception) {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
Services.Notifier.Error(T("Previewing theme failed: " + exception.Message));
|
2010-01-29 00:15:14 +00:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-01-15 09:40:29 +00:00
|
|
|
|
[HttpPost]
|
2009-12-11 00:01:32 +00:00
|
|
|
|
public ActionResult Activate(string themeName) {
|
|
|
|
|
|
try {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't set the current theme")))
|
2009-12-11 20:02:51 +00:00
|
|
|
|
return new HttpUnauthorizedResult();
|
2009-12-15 19:50:37 +00:00
|
|
|
|
_themeService.SetSiteTheme(themeName);
|
2009-12-11 00:01:32 +00:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception) {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
Services.Notifier.Error(T("Activating theme failed: " + exception.Message));
|
2009-12-11 00:01:32 +00:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2009-12-11 00:40:44 +00:00
|
|
|
|
|
|
|
|
|
|
public ActionResult Install() {
|
2010-02-26 13:58:09 -08:00
|
|
|
|
return View(new BaseViewModel());
|
2009-12-11 00:40:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-01-15 09:40:29 +00:00
|
|
|
|
[HttpPost]
|
2009-12-11 00:40:44 +00:00
|
|
|
|
public ActionResult Install(FormCollection input) {
|
|
|
|
|
|
try {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageThemes, T("Couldn't install theme")))
|
2009-12-11 20:02:51 +00:00
|
|
|
|
return new HttpUnauthorizedResult();
|
2009-12-11 19:49:53 +00:00
|
|
|
|
foreach (string fileName in Request.Files) {
|
|
|
|
|
|
HttpPostedFileBase file = Request.Files[fileName];
|
|
|
|
|
|
_themeService.InstallTheme(file);
|
|
|
|
|
|
}
|
2009-12-11 00:40:44 +00:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception) {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
Services.Notifier.Error(T("Installing theme failed: " + exception.Message));
|
2009-12-11 23:10:54 +00:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-01-15 09:40:29 +00:00
|
|
|
|
[HttpPost]
|
2009-12-11 23:10:54 +00:00
|
|
|
|
public ActionResult Uninstall(string themeName) {
|
|
|
|
|
|
try {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageThemes, T("Couldn't uninstall theme")))
|
2009-12-11 23:10:54 +00:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
_themeService.UninstallTheme(themeName);
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception) {
|
2010-02-25 21:59:17 -08:00
|
|
|
|
Services.Notifier.Error(T("Uninstalling theme failed: " + exception.Message));
|
2009-12-11 00:40:44 +00:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2010-01-29 00:15:14 +00:00
|
|
|
|
|
|
|
|
|
|
class FormValueRequiredAttribute : ActionMethodSelectorAttribute {
|
|
|
|
|
|
private readonly string _submitButtonName;
|
|
|
|
|
|
|
|
|
|
|
|
public FormValueRequiredAttribute(string submitButtonName) {
|
|
|
|
|
|
_submitButtonName = submitButtonName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) {
|
|
|
|
|
|
var value = controllerContext.HttpContext.Request.Form[_submitButtonName];
|
|
|
|
|
|
return !string.IsNullOrEmpty(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
|
|
|
|
|
|
class FormValueAbsentAttribute : ActionMethodSelectorAttribute {
|
|
|
|
|
|
private readonly string _submitButtonName;
|
|
|
|
|
|
|
|
|
|
|
|
public FormValueAbsentAttribute(string submitButtonName) {
|
|
|
|
|
|
_submitButtonName = submitButtonName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) {
|
|
|
|
|
|
var value = controllerContext.HttpContext.Request.Form[_submitButtonName];
|
|
|
|
|
|
return string.IsNullOrEmpty(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2009-12-10 00:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|