/// <summary> /// Creates and saves a new dictionary item and assigns a value to all languages if defaultValue is specified. /// </summary> /// <param name="key"></param> /// <param name="parentId"></param> /// <param name="defaultValue"></param> /// <returns></returns> public IDictionaryItem CreateDictionaryItemWithIdentity(string key, Guid?parentId, string?defaultValue = null) { using (ICoreScope scope = ScopeProvider.CreateCoreScope()) { // validate the parent if (parentId.HasValue && parentId.Value != Guid.Empty) { IDictionaryItem?parent = GetDictionaryItemById(parentId.Value); if (parent == null) { throw new ArgumentException($"No parent dictionary item was found with id {parentId.Value}."); } } var item = new DictionaryItem(parentId, key); if (defaultValue.IsNullOrWhiteSpace() == false) { IEnumerable <ILanguage> langs = GetAllLanguages(); var translations = langs.Select(language => new DictionaryTranslation(language, defaultValue !)) .Cast <IDictionaryTranslation>() .ToList(); item.Translations = translations; } EventMessages eventMessages = EventMessagesFactory.Get(); var savingNotification = new DictionaryItemSavingNotification(item, eventMessages); if (scope.Notifications.PublishCancelable(savingNotification)) { scope.Complete(); return(item); } _dictionaryRepository.Save(item); // ensure the lazy Language callback is assigned EnsureDictionaryItemLanguageCallback(item); scope.Notifications.Publish( new DictionaryItemSavedNotification(item, eventMessages).WithStateFrom(savingNotification)); scope.Complete(); return(item); } }
/// <summary> /// Saves a <see cref="IDictionaryItem"/> object /// </summary> /// <param name="dictionaryItem"><see cref="IDictionaryItem"/> to save</param> /// <param name="userId">Optional id of the user saving the dictionary item</param> public void Save(IDictionaryItem dictionaryItem, int userId = Cms.Core.Constants.Security.SuperUserId) { using (var scope = ScopeProvider.CreateScope()) { EventMessages eventMessages = EventMessagesFactory.Get(); var savingNotification = new DictionaryItemSavingNotification(dictionaryItem, eventMessages); if (scope.Notifications.PublishCancelable(savingNotification)) { scope.Complete(); return; } _dictionaryRepository.Save(dictionaryItem); // ensure the lazy Language callback is assigned // ensure the lazy Language callback is assigned EnsureDictionaryItemLanguageCallback(dictionaryItem); scope.Notifications.Publish(new DictionaryItemSavedNotification(dictionaryItem, eventMessages).WithStateFrom(savingNotification)); Audit(AuditType.Save, "Save DictionaryItem", userId, dictionaryItem.Id, "DictionaryItem"); scope.Complete(); } }