Exemplo n.º 1
0
        public IActionResult AddRegion([FromBody] PageRegionModel model)
        {
            var siteType = _api.SiteTypes.GetById(model.PageTypeId);

            if (siteType != null)
            {
                var regionType = siteType.Regions.SingleOrDefault(r => r.Id == model.RegionTypeId);

                if (regionType != null)
                {
                    var region = _contentService.CreateDynamicRegion(siteType, model.RegionTypeId);

                    var editModel = (PageEditRegionCollection)_service.CreateRegion(regionType,
                                                                                    new List <object> {
                        region
                    });

                    ViewData.TemplateInfo.HtmlFieldPrefix = $"Regions[{model.RegionIndex}].FieldSets[{model.ItemIndex}]";
                    return(View("EditorTemplates/PageEditRegionItem", editModel.FieldSets[0]));
                }
            }
            return(new NotFoundResult());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves all of the regions from the source model into the destination.
        /// </summary>
        /// <param name="api">The current api</param>
        /// <param name="src">The source</param>
        /// <param name="dest">The destination</param>
        private void SaveRegions(PageEditModel src, Piranha.Models.DynamicPage dest)
        {
            var type         = _api.PageTypes.GetById(src.TypeId);
            var modelRegions = (IDictionary <string, object>)dest.Regions;

            foreach (var region in src.Regions)
            {
                if (region is PageEditRegion)
                {
                    if (!modelRegions.ContainsKey(region.Id))
                    {
                        modelRegions[region.Id] = _service.CreateDynamicRegion(type, region.Id);
                    }

                    var reg = (PageEditRegion)region;

                    if (reg.FieldSet.Count == 1)
                    {
                        modelRegions[region.Id] = reg.FieldSet[0].Value;
                    }
                    else
                    {
                        var modelFields = (IDictionary <string, object>)modelRegions[region.Id];

                        foreach (var field in reg.FieldSet)
                        {
                            modelFields[field.Id] = field.Value;
                        }
                    }
                }
                else
                {
                    if (modelRegions.ContainsKey(region.Id))
                    {
                        var list = (Piranha.Models.IRegionList)modelRegions[region.Id];
                        var reg  = (PageEditRegionCollection)region;

                        // At this point we clear the values and rebuild them
                        list.Clear();

                        foreach (var set in reg.FieldSets)
                        {
                            if (set.Count == 1)
                            {
                                list.Add(set[0].Value);
                            }
                            else
                            {
                                var modelFields = (IDictionary <string, object>)_service.CreateDynamicRegion(type, region.Id);

                                foreach (var field in set)
                                {
                                    modelFields[field.Id] = field.Value;
                                }
                                list.Add(modelFields);
                            }
                        }
                    }
                }
            }
        }