Files
Umbraco-CMS/src/Umbraco.Web/Editors/DictionaryController.cs

237 lines
8.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Umbraco.Core.Models;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Mvc;
using Umbraco.Web.UI;
using Umbraco.Web.WebApi;
using Umbraco.Web.WebApi.Filters;
using Constants = Umbraco.Core.Constants;
using Notification = Umbraco.Web.Models.ContentEditing.Notification;
namespace Umbraco.Web.Editors
{
/// <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:
/// Dictionar
/// </remarks>
[PluginController("UmbracoApi")]
[UmbracoTreeAuthorize(Constants.Trees.Dictionary)]
[EnableOverrideAuthorization]
public class DictionaryController : BackOfficeNotificationsController
{
/// <summary>
/// Deletes a data type wth a given ID
/// </summary>
/// <param name="id"></param>
/// <returns><see cref="HttpResponseMessage"/></returns>
[HttpDelete]
[HttpPost]
public HttpResponseMessage DeleteById(int id)
{
var foundDictionary = Services.LocalizationService.GetDictionaryItemById(id);
if (foundDictionary == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
Services.LocalizationService.Delete(foundDictionary, Security.CurrentUser.Id);
return Request.CreateResponse(HttpStatusCode.OK);
}
/// <summary>
/// Creates a new dictoinairy item
/// </summary>
/// <param name="parentId">
/// The parent id.
/// </param>
/// <param name="key">
/// The key.
/// </param>
/// <returns>
/// The <see cref="HttpResponseMessage"/>.
/// </returns>
[HttpPost]
public HttpResponseMessage Create(int parentId, string key)
{
if (string.IsNullOrEmpty(key))
return Request
.CreateNotificationValidationErrorResponse("Key can not be empty;"); // TODO translate
if (Services.LocalizationService.DictionaryItemExists(key))
{
var message = Services.TextService.Localize(
"dictionaryItem/changeKeyError",
Merge remote-tracking branch 'origin/dev-v7' into temp8 (after fixing) # Conflicts: # build/Modules/Umbraco.Build/Build-UmbracoDocs.ps1 # src/SolutionInfo.cs # src/Umbraco.Core/Configuration/UmbracoVersion.cs # src/Umbraco.Core/Constants-Applications.cs # src/Umbraco.Core/Models/Rdbms/ConsentDto.cs # src/Umbraco.Core/Persistence/DatabaseSchemaHelper.cs # src/Umbraco.Core/Persistence/Repositories/AuditRepository.cs # src/Umbraco.Core/PropertyEditors/ValueConverters/GridValueConverter.cs # src/Umbraco.Core/Services/ConsentService.cs # src/Umbraco.Core/Services/IRelationService.cs # src/Umbraco.Core/Services/RelationService.cs # src/Umbraco.Tests/Plugins/PluginManagerTests.cs # src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs # src/Umbraco.Tests/Services/ConsentServiceTests.cs # src/Umbraco.Web.UI.Client/src/common/directives/components/content/edit.controller.js # src/Umbraco.Web.UI.Client/src/common/directives/components/editor/umbeditorheader.directive.js # src/Umbraco.Web.UI.Client/src/common/directives/components/tree/umbtree.directive.js # src/Umbraco.Web.UI.Client/src/common/resources/contenttype.resource.js # src/Umbraco.Web.UI.Client/src/common/resources/entity.resource.js # src/Umbraco.Web.UI.Client/src/common/services/tree.service.js # src/Umbraco.Web.UI.Client/src/less/application/grid.less # src/Umbraco.Web.UI.Client/src/less/forms.less # src/Umbraco.Web.UI.Client/src/less/main.less # src/Umbraco.Web.UI.Client/src/views/common/dialogs/login.controller.js # src/Umbraco.Web.UI.Client/src/views/common/dialogs/login.html # src/Umbraco.Web.UI.Client/src/views/common/overlays/iconpicker/iconpicker.controller.js # src/Umbraco.Web.UI.Client/src/views/common/overlays/iconpicker/iconpicker.html # src/Umbraco.Web.UI.Client/src/views/common/overlays/mediaPicker/mediapicker.controller.js # src/Umbraco.Web.UI.Client/src/views/common/overlays/treepicker/treepicker.controller.js # src/Umbraco.Web.UI.Client/src/views/components/content/umb-content-node-info.html # src/Umbraco.Web.UI.Client/src/views/components/html/umb-control-group.html # src/Umbraco.Web.UI.Client/src/views/components/users/change-password.html # src/Umbraco.Web.UI.Client/src/views/content/content.copy.controller.js # src/Umbraco.Web.UI.Client/src/views/content/content.move.controller.js # src/Umbraco.Web.UI.Client/src/views/datatypes/datatype.edit.controller.js # src/Umbraco.Web.UI.Client/src/views/datatypes/edit.html # src/Umbraco.Web.UI.Client/src/views/documenttypes/views/permissions/permissions.html # src/Umbraco.Web.UI.Client/src/views/media/media.move.controller.js # src/Umbraco.Web.UI.Client/src/views/mediatypes/edit.controller.js # src/Umbraco.Web.UI.Client/src/views/mediatypes/views/permissions/permissions.html # src/Umbraco.Web.UI.Client/src/views/propertyeditors/boolean/boolean.controller.js # src/Umbraco.Web.UI.Client/src/views/propertyeditors/contentpicker/contentpicker.controller.js # src/Umbraco.Web.UI.Client/src/views/users/views/user/details.html # src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml # src/Umbraco.Web.UI/config/EmbeddedMedia.Release.config # src/Umbraco.Web.UI/config/EmbeddedMedia.config # src/Umbraco.Web.UI/config/trees.Release.config # src/Umbraco.Web.UI/config/trees.config # src/Umbraco.Web.UI/config/umbracoSettings.config # src/Umbraco.Web.UI/umbraco/Views/Default.cshtml # src/Umbraco.Web.UI/umbraco/config/create/UI.xml # src/Umbraco.Web.UI/umbraco/config/lang/da.xml # src/Umbraco.Web.UI/umbraco/config/lang/en.xml # src/Umbraco.Web.UI/umbraco/config/lang/en_us.xml # src/Umbraco.Web.UI/umbraco/config/lang/it.xml # src/Umbraco.Web.UI/umbraco/config/lang/nl.xml # src/Umbraco.Web.UI/umbraco/config/lang/ru.xml # src/Umbraco.Web/Editors/BackOfficeController.cs # src/Umbraco.Web/Editors/BackOfficeServerVariables.cs # src/Umbraco.Web/Editors/ContentTypeController.cs # src/Umbraco.Web/Editors/MacroController.cs # src/Umbraco.Web/Editors/MemberController.cs # src/Umbraco.Web/Models/RegisterModel.cs # src/Umbraco.Web/PropertyEditors/TagsPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/ValueConverters/MacroContainerValueConverter.cs # src/Umbraco.Web/PublishedContentExtensions.cs # src/Umbraco.Web/Trees/ContentTreeController.cs # src/Umbraco.Web/Trees/ContentTreeControllerBase.cs # src/Umbraco.Web/Trees/TreeControllerBase.cs # src/Umbraco.Web/UmbracoHelper.cs # src/Umbraco.Web/umbraco.presentation/umbraco/settings/EditDictionaryItem.aspx # src/Umbraco.Web/umbraco.presentation/umbraco/settings/EditDictionaryItem.aspx.cs # src/Umbraco.Web/umbraco.presentation/umbraco/settings/EditDictionaryItem.aspx.designer.cs
2018-07-11 15:58:48 +10:00
Security.CurrentUser.GetUserCulture(Services.TextService, GlobalSettings),
new Dictionary<string, string> { { "0", key } });
return Request.CreateNotificationValidationErrorResponse(message);
}
try
{
Guid? parentGuid = null;
if (parentId > 0)
parentGuid = Services.LocalizationService.GetDictionaryItemById(parentId).Key;
var item = Services.LocalizationService.CreateDictionaryItemWithIdentity(
key,
parentGuid,
string.Empty);
return Request.CreateResponse(HttpStatusCode.OK, item.Id);
}
catch (Exception ex)
{
Logger.Error(GetType(), ex, "Error creating dictionary with {Name} under {ParentId}", key, parentId);
return Request.CreateNotificationValidationErrorResponse("Error creating dictionary item");
}
}
/// <summary>
/// Gets a dictionary item by id
/// </summary>
/// <param name="id">
/// The id.
/// </param>
/// <returns>
/// The <see cref="DictionaryDisplay"/>.
/// </returns>
/// <exception cref="HttpResponseException">
/// Returrns a not found response when dictionary item does not exist
/// </exception>
public DictionaryDisplay GetById(int id)
{
var dictionary = Services.LocalizationService.GetDictionaryItemById(id);
if (dictionary == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
return Mapper.Map<IDictionaryItem, DictionaryDisplay>(dictionary);
}
/// <summary>
/// Saves a dictionary item
/// </summary>
/// <param name="dictionary">
/// The dictionary.
/// </param>
/// <returns>
/// The <see cref="DictionaryDisplay"/>.
/// </returns>
public DictionaryDisplay PostSave(DictionarySave dictionary)
{
var dictionaryItem =
Services.LocalizationService.GetDictionaryItemById(int.Parse(dictionary.Id.ToString()));
if (dictionaryItem == null)
throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse("Dictionary item does not exist"));
Merge remote-tracking branch 'origin/dev-v7' into temp8 (after fixing) # Conflicts: # build/Modules/Umbraco.Build/Build-UmbracoDocs.ps1 # src/SolutionInfo.cs # src/Umbraco.Core/Configuration/UmbracoVersion.cs # src/Umbraco.Core/Constants-Applications.cs # src/Umbraco.Core/Models/Rdbms/ConsentDto.cs # src/Umbraco.Core/Persistence/DatabaseSchemaHelper.cs # src/Umbraco.Core/Persistence/Repositories/AuditRepository.cs # src/Umbraco.Core/PropertyEditors/ValueConverters/GridValueConverter.cs # src/Umbraco.Core/Services/ConsentService.cs # src/Umbraco.Core/Services/IRelationService.cs # src/Umbraco.Core/Services/RelationService.cs # src/Umbraco.Tests/Plugins/PluginManagerTests.cs # src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs # src/Umbraco.Tests/Services/ConsentServiceTests.cs # src/Umbraco.Web.UI.Client/src/common/directives/components/content/edit.controller.js # src/Umbraco.Web.UI.Client/src/common/directives/components/editor/umbeditorheader.directive.js # src/Umbraco.Web.UI.Client/src/common/directives/components/tree/umbtree.directive.js # src/Umbraco.Web.UI.Client/src/common/resources/contenttype.resource.js # src/Umbraco.Web.UI.Client/src/common/resources/entity.resource.js # src/Umbraco.Web.UI.Client/src/common/services/tree.service.js # src/Umbraco.Web.UI.Client/src/less/application/grid.less # src/Umbraco.Web.UI.Client/src/less/forms.less # src/Umbraco.Web.UI.Client/src/less/main.less # src/Umbraco.Web.UI.Client/src/views/common/dialogs/login.controller.js # src/Umbraco.Web.UI.Client/src/views/common/dialogs/login.html # src/Umbraco.Web.UI.Client/src/views/common/overlays/iconpicker/iconpicker.controller.js # src/Umbraco.Web.UI.Client/src/views/common/overlays/iconpicker/iconpicker.html # src/Umbraco.Web.UI.Client/src/views/common/overlays/mediaPicker/mediapicker.controller.js # src/Umbraco.Web.UI.Client/src/views/common/overlays/treepicker/treepicker.controller.js # src/Umbraco.Web.UI.Client/src/views/components/content/umb-content-node-info.html # src/Umbraco.Web.UI.Client/src/views/components/html/umb-control-group.html # src/Umbraco.Web.UI.Client/src/views/components/users/change-password.html # src/Umbraco.Web.UI.Client/src/views/content/content.copy.controller.js # src/Umbraco.Web.UI.Client/src/views/content/content.move.controller.js # src/Umbraco.Web.UI.Client/src/views/datatypes/datatype.edit.controller.js # src/Umbraco.Web.UI.Client/src/views/datatypes/edit.html # src/Umbraco.Web.UI.Client/src/views/documenttypes/views/permissions/permissions.html # src/Umbraco.Web.UI.Client/src/views/media/media.move.controller.js # src/Umbraco.Web.UI.Client/src/views/mediatypes/edit.controller.js # src/Umbraco.Web.UI.Client/src/views/mediatypes/views/permissions/permissions.html # src/Umbraco.Web.UI.Client/src/views/propertyeditors/boolean/boolean.controller.js # src/Umbraco.Web.UI.Client/src/views/propertyeditors/contentpicker/contentpicker.controller.js # src/Umbraco.Web.UI.Client/src/views/users/views/user/details.html # src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml # src/Umbraco.Web.UI/config/EmbeddedMedia.Release.config # src/Umbraco.Web.UI/config/EmbeddedMedia.config # src/Umbraco.Web.UI/config/trees.Release.config # src/Umbraco.Web.UI/config/trees.config # src/Umbraco.Web.UI/config/umbracoSettings.config # src/Umbraco.Web.UI/umbraco/Views/Default.cshtml # src/Umbraco.Web.UI/umbraco/config/create/UI.xml # src/Umbraco.Web.UI/umbraco/config/lang/da.xml # src/Umbraco.Web.UI/umbraco/config/lang/en.xml # src/Umbraco.Web.UI/umbraco/config/lang/en_us.xml # src/Umbraco.Web.UI/umbraco/config/lang/it.xml # src/Umbraco.Web.UI/umbraco/config/lang/nl.xml # src/Umbraco.Web.UI/umbraco/config/lang/ru.xml # src/Umbraco.Web/Editors/BackOfficeController.cs # src/Umbraco.Web/Editors/BackOfficeServerVariables.cs # src/Umbraco.Web/Editors/ContentTypeController.cs # src/Umbraco.Web/Editors/MacroController.cs # src/Umbraco.Web/Editors/MemberController.cs # src/Umbraco.Web/Models/RegisterModel.cs # src/Umbraco.Web/PropertyEditors/TagsPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/ValueConverters/MacroContainerValueConverter.cs # src/Umbraco.Web/PublishedContentExtensions.cs # src/Umbraco.Web/Trees/ContentTreeController.cs # src/Umbraco.Web/Trees/ContentTreeControllerBase.cs # src/Umbraco.Web/Trees/TreeControllerBase.cs # src/Umbraco.Web/UmbracoHelper.cs # src/Umbraco.Web/umbraco.presentation/umbraco/settings/EditDictionaryItem.aspx # src/Umbraco.Web/umbraco.presentation/umbraco/settings/EditDictionaryItem.aspx.cs # src/Umbraco.Web/umbraco.presentation/umbraco/settings/EditDictionaryItem.aspx.designer.cs
2018-07-11 15:58:48 +10:00
var userCulture = Security.CurrentUser.GetUserCulture(Services.TextService, GlobalSettings);
if (dictionary.NameIsDirty)
{
// if the name (key) has changed, we need to check if the new key does not exist
var dictionaryByKey = Services.LocalizationService.GetDictionaryItemByKey(dictionary.Name);
if (dictionaryByKey != null && dictionaryItem.Id != dictionaryByKey.Id)
{
var message = Services.TextService.Localize(
"dictionaryItem/changeKeyError",
userCulture,
new Dictionary<string, string> { { "0", dictionary.Name } });
ModelState.AddModelError("Name", message);
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
}
dictionaryItem.ItemKey = dictionary.Name;
}
foreach (var translation in dictionary.Translations)
{
Services.LocalizationService.AddOrUpdateDictionaryValue(dictionaryItem,
Services.LocalizationService.GetLanguageById(translation.LanguageId), translation.Translation);
}
try
{
Services.LocalizationService.Save(dictionaryItem);
var model = Mapper.Map<IDictionaryItem, DictionaryDisplay>(dictionaryItem);
model.Notifications.Add(new Notification(
Services.TextService.Localize("speechBubbles/dictionaryItemSaved", userCulture), string.Empty,
SpeechBubbleIcon.Success));
return model;
}
catch (Exception ex)
{
Logger.Error(GetType(), ex, "Error saving dictionary with {Name} under {ParentId}", dictionary.Name, dictionary.ParentId);
throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse("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 list = new List<DictionaryOverviewDisplay>();
const int level = 0;
foreach (var dictionaryItem in Services.LocalizationService.GetRootDictionaryItems().OrderBy(ItemSort()))
{
var item = Mapper.Map<IDictionaryItem, DictionaryOverviewDisplay>(dictionaryItem);
item.Level = 0;
list.Add(item);
GetChildItemsForList(dictionaryItem, level + 1, list);
}
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, List<DictionaryOverviewDisplay> list)
{
foreach (var childItem in Services.LocalizationService.GetDictionaryItemChildren(dictionaryItem.Key).OrderBy(ItemSort()))
{
var item = Mapper.Map<IDictionaryItem, DictionaryOverviewDisplay>(childItem);
item.Level = level;
list.Add(item);
GetChildItemsForList(childItem, level + 1, list);
}
}
private Func<IDictionaryItem, string> ItemSort() => item => item.ItemKey;
}
}