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

73 lines
2.9 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.Core;
using Umbraco.Core.Mapping;
using Umbraco.Core.Models;
using Umbraco.Core.Models.ContentEditing;
using Umbraco.Core.Models.Entities;
2019-03-26 18:47:35 +01:00
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;
using Umbraco.Web.ContentApps;
using Umbraco.Web.Models.ContentEditing;
2019-03-26 18:47:35 +01:00
using UserProfile = Umbraco.Web.Models.ContentEditing.UserProfile;
namespace Umbraco.Web.Models.Mapping
{
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;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
2019-03-26 18:47:35 +01:00
public CommonMapper(IUserService userService, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, IUmbracoContextAccessor umbracoContextAccessor,
ContentAppFactoryCollection contentAppDefinitions, ILocalizedTextService localizedTextService)
2019-03-26 18:47:35 +01:00
{
_userService = userService;
_contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
_contentAppDefinitions = contentAppDefinitions;
_localizedTextService = localizedTextService;
_umbracoContextAccessor = umbracoContextAccessor;
2019-03-26 18:47:35 +01:00
}
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
}