Switching PageController to use IOrchardServices

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044406
This commit is contained in:
loudej
2009-12-21 20:22:04 +00:00
parent 43bfc8aec4
commit ed93d77c5a

View File

@@ -15,41 +15,37 @@ using Orchard.UI.Notify;
namespace Orchard.Sandbox.Controllers { namespace Orchard.Sandbox.Controllers {
public class PageController : Controller, IUpdateModel { public class PageController : Controller, IUpdateModel {
private readonly IContentManager _contentManager;
private readonly INotifier _notifier;
public PageController(IContentManager contentManager, INotifier notifier) { public PageController(IOrchardServices orchardServices) {
_contentManager = contentManager; Services = orchardServices;
_notifier = notifier;
} }
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; } public IOrchardServices Services { get; set; }
protected virtual IUser CurrentUser { get; [UsedImplicitly] private set; } public virtual ISite CurrentSite { get; set; }
public virtual IUser CurrentUser { get; set; }
public Localizer T { get; set; } public Localizer T { get; set; }
public ActionResult Index() { public ActionResult Index() {
var model = new PageIndexViewModel { var model = new PageIndexViewModel {
Pages = _contentManager.Query<SandboxPage, SandboxPageRecord>() Pages = Services.ContentManager.Query<SandboxPage, SandboxPageRecord>()
.OrderBy(x => x.Name) .OrderBy(x => x.Name)
.List() .List()
.Select(x => _contentManager.BuildDisplayModel(x, "SummaryList")) .Select(x => Services.ContentManager.BuildDisplayModel(x, "SummaryList"))
}; };
return View(model); return View(model);
} }
public ActionResult Show(int id) { public ActionResult Show(int id) {
var page = _contentManager.Get<SandboxPage>(id); return View(new PageShowViewModel {
var model = new PageShowViewModel { Page = Services.ContentManager.BuildDisplayModel<SandboxPage>(id, "Detail")
Page = _contentManager.BuildDisplayModel(page, "Detail") });
};
return View(model);
} }
public ActionResult Create() { public ActionResult Create() {
var settings = CurrentSite.Get<ContentPart<SandboxSettingsRecord>>(); var settings = CurrentSite.Get<ContentPart<SandboxSettingsRecord>>();
if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) { if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) {
_notifier.Error(T("Anonymous users can not create pages")); Services.Notifier.Error(T("Anonymous users can not create pages"));
return RedirectToAction("index"); return RedirectToAction("index");
} }
@@ -61,11 +57,11 @@ namespace Orchard.Sandbox.Controllers {
public ActionResult Create(PageCreateViewModel model) { public ActionResult Create(PageCreateViewModel model) {
var settings = CurrentSite.Get<ContentPart<SandboxSettingsRecord>>(); var settings = CurrentSite.Get<ContentPart<SandboxSettingsRecord>>();
if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) { if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) {
_notifier.Error(T("Anonymous users can not create pages")); Services.Notifier.Error(T("Anonymous users can not create pages"));
return RedirectToAction("index"); return RedirectToAction("index");
} }
var page = _contentManager.Create<SandboxPage>("sandboxpage", item => { var page = Services.ContentManager.Create<SandboxPage>("sandboxpage", item => {
item.Record.Name = model.Name; item.Record.Name = model.Name;
}); });
return RedirectToAction("show", new { page.ContentItem.Id }); return RedirectToAction("show", new { page.ContentItem.Id });
@@ -73,37 +69,41 @@ namespace Orchard.Sandbox.Controllers {
public ActionResult Edit(int id) { public ActionResult Edit(int id) {
var settings = CurrentSite.Get<ContentPart<SandboxSettingsRecord>>(); if (IsEditAllowed() == false) {
if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) {
_notifier.Error(T("Anonymous users can not edit pages"));
return RedirectToAction("show", new { id }); return RedirectToAction("show", new { id });
} }
var page = _contentManager.Get<SandboxPage>(id); return View(new PageEditViewModel {
var model = new PageEditViewModel { Page = Services.ContentManager.BuildEditorModel<SandboxPage>(id)
Page = _contentManager.BuildEditorModel(page) });
};
return View(model);
} }
[HttpPost, ValidateInput(false)] [HttpPost, ActionName("Edit"), ValidateInput(false)]
public ActionResult Edit(int id, FormCollection input) { public ActionResult _Edit(int id) {
var settings = CurrentSite.Get<ContentPart<SandboxSettingsRecord>>(); if (IsEditAllowed() == false) {
if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) {
_notifier.Error(T("Anonymous users can not edit pages"));
return RedirectToAction("show", new { id }); return RedirectToAction("show", new { id });
} }
var page = _contentManager.Get<SandboxPage>(id);
var model = new PageEditViewModel { var model = new PageEditViewModel {
Page = _contentManager.UpdateEditorModel(page, this) Page = Services.ContentManager.UpdateEditorModel<SandboxPage>(id, this)
}; };
if (!ModelState.IsValid) if (!ModelState.IsValid) {
Services.TransactionManager.Cancel();
return View(model); return View(model);
}
return RedirectToAction("show", new { id }); return RedirectToAction("show", new { id });
} }
bool IsEditAllowed() {
var settings = CurrentSite.Get<ContentPart<SandboxSettingsRecord>>();
if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) {
Services.Notifier.Error(T("Anonymous users can not edit pages"));
return false;
}
return true;
}
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) { bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties); return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
} }