public static JArray ConvertFromNestedContent(JArray input) { if (input == null) { return(null); } var result = new List <EmbeddedContentItem>(); // ReSharper disable once PossibleInvalidCastExceptionInForeachLoop foreach (IDictionary <string, JToken> token in input) { JToken ncContentTypeAlias = token["ncContentTypeAlias"]; // if the token doesn't contain a ncAlias, we assume that none of the others does // and return the original value immediately if (ncContentTypeAlias == null) { return(input); } var embeddedContentItem = new EmbeddedContentItem { Name = token["name"]?.Value <string>(), ContentTypeAlias = ncContentTypeAlias.Value <string>(), Key = Guid.NewGuid(), Properties = token.ToDictionary(_ => _.Key, _ => (object)_.Value), Published = token["ncDisabled"]?.Value <string>() != "1", }; result.Add(embeddedContentItem); } return(JArray.FromObject(result)); }
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview) { EmbeddedContentConfig config = GetConfig(propertyType.DataTypeId); using (_profilingLogger.DebugDuration <EmbeddedContentValueConverter>($"ConvertSourceToObject({propertyType.PropertyTypeAlias})")) { if (source == null) { if (config.MaxItems == 1) { return(null); } return(Enumerable.Empty <IPublishedContent>()); } ; var items = ((JArray)source).ToObject <EmbeddedContentItem[]>(); var result = new List <IPublishedContent>(items.Length); PublishedContentSet <IPublishedContent> contentSet = result.ToContentSet(); for (var i = 0; i < items.Length; i++) { EmbeddedContentItem item = items[i]; if (!item.Published) { continue; } if (config.DocumentTypes.FirstOrDefault(x => x.DocumentTypeAlias == item.ContentTypeAlias) == null) { continue; } PublishedContentType contentType = null; try { contentType = PublishedContentType.Get(PublishedItemType.Content, item.ContentTypeAlias); } catch (Exception ex) { _profilingLogger.Logger.Error <EmbeddedContentValueConverter>($"Error getting content type {item.ContentTypeAlias}.", ex); } if (contentType == null) { continue; } IPublishedContent content = new PublishedEmbeddedContent(_userService, item, contentType, contentSet, i, preview); if (_publishedContentModelFactory != null) { content = _publishedContentModelFactory.CreateModel(content); } result.Add(content); } if (config.MaxItems == 1) { return(result.FirstOrDefault()); } return(contentSet); } }
public override object ConvertEditorToDb(ContentPropertyData editorValue, object currentValue) { if (string.IsNullOrEmpty(editorValue.Value?.ToString())) { return(null); } using (_profilingLogger.DebugDuration <EmbeddedContentPropertyEditor>("ConvertEditorToDb()")) { List <IContentType> contentTypes = _contentTypeService.GetAllContentTypes().ToList(); var itemsDisplay = JsonConvert.DeserializeObject <EmbeddedContentItemDisplay[]>(editorValue.Value.ToString()); var currentItems = JsonConvert.DeserializeObject <EmbeddedContentItem[]>(currentValue?.ToString() ?? "[]"); var items = new List <EmbeddedContentItem>(); IEnumerable <ContentItemFile> files = null; if (editorValue.AdditionalData.TryGetValue("files", out object tmp)) { files = tmp as IEnumerable <ContentItemFile>; } foreach (EmbeddedContentItemDisplay itemDisplay in itemsDisplay) { var item = new EmbeddedContentItem { ContentTypeAlias = itemDisplay.ContentTypeAlias, Key = itemDisplay.Key, Name = itemDisplay.Name, Published = itemDisplay.Published, CreateDate = itemDisplay.CreateDate, UpdateDate = itemDisplay.UpdateDate, CreatorId = itemDisplay.CreatorId, WriterId = itemDisplay.WriterId }; IContentType contentType = contentTypes.FirstOrDefault(x => x.Alias == itemDisplay.ContentTypeAlias); if (contentType == null) { continue; } if (item.CreateDate == DateTime.MinValue) { item.CreateDate = DateTime.UtcNow; if (_security?.CurrentUser != null) { item.CreatorId = _security.CurrentUser.Id; } } EmbeddedContentItem currentItem = currentItems.FirstOrDefault(x => x.Key == itemDisplay.Key); if (currentItem != null) { item.CreateDate = currentItem.CreateDate; item.UpdateDate = currentItem.UpdateDate; item.CreatorId = currentItem.CreatorId; item.WriterId = currentItem.WriterId; } foreach (PropertyType propertyType in contentType.CompositionPropertyGroups.SelectMany(x => x.PropertyTypes)) { IEnumerable <Tab <EmbeddedContentPropertyDisplay> > tabs = itemDisplay.Tabs; if (itemDisplay.SettingsTab != null) { tabs = tabs.Concat(new[] { itemDisplay.SettingsTab }); } EmbeddedContentPropertyDisplay property = tabs.SelectMany(x => x.Properties).FirstOrDefault(x => x.Alias == propertyType.Alias); if (property == null) { continue; } PropertyEditor propertyEditor = _propertyEditorResolver.GetByAlias(propertyType.PropertyEditorAlias); PreValueCollection preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId); var additionalData = new Dictionary <string, object>(); if (files != null) { if (propertyEditor.Alias == EmbeddedContent.Constants.PropertyEditorAlias) { additionalData["files"] = files; } else if (property.SelectedFiles != null) { additionalData["files"] = files.Where(x => property.SelectedFiles.Contains(x.FileName)); } additionalData["cuid"] = editorValue.AdditionalData["cuid"]; additionalData["puid"] = editorValue.AdditionalData["puid"]; } var propData = new ContentPropertyData(property.Value, preValues, additionalData); object currentPropertyValue = null; if (currentItem != null && currentItem.Properties.TryGetValue(property.Alias, out currentPropertyValue)) { } item.Properties[propertyType.Alias] = propertyEditor.ValueEditor.ConvertEditorToDb(propData, currentPropertyValue); } if (currentItem == null || currentItem.Name != item.Name || currentItem.Published != item.Published || JsonConvert.SerializeObject(currentItem.Properties) != JsonConvert.SerializeObject(item.Properties)) { item.UpdateDate = DateTime.UtcNow; if (_security?.CurrentUser != null) { item.WriterId = _security.CurrentUser.Id; } } items.Add(item); } if (items.Count == 0) { return(null); } return(JsonConvert.SerializeObject(items)); } }