From ed93d77c5a717a67a808b150de4baa6f27ecc34e Mon Sep 17 00:00:00 2001 From: loudej Date: Mon, 21 Dec 2009 20:22:04 +0000 Subject: [PATCH] Switching PageController to use IOrchardServices --HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044406 --- .../Controllers/PageController.cs | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Orchard.Web/Packages/Orchard.Sandbox/Controllers/PageController.cs b/src/Orchard.Web/Packages/Orchard.Sandbox/Controllers/PageController.cs index 269608e73..8d1b2e204 100644 --- a/src/Orchard.Web/Packages/Orchard.Sandbox/Controllers/PageController.cs +++ b/src/Orchard.Web/Packages/Orchard.Sandbox/Controllers/PageController.cs @@ -15,41 +15,37 @@ using Orchard.UI.Notify; namespace Orchard.Sandbox.Controllers { public class PageController : Controller, IUpdateModel { - private readonly IContentManager _contentManager; - private readonly INotifier _notifier; - public PageController(IContentManager contentManager, INotifier notifier) { - _contentManager = contentManager; - _notifier = notifier; + public PageController(IOrchardServices orchardServices) { + Services = orchardServices; } - protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; } - protected virtual IUser CurrentUser { get; [UsedImplicitly] private set; } + public IOrchardServices Services { get; set; } + public virtual ISite CurrentSite { get; set; } + public virtual IUser CurrentUser { get; set; } public Localizer T { get; set; } public ActionResult Index() { var model = new PageIndexViewModel { - Pages = _contentManager.Query() + Pages = Services.ContentManager.Query() .OrderBy(x => x.Name) .List() - .Select(x => _contentManager.BuildDisplayModel(x, "SummaryList")) + .Select(x => Services.ContentManager.BuildDisplayModel(x, "SummaryList")) }; return View(model); } public ActionResult Show(int id) { - var page = _contentManager.Get(id); - var model = new PageShowViewModel { - Page = _contentManager.BuildDisplayModel(page, "Detail") - }; - return View(model); + return View(new PageShowViewModel { + Page = Services.ContentManager.BuildDisplayModel(id, "Detail") + }); } public ActionResult Create() { var settings = CurrentSite.Get>(); 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"); } @@ -61,11 +57,11 @@ namespace Orchard.Sandbox.Controllers { public ActionResult Create(PageCreateViewModel model) { var settings = CurrentSite.Get>(); 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"); } - var page = _contentManager.Create("sandboxpage", item => { + var page = Services.ContentManager.Create("sandboxpage", item => { item.Record.Name = model.Name; }); return RedirectToAction("show", new { page.ContentItem.Id }); @@ -73,37 +69,41 @@ namespace Orchard.Sandbox.Controllers { public ActionResult Edit(int id) { - var settings = CurrentSite.Get>(); - if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) { - _notifier.Error(T("Anonymous users can not edit pages")); + if (IsEditAllowed() == false) { return RedirectToAction("show", new { id }); } - var page = _contentManager.Get(id); - var model = new PageEditViewModel { - Page = _contentManager.BuildEditorModel(page) - }; - return View(model); + return View(new PageEditViewModel { + Page = Services.ContentManager.BuildEditorModel(id) + }); } - [HttpPost, ValidateInput(false)] - public ActionResult Edit(int id, FormCollection input) { - var settings = CurrentSite.Get>(); - if (settings.Record.AllowAnonymousEdits == false && CurrentUser == null) { - _notifier.Error(T("Anonymous users can not edit pages")); + [HttpPost, ActionName("Edit"), ValidateInput(false)] + public ActionResult _Edit(int id) { + if (IsEditAllowed() == false) { return RedirectToAction("show", new { id }); } - var page = _contentManager.Get(id); var model = new PageEditViewModel { - Page = _contentManager.UpdateEditorModel(page, this) + Page = Services.ContentManager.UpdateEditorModel(id, this) }; - if (!ModelState.IsValid) + if (!ModelState.IsValid) { + Services.TransactionManager.Cancel(); return View(model); + } return RedirectToAction("show", new { id }); } + bool IsEditAllowed() { + var settings = CurrentSite.Get>(); + 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 model, string prefix, string[] includeProperties, string[] excludeProperties) { return TryUpdateModel(model, prefix, includeProperties, excludeProperties); }