Fixing up the RoutePart so a page that is set to be the home page only becomes the home page if the page is published

- previously if the page wasn't immediately published (saved as draft or scheduled for later publishing) it still overrode the home page setting and the home page subsequently became a nice 404 page

--HG--
branch : dev
This commit is contained in:
Nathan Heskew 2010-11-30 14:02:26 -08:00
parent f512e634b1
commit 902769569f
4 changed files with 39 additions and 32 deletions

View File

@ -70,7 +70,7 @@ namespace Orchard.Core.Routable.Drivers {
var containerUrl = new UriBuilder(request.ToRootUrlString()) { Path = (request.ApplicationPath ?? "").TrimEnd('/') + "/" + (part.GetContainerPath() ?? "") };
model.ContainerAbsoluteUrl = containerUrl.Uri.ToString().TrimEnd('/');
model.PromoteToHomePage = model.Id != 0 && part.Path != null && _routableHomePageProvider != null && _services.WorkContext.CurrentSite.HomePage == _routableHomePageProvider.GetSettingValue(model.Id);
model.PromoteToHomePage = model.Id != 0 && _routableHomePageProvider != null && _services.WorkContext.CurrentSite.HomePage == _routableHomePageProvider.GetSettingValue(model.Id);
return ContentShape("Parts_Routable_Edit",
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
}
@ -81,6 +81,7 @@ namespace Orchard.Core.Routable.Drivers {
part.Title = model.Title;
part.Slug = model.Slug;
part.PromoteToHomePage = model.PromoteToHomePage;
if ( !_routableService.IsSlugValid(part.Slug) ) {
var slug = (part.Slug ?? String.Empty);
@ -90,9 +91,6 @@ namespace Orchard.Core.Routable.Drivers {
updater.AddModelError("Routable.Slug", T("Please do not use any of the following characters in your slugs: \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\", \", \"<\", \">\". No spaces are allowed (please use dashes or underscores instead)."));
}
if (part.ContentItem.Id != 0 && model.PromoteToHomePage && _routableHomePageProvider != null)
_services.WorkContext.CurrentSite.HomePage = _routableHomePageProvider.GetSettingValue(part.ContentItem.Id);
return Editor(part, shapeHelper);
}
}

View File

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
@ -6,6 +8,7 @@ using Orchard.Core.Routable.Models;
using Orchard.Core.Routable.Services;
using Orchard.Data;
using Orchard.Localization;
using Orchard.Services;
using Orchard.UI.Notify;
namespace Orchard.Core.Routable.Handlers {
@ -13,11 +16,18 @@ namespace Orchard.Core.Routable.Handlers {
private readonly IOrchardServices _services;
private readonly IRoutablePathConstraint _routablePathConstraint;
private readonly IRoutableService _routableService;
private readonly IHomePageProvider _routableHomePageProvider;
public RoutePartHandler(IOrchardServices services, IRepository<RoutePartRecord> repository, IRoutablePathConstraint routablePathConstraint, IRoutableService routableService) {
public RoutePartHandler(
IOrchardServices services,
IRepository<RoutePartRecord> repository,
IRoutablePathConstraint routablePathConstraint,
IRoutableService routableService,
IEnumerable<IHomePageProvider> homePageProviders) {
_services = services;
_routablePathConstraint = routablePathConstraint;
_routableService = routableService;
_routableHomePageProvider = homePageProviders.SingleOrDefault(p => p.GetProviderName() == RoutableHomePageProvider.Name);
T = NullLocalizer.Instance;
Filters.Add(StorageFilter.For(repository));
@ -34,21 +44,10 @@ namespace Orchard.Core.Routable.Handlers {
OnUpdateEditorShape<RoutePart>(SetModelProperties);
OnPublished<RoutePart>((context, route) => {
var path = route.Path;
route.Path = route.GetPathWithSlug(route.Slug);
FinalizePath(route, context, processSlug);
if (context.PublishingItemVersionRecord != null)
processSlug(route);
// if the path has changed by having the slug changed on the way in (e.g. user input) or to avoid conflict
// then update and publish all contained items
if (path != route.Path) {
_routablePathConstraint.RemovePath(path);
_routableService.FixContainedPaths(route);
}
if (!string.IsNullOrWhiteSpace(route.Path))
_routablePathConstraint.AddPath(route.Path);
if (route.ContentItem.Id != 0 && route.PromoteToHomePage && _routableHomePageProvider != null)
_services.WorkContext.CurrentSite.HomePage = _routableHomePageProvider.GetSettingValue(route.ContentItem.Id);
});
OnRemoved<RoutePart>((context, route) => {
@ -59,6 +58,24 @@ namespace Orchard.Core.Routable.Handlers {
OnIndexing<RoutePart>((context, part) => context.DocumentIndex.Add("title", part.Record.Title).RemoveTags().Analyze());
}
private void FinalizePath(RoutePart route, PublishContentContext context, Action<RoutePart> processSlug) {
var path = route.Path;
route.Path = route.GetPathWithSlug(route.Slug);
if (context.PublishingItemVersionRecord != null)
processSlug(route);
// if the path has changed by having the slug changed on the way in (e.g. user input) or to avoid conflict
// then update and publish all contained items
if (path != route.Path) {
_routablePathConstraint.RemovePath(path);
_routableService.FixContainedPaths(route);
}
if (!string.IsNullOrWhiteSpace(route.Path))
_routablePathConstraint.AddPath(route.Path);
}
private static void SetModelProperties(BuildShapeContext context, RoutePart routable) {
var item = context.Shape;
item.Title = routable.Title;

View File

@ -3,7 +3,6 @@ using Orchard.ContentManagement.Aspects;
namespace Orchard.Core.Routable.Models {
public class RoutePart : ContentPart<RoutePartRecord>, IRoutableAspect {
public string Title {
get { return Record.Title; }
set { Record.Title = value; }
@ -18,5 +17,7 @@ namespace Orchard.Core.Routable.Models {
get { return Record.Path; }
set { Record.Path = value; }
}
public bool PromoteToHomePage { get; set; }
}
}

View File

@ -2,7 +2,6 @@
using JetBrains.Annotations;
using Orchard.Core.Routable.Models;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Services;
using Orchard.ContentManagement;
@ -10,24 +9,16 @@ namespace Orchard.Core.Routable.Services {
[UsedImplicitly]
public class RoutableHomePageProvider : IHomePageProvider {
private readonly IContentManager _contentManager;
private readonly IWorkContextAccessor _workContextAccessor;
public const string Name = "RoutableHomePageProvider";
public RoutableHomePageProvider(
IOrchardServices services,
IContentManager contentManager,
IShapeFactory shapeFactory,
IWorkContextAccessor workContextAccessor) {
IShapeFactory shapeFactory) {
_contentManager = contentManager;
_workContextAccessor = workContextAccessor;
Services = services;
T = NullLocalizer.Instance;
Shape = shapeFactory;
}
dynamic Shape { get; set; }
public IOrchardServices Services { get; private set; }
public Localizer T { get; set; }
public string GetProviderName() {
return Name;
@ -37,8 +28,8 @@ namespace Orchard.Core.Routable.Services {
return GetProviderName() + ";" + id;
}
public ActionResult GetHomePage(int itemId) {
var contentItem = _contentManager.Get(itemId, VersionOptions.Published);
public ActionResult GetHomePage(int id) {
var contentItem = _contentManager.Get(id, VersionOptions.Published);
if (contentItem == null || !contentItem.Is<RoutePart>())
return new HttpNotFoundResult();