private static void BuildCommonEntity(ContentTypeBase entity, ContentTypeDto dto, bool setVariations = true) { entity.Id = dto.NodeDto.NodeId; entity.Key = dto.NodeDto.UniqueId; entity.Alias = dto.Alias ?? string.Empty; entity.Name = dto.NodeDto.Text; entity.Icon = dto.Icon; entity.Thumbnail = dto.Thumbnail; entity.SortOrder = dto.NodeDto.SortOrder; entity.Description = dto.Description; entity.CreateDate = dto.NodeDto.CreateDate; entity.UpdateDate = dto.NodeDto.CreateDate; entity.Path = dto.NodeDto.Path; entity.Level = dto.NodeDto.Level; entity.CreatorId = dto.NodeDto.UserId ?? Cms.Core.Constants.Security.UnknownUserId; entity.AllowedAsRoot = dto.AllowAtRoot; entity.IsContainer = dto.IsContainer; entity.IsElement = dto.IsElement; entity.Trashed = dto.NodeDto.Trashed; if (setVariations) { entity.Variations = (ContentVariation)dto.Variations; } }
public Task<object> CreateDynamicRegionAsync(ContentTypeBase type, string regionId, bool managerInit = false) { V_0 = new ContentFactory.u003cu003ec__DisplayClass3_0(); V_0.regionId = regionId; V_1 = ServiceProviderServiceExtensions.CreateScope(this._services); try { V_2 = type.get_Regions().FirstOrDefault<RegionType>(new Func<RegionType, bool>(V_0.u003cCreateDynamicRegionAsyncu003eb__0)); if (V_2 == null) { V_3 = null; } else { V_3 = this.CreateDynamicRegionAsync(V_1, V_2, true, managerInit); } } finally { if (V_1 != null) { V_1.Dispose(); } } return V_3; }
/// <summary> /// Creates and initializes a new content model. /// </summary> /// <param name="type">The content type</param> /// <typeparam name="T">The model type</typeparam> /// <returns>The new model</returns> public Task <T> CreateAsync <T>(ContentTypeBase type) where T : ContentBase { if (typeof(IDynamicContent).IsAssignableFrom(typeof(T))) { return(CreateDynamicModelAsync <T>(type)); } return(CreateModelAsync <T>(type)); }
private async Task<T> CreateDynamicModelAsync<T>(ContentTypeBase type) where T : ContentBase { V_0.u003cu003e4__this = this; V_0.type = type; V_0.u003cu003et__builder = AsyncTaskMethodBuilder<T>.Create(); V_0.u003cu003e1__state = -1; V_0.u003cu003et__builder.Start<ContentFactory.u003cCreateDynamicModelAsyncu003ed__5<T>>(ref V_0); return V_0.u003cu003et__builder.get_Task(); }
private async Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type, bool managerInit) where T : IDynamicContent { V_0.u003cu003e4__this = this; V_0.model = model; V_0.type = type; V_0.managerInit = managerInit; V_0.u003cu003et__builder = AsyncTaskMethodBuilder<T>.Create(); V_0.u003cu003e1__state = -1; V_0.u003cu003et__builder.Start<ContentFactory.u003cInitDynamicAsyncu003ed__9<T>>(ref V_0); return V_0.u003cu003et__builder.get_Task(); }
/// <summary> /// Creates a new dynamic region. /// </summary> /// <param name="type">The content type</param> /// <param name="regionId">The region id</param> /// <returns>The new region value</returns> public Task <object> CreateDynamicRegionAsync(ContentTypeBase type, string regionId) { using (var scope = _services.CreateScope()) { var region = type.Regions.FirstOrDefault(r => r.Id == regionId); if (region != null) { return(CreateDynamicRegionAsync(scope, region)); } return(null); } }
/// <summary> /// Initializes the given model. /// </summary> /// <param name="model">The model</param> /// <param name="type">The content type</param> /// <param name="managerInit">If this is initialization used by the manager</param> /// <typeparam name="T">The model type</typeparam> /// <returns>The initialized model</returns> private async Task <T> InitAsync <T>(T model, ContentTypeBase type, bool managerInit) where T : ContentBase { if (model is IDynamicContent) { throw new ArgumentException("For dynamic models InitDynamic should be used."); } using (var scope = _services.CreateScope()) { foreach (var regionType in type.Regions) { // Try to get the region from the model var region = model.GetType().GetPropertyValue(regionType.Id, model); if (region != null) { if (!regionType.Collection) { // Initialize it await InitRegionAsync(scope, region, regionType, managerInit).ConfigureAwait(false); } else { // This region was a collection. Initialize all items foreach (var item in (IList)region) { await InitRegionAsync(scope, item, regionType, managerInit).ConfigureAwait(false); } } } } if (!(model is IContentInfo) && model is IBlockContent blockModel) { foreach (var block in blockModel.Blocks) { await InitBlockAsync(scope, block, managerInit).ConfigureAwait(false); if (block is Extend.BlockGroup) { foreach (var child in ((Extend.BlockGroup)block).Items) { await InitBlockAsync(scope, child, managerInit).ConfigureAwait(false); } } } } } return(model); }
/// <summary> /// Creates and initializes a new dynamic model. /// </summary> /// <param name="type">The content type</param> /// <typeparam name="T">The model type</typeparam> /// <returns>The new model</returns> private async Task <T> CreateModelAsync <T>(ContentTypeBase type) where T : ContentBase { using (var scope = _services.CreateScope()) { var modelType = typeof(T); if (!typeof(IContentInfo).IsAssignableFrom(modelType)) { modelType = Type.GetType(type.CLRType); if (modelType != typeof(T) && !typeof(T).IsAssignableFrom(modelType)) { return(null); } } // Create a new instance of the specified type var model = (T)Activator.CreateInstance(modelType); model.TypeId = type.Id; foreach (var regionType in type.Regions) { object region = null; if (!regionType.Collection) { // Create and initialize the region region = await CreateRegionAsync(scope, model, modelType, regionType).ConfigureAwait(false); } else { var property = modelType.GetProperty(regionType.Id, App.PropertyBindings); if (property != null) { region = Activator.CreateInstance(typeof(List <>).MakeGenericType(property.PropertyType.GetGenericArguments()[0])); } } if (region != null) { modelType.SetPropertyValue(regionType.Id, model, region); } } return(model); } }
public ActionResult LoadEditor(ContentType contentType, string json) { ContentTypeBase contentModel = ContentTypeBase.CreateNewModel(contentType); try { JsonConvert.PopulateObject(json, contentModel); contentModel.PrepareForDisplay(); return(PartialView("_HomePageEditor", new HomePageEditorModel(contentType, contentModel))); } catch { return(JsonContent(SimpleJsonMessageResult.Failed("The editor could not be loaded for " + contentType.GetDisplayText()))); } }
private static void BuildCommonEntity(ContentTypeBase entity, ContentTypeDto dto) { entity.Id = dto.NodeDto.NodeId; entity.Key = dto.NodeDto.UniqueId; entity.Alias = dto.Alias; entity.Name = dto.NodeDto.Text; entity.Icon = dto.Icon; entity.Thumbnail = dto.Thumbnail; entity.SortOrder = dto.NodeDto.SortOrder; entity.Description = dto.Description; entity.CreateDate = dto.NodeDto.CreateDate; entity.Path = dto.NodeDto.Path; entity.Level = dto.NodeDto.Level; entity.CreatorId = dto.NodeDto.UserId.Value; entity.AllowedAsRoot = dto.AllowAtRoot; entity.IsContainer = dto.IsContainer; entity.Trashed = dto.NodeDto.Trashed; }
/// <summary> /// Creates a new region for the given content type. /// </summary> /// <param name="type">The content type</param> /// <param name="region">The region id</param> /// <returns>The new region item</returns> private async Task <RegionItemModel> CreateRegionAsync(ContentTypeBase type, string region) { var regionType = type.Regions.First(r => r.Id == region); var regionModel = await _factory.CreateDynamicRegionAsync(type, region, true); var regionItem = new RegionItemModel { Title = regionType.ListTitlePlaceholder ?? "...", IsNew = true }; 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 } }; PopulateFieldOptions(appFieldType, field); if (regionType.Fields.Count > 1) { field.Model = (IField)((IDictionary <string, object>)regionModel)[fieldType.Id]; field.Meta.NotifyChange = regionType.ListTitleField == fieldType.Id; } else { field.Model = (IField)regionModel; field.Meta.NotifyChange = true; } regionItem.Fields.Add(field); } return(regionItem); }
/// <summary> /// Initializes the given dynamic model. /// </summary> /// <param name="model">The model</param> /// <param name="type">The content type</param> /// <param name="managerInit">If this is initialization used by the manager</param> /// <typeparam name="T">The model type</typeparam> /// <returns>The initialized model</returns> private async Task <T> InitDynamicAsync <T>(T model, ContentTypeBase type, bool managerInit) where T : IDynamicContent { using (var scope = _services.CreateScope()) { foreach (var regionType in type.Regions) { // Try to get the region from the model if (((IDictionary <string, object>)model.Regions).TryGetValue(regionType.Id, out var region)) { if (!regionType.Collection) { // Initialize it await InitDynamicRegionAsync(scope, region, regionType, managerInit).ConfigureAwait(false); } else { // This region was a collection. Initialize all items foreach (var item in (IList)region) { await InitDynamicRegionAsync(scope, item, regionType, managerInit).ConfigureAwait(false); } } } } if (model is IBlockContent blockModel) { foreach (var block in blockModel.Blocks) { await InitBlockAsync(scope, block, managerInit).ConfigureAwait(false); if (block is BlockGroup blockGroup) { foreach (var child in blockGroup.Items) { await InitBlockAsync(scope, child, managerInit).ConfigureAwait(false); } } } } } return(model); }
/// <summary> /// Creates and initializes a new dynamic model. /// </summary> /// <param name="type">The content type</param> /// <typeparam name="T">The model type</typeparam> /// <returns>The new model</returns> private async Task <T> CreateDynamicModelAsync <T>(ContentTypeBase type) where T : ContentBase { using (var scope = _services.CreateScope()) { // Create a new instance of the specified type var model = Activator.CreateInstance <T>(); model.TypeId = type.Id; foreach (var regionType in type.Regions) { object region = null; if (!regionType.Collection) { // Create and initialize the region region = await CreateDynamicRegionAsync(scope, regionType).ConfigureAwait(false); } else { // Create a region item without initialization for type reference var listObject = await CreateDynamicRegionAsync(scope, regionType, false).ConfigureAwait(false); if (listObject != null) { // Create the region list region = Activator.CreateInstance(typeof(RegionList <>).MakeGenericType(listObject.GetType())); ((IRegionList)region).Model = (IDynamicContent)model; ((IRegionList)region).TypeId = type.Id; ((IRegionList)region).RegionId = regionType.Id; } } if (region != null) { ((IDictionary <string, object>)((IDynamicContent)model).Regions).Add(regionType.Id, region); } } return(model); } }
/// <summary> /// Adds a complex region to a collection region. /// </summary> /// <typeparam name="T">The model type</typeparam> /// <param name="model">The model</param> /// <param name="contentType">The content type</param> /// <param name="regionId">The region id</param> /// <param name="fields">The field</param> private async Task AddComplexValueAsync <T>(T model, ContentTypeBase contentType, string regionId, IList <TField> fields) where T : ContentBase { if (fields.Count > 0) { if (model is IDynamicContent dynamicModel) { var list = (IList)((IDictionary <string, object>)dynamicModel.Regions)[regionId]; var obj = await _factory.CreateDynamicRegionAsync(contentType, regionId).ConfigureAwait(false); foreach (var field in fields) { if (((IDictionary <string, object>)obj).ContainsKey(field.FieldId)) { ((IDictionary <string, object>)obj)[field.FieldId] = DeserializeValue(field); } } list.Add(obj); } else { var regionProp = model.GetType().GetProperty(regionId, App.PropertyBindings); if (regionProp != null) { var list = (IList)regionProp.GetValue(model); var obj = Activator.CreateInstance(list.GetType().GenericTypeArguments[0]); foreach (var field in fields) { var fieldProp = obj.GetType().GetProperty(field.FieldId, App.PropertyBindings); fieldProp?.SetValue(obj, DeserializeValue(field)); } list.Add(obj); } } } }
/// <summary> /// Transforms the given model into content data. /// </summary> /// <param name="model">The model</param> /// <param name="type">The content type</param> /// <param name="dest">The optional dest object</param> /// <returns>The content data</returns> public TContent Transform <T>(T model, ContentTypeBase type, TContent dest /*= null*/) where T : ContentBase, TModelBase { var content = dest;// == null ? Activator.CreateInstance<TContent>() : dest; // Map id // if (model.Id != Guid.Empty) // { // content.Id = model.Id; // } // else // { // content.Id = model.Id = Guid.NewGuid(); // } //content.Created = DateTime.Now; // Map basic fields _mapper.Map <TModelBase, TContent>(model, content); // Map regions var currentRegions = type.Regions.Select(r => r.Id).ToArray(); foreach (var regionKey in currentRegions) { // Check that the region exists in the current model if (HasRegion(model, regionKey)) { var regionType = type.Regions.Single(r => r.Id == regionKey); if (!regionType.Collection) { MapRegion(content, GetRegion(model, regionKey), regionType, regionKey); } else { var items = new List <Guid>(); var sortOrder = 0; foreach (var region in GetEnumerable(model, regionKey)) { var fields = MapRegion(content, region, regionType, regionKey, sortOrder++); if (fields.Count > 0) { items.AddRange(fields); } } // Now delete removed collection items var removedFields = content.Fields .Where(f => f.RegionId == regionKey && !items.Contains(f.Id)) .ToList(); foreach (var removed in removedFields) { content.Fields.Remove(removed); } } } } return(content); }
/// <summary> /// Loads the given data into a new model. /// </summary> /// <typeparam name="T">The model type</typeparam> /// <param name="content">The content entity</param> /// <param name="type">The content type</param> /// <param name="process">Optional func that should be called after transformation</param> /// <returns>The page model</returns> public async Task <T> TransformAsync <T>(TContent content, ContentTypeBase type, Func <TContent, T, Task> process = null) where T : ContentBase, TModelBase { if (type != null) { var modelType = typeof(T); if (!typeof(IDynamicContent).IsAssignableFrom(modelType) && !typeof(IContentInfo).IsAssignableFrom(modelType)) { modelType = Type.GetType(type.CLRType); if (modelType != typeof(T) && !typeof(T).IsAssignableFrom(modelType)) { return(null); } } // Create an initialized model var model = await _factory.CreateAsync <T>(type).ConfigureAwait(false); // Map basic fields _mapper.Map <TContent, TModelBase>(content, model); if (model is RoutedContentBase routeModel) { // Map route (if available) if (string.IsNullOrWhiteSpace(routeModel.Route) && type.Routes.Count > 0) { routeModel.Route = type.Routes[0]; } } // Map regions if (!(model is IContentInfo)) { var currentRegions = type.Regions.Select(r => r.Id).ToArray(); foreach (var regionKey in currentRegions) { var region = type.Regions.Single(r => r.Id == regionKey); var foo = content.Fields.ToArray(); var fields = content.Fields.Where(f => f.RegionId == regionKey).OrderBy(f => f.SortOrder).ToList(); if (!region.Collection) { foreach (var fieldDef in region.Fields) { var field = fields.SingleOrDefault(f => f.FieldId == fieldDef.Id && f.SortOrder == 0); if (field != null) { if (region.Fields.Count == 1) { SetSimpleValue(model, regionKey, field); break; } else { SetComplexValue(model, regionKey, fieldDef.Id, field); } } } } else { var fieldCount = content.Fields.Where(f => f.RegionId == regionKey).Select(f => f.SortOrder).DefaultIfEmpty(-1).Max() + 1; var sortOrder = 0; while (fieldCount > sortOrder) { if (region.Fields.Count == 1) { var field = fields.SingleOrDefault(f => f.FieldId == region.Fields[0].Id && f.SortOrder == sortOrder); if (field != null) { AddSimpleValue(model, regionKey, field); } } else { await AddComplexValueAsync(model, type, regionKey, fields.Where(f => f.SortOrder == sortOrder).ToList()) .ConfigureAwait(false); } sortOrder++; } } } } if (process != null) { await process(content, model).ConfigureAwait(false); } return(model); } return(null); }
/// <summary> /// Initializes the given model for the manager. /// </summary> /// <param name="model">The model</param> /// <param name="type">The content type</param> /// <typeparam name="T">The model type</typeparam> /// <returns>The initialized model</returns> public Task <T> InitManagerAsync <T>(T model, ContentTypeBase type) where T : ContentBase { return(InitAsync <T>(model, type, true)); }
/// <summary> /// Initializes the given model. /// </summary> /// <param name="model">The model</param> /// <param name="type">The content type</param> /// <typeparam name="T">The model type</typeparam> /// <returns>The initialized model</returns> public Task <T> InitAsync <T>(T model, ContentTypeBase type) where T : ContentBase { return(InitAsync <T>(model, type, false)); }
/// <summary> /// Initializes the given dynamic model for the manager. /// </summary> /// <param name="model">The model</param> /// <param name="type">The content type</param> /// <typeparam name="T">The model type</typeparam> /// <returns>The initialized model</returns> public Task <T> InitDynamicManagerAsync <T>(T model, ContentTypeBase type) where T : IDynamicContent { return(InitDynamicAsync <T>(model, type, true)); }
/* ---------------------------------------------------------------------------------------------------------- */ #region Class Members #endregion /* ---------------------------------------------------------------------------------------------------------- */ #region Constructors/Initialisation public HomePageEditorModel(ContentType contentType, ContentTypeBase contentModel) { this.ContentType = contentType; this.ContentModel = contentModel; }
public Task<T> InitDynamicAsync<T>(T model, ContentTypeBase type) where T : IDynamicContent { return this.InitDynamicAsync<T>(model, type, false); }
public Task<T> CreateAsync<T>(ContentTypeBase type) where T : ContentBase { if (Type.GetTypeFromHandle(//