Updating Routable to fix the paths of contained items when a container's path changes

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-11-15 12:53:20 -08:00
parent 921bc46783
commit fc3ddf560e
14 changed files with 182 additions and 30 deletions

View File

@@ -33,11 +33,27 @@ namespace Orchard.Core.Routable.Handlers {
OnGetEditorShape<RoutePart>(SetModelProperties);
OnUpdateEditorShape<RoutePart>(SetModelProperties);
OnPublishing<RoutePart>((context, routable) => {
OnPublished<RoutePart>((context, route) => {
var path = route.Path;
route.Path = route.GetPathWithSlug(route.Slug);
if (context.PublishingItemVersionRecord != null)
processSlug(routable);
if (!string.IsNullOrEmpty(routable.Path))
_routablePathConstraint.AddPath(routable.Path);
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);
});
OnRemoved<RoutePart>((context, route) => {
if (!string.IsNullOrWhiteSpace(route.Path))
_routablePathConstraint.RemovePath(route.Path);
});
OnIndexing<RoutePart>((context, part) => context.DocumentIndex.Add("title", part.Record.Title).RemoveTags().Analyze());

View File

@@ -1,9 +1,15 @@
<Placement>
<!-- available display shapes -->
<!--
Parts_RoutableTitle
Parts_RoutableTitle_Summary
Parts_RoutableTitle_SummaryAdmin
-->
<Place Parts_Routable_Edit="Content:before.5"/>
<Match DisplayType="Detail">
<Place Parts_RoutableTitle="Header:5"/>
</Match>
<Match DisplayType="Summary">
<Place Parts_RoutableTitle="Header:5"/>
<Place Parts_RoutableTitle_Summary="Header:5"/>
</Match>
</Placement>

View File

@@ -22,5 +22,9 @@ namespace Orchard.Core.Routable.Services {
/// <returns>True if the slug has been created, False if a conflict occured</returns>
bool ProcessSlug(IRoutableAspect part);
/// <summary>
/// Updated the paths of all contained items to reflect the current path of this item
/// </summary>
void FixContainedPaths(IRoutableAspect part);
}
}

View File

@@ -45,7 +45,8 @@ namespace Orchard.Core.Routable.Services {
public void RemovePath(string path) {
lock (_syncLock) {
_paths.Remove(path);
if (path != null && _paths.ContainsKey(path))
_paths.Remove(path);
}
}

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text.RegularExpressions;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Core.Common.Models;
using Orchard.Core.Routable.Models;
namespace Orchard.Core.Routable.Services {
@@ -14,6 +15,18 @@ namespace Orchard.Core.Routable.Services {
_contentManager = contentManager;
}
public void FixContainedPaths(IRoutableAspect part) {
var items = _contentManager.Query(VersionOptions.Published)
.Join<CommonPartRecord>().Where(cr => cr.Container.Id == part.Id)
.List()
.Select(item => item.As<IRoutableAspect>()).Where(item => item != null);
foreach (var itemRoute in items) {
itemRoute.ContentItem.VersionRecord.Published = false; // <- to force a republish
_contentManager.Publish(itemRoute.ContentItem);
}
}
public void FillSlugFromTitle<TModel>(TModel model) where TModel : IRoutableAspect {
if (!string.IsNullOrEmpty(model.Slug) || string.IsNullOrEmpty(model.Title))
return;
@@ -86,6 +99,7 @@ namespace Orchard.Core.Routable.Services {
var originalSlug = part.Slug;
var newSlug = GenerateUniqueSlug(part, pathsLikeThis.Select(p => p.Path));
part.Path = part.GetPathWithSlug(newSlug);
part.Slug = newSlug;
if (originalSlug != newSlug)
return false;

View File

@@ -1,6 +1 @@
@{
Orchard.ContentManagement.ContentItem contentItem = Model.ContentPart.ContentItem;
string title = Model.Title.ToString();
}
<h1>@Html.ItemDisplayLink(title, contentItem)</h1>
<h1>@Model.Title</h1>