Skip to content

Commit

Permalink
feature: element type organiser
Browse files Browse the repository at this point in the history
  • Loading branch information
jcdcdev committed Aug 9, 2024
1 parent 6e1da6b commit e01edf9
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,19 @@

namespace Umbraco.Community.BackOfficeOrganiser.Composing;

public class BackofficeOrganiserNotificationHandler :
INotificationHandler<DataTypeSavedNotification>,
INotificationHandler<ContentTypeSavedNotification>,
INotificationHandler<MemberTypeSavedNotification>,
INotificationHandler<MediaTypeSavedNotification>
public class BackofficeOrganiserNotificationHandler(
DataTypeOrganiser dataTypeOrganiser,
ContentTypeOrganiser contentTypeOrganiser,
MediaTypeOrganiser mediaTypeOrganiser,
MemberTypeOrganiser memberTypeOrganiser,
IOptions<BackOfficeOrganiserOptions> options)
:
INotificationHandler<DataTypeSavedNotification>,
INotificationHandler<ContentTypeSavedNotification>,
INotificationHandler<MemberTypeSavedNotification>,
INotificationHandler<MediaTypeSavedNotification>
{
private readonly ContentTypeOrganiser _contentTypeOrganiser;
private readonly DataTypeOrganiser _dataTypeOrganiser;
private readonly MediaTypeOrganiser _mediaTypeOrganiser;
private readonly MemberTypeOrganiser _memberTypeOrganiser;
private readonly BackOfficeOrganiserOptions _options;

public BackofficeOrganiserNotificationHandler(
DataTypeOrganiser dataTypeOrganiser,
ContentTypeOrganiser contentTypeOrganiser,
MediaTypeOrganiser mediaTypeOrganiser,
MemberTypeOrganiser memberTypeOrganiser,
IOptions<BackOfficeOrganiserOptions> options)
{
_dataTypeOrganiser = dataTypeOrganiser;
_contentTypeOrganiser = contentTypeOrganiser;
_mediaTypeOrganiser = mediaTypeOrganiser;
_memberTypeOrganiser = memberTypeOrganiser;
_options = options.Value;
}
private readonly BackOfficeOrganiserOptions _options = options.Value;

public void Handle(ContentTypeSavedNotification notification)
{
Expand All @@ -44,7 +32,7 @@ public void Handle(ContentTypeSavedNotification notification)

foreach (var item in notification.SavedEntities)
{
_contentTypeOrganiser.Organise(item);
contentTypeOrganiser.Organise(item);
}
}

Expand All @@ -57,7 +45,7 @@ public void Handle(DataTypeSavedNotification notification)

foreach (var dataType in notification.SavedEntities)
{
_dataTypeOrganiser.Organise(dataType);
dataTypeOrganiser.Organise(dataType);
}
}

Expand All @@ -70,7 +58,7 @@ public void Handle(MediaTypeSavedNotification notification)

foreach (var item in notification.SavedEntities)
{
_mediaTypeOrganiser.Organise(item);
mediaTypeOrganiser.Organise(item);
}
}

Expand All @@ -83,7 +71,7 @@ public void Handle(MemberTypeSavedNotification notification)

