/// <summary> /// Maps a region to the given data entity. /// </summary> /// <typeparam name="T">The model type</typeparam> /// <param name="model">The model</param> /// <param name="page">The data entity</param> /// <param name="region">The region to map</param> /// <param name="regionType">The region type</param> /// <param name="regionId">The region id</param> /// <param name="sortOrder">The optional sort order</param> /// <param name="transaction">The optional transaction</param> private void MapRegion <T>(T model, Page page, object region, Models.RegionType regionType, string regionId, int sortOrder = 0, IDbTransaction transaction = null) where T : Models.Page <T> { // Now map all of the fields for (var n = 0; n < regionType.Fields.Count; n++) { var isNew = false; var fieldDef = regionType.Fields[n]; var fieldType = App.Fields.GetByShorthand(fieldDef.Type); if (fieldType == null) { fieldType = App.Fields.GetByType(fieldDef.Type); } if (fieldType != null) { object fieldValue = null; if (regionType.Fields.Count == 1) { // Get the field value for simple region fieldValue = region; } else { // Get the field value for complex region fieldValue = GetComplexValue(region, fieldDef.Id); } if (fieldValue != null) { // Check that the returned value matches the type specified // for the page type, otherwise deserialization won't work // when the model is retrieved from the database. if (fieldValue.GetType() != fieldType.Type) { throw new ArgumentException("Given page field value does not match the configured page type"); } // Check if we have the current field in the database already var field = page.Fields .SingleOrDefault(f => f.RegionId == regionId && f.FieldId == fieldDef.Id && f.SortOrder == sortOrder); // If not, create a new field if (field == null) { field = new PageField() { Id = Guid.NewGuid().ToString(), PageId = page.Id, RegionId = regionId, FieldId = fieldDef.Id }; page.Fields.Add(field); isNew = true; } // Update field info & value field.CLRType = fieldType.TypeName; field.SortOrder = sortOrder; field.Value = App.SerializeObject(fieldValue, fieldType.Type); // Save the field if (isNew) { db.Execute("INSERT INTO [Piranha_PageFields] ([Id], [PageId], [RegionId], [FieldId], [CLRType], [SortOrder], [Value]) VALUES(@Id, @PageId, @RegionId, @FieldId, @CLRType, @SortOrder, @Value)", field, transaction: transaction); } else { db.Execute("UPDATE [Piranha_PageFields] Set [PageId]=@PageId, [RegionId]=@RegionId, [FieldId]=@FieldId, [CLRType]=@CLRType, [SortOrder]=@SortOrder, [Value]=@Value WHERE [Id]=@Id", field, transaction: transaction); } } } } }
/// <summary> /// Maps a region to the given data entity. /// </summary> /// <typeparam name="T">The model type</typeparam> /// <param name="model">The model</param> /// <param name="page">The data entity</param> /// <param name="region">The region to map</param> /// <param name="regionType">The region type</param> /// <param name="regionId">The region id</param> /// <param name="sortOrder">The optional sort order</param> private IList <Guid> MapRegion <T>(T model, Page page, object region, Models.RegionType regionType, string regionId, int sortOrder = 0) where T : Models.Page <T> { var items = new List <Guid>(); // Now map all of the fields for (var n = 0; n < regionType.Fields.Count; n++) { var fieldDef = regionType.Fields[n]; var fieldType = App.Fields.GetByShorthand(fieldDef.Type); if (fieldType == null) { fieldType = App.Fields.GetByType(fieldDef.Type); } if (fieldType != null) { object fieldValue = null; if (regionType.Fields.Count == 1) { // Get the field value for simple region fieldValue = region; } else { // Get the field value for complex region fieldValue = GetComplexValue(region, fieldDef.Id); } if (fieldValue != null) { // Check that the returned value matches the type specified // for the page type, otherwise deserialization won't work // when the model is retrieved from the database. if (fieldValue.GetType() != fieldType.Type) { throw new ArgumentException("Given page field value does not match the configured page type"); } // Check if we have the current field in the database already var field = page.Fields .SingleOrDefault(f => f.RegionId == regionId && f.FieldId == fieldDef.Id && f.SortOrder == sortOrder); // If not, create a new field if (field == null) { field = new PageField() { Id = Guid.NewGuid(), PageId = page.Id, RegionId = regionId, FieldId = fieldDef.Id }; page.Fields.Add(field); } // Update field info & value field.CLRType = fieldType.TypeName; field.SortOrder = sortOrder; field.Value = App.SerializeObject(fieldValue, fieldType.Type); items.Add(field.Id); } } } return(items); }
/// <summary> /// Maps a region to the given data entity. /// </summary> /// <param name="content">The content entity</param> /// <param name="region">The region to map</param> /// <param name="regionType">The region type</param> /// <param name="regionId">The region id</param> /// <param name="sortOrder">The optional sort order</param> /// <param name="languageId">The optional language id</param> private IList <Guid> MapRegion(TContent content, object region, Models.RegionType regionType, string regionId, int sortOrder = 0, Guid?languageId = null) { var items = new List <Guid>(); // Now map all of the fields for (var n = 0; n < regionType.Fields.Count; n++) { var fieldDef = regionType.Fields[n]; var fieldType = App.Fields.GetByShorthand(fieldDef.Type); if (fieldType == null) { fieldType = App.Fields.GetByType(fieldDef.Type); } if (fieldType != null) { object fieldValue = null; if (regionType.Fields.Count == 1) { // Get the field value for simple region fieldValue = region; } else { // Get the field value for complex region fieldValue = GetComplexValue(region, fieldDef.Id); } if (fieldValue != null) { // Check that the returned value matches the type specified // for the page type, otherwise deserialization won't work // when the model is retrieved from the database. if (fieldValue.GetType() != fieldType.Type) { throw new ArgumentException("Given field value does not match the configured type"); } // Check if we have the current field in the database already var field = content.Fields .SingleOrDefault(f => f.RegionId == regionId && f.FieldId == fieldDef.Id && f.SortOrder == sortOrder); // If not, create a new field if (field == null) { field = Activator.CreateInstance <TField>(); field.Id = Guid.NewGuid(); field.RegionId = regionId; field.FieldId = fieldDef.Id; content.Fields.Add(field); } // Update field info field.CLRType = fieldType.TypeName; field.SortOrder = sortOrder; // Update field value if (fieldValue is Extend.ITranslatable && field is ITranslatable translatableField && languageId.HasValue) { // This is a translatable value translatableField.SetTranslation(field.Id, languageId.Value, fieldValue); } else { // this is a non translatable valuye field.Value = App.SerializeObject(fieldValue, fieldType.Type); } items.Add(field.Id); } } }