/// <summary> /// Transform content to a edit model. /// </summary> /// <param name="content">The dynamic content object</param> /// <returns>Edit model</returns> private async Task <ContentEditModel> Transform(DynamicContent content) { var config = new Config(_api); var type = App.ContentTypes.GetById(content.TypeId); var languages = await _api.Languages.GetAllAsync(); var categories = await _api.Content.GetAllCategoriesAsync(type.Group); var tags = await _api.Content.GetAllTagsAsync(type.Group); var model = new ContentEditModel { Id = content.Id, TypeId = content.TypeId, PrimaryImage = content.PrimaryImage, Title = content.Title, Excerpt = content.Excerpt, UseBlocks = type.UseBlocks, UseCategory = type.UseCategory, UseTags = type.UseTags, UsePrimaryImage = type.UsePrimaryImage, UseExcerpt = type.UseExcerpt, UseHtmlExcerpt = config.HtmlExcerpt, UseTranslations = languages.Count() > 1, Languages = languages, SelectedCategory = type.UseCategory ? content.Category?.Title : null, SelectedTags = type.UseTags ? content.Tags.Select(t => t.Title).ToList() : null, Categories = categories.Select(c => c.Title).ToList(), Tags = tags.Select(c => c.Title).ToList() }; foreach (var regionType in type.Regions) { var region = new Models.Content.RegionModel { Meta = new Models.Content.RegionMeta { Id = regionType.Id, Name = regionType.Title, Description = regionType.Description, Placeholder = regionType.ListTitlePlaceholder, IsCollection = regionType.Collection, Expanded = regionType.ListExpand, Icon = regionType.Icon, Display = regionType.Display.ToString().ToLower() } }; var regionListModel = ((IDictionary <string, object>)content.Regions)[regionType.Id]; if (!regionType.Collection) { var regionModel = (IRegionList)Activator.CreateInstance(typeof(RegionList <>).MakeGenericType(regionListModel.GetType())); regionModel.Add(regionListModel); regionListModel = regionModel; } foreach (var regionModel in (IEnumerable)regionListModel) { var regionItem = new Models.Content.RegionItemModel(); foreach (var fieldType in regionType.Fields) { var appFieldType = App.Fields.GetByType(fieldType.Type); var field = new Models.Content.FieldModel { Meta = new Models.Content.FieldMeta { Id = fieldType.Id, Name = fieldType.Title, Component = appFieldType.Component, Placeholder = fieldType.Placeholder, IsHalfWidth = fieldType.Options.HasFlag(FieldOption.HalfWidth), IsTranslatable = typeof(ITranslatable).IsAssignableFrom(appFieldType.Type), Description = fieldType.Description, Settings = fieldType.Settings } }; if (typeof(SelectFieldBase).IsAssignableFrom(appFieldType.Type)) { foreach (var item in ((SelectFieldBase)Activator.CreateInstance(appFieldType.Type)).Items) { field.Meta.Options.Add(Convert.ToInt32(item.Value), item.Title); } } if (regionType.Fields.Count > 1) { field.Model = (IField)((IDictionary <string, object>)regionModel)[fieldType.Id]; if (regionType.ListTitleField == fieldType.Id) { regionItem.Title = field.Model.GetTitle(); field.Meta.NotifyChange = true; } } else { field.Model = (IField)regionModel; field.Meta.NotifyChange = true; regionItem.Title = field.Model.GetTitle(); } regionItem.Fields.Add(field); } if (string.IsNullOrWhiteSpace(regionItem.Title)) { regionItem.Title = "..."; } region.Items.Add(regionItem); } model.Regions.Add(region); } if (type.UseBlocks) { model.UseBlocks = true; foreach (var block in content.Blocks) { var blockType = App.Blocks.GetByType(block.Type); if (block is BlockGroup) { var group = new Models.Content.BlockGroupModel { Id = block.Id, Type = block.Type, Meta = new Models.Content.BlockMeta { Name = blockType.Name, Icon = blockType.Icon, Component = blockType.Component, Width = blockType.Width.ToString().ToLower(), IsGroup = true, isCollapsed = config.ManagerDefaultCollapsedBlocks, ShowHeader = !config.ManagerDefaultCollapsedBlockGroupHeaders } }; group.Fields = ContentUtils.GetBlockFields(block); bool firstChild = true; foreach (var child in ((BlockGroup)block).Items) { blockType = App.Blocks.GetByType(child.Type); if (!blockType.IsGeneric) { // Regular block item model group.Items.Add(new Models.Content.BlockItemModel { IsActive = firstChild, Model = child, Meta = new Models.Content.BlockMeta { Name = blockType.Name, Title = child.GetTitle(), Icon = blockType.Icon, Component = blockType.Component, Width = blockType.Width.ToString().ToLower() } }); } else { // Generic block item model group.Items.Add(new Models.Content.BlockGenericModel { Id = child.Id, IsActive = firstChild, Model = ContentUtils.GetBlockFields(child), Type = child.Type, Meta = new Models.Content.BlockMeta { Name = blockType.Name, Title = child.GetTitle(), Icon = blockType.Icon, Component = blockType.Component, Width = blockType.Width.ToString().ToLower() } }); } firstChild = false; } model.Blocks.Add(group); } else { if (!blockType.IsGeneric) { // Regular block item model model.Blocks.Add(new Models.Content.BlockItemModel { Model = block, Meta = new Models.Content.BlockMeta { Name = blockType.Name, Title = block.GetTitle(), Icon = blockType.Icon, Component = blockType.Component, Width = blockType.Width.ToString().ToLower(), isCollapsed = config.ManagerDefaultCollapsedBlocks } }); } else { // Generic block item model model.Blocks.Add(new Models.Content.BlockGenericModel { Id = block.Id, Model = ContentUtils.GetBlockFields(block), Type = block.Type, Meta = new Models.Content.BlockMeta { Name = blockType.Name, Title = block.GetTitle(), Icon = blockType.Icon, Component = blockType.Component, Width = blockType.Width.ToString().ToLower(), isCollapsed = config.ManagerDefaultCollapsedBlocks } }); } } } } // Custom editors foreach (var editor in type.CustomEditors) { model.Editors.Add(new Models.Content.EditorModel { Component = editor.Component, Icon = editor.Icon, Name = editor.Title }); } return(model); }
/// <summary> /// Transform content to a edit model. /// </summary> /// <param name="content">The dynamic content object</param> /// <returns>Edit model</returns> private ContentEditModel Transform(DynamicContent content) { var config = new Config(_api); var type = App.ContentTypes.GetById(content.TypeId); var model = new ContentEditModel { Id = content.Id, TypeId = content.TypeId, PrimaryImage = content.PrimaryImage, Title = content.Title, UsePrimaryImage = type.UsePrimaryImage, UseExcerpt = type.UseExcerpt, UseHtmlExcerpt = config.HtmlExcerpt }; foreach (var regionType in type.Regions) { var region = new Models.Content.RegionModel { Meta = new Models.Content.RegionMeta { Id = regionType.Id, Name = regionType.Title, Description = regionType.Description, Placeholder = regionType.ListTitlePlaceholder, IsCollection = regionType.Collection, Expanded = regionType.ListExpand, Icon = regionType.Icon, Display = regionType.Display.ToString().ToLower() } }; var regionListModel = ((IDictionary <string, object>)content.Regions)[regionType.Id]; if (!regionType.Collection) { var regionModel = (IRegionList)Activator.CreateInstance(typeof(RegionList <>).MakeGenericType(regionListModel.GetType())); regionModel.Add(regionListModel); regionListModel = regionModel; } foreach (var regionModel in (IEnumerable)regionListModel) { var regionItem = new Models.Content.RegionItemModel(); foreach (var fieldType in regionType.Fields) { var appFieldType = App.Fields.GetByType(fieldType.Type); var field = new Models.Content.FieldModel { Meta = new Models.Content.FieldMeta { Id = fieldType.Id, Name = fieldType.Title, Component = appFieldType.Component, Placeholder = fieldType.Placeholder, IsHalfWidth = fieldType.Options.HasFlag(FieldOption.HalfWidth), Description = fieldType.Description } }; if (typeof(SelectFieldBase).IsAssignableFrom(appFieldType.Type)) { foreach (var item in ((SelectFieldBase)Activator.CreateInstance(appFieldType.Type)).Items) { field.Meta.Options.Add(Convert.ToInt32(item.Value), item.Title); } } if (regionType.Fields.Count > 1) { field.Model = (IField)((IDictionary <string, object>)regionModel)[fieldType.Id]; if (regionType.ListTitleField == fieldType.Id) { regionItem.Title = field.Model.GetTitle(); field.Meta.NotifyChange = true; } } else { field.Model = (IField)regionModel; field.Meta.NotifyChange = true; regionItem.Title = field.Model.GetTitle(); } regionItem.Fields.Add(field); } if (string.IsNullOrWhiteSpace(regionItem.Title)) { regionItem.Title = "..."; } region.Items.Add(regionItem); } model.Regions.Add(region); } // Custom editors foreach (var editor in type.CustomEditors) { model.Editors.Add(new Models.Content.EditorModel { Component = editor.Component, Icon = editor.Icon, Name = editor.Title }); } return(model); }