foreach (var item in notification.SavedEntities)
{
_memberTypeOrganiser.Organise(item);
memberTypeOrganiser.Organise(item);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void Compose(IUmbracoBuilder builder)
builder.ManifestFilters().Append<ManifestFilter>();

builder.DataTypeOrganiseActions().Append<DefaultDataTypeOrganiseAction>();
builder.ContentTypeOrganiseActions().Append<ElementTypeOrganiser>();
builder.ContentTypeOrganiseActions().Append<DefaultContentTypeOrganiseAction>();
builder.MediaTypeOrganiseActions().Append<DefaultMediaTypeOrganiseAction>();
builder.MemberTypeOrganiseActions().Append<DefaultMemberTypeOrganiseAction>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@ namespace Umbraco.Community.BackOfficeOrganiser.Controllers;
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
[DisableBrowserCache]
[UmbracoRequireHttps]
public class BackOfficeOrganiserController : UmbracoApiController
public class BackOfficeOrganiserController(IBackOfficeOrganiserService service) : UmbracoApiController
{
private readonly IBackOfficeOrganiserService _service;

public BackOfficeOrganiserController(IBackOfficeOrganiserService service)
{
_service = service;
}

[HttpPost]
public IActionResult Organise(OrganiseRequest model)
{
var success = true;
foreach (var type in model.Types.Select(DetermineOrganiseType).Distinct())
{
var attempt = _service.Organise(type);
var attempt = service.Organise(type);
if (!attempt.Success)
{
success = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace Umbraco.Community.BackOfficeOrganiser.Organisers;

public abstract class BackOfficeOrganiserBase<T> : IBackOfficeOrganiser<T>
public abstract class BackOfficeOrganiserBase<T>(ILogger logger) : IBackOfficeOrganiser<T>
{
public readonly ILogger Logger;

protected BackOfficeOrganiserBase(ILogger logger)
{
Logger = logger;
}
public readonly ILogger Logger = logger;

protected virtual void PostOrganiseAll()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@

namespace Umbraco.Community.BackOfficeOrganiser.Organisers.ContentTypes;

public class ContentTypeOrganiseActionCollection : BuilderCollectionBase<IContentTypeOrganiseAction>
{
public ContentTypeOrganiseActionCollection(Func<IEnumerable<IContentTypeOrganiseAction>> items)
: base(items)
{
}
}
public class ContentTypeOrganiseActionCollection(Func<IEnumerable<IContentTypeOrganiseAction>> items) : BuilderCollectionBase<IContentTypeOrganiseAction>(items);
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,22 @@

namespace Umbraco.Community.BackOfficeOrganiser.Organisers.ContentTypes;

public class ContentTypeOrganiser : BackOfficeOrganiserBase<IContentType>
public class ContentTypeOrganiser(
ILogger<ContentTypeOrganiser> logger,
IContentTypeService contentTypeService,
ContentTypeOrganiseActionCollection organiseActions)
: BackOfficeOrganiserBase<IContentType>(logger)
{
private readonly IContentTypeService _contentTypeService;
private readonly ContentTypeOrganiseActionCollection _organiseActions;

public ContentTypeOrganiser(
ILogger<ContentTypeOrganiser> logger,
IContentTypeService contentTypeService,
ContentTypeOrganiseActionCollection organiseActions) : base(logger)
{
_contentTypeService = contentTypeService;
_organiseActions = organiseActions;
}

protected override List<IContentType> GetAll() => _contentTypeService.GetAll().ToList();
protected override List<IContentType> GetAll() => contentTypeService.GetAll().ToList();

public override void Organise(IContentType contentType)
{
var organiser = _organiseActions.FirstOrDefault(x => x.CanMove(contentType, _contentTypeService));
organiser?.Move(contentType, _contentTypeService);
var organiser = organiseActions.FirstOrDefault(x => x.CanMove(contentType, contentTypeService));
organiser?.Move(contentType, contentTypeService);
}

protected override void PostOrganiseAll()
{
_contentTypeService.DeleteAllEmptyContainers();
contentTypeService.DeleteAllEmptyContainers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using jcdcdev.Umbraco.Core.Extensions;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;

namespace Umbraco.Community.BackOfficeOrganiser.Organisers.ContentTypes;

public class ElementTypeOrganiser(IDataTypeService dataTypeService) : IContentTypeOrganiseAction
{
public bool CanMove(IContentType contentType, IContentTypeService contentTypeService) => contentType.IsElement;

public void Move(IContentType contentType, IContentTypeService contentTypeService)
{
var nestedContentDataTypes = dataTypeService.GetByEditorAlias(Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.NestedContent).Select(x => x.ConfigurationAs<NestedContentConfiguration>());

Check warning on line 15 in src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ElementTypeOrganiser.cs

View workflow job for this annotation

GitHub Actions / build

'NestedContentConfiguration' is obsolete: 'Nested content is obsolete, will be removed in V13'

Check warning on line 15 in src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ElementTypeOrganiser.cs

View workflow job for this annotation

GitHub Actions / build

'NestedContentConfiguration' is obsolete: 'Nested content is obsolete, will be removed in V13'

Check warning on line 15 in src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ElementTypeOrganiser.cs

View workflow job for this annotation

GitHub Actions / release

'NestedContentConfiguration' is obsolete: 'Nested content is obsolete, will be removed in V13'

Check warning on line 15 in src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ElementTypeOrganiser.cs

View workflow job for this annotation

GitHub Actions / release

'NestedContentConfiguration' is obsolete: 'Nested content is obsolete, will be removed in V13'

Check warning on line 15 in src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ElementTypeOrganiser.cs

View workflow job for this annotation

GitHub Actions / release

'NestedContentConfiguration' is obsolete: 'Nested content is obsolete, will be removed in V13'

Check warning on line 15 in src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ElementTypeOrganiser.cs

View workflow job for this annotation

GitHub Actions / release

'NestedContentConfiguration' is obsolete: 'Nested content is obsolete, will be removed in V13'

Check warning on line 15 in src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ElementTypeOrganiser.cs

View workflow job for this annotation

GitHub Actions / release

'NestedContentConfiguration' is obsolete: 'Nested content is obsolete, will be removed in V13'

Check warning on line 15 in src/Umbraco.Community.BackOfficeOrganiser/Organisers/ContentTypes/ElementTypeOrganiser.cs

View workflow job for this annotation

GitHub Actions / release

'NestedContentConfiguration' is obsolete: 'Nested content is obsolete, will be removed in V13'
var blockGridDataTypes = dataTypeService.GetByEditorAlias(Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockGrid).Select(x => x.ConfigurationAs<BlockGridConfiguration>());
var blockListDataTypes = dataTypeService.GetByEditorAlias(Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockList).Select(x => x.ConfigurationAs<BlockListConfiguration>());

var nestedContentContentTypeAliases = new List<string>();
var gridContentTypeKeys = new List<Guid>();
var blockContentTypeKeys = new List<Guid>();
foreach (var blockGridDataType in blockGridDataTypes)
{
foreach (var blockGrid in blockGridDataType?.Blocks ?? [])
{
gridContentTypeKeys.Add(blockGrid.ContentElementTypeKey);
if (blockGrid.SettingsElementTypeKey.HasValue)
{
gridContentTypeKeys.Add(blockGrid.SettingsElementTypeKey.Value);
}
}
}

foreach (var blockListDataType in blockListDataTypes)
{
foreach (var blockList in blockListDataType?.Blocks ?? [])
{
blockContentTypeKeys.Add(blockList.ContentElementTypeKey);
if (blockList.SettingsElementTypeKey.HasValue)
{
blockContentTypeKeys.Add(blockList.SettingsElementTypeKey.Value);
}
}
}

foreach (var nestedContentDataType in nestedContentDataTypes)
{
if (nestedContentDataType == null)
{
continue;
}

var aliases = nestedContentDataType.ContentTypes?.Select(x => x.Alias).WhereNotNull() ?? [];
nestedContentContentTypeAliases.AddRange(aliases);
}

var isNestedContent = nestedContentContentTypeAliases.Contains(contentType.Alias);
var isBlockGrid = gridContentTypeKeys.Contains(contentType.Key);
var isBlockList = blockContentTypeKeys.Contains(contentType.Key);

var parent = contentTypeService.GetOrCreateFolder("Element Types");
var folderName = string.Empty;
if (isNestedContent && !isBlockGrid && !isBlockList)
{
folderName = "Nested Content";
}
else if (isBlockGrid && !isNestedContent && !isBlockList)
{
folderName = "Block Grid";
}
else if (isBlockList && !isNestedContent && !isBlockGrid)
{
folderName = "Block List";
}
if (!string.IsNullOrWhiteSpace(folderName))
{
parent = contentTypeService.GetOrCreateFolder(folderName, parent.Id);
}

contentTypeService.Move(contentType, parent.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@

namespace Umbraco.Community.BackOfficeOrganiser.Organisers.DataTypes;

public class DataTypeOrganiseActionCollection : BuilderCollectionBase<IDataTypeOrganiseAction>
{
public DataTypeOrganiseActionCollection(Func<IEnumerable<IDataTypeOrganiseAction>> items)
: base(items)
{
}
}
public class DataTypeOrganiseActionCollection(Func<IEnumerable<IDataTypeOrganiseAction>> items) : BuilderCollectionBase<IDataTypeOrganiseAction>(items);
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,22 @@

namespace Umbraco.Community.BackOfficeOrganiser.Organisers.DataTypes;

public class DataTypeOrganiser : BackOfficeOrganiserBase<IDataType>
public class DataTypeOrganiser(
ILogger<DataTypeOrganiser> logger,
IDataTypeService dataTypeService,
DataTypeOrganiseActionCollection organiseActions)
: BackOfficeOrganiserBase<IDataType>(logger)
{
private readonly IDataTypeService _dataTypeService;
private readonly DataTypeOrganiseActionCollection _organiseActions;

public DataTypeOrganiser(
ILogger<DataTypeOrganiser> logger,
IDataTypeService dataTypeService,
DataTypeOrganiseActionCollection organiseActions) : base(logger)
{
_dataTypeService = dataTypeService;
_organiseActions = organiseActions;
}

public override void Organise(IDataType dataType)
{
var organiser = _organiseActions.FirstOrDefault(x => x.CanMove(dataType, _dataTypeService));
organiser?.Move(dataType, _dataTypeService);
var organiser = organiseActions.FirstOrDefault(x => x.CanMove(dataType, dataTypeService));
organiser?.Move(dataType, dataTypeService);
}

protected override List<IDataType> GetAll() => _dataTypeService.GetAll().ToList();
protected override List<IDataType> GetAll() => dataTypeService.GetAll().ToList();

protected override void PostOrganiseAll()
{
_dataTypeService.DeleteAllEmptyContainers();
dataTypeService.DeleteAllEmptyContainers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@

namespace Umbraco.Community.BackOfficeOrganiser.Organisers.DataTypes;

public class DefaultDataTypeOrganiseAction : IDataTypeOrganiseAction
public class DefaultDataTypeOrganiseAction(IOptions<BackOfficeOrganiserOptions> options, ILogger<DefaultDataTypeOrganiseAction> logger) : IDataTypeOrganiseAction
{
private readonly ILogger _logger;
private readonly BackOfficeOrganiserOptions _options;

public DefaultDataTypeOrganiseAction(IOptions<BackOfficeOrganiserOptions> options, ILogger<DefaultDataTypeOrganiseAction> logger)
{
_logger = logger;
_options = options.Value;
}
private readonly ILogger _logger = logger;
private readonly BackOfficeOrganiserOptions _options = options.Value;

public bool CanMove(IDataType dataType, IDataTypeService dataTypeService) => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@

namespace Umbraco.Community.BackOfficeOrganiser.Organisers.MediaTypes;

public class MediaTypeOrganiseActionCollection : BuilderCollectionBase<IMediaTypeOrganiseAction>
{
public MediaTypeOrganiseActionCollection(Func<IEnumerable<IMediaTypeOrganiseAction>> items)
: base(items)
{
}
}
public class MediaTypeOrganiseActionCollection(Func<IEnumerable<IMediaTypeOrganiseAction>> items) : BuilderCollectionBase<IMediaTypeOrganiseAction>(items);
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,22 @@

namespace Umbraco.Community.BackOfficeOrganiser.Organisers.MediaTypes;

public class MediaTypeOrganiser : BackOfficeOrganiserBase<IMediaType>
public class MediaTypeOrganiser(
ILogger<MediaTypeOrganiser> logger,
IMediaTypeService mediaTypeService,
MediaTypeOrganiseActionCollection organiseActions)
: BackOfficeOrganiserBase<IMediaType>(logger)
{
private readonly IMediaTypeService _mediaTypeService;
private readonly MediaTypeOrganiseActionCollection _organiseActions;

public MediaTypeOrganiser(
ILogger<MediaTypeOrganiser> logger,
IMediaTypeService mediaTypeService,
MediaTypeOrganiseActionCollection organiseActions) : base(logger)
{
_mediaTypeService = mediaTypeService;
_organiseActions = organiseActions;
}

public override void Organise(IMediaType mediaType)
{
var organiser = _organiseActions.FirstOrDefault(x => x.CanMove(mediaType, _mediaTypeService));
organiser?.Move(mediaType, _mediaTypeService);
var organiser = organiseActions.FirstOrDefault(x => x.CanMove(mediaType, mediaTypeService));
organiser?.Move(mediaType, mediaTypeService);
}

protected override List<IMediaType> GetAll() => _mediaTypeService.GetAll().ToList();
protected override List<IMediaType> GetAll() => mediaTypeService.GetAll().ToList();

protected override void PostOrganiseAll()
{
_mediaTypeService.DeleteAllEmptyContainers();
mediaTypeService.DeleteAllEmptyContainers();
}
}
Loading

0 comments on commit e01edf9

Please sign in to comment.