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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Orchard.Alias;
using Orchard.Autoroute.Models; using Orchard.Autoroute.Models;
using Orchard.Autoroute.Services; using Orchard.Autoroute.Services;
using Orchard.Autoroute.Settings;
using Orchard.Autoroute.ViewModels;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers; using Orchard.ContentManagement.Drivers;
using Orchard.Autoroute.ViewModels; using Orchard.ContentManagement.Handlers;
using Orchard.Autoroute.Settings;
using Orchard.Localization; using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Notify; using Orchard.UI.Notify;
using Orchard.Utility.Extensions;
namespace Orchard.Autoroute.Drivers { namespace Orchard.Autoroute.Drivers {
public class AutoroutePartDriver : ContentPartDriver<AutoroutePart> { public class AutoroutePartDriver : ContentPartDriver<AutoroutePart> {
private readonly IAliasService _aliasService;
private readonly IContentManager _contentManager;
private readonly IAutorouteService _autorouteService; private readonly IAutorouteService _autorouteService;
private readonly IAuthorizer _authorizer;
private readonly INotifier _notifier; private readonly INotifier _notifier;
private readonly IHomeAliasService _homeAliasService;
public AutoroutePartDriver( public AutoroutePartDriver(
IAliasService aliasService,
IContentManager contentManager,
IAutorouteService autorouteService, IAutorouteService autorouteService,
IAuthorizer authorizer, INotifier notifier,
INotifier notifier) { IHomeAliasService homeAliasService) {
_aliasService = aliasService;
_contentManager = contentManager;
_autorouteService = autorouteService; _autorouteService = autorouteService;
_authorizer = authorizer;
_notifier = notifier; _notifier = notifier;
_homeAliasService = homeAliasService;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
} }
@@ -64,15 +57,14 @@ namespace Orchard.Autoroute.Drivers {
}; };
// Retrieve home page. // Retrieve home page.
//var homepage = _aliasService.Get(string.Empty); var homePageId = _homeAliasService.GetHomePageId();
var displayRouteValues = _contentManager.GetItemMetadata(part).DisplayRouteValues; var isHomePage = part.Id == homePageId;
//viewModel.IsHomePage = homepage.Match(displayRouteValues); viewModel.IsHomePage = isHomePage;
//viewModel.PromoteToHomePage = viewModel.IsHomePage || part.DisplayAlias == "/"; viewModel.PromoteToHomePage = part.IsHomePage;
if (settings.PerItemConfiguration) { if (settings.PerItemConfiguration) {
// If enabled, the list of all available patterns is displayed, and the user can select which one to use. // If enabled, the list of all available patterns is displayed, and the user can select which one to use.
// todo: later // 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. // Reset the alias if we need to force regeneration, and the user didn't provide a custom one.
if(settings.AutomaticAdjustmentOnEdit && previous == part.DisplayAlias) { if(settings.AutomaticAdjustmentOnEdit && previous == part.DisplayAlias) {
part.DisplayAlias = string.Empty; part.DisplayAlias = String.Empty;
} }
if (!_autorouteService.IsPathValid(part.DisplayAlias)) { 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).")); 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. // 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;
//// If home page is requested, use "/" to have the handler create a homepage alias.
//if(viewModel.PromoteToHomePage) {
// part.DisplayAlias = "/";
//}
} }
return ContentShape("Parts_Autoroute_Edit", return ContentShape("Parts_Autoroute_Edit",
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Autoroute.Edit", Model: viewModel, Prefix: Prefix)); () => 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"); var displayAlias = context.Attribute(part.PartDefinition.Name, "Alias");
if (displayAlias != null) { if (displayAlias != null) {
part.DisplayAlias = displayAlias; 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("Alias", part.Record.DisplayAlias);
context.Element(part.PartDefinition.Name).SetAttributeValue("CustomPattern", part.Record.CustomPattern); context.Element(part.PartDefinition.Name).SetAttributeValue("CustomPattern", part.Record.CustomPattern);
context.Element(part.PartDefinition.Name).SetAttributeValue("UseCustomPattern", part.Record.UseCustomPattern); context.Element(part.PartDefinition.Name).SetAttributeValue("UseCustomPattern", part.Record.UseCustomPattern);

View File

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

View File

@@ -17,6 +17,11 @@ namespace Orchard.Autoroute.Models {
set { Store(x => x.DisplayAlias, value); } 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 { public string Path {
get { return DisplayAlias; } get { return DisplayAlias; }
} }

View File

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

View File

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