mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Minor code formatting + cleanup.
This commit is contained in:
@@ -124,7 +124,7 @@ namespace Orchard.Alias.Implementation {
|
||||
return _aliasStorage.List(sourceStartsWith).Select(item => Tuple.Create(item.Item1, item.Item3.ToRouteValueDictionary(), item.Item4));
|
||||
}
|
||||
|
||||
public IEnumerable<VirtualPathData> LookupVirtualPaths(RouteValueDictionary routeValues,HttpContextBase httpContext) {
|
||||
public IEnumerable<VirtualPathData> LookupVirtualPaths(RouteValueDictionary routeValues, HttpContextBase httpContext) {
|
||||
return Utils.LookupVirtualPaths(httpContext, _routeDescriptors.Value, routeValues);
|
||||
}
|
||||
|
||||
|
@@ -106,7 +106,7 @@ namespace Orchard.Alias.Implementation.Storage {
|
||||
|
||||
foreach (var aliasRecord in _aliasRepository.Fetch(filter)) {
|
||||
_aliasRepository.Delete(aliasRecord);
|
||||
// Bulk updates might go wrong if we don't flush
|
||||
// Bulk updates might go wrong if we don't flush.
|
||||
_aliasRepository.Flush();
|
||||
var dict = ToDictionary(aliasRecord);
|
||||
_aliasHolder.RemoveAlias(new AliasInfo() { Path = dict.Item1, Area = dict.Item2, RouteValues = dict.Item3 });
|
||||
@@ -114,7 +114,7 @@ namespace Orchard.Alias.Implementation.Storage {
|
||||
}
|
||||
|
||||
public IEnumerable<Tuple<string, string, IDictionary<string, string>, string, int>> List() {
|
||||
return List((Expression<Func<AliasRecord, bool>>) null);
|
||||
return List((Expression<Func<AliasRecord, bool>>)null);
|
||||
}
|
||||
|
||||
public IEnumerable<Tuple<string, string, IDictionary<string, string>, string, int>> List(Expression<Func<AliasRecord, bool>> predicate) {
|
||||
|
@@ -9,12 +9,10 @@ using Orchard.Autoroute.Settings;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.ContentManagement.MetaData.Models;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Tokens;
|
||||
|
||||
namespace Orchard.Autoroute.Services {
|
||||
public class AutorouteService : IAutorouteService {
|
||||
public class AutorouteService : Component, IAutorouteService {
|
||||
|
||||
private readonly IAliasService _aliasService;
|
||||
private readonly ITokenizer _tokenizer;
|
||||
@@ -31,39 +29,34 @@ namespace Orchard.Autoroute.Services {
|
||||
IContentManager contentManager,
|
||||
IRouteEvents routeEvents,
|
||||
IAliasStorage aliasStorage) {
|
||||
|
||||
_aliasService = aliasService;
|
||||
_tokenizer = tokenizer;
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
_contentManager = contentManager;
|
||||
_routeEvents = routeEvents;
|
||||
_aliasStorage = aliasStorage;
|
||||
|
||||
Logger = NullLogger.Instance;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public string GenerateAlias(AutoroutePart part) {
|
||||
|
||||
if (part == null) {
|
||||
throw new ArgumentNullException("part");
|
||||
}
|
||||
|
||||
string pattern = GetDefaultPattern(part.ContentItem.ContentType).Pattern;
|
||||
var pattern = GetDefaultPattern(part.ContentItem.ContentType).Pattern;
|
||||
|
||||
// String.Empty forces pattern based generation. "/" forces homepage
|
||||
if(part.UseCustomPattern
|
||||
// String.Empty forces pattern based generation. "/" forces homepage.
|
||||
if (part.UseCustomPattern
|
||||
&& (!String.IsNullOrWhiteSpace(part.CustomPattern) || String.Equals(part.CustomPattern, "/"))) {
|
||||
pattern = part.CustomPattern;
|
||||
}
|
||||
|
||||
// Convert the pattern and route values via tokens
|
||||
// Convert the pattern and route values via tokens.
|
||||
var path = _tokenizer.Replace(pattern, BuildTokenContext(part.ContentItem), new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
|
||||
|
||||
// removing trailing slashes in case the container is empty, and tokens are base on it (e.g. home page)
|
||||
while(path.StartsWith("/")) {
|
||||
// Removing trailing slashes in case the container is empty, and tokens are base on it (e.g. home page).
|
||||
while (path.StartsWith("/")) {
|
||||
path = path.Substring(1);
|
||||
}
|
||||
|
||||
@@ -72,9 +65,7 @@ namespace Orchard.Autoroute.Services {
|
||||
|
||||
public void PublishAlias(AutoroutePart part) {
|
||||
var displayRouteValues = _contentManager.GetItemMetadata(part).DisplayRouteValues;
|
||||
|
||||
_aliasService.Replace(part.DisplayAlias, displayRouteValues, AliasSource);
|
||||
|
||||
_routeEvents.Routed(part, part.DisplayAlias);
|
||||
}
|
||||
|
||||
@@ -101,7 +92,7 @@ namespace Orchard.Autoroute.Services {
|
||||
patterns.Add(routePattern);
|
||||
settings.Patterns = patterns;
|
||||
|
||||
// define which pattern is the default
|
||||
// Define which pattern is the default.
|
||||
if (makeDefault || settings.Patterns.Count == 1) {
|
||||
settings.DefaultPatternIndex = settings.Patterns.IndexOf(routePattern);
|
||||
}
|
||||
@@ -117,12 +108,12 @@ namespace Orchard.Autoroute.Services {
|
||||
public RoutePattern GetDefaultPattern(string contentType) {
|
||||
var settings = GetTypePartSettings(contentType).GetModel<AutorouteSettings>();
|
||||
|
||||
// return a default pattern if none is defined
|
||||
if(settings.DefaultPatternIndex < settings.Patterns.Count) {
|
||||
// Return a default pattern if none is defined.
|
||||
if (settings.DefaultPatternIndex < settings.Patterns.Count) {
|
||||
return settings.Patterns.ElementAt(settings.DefaultPatternIndex);
|
||||
}
|
||||
|
||||
return new RoutePattern {Name = "Title", Description = "my-title", Pattern = "{Content.Slug}"};
|
||||
return new RoutePattern { Name = "Title", Description = "my-title", Pattern = "{Content.Slug}" };
|
||||
}
|
||||
|
||||
public void RemoveAliases(AutoroutePart part) {
|
||||
@@ -146,16 +137,6 @@ namespace Orchard.Autoroute.Services {
|
||||
_aliasService.Delete(part.Path, AliasSource);
|
||||
}
|
||||
|
||||
private SettingsDictionary GetTypePartSettings(string contentType) {
|
||||
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
|
||||
|
||||
if (contentDefinition == null) {
|
||||
throw new OrchardException(T("Unknown content type: {0}", contentType));
|
||||
}
|
||||
|
||||
return contentDefinition.Parts.First(x => x.PartDefinition.Name == "AutoroutePart").Settings;
|
||||
}
|
||||
|
||||
public string GenerateUniqueSlug(AutoroutePart part, IEnumerable<string> existingPaths) {
|
||||
if (existingPaths == null || !existingPaths.Contains(part.Path))
|
||||
return part.Path;
|
||||
@@ -163,22 +144,10 @@ namespace Orchard.Autoroute.Services {
|
||||
int? version = existingPaths.Select(s => GetSlugVersion(part.Path, s)).OrderBy(i => i).LastOrDefault();
|
||||
|
||||
return version != null
|
||||
? string.Format("{0}-{1}", part.Path, version)
|
||||
? String.Format("{0}-{1}", part.Path, version)
|
||||
: part.Path;
|
||||
}
|
||||
|
||||
private static int? GetSlugVersion(string path, string potentialConflictingPath) {
|
||||
int v;
|
||||
string[] slugParts = potentialConflictingPath.Split(new[] { path }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (slugParts.Length == 0)
|
||||
return 2;
|
||||
|
||||
return int.TryParse(slugParts[0].TrimStart('-'), out v)
|
||||
? (int?)++v
|
||||
: null;
|
||||
}
|
||||
|
||||
public IEnumerable<AutoroutePart> GetSimilarPaths(string path) {
|
||||
return
|
||||
_contentManager.Query<AutoroutePart, AutoroutePartRecord>()
|
||||
@@ -191,11 +160,10 @@ namespace Orchard.Autoroute.Services {
|
||||
}
|
||||
|
||||
public bool ProcessPath(AutoroutePart part) {
|
||||
|
||||
var pathsLikeThis = GetSimilarPaths(part.Path).ToArray();
|
||||
|
||||
// Don't include *this* part in the list
|
||||
// of slugs to consider for conflict detection
|
||||
// of slugs to consider for conflict detection.
|
||||
pathsLikeThis = pathsLikeThis.Where(p => p.ContentItem.Id != part.ContentItem.Id).ToArray();
|
||||
|
||||
if (pathsLikeThis.Any()) {
|
||||
@@ -219,5 +187,27 @@ namespace Orchard.Autoroute.Services {
|
||||
private int GetHomePageAliasRecordId() {
|
||||
return _aliasStorage.List(x => x.Path == "").First().Item5;
|
||||
}
|
||||
|
||||
private SettingsDictionary GetTypePartSettings(string contentType) {
|
||||
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
|
||||
|
||||
if (contentDefinition == null) {
|
||||
throw new OrchardException(T("Unknown content type: {0}", contentType));
|
||||
}
|
||||
|
||||
return contentDefinition.Parts.First(x => x.PartDefinition.Name == "AutoroutePart").Settings;
|
||||
}
|
||||
|
||||
private static int? GetSlugVersion(string path, string potentialConflictingPath) {
|
||||
int v;
|
||||
var slugParts = potentialConflictingPath.Split(new[] { path }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (slugParts.Length == 0)
|
||||
return 2;
|
||||
|
||||
return Int32.TryParse(slugParts[0].TrimStart('-'), out v)
|
||||
? (int?)++v
|
||||
: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,7 @@
|
||||
@model Orchard.Autoroute.ViewModels.AutoroutePartEditViewModel
|
||||
@using Orchard.Autoroute
|
||||
@using Orchard.Mvc.Extensions
|
||||
@using Orchard.Utility.Extensions;
|
||||
@using Orchard.Autoroute
|
||||
@using Orchard.Environment.Configuration
|
||||
@using Orchard.Mvc.Extensions
|
||||
@model Orchard.Autoroute.ViewModels.AutoroutePartEditViewModel
|
||||
|
||||
@if(Model.Settings.DefaultPatternIndex == -1) {
|
||||
<div class="message message-Error">@T("The current Content Type does not have a default Autoroute Pattern. Please edit the settings first.")</div>
|
||||
|
Reference in New Issue
Block a user