Files
Umbraco-CMS/src/Umbraco.Core/Models/Mapping/CommonMapper.cs

68 lines
2.6 KiB
C#
Raw Normal View History

2019-03-26 18:47:35 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
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
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;
public CommonMapper(IUserService userService, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
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
{
var contentType = _contentTypeBaseServiceProvider.GetContentTypeOf(source);
var contentTypeBasic = context.Map<IContentTypeComposition, ContentTypeBasic>(contentType);
return contentTypeBasic;
2019-03-26 18:47:35 +01: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
}