コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Creates a new block of the specified type.
        /// </summary>
        /// <param name="type">The block type</param>
        /// <returns>The new block</returns>
        public async Task <AsyncResult <BlockModel> > CreateBlockAsync(string type)
        {
            var blockType = App.Blocks.GetByType(type);

            if (blockType != null)
            {
                var block = (Extend.Block)(await _factory.CreateBlockAsync(type));

                if (block is Extend.BlockGroup)
                {
                    var item = new BlockGroupModel
                    {
                        Type = block.Type,
                        Meta = new BlockMeta
                        {
                            Name      = blockType.Name,
                            Title     = block.GetTitle(),
                            Icon      = blockType.Icon,
                            Component = "block-group",
                            IsGroup   = true
                        }
                    };

                    if (blockType.Display != BlockDisplayMode.MasterDetail)
                    {
                        item.Meta.Component = blockType.Display == BlockDisplayMode.Horizontal ?
                                              "block-group-horizontal" : "block-group-vertical";
                    }

                    item.Fields = ContentUtils.GetBlockFields(block);

                    return(new AsyncResult <BlockModel>
                    {
                        Body = item
                    });
                }
                else
                {
                    if (!blockType.IsGeneric)
                    {
                        // Regular block model
                        return(new AsyncResult <BlockModel>
                        {
                            Body = new BlockItemModel
                            {
                                Model = block,
                                Meta = new BlockMeta
                                {
                                    Name = blockType.Name,
                                    Title = block.GetTitle(),
                                    Icon = blockType.Icon,
                                    Component = blockType.Component
                                }
                            }
                        });
                    }
                    else
                    {
                        // Generic block model
                        return(new AsyncResult <BlockModel>
                        {
                            Body = new BlockGenericModel
                            {
                                Model = ContentUtils.GetBlockFields(block),
                                Type = block.Type,
                                Meta = new BlockMeta
                                {
                                    Name = blockType.Name,
                                    Title = block.GetTitle(),
                                    Icon = blockType.Icon,
                                    Component = blockType.Component
                                }
                            }
                        });
                    }
                }
            }
            return(null);
        }
