Skip to content

Commit

Permalink
Added manager init for fields. Fixes #1144
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed May 5, 2020
1 parent 542463f commit e911a33
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 25 deletions.
4 changes: 4 additions & 0 deletions core/Piranha.Manager.Core/Services/PageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ public async Task<PageEditModel> GetById(Guid id, bool useDraft = true)

if (page != null)
{
// Perform manager init
await _factory.InitDynamicManagerAsync(page,
App.PageTypes.GetById(page.TypeId));

var model = Transform(page, isDraft);

model.PendingCommentCount = (await _api.Pages.GetAllPendingCommentsAsync(id))
Expand Down
4 changes: 4 additions & 0 deletions core/Piranha.Manager.Core/Services/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public async Task<PostEditModel> GetById(Guid id, bool useDraft = true)

if (post != null)
{
// Perform manager init
await _factory.InitDynamicManagerAsync(post,
App.PostTypes.GetById(post.TypeId));

var postModel = Transform(post, isDraft);

postModel.Categories = (await _api.Posts.GetAllCategoriesAsync(post.BlogId))
Expand Down
4 changes: 4 additions & 0 deletions core/Piranha.Manager.Core/Services/SiteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public async Task<SiteContentEditModel> GetContentById(Guid id)

if (site != null)
{
// Perform manager init
await _factory.InitDynamicManagerAsync(site,
App.SiteTypes.GetById(site.TypeId));

return Transform(site);
}
return null;
Expand Down
114 changes: 89 additions & 25 deletions core/Piranha/Services/ContentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Piranha.Extend;
Expand Down Expand Up @@ -93,7 +94,7 @@ public async Task<object> CreateBlockAsync(string typeName)
if (typeof(Extend.IField).IsAssignableFrom(prop.PropertyType))
{
var field = Activator.CreateInstance(prop.PropertyType);
await InitFieldAsync(scope, field).ConfigureAwait(false);
await InitFieldAsync(scope, field, false).ConfigureAwait(false);
prop.SetValue(block, field);
}
}
Expand Down Expand Up @@ -213,7 +214,32 @@ private async Task<T> CreateModelAsync<T>(ContentTypeBase type) where T : Conten
/// <param name="type">The content type</param>
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
public async Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type) where T : IDynamicContent
public Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type) where T : IDynamicContent
{
return InitDynamicAsync<T>(model, type, false);
}

/// <summary>
/// Initializes the given dynamic model for the manager.
/// </summary>
/// <param name="model">The model</param>
/// <param name="type">The content type</param>
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
public Task<T> InitDynamicManagerAsync<T>(T model, ContentTypeBase type) where T : IDynamicContent
{
return InitDynamicAsync<T>(model, type, true);
}

/// <summary>
/// Initializes the given dynamic model.
/// </summary>
/// <param name="model">The model</param>
/// <param name="type">The content type</param>
/// <param name="managerInit">If this is initialization used by the manager</param>
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
private async Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type, bool managerInit) where T : IDynamicContent
{
using (var scope = _services.CreateScope())
{
Expand All @@ -225,14 +251,14 @@ public async Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type) where T
if (!regionType.Collection)
{
// Initialize it
await InitDynamicRegionAsync(scope, region, regionType).ConfigureAwait(false);
await InitDynamicRegionAsync(scope, region, regionType, managerInit).ConfigureAwait(false);
}
else
{
// This region was a collection. Initialize all items
foreach (var item in (IList)region)
{
await InitDynamicRegionAsync(scope, item, regionType).ConfigureAwait(false);
await InitDynamicRegionAsync(scope, item, regionType, managerInit).ConfigureAwait(false);
}
}
}
Expand All @@ -242,13 +268,13 @@ public async Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type) where T
{
foreach (var block in blockModel.Blocks)
{
await InitBlockAsync(scope, block).ConfigureAwait(false);
await InitBlockAsync(scope, block, managerInit).ConfigureAwait(false);

if (block is BlockGroup blockGroup)
{
foreach (var child in blockGroup.Items)
{
await InitBlockAsync(scope, child).ConfigureAwait(false);
await InitBlockAsync(scope, child, managerInit).ConfigureAwait(false);
}
}
}
Expand All @@ -264,7 +290,32 @@ public async Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type) where T
/// <param name="type">The content type</param>
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
public async Task<T> InitAsync<T>(T model, ContentTypeBase type) where T : ContentBase
public Task<T> InitAsync<T>(T model, ContentTypeBase type) where T : ContentBase
{
return InitAsync<T>(model, type, false);
}

