V10: fix build warnings in Web.BackOffice (#12479)
* Run code cleanup * Start manual run * Finish dotnet format + manual cleanup * Fix up after merge * Fix substrings changed to [..] Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Mime;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -19,341 +15,373 @@ using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Web.Common.Attributes;
|
||||
using Umbraco.Cms.Web.Common.Authorization;
|
||||
using Umbraco.Extensions;
|
||||
using Constants = Umbraco.Cms.Core.Constants;
|
||||
|
||||
namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
namespace Umbraco.Cms.Web.BackOffice.Controllers;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// The API controller used for editing dictionary items
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The security for this controller is defined to allow full CRUD access to dictionary if the user has access to
|
||||
/// either:
|
||||
/// Dictionary
|
||||
/// </remarks>
|
||||
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
||||
[Authorize(Policy = AuthorizationPolicies.TreeAccessDictionary)]
|
||||
[ParameterSwapControllerActionSelector(nameof(GetById), "id", typeof(int), typeof(Guid), typeof(Udi))]
|
||||
public class DictionaryController : BackOfficeNotificationsController
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// The API controller used for editing dictionary items
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The security for this controller is defined to allow full CRUD access to dictionary if the user has access to either:
|
||||
/// Dictionary
|
||||
/// </remarks>
|
||||
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
||||
[Authorize(Policy = AuthorizationPolicies.TreeAccessDictionary)]
|
||||
[ParameterSwapControllerActionSelector(nameof(GetById), "id", typeof(int), typeof(Guid), typeof(Udi))]
|
||||
public class DictionaryController : BackOfficeNotificationsController
|
||||
private readonly IBackOfficeSecurityAccessor _backofficeSecurityAccessor;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly ILocalizationService _localizationService;
|
||||
private readonly ILocalizedTextService _localizedTextService;
|
||||
private readonly ILogger<DictionaryController> _logger;
|
||||
private readonly IUmbracoMapper _umbracoMapper;
|
||||
|
||||
public DictionaryController(
|
||||
ILogger<DictionaryController> logger,
|
||||
ILocalizationService localizationService,
|
||||
IBackOfficeSecurityAccessor backofficeSecurityAccessor,
|
||||
IOptionsSnapshot<GlobalSettings> globalSettings,
|
||||
ILocalizedTextService localizedTextService,
|
||||
IUmbracoMapper umbracoMapper)
|
||||
{
|
||||
private readonly ILogger<DictionaryController> _logger;
|
||||
private readonly ILocalizationService _localizationService;
|
||||
private readonly IBackOfficeSecurityAccessor _backofficeSecurityAccessor;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly ILocalizedTextService _localizedTextService;
|
||||
private readonly IUmbracoMapper _umbracoMapper;
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_localizationService = localizationService ?? throw new ArgumentNullException(nameof(localizationService));
|
||||
_backofficeSecurityAccessor = backofficeSecurityAccessor ??
|
||||
throw new ArgumentNullException(nameof(backofficeSecurityAccessor));
|
||||
_globalSettings = globalSettings.Value ?? throw new ArgumentNullException(nameof(globalSettings));
|
||||
_localizedTextService = localizedTextService ?? throw new ArgumentNullException(nameof(localizedTextService));
|
||||
_umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper));
|
||||
}
|
||||
|
||||
public DictionaryController(
|
||||
ILogger<DictionaryController> logger,
|
||||
ILocalizationService localizationService,
|
||||
IBackOfficeSecurityAccessor backofficeSecurityAccessor,
|
||||
IOptionsSnapshot<GlobalSettings> globalSettings,
|
||||
ILocalizedTextService localizedTextService,
|
||||
IUmbracoMapper umbracoMapper
|
||||
)
|
||||
/// <summary>
|
||||
/// Deletes a data type with a given ID
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns>
|
||||
/// <see cref="HttpResponseMessage" />
|
||||
/// </returns>
|
||||
[HttpDelete]
|
||||
[HttpPost]
|
||||
public IActionResult DeleteById(int id)
|
||||
{
|
||||
IDictionaryItem? foundDictionary = _localizationService.GetDictionaryItemById(id);
|
||||
|
||||
if (foundDictionary == null)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_localizationService = localizationService ?? throw new ArgumentNullException(nameof(localizationService));
|
||||
_backofficeSecurityAccessor = backofficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backofficeSecurityAccessor));
|
||||
_globalSettings = globalSettings.Value ?? throw new ArgumentNullException(nameof(globalSettings));
|
||||
_localizedTextService = localizedTextService ?? throw new ArgumentNullException(nameof(localizedTextService));
|
||||
_umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper));
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a data type with a given ID
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns><see cref="HttpResponseMessage"/></returns>
|
||||
[HttpDelete]
|
||||
[HttpPost]
|
||||
public IActionResult DeleteById(int id)
|
||||
IEnumerable<IDictionaryItem> foundDictionaryDescendants =
|
||||
_localizationService.GetDictionaryItemDescendants(foundDictionary.Key);
|
||||
|
||||
foreach (IDictionaryItem dictionaryItem in foundDictionaryDescendants)
|
||||
{
|
||||
var foundDictionary = _localizationService.GetDictionaryItemById(id);
|
||||
_localizationService.Delete(dictionaryItem, _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? -1);
|
||||
}
|
||||
|
||||
if (foundDictionary == null)
|
||||
return NotFound();
|
||||
_localizationService.Delete(foundDictionary, _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? -1);
|
||||
|
||||
var foundDictionaryDescendants = _localizationService.GetDictionaryItemDescendants(foundDictionary.Key);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
foreach (var dictionaryItem in foundDictionaryDescendants)
|
||||
/// <summary>
|
||||
/// Creates a new dictionary item
|
||||
/// </summary>
|
||||
/// <param name="parentId">
|
||||
/// The parent id.
|
||||
/// </param>
|
||||
/// <param name="key">
|
||||
/// The key.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="HttpResponseMessage" />.
|
||||
/// </returns>
|
||||
[HttpPost]
|
||||
public ActionResult<int> Create(int parentId, string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
return ValidationProblem("Key can not be empty."); // TODO: translate
|
||||
}
|
||||
|
||||
if (_localizationService.DictionaryItemExists(key))
|
||||
{
|
||||
var message = _localizedTextService.Localize(
|
||||
"dictionaryItem",
|
||||
"changeKeyError",
|
||||
_backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.GetUserCulture(_localizedTextService, _globalSettings),
|
||||
new Dictionary<string, string?>
|
||||
{
|
||||
{"0", key}
|
||||
});
|
||||
return ValidationProblem(message);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Guid? parentGuid = null;
|
||||
|
||||
if (parentId > 0)
|
||||
{
|
||||
_localizationService.Delete(dictionaryItem, _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? -1);
|
||||
parentGuid = _localizationService.GetDictionaryItemById(parentId)?.Key;
|
||||
}
|
||||
|
||||
_localizationService.Delete(foundDictionary, _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? -1);
|
||||
IDictionaryItem item = _localizationService.CreateDictionaryItemWithIdentity(
|
||||
key,
|
||||
parentGuid,
|
||||
string.Empty);
|
||||
|
||||
return Ok();
|
||||
|
||||
return item.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error creating dictionary with {Name} under {ParentId}", key, parentId);
|
||||
return ValidationProblem("Error creating dictionary item");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a dictionary item by id
|
||||
/// </summary>
|
||||
/// <param name="id">
|
||||
/// The id.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="DictionaryDisplay" />. Returns a not found response when dictionary item does not exist
|
||||
/// </returns>
|
||||
public ActionResult<DictionaryDisplay?> GetById(int id)
|
||||
{
|
||||
IDictionaryItem? dictionary = _localizationService.GetDictionaryItemById(id);
|
||||
if (dictionary == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new dictionary item
|
||||
/// </summary>
|
||||
/// <param name="parentId">
|
||||
/// The parent id.
|
||||
/// </param>
|
||||
/// <param name="key">
|
||||
/// The key.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="HttpResponseMessage"/>.
|
||||
/// </returns>
|
||||
[HttpPost]
|
||||
public ActionResult<int> Create(int parentId, string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
return ValidationProblem("Key can not be empty."); // TODO: translate
|
||||
return _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionary);
|
||||
}
|
||||
|
||||
if (_localizationService.DictionaryItemExists(key))
|
||||
/// <summary>
|
||||
/// Gets a dictionary item by guid
|
||||
/// </summary>
|
||||
/// <param name="id">
|
||||
/// The id.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="DictionaryDisplay" />. Returns a not found response when dictionary item does not exist
|
||||
/// </returns>
|
||||
public ActionResult<DictionaryDisplay?> GetById(Guid id)
|
||||
{
|
||||
IDictionaryItem? dictionary = _localizationService.GetDictionaryItemById(id);
|
||||
if (dictionary == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a dictionary item by udi
|
||||
/// </summary>
|
||||
/// <param name="id">
|
||||
/// The id.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="DictionaryDisplay" />. Returns a not found response when dictionary item does not exist
|
||||
/// </returns>
|
||||
public ActionResult<DictionaryDisplay?> GetById(Udi id)
|
||||
{
|
||||
var guidUdi = id as GuidUdi;
|
||||
if (guidUdi == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
IDictionaryItem? dictionary = _localizationService.GetDictionaryItemById(guidUdi.Guid);
|
||||
if (dictionary == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the structure for dictionary items
|
||||
/// </summary>
|
||||
/// <param name="move"></param>
|
||||
/// <returns></returns>
|
||||
public IActionResult? PostMove(MoveOrCopy move)
|
||||
{
|
||||
IDictionaryItem? dictionaryItem = _localizationService.GetDictionaryItemById(move.Id);
|
||||
if (dictionaryItem == null)
|
||||
{
|
||||
return ValidationProblem(_localizedTextService.Localize("dictionary", "itemDoesNotExists"));
|
||||
}
|
||||
|
||||
IDictionaryItem? parent = _localizationService.GetDictionaryItemById(move.ParentId);
|
||||
if (parent == null)
|
||||
{
|
||||
if (move.ParentId == Constants.System.Root)
|
||||
{
|
||||
var message = _localizedTextService.Localize(
|
||||
"dictionaryItem","changeKeyError",
|
||||
_backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.GetUserCulture(_localizedTextService, _globalSettings),
|
||||
new Dictionary<string, string?> { { "0", key } });
|
||||
return ValidationProblem(message);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Guid? parentGuid = null;
|
||||
|
||||
if (parentId > 0)
|
||||
parentGuid = _localizationService.GetDictionaryItemById(parentId)?.Key;
|
||||
|
||||
var item = _localizationService.CreateDictionaryItemWithIdentity(
|
||||
key,
|
||||
parentGuid,
|
||||
string.Empty);
|
||||
|
||||
|
||||
return item.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error creating dictionary with {Name} under {ParentId}", key, parentId);
|
||||
return ValidationProblem("Error creating dictionary item");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a dictionary item by id
|
||||
/// </summary>
|
||||
/// <param name="id">
|
||||
/// The id.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="DictionaryDisplay"/>. Returns a not found response when dictionary item does not exist
|
||||
/// </returns>
|
||||
public ActionResult<DictionaryDisplay?> GetById(int id)
|
||||
{
|
||||
var dictionary = _localizationService.GetDictionaryItemById(id);
|
||||
if (dictionary == null)
|
||||
return NotFound();
|
||||
|
||||
return _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a dictionary item by guid
|
||||
/// </summary>
|
||||
/// <param name="id">
|
||||
/// The id.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="DictionaryDisplay"/>. Returns a not found response when dictionary item does not exist
|
||||
/// </returns>
|
||||
public ActionResult<DictionaryDisplay?> GetById(Guid id)
|
||||
{
|
||||
var dictionary = _localizationService.GetDictionaryItemById(id);
|
||||
if (dictionary == null)
|
||||
return NotFound();
|
||||
|
||||
return _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a dictionary item by udi
|
||||
/// </summary>
|
||||
/// <param name="id">
|
||||
/// The id.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="DictionaryDisplay"/>. Returns a not found response when dictionary item does not exist
|
||||
/// </returns>
|
||||
public ActionResult<DictionaryDisplay?> GetById(Udi id)
|
||||
{
|
||||
var guidUdi = id as GuidUdi;
|
||||
if (guidUdi == null)
|
||||
return NotFound();
|
||||
|
||||
var dictionary = _localizationService.GetDictionaryItemById(guidUdi.Guid);
|
||||
if (dictionary == null)
|
||||
return NotFound();
|
||||
|
||||
return _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the structure for dictionary items
|
||||
/// </summary>
|
||||
/// <param name="move"></param>
|
||||
/// <returns></returns>
|
||||
public IActionResult? PostMove(MoveOrCopy move)
|
||||
{
|
||||
var dictionaryItem = _localizationService.GetDictionaryItemById(move.Id);
|
||||
if (dictionaryItem == null)
|
||||
return ValidationProblem(_localizedTextService.Localize("dictionary", "itemDoesNotExists"));
|
||||
|
||||
var parent = _localizationService.GetDictionaryItemById(move.ParentId);
|
||||
if (parent == null)
|
||||
{
|
||||
if (move.ParentId == Constants.System.Root)
|
||||
dictionaryItem.ParentId = null;
|
||||
else
|
||||
return ValidationProblem(_localizedTextService.Localize("dictionary", "parentDoesNotExists"));
|
||||
dictionaryItem.ParentId = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
dictionaryItem.ParentId = parent.Key;
|
||||
if (dictionaryItem.Key == parent.ParentId)
|
||||
return ValidationProblem(_localizedTextService.Localize("moveOrCopy", "notAllowedByPath"));
|
||||
return ValidationProblem(_localizedTextService.Localize("dictionary", "parentDoesNotExists"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dictionaryItem.ParentId = parent.Key;
|
||||
if (dictionaryItem.Key == parent.ParentId)
|
||||
{
|
||||
return ValidationProblem(_localizedTextService.Localize("moveOrCopy", "notAllowedByPath"));
|
||||
}
|
||||
}
|
||||
|
||||
_localizationService.Save(dictionaryItem);
|
||||
|
||||
DictionaryDisplay? model = _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionaryItem);
|
||||
|
||||
return Content(model!.Path, MediaTypeNames.Text.Plain, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a dictionary item
|
||||
/// </summary>
|
||||
/// <param name="dictionary">
|
||||
/// The dictionary.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="DictionaryDisplay" />.
|
||||
/// </returns>
|
||||
public ActionResult<DictionaryDisplay?> PostSave(DictionarySave dictionary)
|
||||
{
|
||||
IDictionaryItem? dictionaryItem = dictionary.Id is null
|
||||
? null
|
||||
: _localizationService.GetDictionaryItemById(int.Parse(dictionary.Id.ToString()!, CultureInfo.InvariantCulture));
|
||||
|
||||
if (dictionaryItem == null)
|
||||
{
|
||||
return ValidationProblem("Dictionary item does not exist");
|
||||
}
|
||||
|
||||
CultureInfo? userCulture =
|
||||
_backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.GetUserCulture(_localizedTextService, _globalSettings);
|
||||
|
||||
if (dictionary.NameIsDirty)
|
||||
{
|
||||
// if the name (key) has changed, we need to check if the new key does not exist
|
||||
IDictionaryItem? dictionaryByKey = _localizationService.GetDictionaryItemByKey(dictionary.Name!);
|
||||
|
||||
if (dictionaryByKey != null && dictionaryItem.Id != dictionaryByKey.Id)
|
||||
{
|
||||
var message = _localizedTextService.Localize(
|
||||
"dictionaryItem",
|
||||
"changeKeyError",
|
||||
userCulture,
|
||||
new Dictionary<string, string?> { { "0", dictionary.Name } });
|
||||
ModelState.AddModelError("Name", message);
|
||||
return ValidationProblem(ModelState);
|
||||
}
|
||||
|
||||
dictionaryItem.ItemKey = dictionary.Name!;
|
||||
}
|
||||
|
||||
foreach (DictionaryTranslationSave translation in dictionary.Translations)
|
||||
{
|
||||
_localizationService.AddOrUpdateDictionaryValue(dictionaryItem, _localizationService.GetLanguageById(translation.LanguageId), translation.Translation);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_localizationService.Save(dictionaryItem);
|
||||
|
||||
var model = _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionaryItem);
|
||||
DictionaryDisplay? model = _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionaryItem);
|
||||
|
||||
return Content(model!.Path, MediaTypeNames.Text.Plain, Encoding.UTF8);
|
||||
model?.Notifications.Add(new BackOfficeNotification(
|
||||
_localizedTextService.Localize("speechBubbles", "dictionaryItemSaved", userCulture), string.Empty, NotificationStyle.Success));
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a dictionary item
|
||||
/// </summary>
|
||||
/// <param name="dictionary">
|
||||
/// The dictionary.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="DictionaryDisplay"/>.
|
||||
/// </returns>
|
||||
public ActionResult<DictionaryDisplay?> PostSave(DictionarySave dictionary)
|
||||
catch (Exception ex)
|
||||
{
|
||||
var dictionaryItem = dictionary.Id is null ? null :
|
||||
_localizationService.GetDictionaryItemById(int.Parse(dictionary.Id.ToString()!, CultureInfo.InvariantCulture));
|
||||
|
||||
if (dictionaryItem == null)
|
||||
return ValidationProblem("Dictionary item does not exist");
|
||||
|
||||
var userCulture = _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.GetUserCulture(_localizedTextService, _globalSettings);
|
||||
|
||||
if (dictionary.NameIsDirty)
|
||||
{
|
||||
// if the name (key) has changed, we need to check if the new key does not exist
|
||||
var dictionaryByKey = _localizationService.GetDictionaryItemByKey(dictionary.Name!);
|
||||
|
||||
if (dictionaryByKey != null && dictionaryItem.Id != dictionaryByKey.Id)
|
||||
{
|
||||
|
||||
var message = _localizedTextService.Localize(
|
||||
"dictionaryItem","changeKeyError",
|
||||
userCulture,
|
||||
new Dictionary<string, string?> { { "0", dictionary.Name } });
|
||||
ModelState.AddModelError("Name", message);
|
||||
return ValidationProblem(ModelState);
|
||||
}
|
||||
|
||||
dictionaryItem.ItemKey = dictionary.Name!;
|
||||
}
|
||||
|
||||
foreach (var translation in dictionary.Translations)
|
||||
{
|
||||
_localizationService.AddOrUpdateDictionaryValue(dictionaryItem,
|
||||
_localizationService.GetLanguageById(translation.LanguageId), translation.Translation);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_localizationService.Save(dictionaryItem);
|
||||
|
||||
var model = _umbracoMapper.Map<IDictionaryItem, DictionaryDisplay>(dictionaryItem);
|
||||
|
||||
model?.Notifications.Add(new BackOfficeNotification(
|
||||
_localizedTextService.Localize("speechBubbles","dictionaryItemSaved", userCulture), string.Empty,
|
||||
NotificationStyle.Success));
|
||||
|
||||
return model;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error saving dictionary with {Name} under {ParentId}", dictionary.Name, dictionary.ParentId);
|
||||
return ValidationProblem("Something went wrong saving dictionary");
|
||||
}
|
||||
_logger.LogError(ex, "Error saving dictionary with {Name} under {ParentId}", dictionary.Name, dictionary.ParentId);
|
||||
return ValidationProblem("Something went wrong saving dictionary");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a list with all dictionary items
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="IEnumerable{T}"/>.
|
||||
/// </returns>
|
||||
public IEnumerable<DictionaryOverviewDisplay> GetList()
|
||||
{
|
||||
var items = _localizationService.GetDictionaryItemDescendants(null).ToArray();
|
||||
var list = new List<DictionaryOverviewDisplay>(items.Length);
|
||||
|
||||
// recursive method to build a tree structure from the flat structure returned above
|
||||
void BuildTree(int level = 0, Guid? parentId = null)
|
||||
{
|
||||
var children = items.Where(t => t.ParentId == parentId).ToArray();
|
||||
if(children.Any() == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var child in children.OrderBy(ItemSort()))
|
||||
{
|
||||
var display = _umbracoMapper.Map<IDictionaryItem, DictionaryOverviewDisplay>(child);
|
||||
if (display is not null)
|
||||
{
|
||||
display.Level = level;
|
||||
list.Add(display);
|
||||
}
|
||||
|
||||
BuildTree(level + 1, child.Key);
|
||||
}
|
||||
}
|
||||
|
||||
BuildTree();
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get child items for list.
|
||||
/// </summary>
|
||||
/// <param name="dictionaryItem">
|
||||
/// The dictionary item.
|
||||
/// </param>
|
||||
/// <param name="level">
|
||||
/// The level.
|
||||
/// </param>
|
||||
/// <param name="list">
|
||||
/// The list.
|
||||
/// </param>
|
||||
private void GetChildItemsForList(IDictionaryItem dictionaryItem, int level, ICollection<DictionaryOverviewDisplay> list)
|
||||
{
|
||||
foreach (var childItem in _localizationService.GetDictionaryItemChildren(dictionaryItem.Key)?.OrderBy(ItemSort()) ?? Enumerable.Empty<IDictionaryItem>())
|
||||
{
|
||||
var item = _umbracoMapper.Map<IDictionaryItem, DictionaryOverviewDisplay>(childItem);
|
||||
if (item is not null)
|
||||
{
|
||||
item.Level = level;
|
||||
list.Add(item);
|
||||
}
|
||||
|
||||
GetChildItemsForList(childItem, level + 1, list);
|
||||
}
|
||||
}
|
||||
|
||||
private static Func<IDictionaryItem, string> ItemSort() => item => item.ItemKey;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a list with all dictionary items
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="IEnumerable{T}" />.
|
||||
/// </returns>
|
||||
public IEnumerable<DictionaryOverviewDisplay> GetList()
|
||||
{
|
||||
IDictionaryItem[] items = _localizationService.GetDictionaryItemDescendants(null).ToArray();
|
||||
var list = new List<DictionaryOverviewDisplay>(items.Length);
|
||||
|
||||
// recursive method to build a tree structure from the flat structure returned above
|
||||
void BuildTree(int level = 0, Guid? parentId = null)
|
||||
{
|
||||
IDictionaryItem[] children = items.Where(t => t.ParentId == parentId).ToArray();
|
||||
if (children.Any() == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (IDictionaryItem child in children.OrderBy(ItemSort()))
|
||||
{
|
||||
DictionaryOverviewDisplay? display =
|
||||
_umbracoMapper.Map<IDictionaryItem, DictionaryOverviewDisplay>(child);
|
||||
if (display is not null)
|
||||
{
|
||||
display.Level = level;
|
||||
list.Add(display);
|
||||
}
|
||||
|
||||
BuildTree(level + 1, child.Key);
|
||||
}
|
||||
}
|
||||
|
||||
BuildTree();
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get child items for list.
|
||||
/// </summary>
|
||||
/// <param name="dictionaryItem">
|
||||
/// The dictionary item.
|
||||
/// </param>
|
||||
/// <param name="level">
|
||||
/// The level.
|
||||
/// </param>
|
||||
/// <param name="list">
|
||||
/// The list.
|
||||
/// </param>
|
||||
private void GetChildItemsForList(IDictionaryItem dictionaryItem, int level, ICollection<DictionaryOverviewDisplay> list)
|
||||
{
|
||||
foreach (IDictionaryItem childItem in _localizationService.GetDictionaryItemChildren(dictionaryItem.Key)
|
||||
?.OrderBy(ItemSort()) ?? Enumerable.Empty<IDictionaryItem>())
|
||||
{
|
||||
DictionaryOverviewDisplay? item = _umbracoMapper.Map<IDictionaryItem, DictionaryOverviewDisplay>(childItem);
|
||||
if (item is not null)
|
||||
{
|
||||
item.Level = level;
|
||||
list.Add(item);
|
||||
}
|
||||
|
||||
GetChildItemsForList(childItem, level + 1, list);
|
||||
}
|
||||
}
|
||||
|
||||
private static Func<IDictionaryItem, string> ItemSort() => item => item.ItemKey;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user