Example #1
0
        /// <summary>
        /// Loads all of the regions from the source model into the destination.
        /// </summary>
        /// <param name="src">The source</param>
        /// <param name="dest">The destination</param>
        private static void LoadRegions(Piranha.Models.DynamicPage src, PageEditModel dest)
        {
            if (dest.PageType != null)
            {
                foreach (var region in dest.PageType.Regions)
                {
                    var regions = (IDictionary <string, object>)src.Regions;

                    if (regions.ContainsKey(region.Id))
                    {
                        PageEditRegionBase editRegion;

                        if (region.Collection)
                        {
                            editRegion = new PageEditRegionCollection();
                        }
                        else
                        {
                            editRegion = new PageEditRegion();
                        }
                        editRegion.Id      = region.Id;
                        editRegion.Title   = region.Title ?? region.Id;
                        editRegion.CLRType = editRegion.GetType().FullName;

                        IList items = new List <object>();

                        if (region.Collection)
                        {
                            items = (IList)regions[region.Id];
                        }
                        else
                        {
                            items.Add(regions[region.Id]);
                        }

                        foreach (var item in items)
                        {
                            if (region.Fields.Count == 1)
                            {
                                editRegion.Add(new PageEditFieldSet()
                                {
                                    new PageEditField()
                                    {
                                        Id      = region.Fields[0].Id,
                                        Title   = region.Fields[0].Title ?? region.Fields[0].Id,
                                        CLRType = item.GetType().FullName,
                                        Value   = (Extend.IField)item
                                    }
                                });
                            }
                            else
                            {
                                var fieldData = (IDictionary <string, object>)item;
                                var fieldSet  = new PageEditFieldSet();

                                foreach (var field in region.Fields)
                                {
                                    if (fieldData.ContainsKey(field.Id))
                                    {
                                        fieldSet.Add(new PageEditField()
                                        {
                                            Id      = field.Id,
                                            Title   = field.Title ?? field.Id,
                                            CLRType = fieldData[field.Id].GetType().FullName,
                                            Value   = (Extend.IField)fieldData[field.Id]
                                        });
                                    }
                                }
                                editRegion.Add(fieldSet);
                            }
                        }
                        dest.Regions.Add(editRegion);
                    }
                }
            }
        }
        /// <summary>
        /// Creates a new edit region model for the given region type and value.
        /// </summary>
        /// <param name="region">The region type</param>
        /// <param name="value">The region value</param>
        /// <returns>The edit model</returns>
        public static PageEditRegionBase CreateRegion(Piranha.Models.RegionType region, object value)
        {
            PageEditRegionBase editRegion;

            if (region.Collection)
            {
                editRegion = new PageEditRegionCollection();
            }
            else
            {
                editRegion = new PageEditRegion();
            }
            editRegion.Id      = region.Id;
            editRegion.Title   = region.Title ?? region.Id;
            editRegion.CLRType = editRegion.GetType().FullName;

            IList items = new List <object>();

            if (region.Collection)
            {
                items = (IList)value;
            }
            else
            {
                items.Add(value);
            }

            foreach (var item in items)
            {
                if (region.Fields.Count == 1)
                {
                    var itemTitle = "";

                    // Get the item title if this is a collection region.
                    if (region.Collection)
                    {
                        if (item != null)
                        {
                            itemTitle = ((Extend.IField)item).GetTitle();
                        }
                        if (string.IsNullOrWhiteSpace(itemTitle) && !string.IsNullOrWhiteSpace(region.ListTitlePlaceholder))
                        {
                            itemTitle = region.ListTitlePlaceholder;
                        }
                        else
                        {
                            itemTitle = "Item";
                        }
                    }

                    var set = new PageEditFieldSet()
                    {
                        new PageEditField()
                        {
                            Id      = region.Fields[0].Id,
                            Title   = region.Fields[0].Title ?? region.Fields[0].Id,
                            CLRType = item.GetType().FullName,
                            Options = region.Fields[0].Options,
                            Value   = (Extend.IField)item
                        }
                    };
                    set.ListTitle = itemTitle;
                    set.NoExpand  = !region.ListExpand;

                    editRegion.Add(set);
                }
                else
                {
                    var fieldData = (IDictionary <string, object>)item;
                    var fieldSet  = new PageEditFieldSet();

                    foreach (var field in region.Fields)
                    {
                        if (fieldData.ContainsKey(field.Id))
                        {
                            // Get the item title if this is a collection region.
                            if (region.Collection)
                            {
                                if (!string.IsNullOrWhiteSpace(region.ListTitleField) && field.Id == region.ListTitleField)
                                {
                                    var itemTitle = "";

                                    if (fieldData[field.Id] != null)
                                    {
                                        itemTitle = ((Extend.IField)fieldData[field.Id]).GetTitle();
                                    }
                                    if (string.IsNullOrWhiteSpace(itemTitle) && !string.IsNullOrWhiteSpace(region.ListTitlePlaceholder))
                                    {
                                        itemTitle = region.ListTitlePlaceholder;
                                    }
                                    else if (string.IsNullOrWhiteSpace(itemTitle))
                                    {
                                        itemTitle = "Item";
                                    }

                                    fieldSet.ListTitle = itemTitle;
                                    fieldSet.NoExpand  = !region.ListExpand;
                                }
                            }

                            fieldSet.Add(new PageEditField()
                            {
                                Id      = field.Id,
                                Title   = field.Title ?? field.Id,
                                CLRType = fieldData[field.Id].GetType().FullName,
                                Options = field.Options,
                                Value   = (Extend.IField)fieldData[field.Id]
                            });
                        }
                    }
                    editRegion.Add(fieldSet);
                }
            }
            return(editRegion);
        }