コード例 #3
0
        /// <summary>
        /// Save content
        /// </summary>
        /// <param name="model">The edit model</param>
        public async Task SaveAsync(ContentEditModel model)
        {
            var contentType = App.ContentTypes.GetById(model.TypeId);

            if (contentType != null)
            {
                if (model.Id == Guid.Empty)
                {
                    model.Id = Guid.NewGuid();
                }

                var content = await _api.Content.GetByIdAsync(model.Id, model.LanguageId);

                if (content == null)
                {
                    content = await _factory.CreateAsync <DynamicContent>(contentType);

                    content.Id = model.Id;
                }

                content.TypeId       = model.TypeId;
                content.Title        = model.Title;
                content.Excerpt      = model.Excerpt;
                content.PrimaryImage = model.PrimaryImage;

                // Save category
                if (contentType.UseCategory)
                {
                    content.Category = new Taxonomy
                    {
                        Title = model.SelectedCategory
                    };
                }

                // Save tags
                if (contentType.UseTags)
                {
                    content.Tags.Clear();
                    foreach (var tag in model.SelectedTags)
                    {
                        content.Tags.Add(new Taxonomy
                        {
                            Title = tag
                        });
                    }
                }

                // Save regions
                foreach (var region in contentType.Regions)
                {
                    var modelRegion = model.Regions
                                      .FirstOrDefault(r => r.Meta.Id == region.Id);

                    if (region.Collection)
                    {
                        var listRegion = (IRegionList)((IDictionary <string, object>)content.Regions)[region.Id];

                        listRegion.Clear();

                        foreach (var item in modelRegion.Items)
                        {
                            if (region.Fields.Count == 1)
                            {
                                listRegion.Add(item.Fields[0].Model);
                            }
                            else
                            {
                                var postRegion = new ExpandoObject();

                                foreach (var field in region.Fields)
                                {
                                    var modelField = item.Fields
                                                     .FirstOrDefault(f => f.Meta.Id == field.Id);
                                    ((IDictionary <string, object>)postRegion)[field.Id] = modelField.Model;
                                }
                                listRegion.Add(postRegion);
                            }
                        }
                    }
                    else
                    {
                        var postRegion = ((IDictionary <string, object>)content.Regions)[region.Id];

                        if (region.Fields.Count == 1)
                        {
                            ((IDictionary <string, object>)content.Regions)[region.Id] =
                                modelRegion.Items[0].Fields[0].Model;
                        }
                        else
                        {
                            foreach (var field in region.Fields)
                            {
                                var modelField = modelRegion.Items[0].Fields
                                                 .FirstOrDefault(f => f.Meta.Id == field.Id);
                                ((IDictionary <string, object>)postRegion)[field.Id] = modelField.Model;
                            }
                        }
                    }
                }

                // Save blocks
                content.Blocks.Clear();

                foreach (var block in model.Blocks)
                {
                    if (block is Models.Content.BlockGroupModel blockGroup)
                    {
                        var groupType = App.Blocks.GetByType(blockGroup.Type);

                        if (groupType != null)
                        {
                            var pageBlock = (BlockGroup)Activator.CreateInstance(groupType.Type);

                            pageBlock.Id   = blockGroup.Id;
                            pageBlock.Type = blockGroup.Type;

                            foreach (var field in blockGroup.Fields)
                            {
                                var prop = pageBlock.GetType().GetProperty(field.Meta.Id, App.PropertyBindings);
                                prop.SetValue(pageBlock, field.Model);
                            }

                            foreach (var item in blockGroup.Items)
                            {
                                if (item is Models.Content.BlockItemModel blockItem)
                                {
                                    pageBlock.Items.Add(blockItem.Model);
                                }
                                else if (item is Models.Content.BlockGenericModel blockGeneric)
                                {
                                    var transformed = ContentUtils.TransformGenericBlock(blockGeneric);

                                    if (transformed != null)
                                    {
                                        pageBlock.Items.Add(transformed);
                                    }
                                }
                            }
                            content.Blocks.Add(pageBlock);
                        }
                    }
                    else if (block is Models.Content.BlockItemModel blockItem)
                    {
                        content.Blocks.Add(blockItem.Model);
                    }
                    else if (block is Models.Content.BlockGenericModel blockGeneric)
                    {
                        var transformed = ContentUtils.TransformGenericBlock(blockGeneric);

                        if (transformed != null)
                        {
                            content.Blocks.Add(transformed);
                        }
                    }
                }

                // Save content
                await _api.Content.SaveAsync(content, model.LanguageId);
            }
            else
            {
                throw new ValidationException("Invalid Content Type.");
            }
        }