/// <summary>
/// Initializes the given model for the manager.
/// </summary>
/// <param name="model">The model</param>
/// <param name="type">The content type</param>
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
public Task<T> InitManagerAsync<T>(T model, ContentTypeBase type) where T : ContentBase
{
return InitAsync<T>(model, type, true);
}

/// <summary>
/// Initializes the given model.
/// </summary>
/// <param name="model">The model</param>
/// <param name="type">The content type</param>
/// <param name="managerInit">If this is initialization used by the manager</param>
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
private async Task<T> InitAsync<T>(T model, ContentTypeBase type, bool managerInit) where T : ContentBase
{
if (model is IDynamicContent)
{
Expand All @@ -283,14 +334,14 @@ public async Task<T> InitAsync<T>(T model, ContentTypeBase type) where T : Conte
if (!regionType.Collection)
{
// Initialize it
await InitRegionAsync(scope, region, regionType).ConfigureAwait(false);
await InitRegionAsync(scope, region, regionType, managerInit).ConfigureAwait(false);
}
else
{
// This region was a collection. Initialize all items
foreach (var item in (IList)region)
{
await InitRegionAsync(scope, item, regionType).ConfigureAwait(false);
await InitRegionAsync(scope, item, regionType, managerInit).ConfigureAwait(false);
}
}
}
Expand All @@ -300,13 +351,13 @@ public async Task<T> InitAsync<T>(T model, ContentTypeBase type) where T : Conte
{
foreach (var block in blockModel.Blocks)
{
await InitBlockAsync(scope, block).ConfigureAwait(false);
await InitBlockAsync(scope, block, managerInit).ConfigureAwait(false);

if (block is Extend.BlockGroup)
{
foreach (var child in ((Extend.BlockGroup)block).Items)
{
await InitBlockAsync(scope, child).ConfigureAwait(false);
await InitBlockAsync(scope, child, managerInit).ConfigureAwait(false);
}
}
}
Expand All @@ -321,15 +372,16 @@ public async Task<T> InitAsync<T>(T model, ContentTypeBase type) where T : Conte
/// <param name="scope">The current service scope</param>
/// <param name="region">The region</param>
/// <param name="regionType">The region type</param>
private async Task InitDynamicRegionAsync(IServiceScope scope, object region, RegionType regionType)
/// <param name="managerInit">If this is initialization used by the manager</param>
private async Task InitDynamicRegionAsync(IServiceScope scope, object region, RegionType regionType, bool managerInit)
{
if (region != null)
{
if (regionType.Fields.Count == 1)
{
// This region only has one field, that means
// the region is in fact a field.
await InitFieldAsync(scope, region).ConfigureAwait(false);
await InitFieldAsync(scope, region, managerInit).ConfigureAwait(false);
}
else
{
Expand All @@ -338,7 +390,7 @@ private async Task InitDynamicRegionAsync(IServiceScope scope, object region, Re
{
if (((IDictionary<string, object>)region).TryGetValue(fieldType.Id, out var field))
{
await InitFieldAsync(scope, field).ConfigureAwait(false);
await InitFieldAsync(scope, field, managerInit).ConfigureAwait(false);
}
}
}
Expand All @@ -351,15 +403,16 @@ private async Task InitDynamicRegionAsync(IServiceScope scope, object region, Re
/// <param name="scope">The current service scope</param>
/// <param name="region">The region</param>
/// <param name="regionType">The region type</param>
private async Task InitRegionAsync(IServiceScope scope, object region, RegionType regionType)
/// <param name="managerInit">If this is initialization used by the manager</param>
private async Task InitRegionAsync(IServiceScope scope, object region, RegionType regionType, bool managerInit)
{
if (region != null)
{
if (regionType.Fields.Count == 1)
{
// This region only has one field, that means
// the region is in fact a field.
await InitFieldAsync(scope, region).ConfigureAwait(false);
await InitFieldAsync(scope, region, managerInit).ConfigureAwait(false);
}
else
{
Expand All @@ -372,7 +425,7 @@ private async Task InitRegionAsync(IServiceScope scope, object region, RegionTyp

if (field != null)
{
await InitFieldAsync(scope, field).ConfigureAwait(false);
await InitFieldAsync(scope, field, managerInit).ConfigureAwait(false);
}
}
}
Expand All @@ -384,7 +437,8 @@ private async Task InitRegionAsync(IServiceScope scope, object region, RegionTyp
/// </summary>
/// <param name="scope">The current service scope</param>
/// <param name="block">The block</param>
private async Task InitBlockAsync(IServiceScope scope, Extend.Block block)
/// <param name="managerInit">If this is initialization used by the manager</param>
private async Task InitBlockAsync(IServiceScope scope, Extend.Block block, bool managerInit)
{
if (block != null)
{
Expand All @@ -399,7 +453,7 @@ private async Task InitBlockAsync(IServiceScope scope, Extend.Block block)

if (field != null)
{
await InitFieldAsync(scope, field).ConfigureAwait(false);
await InitFieldAsync(scope, field, managerInit).ConfigureAwait(false);
}
}
}
Expand All @@ -422,7 +476,7 @@ private async Task<object> CreateDynamicRegionAsync(IServiceScope scope, RegionT
{
if (initFields)
{
await InitFieldAsync(scope, field).ConfigureAwait(false);
await InitFieldAsync(scope, field, false).ConfigureAwait(false);
}
return field;
}
Expand All @@ -438,7 +492,7 @@ private async Task<object> CreateDynamicRegionAsync(IServiceScope scope, RegionT
{
if (initFields)
{
await InitFieldAsync(scope, field).ConfigureAwait(false);
await InitFieldAsync(scope, field, false).ConfigureAwait(false);
}
((IDictionary<string, object>)reg).Add(fieldType.Id, field);
}
Expand Down Expand Up @@ -466,7 +520,7 @@ private async Task<object> CreateRegionAsync(IServiceScope scope, object model,
{
if (initFields)
{
await InitFieldAsync(scope, field).ConfigureAwait(false);
await InitFieldAsync(scope, field, false).ConfigureAwait(false);
}
return field;
}
Expand All @@ -486,7 +540,7 @@ private async Task<object> CreateRegionAsync(IServiceScope scope, object model,
{
if (initFields)
{
await InitFieldAsync(scope, field).ConfigureAwait(false);
await InitFieldAsync(scope, field, false).ConfigureAwait(false);
}
reg.GetType().SetPropertyValue(fieldType.Id, reg, field);
}
Expand Down Expand Up @@ -518,10 +572,20 @@ protected object CreateField(FieldType fieldType)
/// </summary>
/// <param name="scope">The current service scope</param>
/// <param name="field">The field</param>
/// <param name="managerInit">If this is initialization used by the manager</param>
/// <returns>The initialized field</returns>
protected async Task<object> InitFieldAsync(IServiceScope scope, object field)
protected async Task<object> InitFieldAsync(IServiceScope scope, object field, bool managerInit)
{
var init = field.GetType().GetMethod("Init");
MethodInfo init = null;

if (!managerInit)
{
init = field.GetType().GetMethod("Init");
}
else
{
init = field.GetType().GetMethod("InitManager");
}

if (init != null)
{
Expand Down
18 changes: 18 additions & 0 deletions core/Piranha/Services/IContentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public interface IContentFactory
/// <returns>The initialized model</returns>
Task<T> InitAsync<T>(T model, ContentTypeBase type) where T : ContentBase;

/// <summary>
/// Initializes the given model for the manager.
/// </summary>
/// <param name="model">The model</param>
/// <param name="type">The content type</param>
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
Task<T> InitManagerAsync<T>(T model, ContentTypeBase type) where T : ContentBase;

/// <summary>
/// Initializes the given dynamic model.
/// </summary>
Expand All @@ -55,5 +64,14 @@ public interface IContentFactory
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type) where T : IDynamicContent;

/// <summary>
/// Initializes the given dynamic model.
/// </summary>
/// <param name="model">The model</param>
/// <param name="type">The content type</param>
/// <typeparam name="T">The model type</typeparam>
/// <returns>The initialized model</returns>
Task<T> InitDynamicManagerAsync<T>(T model, ContentTypeBase type) where T : IDynamicContent;
}
}

0 comments on commit e911a33

Please sign in to comment.