2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Dictionary;
|
|
|
|
|
|
using Umbraco.Cms.Core.Mapping;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
|
using Umbraco.Extensions;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Core.Models.Mapping
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-02-17 09:15:48 +01:00
|
|
|
|
public abstract class TabsAndPropertiesMapper
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-11-22 13:13:19 +11:00
|
|
|
|
protected ICultureDictionary CultureDictionary { get; }
|
2018-06-29 19:52:40 +02:00
|
|
|
|
protected ILocalizedTextService LocalizedTextService { get; }
|
|
|
|
|
|
protected IEnumerable<string> IgnoreProperties { get; set; }
|
|
|
|
|
|
|
2019-11-22 13:13:19 +11:00
|
|
|
|
protected TabsAndPropertiesMapper(ICultureDictionary cultureDictionary, ILocalizedTextService localizedTextService)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-11-22 13:13:19 +11:00
|
|
|
|
CultureDictionary = cultureDictionary ?? throw new ArgumentNullException(nameof(cultureDictionary));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
LocalizedTextService = localizedTextService ?? throw new ArgumentNullException(nameof(localizedTextService));
|
|
|
|
|
|
IgnoreProperties = new List<string>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-22 13:13:19 +11:00
|
|
|
|
protected TabsAndPropertiesMapper(ICultureDictionary cultureDictionary, ILocalizedTextService localizedTextService, IEnumerable<string> ignoreProperties)
|
|
|
|
|
|
: this(cultureDictionary, localizedTextService)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
IgnoreProperties = ignoreProperties ?? throw new ArgumentNullException(nameof(ignoreProperties));
|
|
|
|
|
|
}
|
2019-04-07 12:56:11 +02:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns a collection of custom generic properties that exist on the generic properties tab
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected virtual IEnumerable<ContentPropertyDisplay> GetCustomGenericProperties(IContentBase content)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Enumerable.Empty<ContentPropertyDisplay>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps properties on to the generic properties tab
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content"></param>
|
|
|
|
|
|
/// <param name="tabs"></param>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <remarks>
|
2019-02-05 14:06:48 +01:00
|
|
|
|
/// The generic properties tab is responsible for
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// setting up the properties such as Created date, updated date, template selected, etc...
|
|
|
|
|
|
/// </remarks>
|
2019-03-26 10:39:50 +01:00
|
|
|
|
protected virtual void MapGenericProperties(IContentBase content, List<Tab<ContentPropertyDisplay>> tabs, MapperContext context)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
// add the generic properties tab, for properties that don't belong to a tab
|
|
|
|
|
|
// get the properties, map and translate them, then add the tab
|
2019-02-07 14:01:00 +11:00
|
|
|
|
var noGroupProperties = content.GetNonGroupedProperties()
|
2018-06-29 19:52:40 +02:00
|
|
|
|
.Where(x => IgnoreProperties.Contains(x.Alias) == false) // skip ignored
|
|
|
|
|
|
.ToList();
|
2018-07-18 15:42:12 +10:00
|
|
|
|
var genericproperties = MapProperties(content, noGroupProperties, context);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
tabs.Add(new Tab<ContentPropertyDisplay>
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = 0,
|
2021-01-17 21:33:35 +13:00
|
|
|
|
Label = LocalizedTextService.Localize("general", "properties"),
|
2018-06-29 19:52:40 +02:00
|
|
|
|
Alias = "Generic properties",
|
|
|
|
|
|
Properties = genericproperties
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var genericProps = tabs.Single(x => x.Id == 0);
|
|
|
|
|
|
|
|
|
|
|
|
//store the current props to append to the newly inserted ones
|
|
|
|
|
|
var currProps = genericProps.Properties.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
var contentProps = new List<ContentPropertyDisplay>();
|
|
|
|
|
|
|
|
|
|
|
|
var customProperties = GetCustomGenericProperties(content);
|
|
|
|
|
|
if (customProperties != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//add the custom ones
|
|
|
|
|
|
contentProps.AddRange(customProperties);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//now add the user props
|
|
|
|
|
|
contentProps.AddRange(currProps);
|
|
|
|
|
|
|
|
|
|
|
|
//re-assign
|
|
|
|
|
|
genericProps.Properties = contentProps;
|
|
|
|
|
|
|
2019-02-05 14:06:48 +01:00
|
|
|
|
//Show or hide properties tab based on whether it has or not any properties
|
2018-06-29 19:52:40 +02:00
|
|
|
|
if (genericProps.Properties.Any() == false)
|
|
|
|
|
|
{
|
2019-01-26 10:52:19 -05:00
|
|
|
|
//loop through the tabs, remove the one with the id of zero and exit the loop
|
2018-06-29 19:52:40 +02:00
|
|
|
|
for (var i = 0; i < tabs.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (tabs[i].Id != 0) continue;
|
|
|
|
|
|
tabs.RemoveAt(i);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps a list of <see cref="Property"/> to a list of <see cref="ContentPropertyDisplay"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content"></param>
|
|
|
|
|
|
/// <param name="properties"></param>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2019-11-08 15:10:05 +01:00
|
|
|
|
protected virtual List<ContentPropertyDisplay> MapProperties(IContentBase content, List<IProperty> properties, MapperContext context)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-11-08 15:10:05 +01:00
|
|
|
|
return context.MapEnumerable<IProperty, ContentPropertyDisplay>(properties.OrderBy(x => x.PropertyType.SortOrder));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates the tabs collection with properties assigned for display models
|
|
|
|
|
|
/// </summary>
|
2020-02-17 09:15:48 +01:00
|
|
|
|
public class TabsAndPropertiesMapper<TSource> : TabsAndPropertiesMapper
|
2018-06-29 19:52:40 +02:00
|
|
|
|
where TSource : IContentBase
|
|
|
|
|
|
{
|
2020-01-22 21:43:25 -08:00
|
|
|
|
private readonly IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
|
|
|
|
|
|
|
|
|
|
|
|
public TabsAndPropertiesMapper(ICultureDictionary cultureDictionary, ILocalizedTextService localizedTextService, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider)
|
2019-11-22 13:13:19 +11:00
|
|
|
|
: base(cultureDictionary, localizedTextService)
|
2020-01-22 21:43:25 -08:00
|
|
|
|
{
|
|
|
|
|
|
_contentTypeBaseServiceProvider = contentTypeBaseServiceProvider ?? throw new ArgumentNullException(nameof(contentTypeBaseServiceProvider));
|
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2019-03-26 10:39:50 +01:00
|
|
|
|
public virtual IEnumerable<Tab<ContentPropertyDisplay>> Map(TSource source, MapperContext context)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var tabs = new List<Tab<ContentPropertyDisplay>>();
|
|
|
|
|
|
|
2020-01-22 21:43:25 -08:00
|
|
|
|
var contentType = _contentTypeBaseServiceProvider.GetContentTypeOf(source);
|
2019-02-05 14:06:48 +01:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
// add the tabs, for properties that belong to a tab
|
|
|
|
|
|
// need to aggregate the tabs, as content.PropertyGroups contains all the composition tabs,
|
|
|
|
|
|
// and there might be duplicates (content does not work like contentType and there is no
|
|
|
|
|
|
// content.CompositionPropertyGroups).
|
2019-02-05 14:06:48 +01:00
|
|
|
|
var groupsGroupsByName = contentType.CompositionPropertyGroups.OrderBy(x => x.SortOrder).GroupBy(x => x.Name);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
foreach (var groupsByName in groupsGroupsByName)
|
|
|
|
|
|
{
|
2019-11-08 15:10:05 +01:00
|
|
|
|
var properties = new List<IProperty>();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
// merge properties for groups with the same name
|
|
|
|
|
|
foreach (var group in groupsByName)
|
|
|
|
|
|
{
|
2020-11-17 20:27:10 +01:00
|
|
|
|
var groupProperties = source.GetPropertiesForGroup(group)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
.Where(x => IgnoreProperties.Contains(x.Alias) == false); // skip ignored
|
|
|
|
|
|
|
|
|
|
|
|
properties.AddRange(groupProperties);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (properties.Count == 0)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
//map the properties
|
2018-07-18 15:42:12 +10:00
|
|
|
|
var mappedProperties = MapProperties(source, properties, context);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
// add the tab
|
|
|
|
|
|
// we need to pick an identifier... there is no "right" way...
|
|
|
|
|
|
var g = groupsByName.FirstOrDefault(x => x.Id == source.ContentTypeId) // try local
|
|
|
|
|
|
?? groupsByName.First(); // else pick one randomly
|
|
|
|
|
|
var groupId = g.Id;
|
|
|
|
|
|
var groupName = groupsByName.Key;
|
|
|
|
|
|
tabs.Add(new Tab<ContentPropertyDisplay>
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = groupId,
|
|
|
|
|
|
Alias = groupName,
|
2019-11-22 13:13:19 +11:00
|
|
|
|
Label = LocalizedTextService.UmbracoDictionaryTranslate(CultureDictionary, groupName),
|
2018-06-29 19:52:40 +02:00
|
|
|
|
Properties = mappedProperties,
|
|
|
|
|
|
IsActive = false
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-18 15:42:12 +10:00
|
|
|
|
MapGenericProperties(source, tabs, context);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
// activate the first tab, if any
|
|
|
|
|
|
if (tabs.Count > 0)
|
|
|
|
|
|
tabs[0].IsActive = true;
|
|
|
|
|
|
|
|
|
|
|
|
return tabs;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|