2019-03-26 18:47:35 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.ContentApps;
|
|
|
|
|
|
using Umbraco.Cms.Core.Mapping;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.Entities;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
|
using UserProfile = Umbraco.Cms.Core.Models.ContentEditing.UserProfile;
|
2019-03-26 18:47:35 +01:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Core.Models.Mapping
|
2019-03-26 18:47:35 +01:00
|
|
|
|
{
|
2020-02-17 09:15:48 +01:00
|
|
|
|
public class CommonMapper
|
2019-03-26 18:47:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
private readonly IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
|
|
|
|
|
|
private readonly ContentAppFactoryCollection _contentAppDefinitions;
|
|
|
|
|
|
private readonly ILocalizedTextService _localizedTextService;
|
|
|
|
|
|
|
2020-11-17 20:27:10 +01:00
|
|
|
|
public CommonMapper(IUserService userService, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
|
2020-02-26 10:33:05 +01:00
|
|
|
|
ContentAppFactoryCollection contentAppDefinitions, ILocalizedTextService localizedTextService)
|
2019-03-26 18:47:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
|
|
|
|
|
|
_contentAppDefinitions = contentAppDefinitions;
|
|
|
|
|
|
_localizedTextService = localizedTextService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-07 11:26:47 +02:00
|
|
|
|
public UserProfile GetOwner(IContentBase source, MapperContext context)
|
2019-03-26 18:47:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
var profile = source.GetCreatorProfile(_userService);
|
2019-04-07 11:26:47 +02:00
|
|
|
|
return profile == null ? null : context.Map<IProfile, UserProfile>(profile);
|
2019-03-26 18:47:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-07 11:26:47 +02:00
|
|
|
|
public UserProfile GetCreator(IContent source, MapperContext context)
|
2019-03-26 18:47:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
var profile = source.GetWriterProfile(_userService);
|
2019-04-07 11:26:47 +02:00
|
|
|
|
return profile == null ? null : context.Map<IProfile, UserProfile>(profile);
|
2019-03-26 18:47:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-07 11:26:47 +02:00
|
|
|
|
public ContentTypeBasic GetContentType(IContentBase source, MapperContext context)
|
2019-03-26 18:47:35 +01:00
|
|
|
|
{
|
2020-08-12 12:04:19 +01:00
|
|
|
|
var contentType = _contentTypeBaseServiceProvider.GetContentTypeOf(source);
|
|
|
|
|
|
var contentTypeBasic = context.Map<IContentTypeComposition, ContentTypeBasic>(contentType);
|
|
|
|
|
|
return contentTypeBasic;
|
2019-03-26 18:47:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-19 19:10:16 +02:00
|
|
|
|
public IEnumerable<ContentApp> GetContentApps(IUmbracoEntity source)
|
2019-03-26 18:47:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
var apps = _contentAppDefinitions.GetContentAppsFor(source).ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
// localize content app names
|
|
|
|
|
|
foreach (var app in apps)
|
|
|
|
|
|
{
|
|
|
|
|
|
var localizedAppName = _localizedTextService.Localize($"apps/{app.Alias}");
|
|
|
|
|
|
if (localizedAppName.Equals($"[{app.Alias}]", StringComparison.OrdinalIgnoreCase) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
app.Name = localizedAppName;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return apps;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-04-07 11:26:47 +02:00
|
|
|
|
}
|