diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogPostAdminController.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogPostAdminController.cs index bc389815a..0155c4ea8 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogPostAdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogPostAdminController.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; using System.Web.Mvc; using Orchard.Blogs.Extensions; @@ -98,6 +99,18 @@ namespace Orchard.Blogs.Controllers { return Redirect(Url.BlogPostEdit(blogPost)); } + public ActionResult CreateWithoutBlog() { + var blogs = _blogService.Get().ToArray(); + + if (blogs.Count() == 0) { + Services.Notifier.Warning(T("To create a BlogPost you need to create a blog first. You have been redirected to the Blog creation page.")); + return RedirectToAction("Create", "BlogAdmin", new { area = "Orchard.Blogs" }); + } else { + Services.Notifier.Warning(T("To create a BlogPost you need to choose a blog first. You have been redirected to the Blog selection page.")); + return RedirectToAction("List", "BlogAdmin", new { area = "Orchard.Blogs" }); + } + } + //todo: the content shape template has extra bits that the core contents module does not (remove draft functionality) //todo: - move this extra functionality there or somewhere else that's appropriate? public ActionResult Edit(int blogId, int postId) { diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPostPartHandler.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPostPartHandler.cs index 5b3bcca0c..02fd498a2 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPostPartHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPostPartHandler.cs @@ -5,13 +5,18 @@ using Orchard.Blogs.Services; using Orchard.ContentManagement; using Orchard.ContentManagement.Handlers; using Orchard.Core.Common.Models; +using Orchard.Security; namespace Orchard.Blogs.Handlers { public class BlogPostPartHandler : ContentHandler { + private readonly IAuthorizationService _authorizationService; private readonly IBlogService _blogService; + private readonly IWorkContextAccessor _workContextAccessor; - public BlogPostPartHandler(IBlogService blogService, IBlogPostService blogPostService, RequestContext requestContext) { + public BlogPostPartHandler(IAuthorizationService authorizationService, IBlogService blogService, IBlogPostService blogPostService, RequestContext requestContext, IWorkContextAccessor workContextAccessor) { + _authorizationService = authorizationService; _blogService = blogService; + _workContextAccessor = workContextAccessor; OnGetDisplayShape(SetModelProperties); OnGetEditorShape(SetModelProperties); @@ -45,8 +50,29 @@ namespace Orchard.Blogs.Handlers { protected override void GetItemMetadata(GetContentItemMetadataContext context) { var blogPost = context.ContentItem.As(); + if (blogPost == null) { + return; + } + + int blogId = 0; // BlogPart can be null if this is a new Blog Post item. - if (blogPost == null || blogPost.BlogPart == null) { + if (blogPost.BlogPart == null) { + var blogs = _blogService.Get().Where(x => _authorizationService.TryCheckAccess(Permissions.MetaListBlogs, _workContextAccessor.GetContext().CurrentUser, x)).ToArray(); + if (blogs.Count() == 1) { + var singleBlog = blogs.ElementAt(0); + if (singleBlog != null) blogId = singleBlog.Id; + } + } else { + blogId = blogPost.BlogPart.Id; + } + + if (blogId == 0) { + context.Metadata.CreateRouteValues = new RouteValueDictionary { + {"Area", "Orchard.Blogs"}, + {"Controller", "BlogPostAdmin"}, + {"Action", "CreateWithoutBlog"} + }; + return; } @@ -54,21 +80,21 @@ namespace Orchard.Blogs.Handlers { {"Area", "Orchard.Blogs"}, {"Controller", "BlogPostAdmin"}, {"Action", "Create"}, - {"blogId", blogPost.BlogPart.Id} + {"blogId", blogId} }; context.Metadata.EditorRouteValues = new RouteValueDictionary { {"Area", "Orchard.Blogs"}, {"Controller", "BlogPostAdmin"}, {"Action", "Edit"}, {"postId", context.ContentItem.Id}, - {"blogId", blogPost.BlogPart.Id} + {"blogId", blogId} }; context.Metadata.RemoveRouteValues = new RouteValueDictionary { {"Area", "Orchard.Blogs"}, {"Controller", "BlogPostAdmin"}, {"Action", "Delete"}, {"postId", context.ContentItem.Id}, - {"blogId", blogPost.BlogPart.Id} + {"blogId", blogId} }; } } diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Routes.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Routes.cs index 20219695e..915e44684 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Routes.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Routes.cs @@ -93,6 +93,20 @@ namespace Orchard.Blogs { }, new MvcRouteHandler()) }, + new RouteDescriptor { + Route = new Route( + "Admin/Blogs/Posts/CreateWithoutBlog", + new RouteValueDictionary { + {"area", "Orchard.Blogs"}, + {"controller", "BlogPostAdmin"}, + {"action", "CreateWithoutBlog"} + }, + new RouteValueDictionary (), + new RouteValueDictionary { + {"area", "Orchard.Blogs"} + }, + new MvcRouteHandler()) + }, new RouteDescriptor { Route = new Route( "Admin/Blogs/{blogId}/Posts/{postId}/Edit",