Updated Autoroute part, driver and handler to simplify setting the homepage.

This commit is contained in:
Sipke Schoorstra 2015-07-21 11:53:25 +01:00
parent 32584416b9
commit 8f302d1b90
5 changed files with 57 additions and 63 deletions

View File

@ -1,37 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Alias;
using Orchard.Autoroute.Models;
using Orchard.Autoroute.Services;
using Orchard.Autoroute.Settings;
using Orchard.Autoroute.ViewModels;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Autoroute.ViewModels;
using Orchard.Autoroute.Settings;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Notify;
using Orchard.Utility.Extensions;
namespace Orchard.Autoroute.Drivers {
public class AutoroutePartDriver : ContentPartDriver<AutoroutePart> {
private readonly IAliasService _aliasService;
private readonly IContentManager _contentManager;
private readonly IAutorouteService _autorouteService;
private readonly IAuthorizer _authorizer;
private readonly INotifier _notifier;
private readonly IHomeAliasService _homeAliasService;
public AutoroutePartDriver(
IAliasService aliasService,
IContentManager contentManager,
IAutorouteService autorouteService,
IAuthorizer authorizer,
INotifier notifier) {
_aliasService = aliasService;
_contentManager = contentManager;
INotifier notifier,
IHomeAliasService homeAliasService) {
_autorouteService = autorouteService;
_authorizer = authorizer;
_notifier = notifier;
_homeAliasService = homeAliasService;
T = NullLocalizer.Instance;
}
@ -64,15 +57,14 @@ namespace Orchard.Autoroute.Drivers {
};
// Retrieve home page.
//var homepage = _aliasService.Get(string.Empty);
var displayRouteValues = _contentManager.GetItemMetadata(part).DisplayRouteValues;
var homePageId = _homeAliasService.GetHomePageId();
var isHomePage = part.Id == homePageId;
//viewModel.IsHomePage = homepage.Match(displayRouteValues);
//viewModel.PromoteToHomePage = viewModel.IsHomePage || part.DisplayAlias == "/";
viewModel.IsHomePage = isHomePage;
viewModel.PromoteToHomePage = part.IsHomePage;
if (settings.PerItemConfiguration) {
// If enabled, the list of all available patterns is displayed, and the user can select which one to use.
// todo: later
}
@ -88,26 +80,22 @@ namespace Orchard.Autoroute.Drivers {
// Reset the alias if we need to force regeneration, and the user didn't provide a custom one.
if(settings.AutomaticAdjustmentOnEdit && previous == part.DisplayAlias) {
part.DisplayAlias = string.Empty;
part.DisplayAlias = String.Empty;
}
if (!_autorouteService.IsPathValid(part.DisplayAlias)) {
updater.AddModelError("CurrentUrl", T("Please do not use any of the following characters in your permalink: \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\", \", \"<\", \">\", \"\\\", \"|\", \"%\", \".\". No spaces are allowed (please use dashes or underscores instead)."));
}
// If CurrentUrl is set, the handler won't try to create an alias for it but instead keep the value.
//// If home page is requested, use "/" to have the handler create a homepage alias.
//if(viewModel.PromoteToHomePage) {
// part.DisplayAlias = "/";
//}
// Mark the content item to be the homepage. Once this content isp ublished, the home alias will be updated to point to this content item.
part.IsHomePage = viewModel.PromoteToHomePage;
}
return ContentShape("Parts_Autoroute_Edit",
return ContentShape("Parts_Autoroute_Edit",
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Autoroute.Edit", Model: viewModel, Prefix: Prefix));
}
protected override void Importing(AutoroutePart part, ContentManagement.Handlers.ImportContentContext context) {
protected override void Importing(AutoroutePart part, ImportContentContext context) {
var displayAlias = context.Attribute(part.PartDefinition.Name, "Alias");
if (displayAlias != null) {
part.DisplayAlias = displayAlias;
@ -124,7 +112,7 @@ namespace Orchard.Autoroute.Drivers {
}
}
protected override void Exporting(AutoroutePart part, ContentManagement.Handlers.ExportContentContext context) {
protected override void Exporting(AutoroutePart part, ExportContentContext context) {
context.Element(part.PartDefinition.Name).SetAttributeValue("Alias", part.Record.DisplayAlias);
context.Element(part.PartDefinition.Name).SetAttributeValue("CustomPattern", part.Record.CustomPattern);
context.Element(part.PartDefinition.Name).SetAttributeValue("UseCustomPattern", part.Record.UseCustomPattern);

View File

@ -1,6 +1,7 @@
using System;
using Orchard.Autoroute.Models;
using Orchard.Autoroute.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using Orchard.Localization;
@ -11,17 +12,20 @@ namespace Orchard.Autoroute.Handlers {
private readonly Lazy<IAutorouteService> _autorouteService;
private readonly IOrchardServices _orchardServices;
private readonly IHomeAliasService _homeAliasService;
public Localizer T { get; set; }
public AutoroutePartHandler(
IRepository<AutoroutePartRecord> autoroutePartRepository,
Lazy<IAutorouteService> autorouteService,
IOrchardServices orchardServices) {
IOrchardServices orchardServices,
IHomeAliasService homeAliasService) {
Filters.Add(StorageFilter.For(autoroutePartRepository));
_autorouteService = autorouteService;
_orchardServices = orchardServices;
_homeAliasService = homeAliasService;
OnUpdated<AutoroutePart>((ctx, part) => CreateAlias(part));
@ -54,42 +58,41 @@ namespace Orchard.Autoroute.Handlers {
private void PublishAlias(AutoroutePart part) {
ProcessAlias(part);
//// should it become the home page ?
//if (part.DisplayAlias == "/") {
// part.DisplayAlias = String.Empty;
// Should it become the home page?
if (part.IsHomePage) {
// Get the current homepage an unmark it as the homepage.
var currentHomePage = _homeAliasService.GetHomePage(VersionOptions.Latest);
if(currentHomePage != null && currentHomePage.Id != part.Id) {
var autoroutePart = currentHomePage.As<AutoroutePart>();
// // regenerate the alias for the previous home page
// var currentHomePages = _orchardServices.ContentManager.Query<AutoroutePart, AutoroutePartRecord>().Where(x => x.DisplayAlias == "").List();
// foreach (var current in currentHomePages.Where(x => x.Id != part.Id)) {
// if (current != null) {
// current.CustomPattern = String.Empty; // force the regeneration
// current.DisplayAlias = _autorouteService.Value.GenerateAlias(current);
if (autoroutePart != null) {
autoroutePart.IsHomePage = false;
if(autoroutePart.IsPublished())
_orchardServices.ContentManager.Publish(autoroutePart.ContentItem);
}
}
// // we changed the alias of the previous homepage, so publish this change if the content item was published.
// if(current.IsPublished())
// _orchardServices.ContentManager.Publish(current.ContentItem);
// }
// _autorouteService.Value.PublishAlias(current);
// }
//}
// Update the home alias to point to this item being published.
_homeAliasService.SetHomeAlias(part);
}
_autorouteService.Value.PublishAlias(part);
}
private void ProcessAlias(AutoroutePart part) {
// generate an alias if one as not already been entered
// Generate an alias if one as not already been entered.
if (String.IsNullOrWhiteSpace(part.DisplayAlias)) {
part.DisplayAlias = _autorouteService.Value.GenerateAlias(part);
}
// if the generated alias is empty, compute a new one
// If the generated alias is empty, compute a new one.
if (String.IsNullOrWhiteSpace(part.DisplayAlias)) {
_autorouteService.Value.ProcessPath(part);
_orchardServices.Notifier.Warning(T("The permalink could not be generated, a new slug has been defined: \"{0}\"", part.Path));
return;
}
// check for permalink conflict, unless we are trying to set the home page
// Check for permalink conflict, unless we are trying to set the home page.
if (part.DisplayAlias != "/") {
var previous = part.Path;
if (!_autorouteService.Value.ProcessPath(part))

View File

@ -17,6 +17,11 @@ namespace Orchard.Autoroute.Models {
set { Store(x => x.DisplayAlias, value); }
}
public bool IsHomePage {
get { return this.Retrieve(x => x.IsHomePage); }
set { this.Store(x => x.IsHomePage, value); }
}
public string Path {
get { return DisplayAlias; }
}

View File

@ -102,6 +102,8 @@
<Compile Include="Commands\AutorouteCommands.cs" />
<Compile Include="ResourceManifest.cs" />
<Compile Include="Services\AliasResolverSelector.cs" />
<Compile Include="Services\HomeAliasService.cs" />
<Compile Include="Services\IHomeAliasService.cs" />
<Compile Include="Services\PathResolutionService.cs" />
<Compile Include="Services\IPathResolutionService.cs" />
<Compile Include="Services\IRouteEvents.cs" />

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Orchard.Alias;
using Orchard.Alias.Implementation.Storage;
using Orchard.Autoroute.Models;
using Orchard.Autoroute.Settings;
using Orchard.ContentManagement;
@ -19,7 +18,6 @@ namespace Orchard.Autoroute.Services {
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly IContentManager _contentManager;
private readonly IRouteEvents _routeEvents;
private readonly IAliasStorage _aliasStorage;
private const string AliasSource = "Autoroute:View";
public AutorouteService(
@ -27,15 +25,13 @@ namespace Orchard.Autoroute.Services {
ITokenizer tokenizer,
IContentDefinitionManager contentDefinitionManager,
IContentManager contentManager,
IRouteEvents routeEvents,
IAliasStorage aliasStorage) {
IRouteEvents routeEvents) {
_aliasService = aliasService;
_tokenizer = tokenizer;
_contentDefinitionManager = contentDefinitionManager;
_contentManager = contentManager;
_routeEvents = routeEvents;
_aliasStorage = aliasStorage;
}
public string GenerateAlias(AutoroutePart part) {
@ -178,15 +174,15 @@ namespace Orchard.Autoroute.Services {
return true;
}
private bool IsHomePage(IContent content) {
var homePageRoute = _aliasService.Get("");
var homePageId = homePageRoute.ContainsKey("id") ? XmlHelper.Parse<int>((string)homePageRoute["id"]) : default(int?);
return content.Id == homePageId;
}
//private bool IsHomePage(IContent content) {
// var homePageRoute = _aliasService.Get("");
// var homePageId = homePageRoute.ContainsKey("id") ? XmlHelper.Parse<int>((string)homePageRoute["id"]) : default(int?);
// return content.Id == homePageId;
//}
private int GetHomePageAliasRecordId() {
return _aliasStorage.List(x => x.Path == "").First().Item5;
}
//private int GetHomePageAliasRecordId() {
// return _aliasStorage.List(x => x.Path == "").First().Item5;
//}
private SettingsDictionary GetTypePartSettings(string contentType) {
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);