コード例 #4
0
        private PostEditModel Transform(DynamicPost post, bool isDraft)
        {
            var config = new Config(_api);
            var type   = App.PostTypes.GetById(post.TypeId);
            var route  = type.Routes.FirstOrDefault(r => r.Route == post.Route) ?? type.Routes.FirstOrDefault();

            var model = new PostEditModel
            {
                Id                     = post.Id,
                BlogId                 = post.BlogId,
                TypeId                 = post.TypeId,
                PrimaryImage           = post.PrimaryImage,
                Title                  = post.Title,
                Slug                   = post.Slug,
                MetaTitle              = post.MetaTitle,
                MetaKeywords           = post.MetaKeywords,
                MetaDescription        = post.MetaDescription,
                OgTitle                = post.OgTitle,
                OgDescription          = post.OgDescription,
                OgImage                = post.OgImage,
                Excerpt                = post.Excerpt,
                Published              = post.Published?.ToString("yyyy-MM-dd HH:mm"),
                RedirectUrl            = post.RedirectUrl,
                RedirectType           = post.RedirectType.ToString(),
                EnableComments         = post.EnableComments,
                CloseCommentsAfterDays = post.CloseCommentsAfterDays,
                CommentCount           = post.CommentCount,
                State                  = post.GetState(isDraft),
                UseBlocks              = type.UseBlocks,
                UsePrimaryImage        = type.UsePrimaryImage,
                UseExcerpt             = type.UseExcerpt,
                UseHtmlExcerpt         = config.HtmlExcerpt,
                SelectedRoute          = route == null ? null : new RouteModel
                {
                    Title = route.Title,
                    Route = route.Route
                },
                Permissions = App.Permissions
                              .GetPublicPermissions()
                              .Select(p => new KeyValuePair <string, string>(p.Name, p.Title))
                              .ToList(),
                SelectedPermissions = post.Permissions
            };

            foreach (var r in type.Routes)
            {
                model.Routes.Add(new RouteModel
                {
                    Title = r.Title,
                    Route = r.Route
                });
            }

            foreach (var regionType in type.Regions)
            {
                var region = new RegionModel
                {
                    Meta = new 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>)post.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 RegionItemModel();

                    foreach (var fieldType in regionType.Fields)
                    {
                        var appFieldType = App.Fields.GetByType(fieldType.Type);

                        var field = new FieldModel
                        {
                            Meta = new 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);
            }

            foreach (var block in post.Blocks)
            {
                var blockType = App.Blocks.GetByType(block.Type);

                if (block is BlockGroup blockGroup)
                {
                    var group = new BlockGroupModel
                    {
                        Id   = block.Id,
                        Type = block.Type,
                        Meta = new BlockMeta
                        {
                            Name        = blockType.Name,
                            Icon        = blockType.Icon,
                            Component   = "block-group",
                            IsGroup     = true,
                            isCollapsed = config.ManagerDefaultCollapsedBlocks,
                            ShowHeader  = !config.ManagerDefaultCollapsedBlockGroupHeaders
                        }
                    };

                    if (blockType.Display != BlockDisplayMode.MasterDetail)
                    {
                        group.Meta.Component = blockType.Display == BlockDisplayMode.Horizontal ?
                                               "block-group-horizontal" : "block-group-vertical";
                    }

                    group.Fields = ContentUtils.GetBlockFields(block);

                    bool firstChild = true;
                    foreach (var child in blockGroup.Items)
                    {
                        blockType = App.Blocks.GetByType(child.Type);

                        if (!blockType.IsGeneric)
                        {
                            group.Items.Add(new BlockItemModel
                            {
                                IsActive = firstChild,
                                Model    = child,
                                Meta     = new BlockMeta
                                {
                                    Name      = blockType.Name,
                                    Title     = child.GetTitle(),
                                    Icon      = blockType.Icon,
                                    Component = blockType.Component
                                }
                            });
                        }
                        else
                        {
                            // Generic block item model
                            group.Items.Add(new BlockGenericModel
                            {
                                IsActive = firstChild,
                                Model    = ContentUtils.GetBlockFields(child),
                                Type     = child.Type,
                                Meta     = new BlockMeta
                                {
                                    Name      = blockType.Name,
                                    Title     = child.GetTitle(),
                                    Icon      = blockType.Icon,
                                    Component = blockType.Component,
                                }
                            });
                        }
                        firstChild = false;
                    }
                    model.Blocks.Add(group);
                }
                else
                {
                    if (!blockType.IsGeneric)
                    {
                        // Regular block item model
                        model.Blocks.Add(new BlockItemModel
                        {
                            Model = block,
                            Meta  = new BlockMeta
                            {
                                Name        = blockType.Name,
                                Title       = block.GetTitle(),
                                Icon        = blockType.Icon,
                                Component   = blockType.Component,
                                isCollapsed = config.ManagerDefaultCollapsedBlocks
                            }
                        });
                    }
                    else
                    {
                        // Generic block item model
                        model.Blocks.Add(new BlockGenericModel
                        {
                            Model = ContentUtils.GetBlockFields(block),
                            Type  = block.Type,
                            Meta  = new BlockMeta
                            {
                                Name        = blockType.Name,
                                Title       = block.GetTitle(),
                                Icon        = blockType.Icon,
                                Component   = blockType.Component,
                                isCollapsed = config.ManagerDefaultCollapsedBlocks
                            }
                        });
                    }
                }
            }

            // Custom editors
            foreach (var editor in type.CustomEditors)
            {
                model.Editors.Add(new EditorModel
                {
                    Component = editor.Component,
                    Icon      = editor.Icon,
                    Name      = editor.Title
                });
            }
            return(model);
        }
コード例 #5
0
        public async Task Save(PostEditModel model, bool draft)
        {
            var postType = App.PostTypes.GetById(model.TypeId);

            if (postType != null)
            {
                if (model.Id == Guid.Empty)
                {
                    model.Id = Guid.NewGuid();
                }

                var post = await _api.Posts.GetByIdAsync(model.Id);

                if (post == null)
                {
                    post = await _factory.CreateAsync <DynamicPost>(postType);

                    post.Id = model.Id;
                }

                post.BlogId                 = model.BlogId;
                post.TypeId                 = model.TypeId;
                post.Title                  = model.Title;
                post.Slug                   = model.Slug;
                post.MetaTitle              = model.MetaTitle;
                post.MetaKeywords           = model.MetaKeywords;
                post.MetaDescription        = model.MetaDescription;
                post.OgTitle                = model.OgTitle;
                post.OgDescription          = model.OgDescription;
                post.OgImage                = model.OgImage;
                post.Excerpt                = model.Excerpt;
                post.Published              = !string.IsNullOrEmpty(model.Published) ? DateTime.Parse(model.Published) : (DateTime?)null;
                post.RedirectUrl            = model.RedirectUrl;
                post.RedirectType           = (RedirectType)Enum.Parse(typeof(RedirectType), model.RedirectType);
                post.EnableComments         = model.EnableComments;
                post.CloseCommentsAfterDays = model.CloseCommentsAfterDays;
                post.Permissions            = model.SelectedPermissions;
                post.PrimaryImage           = model.PrimaryImage;

                if (postType.Routes.Count > 1)
                {
                    post.Route = postType.Routes.FirstOrDefault(r => r.Route == model.SelectedRoute?.Route)
                                 ?? postType.Routes.First();
                }

                // Save category
                post.Category = new Taxonomy
                {
                    Title = model.SelectedCategory
                };

                // Save tags
                post.Tags.Clear();
                foreach (var tag in model.SelectedTags)
                {
                    post.Tags.Add(new Taxonomy
                    {
                        Title = tag
                    });
                }

                // Save regions
                foreach (var region in postType.Regions)
                {
                    var modelRegion = model.Regions
                                      .FirstOrDefault(r => r.Meta.Id == region.Id);

                    if (region.Collection)
                    {
                        var listRegion = (IRegionList)((IDictionary <string, object>)post.Regions)[region.Id];

                        listRegion.Clear();

                        foreach (var item in modelRegion.Items)
                        {
                            if (region.Fields.Count == 1)
                            {
                                listRegion.Add(item.Fields[0].Model);
                            }
                            else
                            {
                                var postRegion = new ExpandoObject();

                                foreach (var field in region.Fields)
                                {
                                    var modelField = item.Fields
                                                     .FirstOrDefault(f => f.Meta.Id == field.Id);
                                    ((IDictionary <string, object>)postRegion)[field.Id] = modelField.Model;
                                }
                                listRegion.Add(postRegion);
                            }
                        }
                    }
                    else
                    {
                        var postRegion = ((IDictionary <string, object>)post.Regions)[region.Id];

                        if (region.Fields.Count == 1)
                        {
                            ((IDictionary <string, object>)post.Regions)[region.Id] =
                                modelRegion.Items[0].Fields[0].Model;
                        }
                        else
                        {
                            foreach (var field in region.Fields)
                            {
                                var modelField = modelRegion.Items[0].Fields
                                                 .FirstOrDefault(f => f.Meta.Id == field.Id);
                                ((IDictionary <string, object>)postRegion)[field.Id] = modelField.Model;
                            }
                        }
                    }
                }

                // Save blocks
                post.Blocks.Clear();

                foreach (var block in model.Blocks)
                {
                    if (block is BlockGroupModel blockGroup)
                    {
                        var groupType = App.Blocks.GetByType(blockGroup.Type);

                        if (groupType != null)
                        {
                            var postBlock = (BlockGroup)Activator.CreateInstance(groupType.Type);

                            postBlock.Id   = blockGroup.Id;
                            postBlock.Type = blockGroup.Type;

                            foreach (var field in blockGroup.Fields)
                            {
                                var prop = postBlock.GetType().GetProperty(field.Meta.Id, App.PropertyBindings);
                                prop.SetValue(postBlock, field.Model);
                            }

                            foreach (var item in blockGroup.Items)
                            {
                                if (item is BlockItemModel blockItem)
                                {
                                    postBlock.Items.Add(blockItem.Model);
                                }
                                else if (item is BlockGenericModel blockGeneric)
                                {
                                    var transformed = ContentUtils.TransformGenericBlock(blockGeneric);

                                    if (transformed != null)
                                    {
                                        postBlock.Items.Add(transformed);
                                    }
                                }
                            }
                            post.Blocks.Add(postBlock);
                        }
                    }
                    else if (block is BlockItemModel blockItem)
                    {
                        post.Blocks.Add(blockItem.Model);
                    }
                    else if (block is BlockGenericModel blockGeneric)
                    {
                        var transformed = ContentUtils.TransformGenericBlock(blockGeneric);

                        if (transformed != null)
                        {
                            post.Blocks.Add(transformed);
                        }
                    }
                }

                // Save post
                if (draft)
                {
                    await _api.Posts.SaveDraftAsync(post);
                }
                else
                {
                    await _api.Posts.SaveAsync(post);
                }
            }
            else
            {
                throw new ValidationException("Invalid Post Type.");
            }
        }
コード例 #6
0
        public async Task Save(PageEditModel model, bool draft)
        {
            var pageType = App.PageTypes.GetById(model.TypeId);

            if (pageType != null)
            {
                if (model.Id == Guid.Empty)
                {
                    model.Id = Guid.NewGuid();
                }

                var page = await _api.Pages.GetByIdAsync(model.Id);

                if (page == null)
                {
                    page = await _factory.CreateAsync <DynamicPage>(pageType);

                    page.Id = model.Id;
                }

                page.SiteId                 = model.SiteId;
                page.ParentId               = model.ParentId;
                page.OriginalPageId         = model.OriginalId;
                page.SortOrder              = model.SortOrder;
                page.TypeId                 = model.TypeId;
                page.Title                  = model.Title;
                page.NavigationTitle        = model.NavigationTitle;
                page.Slug                   = model.Slug;
                page.MetaKeywords           = model.MetaKeywords;
                page.MetaDescription        = model.MetaDescription;
                page.IsHidden               = model.IsHidden;
                page.Published              = !string.IsNullOrEmpty(model.Published) ? DateTime.Parse(model.Published) : (DateTime?)null;
                page.RedirectUrl            = model.RedirectUrl;
                page.RedirectType           = (RedirectType)Enum.Parse(typeof(RedirectType), model.RedirectType);
                page.EnableComments         = model.EnableComments;
                page.CloseCommentsAfterDays = model.CloseCommentsAfterDays;
                page.Permissions            = model.SelectedPermissions;

                if (pageType.Routes.Count > 1)
                {
                    page.Route = pageType.Routes.FirstOrDefault(r => r.Route == model.SelectedRoute?.Route)
                                 ?? pageType.Routes.First();
                }

                //
                // We only need to save regions & blocks for pages that are not copies
                //
                if (!page.OriginalPageId.HasValue)
                {
                    // Save regions
                    foreach (var region in pageType.Regions)
                    {
                        var modelRegion = model.Regions
                                          .FirstOrDefault(r => r.Meta.Id == region.Id);

                        if (region.Collection)
                        {
                            var listRegion = (IRegionList)((IDictionary <string, object>)page.Regions)[region.Id];

                            listRegion.Clear();

                            foreach (var item in modelRegion.Items)
                            {
                                if (region.Fields.Count == 1)
                                {
                                    listRegion.Add(item.Fields[0].Model);
                                }
                                else
                                {
                                    var pageRegion = new ExpandoObject();

                                    foreach (var field in region.Fields)
                                    {
                                        var modelField = item.Fields
                                                         .FirstOrDefault(f => f.Meta.Id == field.Id);
                                        ((IDictionary <string, object>)pageRegion)[field.Id] = modelField.Model;
                                    }
                                    listRegion.Add(pageRegion);
                                }
                            }
                        }
                        else
                        {
                            var pageRegion = ((IDictionary <string, object>)page.Regions)[region.Id];

                            if (region.Fields.Count == 1)
                            {
                                ((IDictionary <string, object>)page.Regions)[region.Id] =
                                    modelRegion.Items[0].Fields[0].Model;
                            }
                            else
                            {
                                foreach (var field in region.Fields)
                                {
                                    var modelField = modelRegion.Items[0].Fields
                                                     .FirstOrDefault(f => f.Meta.Id == field.Id);
                                    ((IDictionary <string, object>)pageRegion)[field.Id] = modelField.Model;
                                }
                            }
                        }
                    }

                    // Save blocks
                    page.Blocks.Clear();

                    foreach (var block in model.Blocks)
                    {
                        if (block is BlockGroupModel blockGroup)
                        {
                            var groupType = App.Blocks.GetByType(blockGroup.Type);

                            if (groupType != null)
                            {
                                var pageBlock = (Extend.BlockGroup)Activator.CreateInstance(groupType.Type);

                                pageBlock.Id   = blockGroup.Id;
                                pageBlock.Type = blockGroup.Type;

                                foreach (var field in blockGroup.Fields)
                                {
                                    var prop = pageBlock.GetType().GetProperty(field.Meta.Id, App.PropertyBindings);
                                    prop.SetValue(pageBlock, field.Model);
                                }

                                foreach (var item in blockGroup.Items)
                                {
                                    if (item is BlockItemModel blockItem)
                                    {
                                        pageBlock.Items.Add(blockItem.Model);
                                    }
                                    else if (item is BlockGenericModel blockGeneric)
                                    {
                                        var transformed = ContentUtils.TransformGenericBlock(blockGeneric);

                                        if (transformed != null)
                                        {
                                            pageBlock.Items.Add(transformed);
                                        }
                                    }
                                }
                                page.Blocks.Add(pageBlock);
                            }
                        }
                        else if (block is BlockItemModel blockItem)
                        {
                            page.Blocks.Add(blockItem.Model);
                        }
                        else if (block is BlockGenericModel blockGeneric)
                        {
                            var transformed = ContentUtils.TransformGenericBlock(blockGeneric);

                            if (transformed != null)
                            {
                                page.Blocks.Add(transformed);
                            }
                        }
                    }
                }

                // Save page
                if (draft)
                {
                    await _api.Pages.SaveDraftAsync(page);
                }
                else
                {
                    await _api.Pages.SaveAsync(page);
                }
            }
            else
            {
                throw new ValidationException("Invalid Page Type.");
            }
        }
コード例 #7
0
        /// <summary>
        /// Creates a new block of the specified type.
        /// </summary>
        /// <param name="type">The block type</param>
        /// <returns>The new block</returns>
        public async Task <AsyncResult <BlockModel> > CreateBlockAsync(string type)
        {
            var blockType = App.Blocks.GetByType(type);

            if (blockType != null)
            {
                var block = (Block)(await _factory.CreateBlockAsync(type, true));

                if (block is BlockGroup)
                {
                    var item = new BlockGroupModel
                    {
                        Type = block.Type,
                        Meta = new BlockMeta
                        {
                            Name      = blockType.Name,
                            Title     = block.GetTitle(),
                            Icon      = blockType.Icon,
                            Component = blockType.Component,
                            Width     = blockType.Width.ToString().ToLower(),
                            IsGroup   = true
                        }
                    };

                    item.Fields = ContentUtils.GetBlockFields(block);

                    return(new AsyncResult <BlockModel>
                    {
                        Body = item
                    });
                }
                else
                {
                    if (!blockType.IsGeneric)
                    {
                        // Regular block model
                        return(new AsyncResult <BlockModel>
                        {
                            Body = new BlockItemModel
                            {
                                Model = block,
                                Meta = new BlockMeta
                                {
                                    Name = blockType.Name,
                                    Title = block.GetTitle(),
                                    Icon = blockType.Icon,
                                    Component = blockType.Component,
                                    Width = blockType.Width.ToString().ToLower()
                                }
                            }
                        });
                    }
                    else
                    {
                        var blockModel = new BlockGenericModel
                        {
                            Model = ContentUtils.GetBlockFields(block),
                            Type  = block.Type,
                            Meta  = new BlockMeta
                            {
                                Name      = blockType.Name,
                                Title     = block.GetTitle(),
                                Icon      = blockType.Icon,
                                Component = blockType.Component,
                                Width     = blockType.Width.ToString().ToLower()
                            }
                        };

                        if (blockModel.Model.Count == 1)
                        {
                            blockModel.Model[0].Meta.NotifyChange = true;
                        }
                        else
                        {
                            foreach (var blockField in blockModel.Model)
                            {
                                blockField.Meta.NotifyChange =
                                    blockField.Meta.Id == blockType.ListTitleField;
                            }
                        }

                        // Generic block model
                        return(new AsyncResult <BlockModel>
                        {
                            Body = blockModel
                        });
                    }
                }
            }
            return(null);
        }