private void SetWidgetOptions(EditWidgetViewModel model, Widget content, bool treatNullsAsLists, bool isNew) { if (model.Options != null) { content.ContentOptions = new List <ContentOption>(); // NOTE: Loading custom options before saving. // In other case, when loading custom options from option service, nHibernate updates version number (nHibernate bug) var customOptionsIdentifiers = model.Options.Where(o => o.Type == OptionType.Custom).Select(o => o.CustomOption.Identifier).Distinct().ToArray(); var customOptions = optionService.GetCustomOptionsById(customOptionsIdentifiers); foreach (var requestContentOption in model.Options) { var contentOption = new ContentOption { Content = content, Key = requestContentOption.OptionKey, DefaultValue = optionService.ClearFixValueForSave( requestContentOption.OptionKey, requestContentOption.Type, requestContentOption.OptionDefaultValue), Type = requestContentOption.Type, CustomOption = requestContentOption.Type == OptionType.Custom ? repository.AsProxy <CustomOption>(customOptions.First(o => o.Identifier == requestContentOption.CustomOption.Identifier).Id) : null }; optionService.ValidateOptionValue(contentOption); if (cmsConfiguration.EnableMultilanguage && requestContentOption.Translations != null) { var translations = requestContentOption.Translations.Select(x => new ContentOptionTranslation { ContentOption = contentOption, Language = repository.AsProxy <Language>(x.LanguageId.ToGuidOrDefault()), Value = optionService.ClearFixValueForSave(requestContentOption.OptionKey, requestContentOption.Type, x.OptionValue) }).ToList(); foreach (var translation in translations) { optionService.ValidateOptionValue(contentOption.Key, translation.Value, contentOption.Type, contentOption.CustomOption); } contentOption.Translations = translations; } content.ContentOptions.Add(contentOption); } } else if (!treatNullsAsLists) { // When calling from API with null list, options should be loaded before process // Null from API means, that list should be kept unchanged content.ContentOptions = repository .AsQueryable <ContentOption>(pco => pco.Content.Id == model.Id) .Fetch(pco => pco.CustomOption) .ToList(); } }
private TEntity GetWidgetForSave <TEntity>(TEntity widgetContent, EditWidgetViewModel model, bool createIfNotExists, out bool isCreatingNew) where TEntity : Widget { TEntity widget, originalWidget = null; var createNewWithId = false; isCreatingNew = model.Id.HasDefaultValue(); if (createIfNotExists || !isCreatingNew) { originalWidget = repository.FirstOrDefault <TEntity>(model.Id); isCreatingNew = originalWidget == null; createNewWithId = isCreatingNew && !model.Id.HasDefaultValue(); } var dynamicContentContainer = widgetContent as IDynamicContentContainer; if (dynamicContentContainer != null && !isCreatingNew && !model.IsUserConfirmed) { contentService.CheckIfContentHasDeletingChildrenWithException(null, widgetContent.Id, dynamicContentContainer.Html); CheckIfContentHasDeletingWidgetsWithDynamicRegions(originalWidget, dynamicContentContainer.Html); } if (model.DesirableStatus == ContentStatus.Published) { if (isCreatingNew) { if (model.PublishedOn.HasValue) { widgetContent.PublishedOn = model.PublishedOn; } if (!string.IsNullOrEmpty(model.PublishedByUser)) { widgetContent.PublishedByUser = model.PublishedByUser; } } else { widgetContent.PublishedOn = originalWidget.PublishedOn; widgetContent.PublishedByUser = originalWidget.PublishedByUser; } } if (createNewWithId) { widget = widgetContent; contentService.UpdateDynamicContainer(widget); widget.Status = model.DesirableStatus; widget.Id = model.Id; } else { widget = (TEntity)contentService.SaveContentWithStatusUpdate(widgetContent, model.DesirableStatus); } return(widget); }
private void SetWidgetCategories(EditWidgetViewModel request, Widget content, bool treatNullsAsLists, bool isNew) { if (request.Categories != null) { content.Categories = new List <WidgetCategory>(); var categories = repository.AsQueryable <Category>() .Where(c => !c.CategoryTree.IsDeleted && c.CategoryTree.AvailableFor.Any(e => e.CategorizableItem.Name == content.GetCategorizableItemKey())) .ToList(); foreach (var categoryItem in request.Categories) { var category = categories.FirstOrDefault(c => c.Id == categoryItem.Key.ToGuidOrDefault()); if (category == null) { var message = string.Format(RootGlobalization.Validation_Category_Unavailable_Message, categoryItem.Value); throw new ValidationException(() => message, message); } var widgetCategory = new WidgetCategory { Widget = content, Category = repository.AsProxy <Category>(category.Id) }; content.Categories.Add(widgetCategory); } } else if (!treatNullsAsLists) { // When calling from API with null list, categories should be loaded before process. // Null from API means, that list should be kept unchanged. content.Categories = repository .AsQueryable <WidgetCategory>(pco => pco.Widget.Id == request.Id) .Fetch(pco => pco.Category) .ToList(); } }