public IDictionary <string, VueComponentDefinition[]> ConvertFormToVues( IDictionary <string, ContentModifierForm> modifierForm, ProtoContent content, ContentModifyOperation operation, ContentType contentType) { var vues = new Dictionary <string, VueComponentDefinition[]>(); foreach (var fd in contentType.Fields) { if (!operation.IsAny(fd.Config.HandledModifyOperationNames)) { continue; } var fdr = fd.FieldFinder(); var mod = fd.FieldModifier(); var field = operation.Is(CommonFieldModifyOperationsProvider.CREATE_OPERATION_NAME) ? Activator.CreateInstance(fd.ModelType).DirectCastTo <ContentField>() : fdr.GetModel(content, fd); modifierForm.TryGetValue(fd.FieldName, out var form); var fvs = mod.ConvertFormToVues(form, field, operation, content, fd); if (fvs != null && fvs.Length > 0) { vues.Add(fd.FieldName, fvs); } } return(vues); }
public void PerformModification(ContentModifierForm modifierForm, ContentField field, ContentModifyOperation operation, ProtoContent content, ContentFieldDefinition fieldDefinition) { if (operation.Is(CommonFieldModifyOperationsProvider.CREATE_OPERATION_NAME)) { content.CreatedUtc = DateTime.UtcNow; content.UpdatedUtc = content.CreatedUtc; _dbContext.ProtoContents.Add(content); } else if (operation.Is(CommonFieldModifyOperationsProvider.DELETE_OPERATION_NAME)) { content.UpdatedUtc = DateTime.UtcNow; _dbContext.ProtoContents.Remove(content); } else { content.UpdatedUtc = DateTime.UtcNow; } if (string.IsNullOrWhiteSpace(content.CreatedByUserId)) { var rctx = ProtoCmsRuntimeContext.Current; if (rctx.CurrentUser != null) { content.CreatedByUserId = rctx.CurrentUser.Id; } } var creator = _userMgr.ProtoUsers.FirstOrDefault(x => x.Id == content.CreatedByUserId); if (creator != null) { content.CreatedByUserName = creator.UserName; content.CreatedByUserDisplayName = creator.DisplayName; } _dbContext.ThisDbContext().SaveChanges(); }
public void PerformModification(IDictionary <string, ContentModifierForm> modifierForm, ProtoContent content, ContentModifyOperation operation, ContentType contentType) { foreach (var fd in contentType.Fields) { if (!operation.IsAny(fd.Config.HandledModifyOperationNames)) { continue; } var fdr = fd.FieldFinder(); var mod = fd.FieldModifier(); var field = operation.Is(CommonFieldModifyOperationsProvider.CREATE_OPERATION_NAME) ? Activator.CreateInstance(fd.ModelType).DirectCastTo <ContentField>() : fdr.GetModel(content, fd); modifierForm.TryGetValue(fd.FieldName, out var form); mod.PerformModification(form, field, operation, content, fd); } }
public IDictionary <string, ContentModifierForm> BuildModifierForm(ProtoContent content, ContentModifyOperation operation, ContentType contentType) { var fps = new Dictionary <string, ContentModifierForm>(); foreach (var fd in contentType.Fields) { if (!operation.IsAny(fd.Config.HandledModifyOperationNames)) { continue; } var fdr = fd.FieldFinder(); var mod = fd.FieldModifier(); var field = operation.Is(CommonFieldModifyOperationsProvider.CREATE_OPERATION_NAME) ? Activator.CreateInstance(fd.ModelType).DirectCastTo <ContentField>() : fdr.GetModel(content, fd); var form = mod.BuildModifierForm(field, operation, content, fd); if (form != null) { fps.Add(fd.FieldName, form); } } return(fps); }
public FurtherValidationResult ValidateModifierForm(IDictionary <string, ContentModifierForm> modifierForm, ProtoContent content, ContentModifyOperation operation, ContentType contentType) { var result = new FurtherValidationResult(); foreach (var fd in contentType.Fields) { if (!operation.IsAny(fd.Config.HandledModifyOperationNames)) { continue; } var fdr = fd.FieldFinder(); var mod = fd.FieldModifier(); var field = operation.Is(CommonFieldModifyOperationsProvider.CREATE_OPERATION_NAME) ? Activator.CreateInstance(fd.ModelType).DirectCastTo <ContentField>() : fdr.GetModel(content, fd); modifierForm.TryGetValue(fd.FieldName, out var form); if (form == null) { var testForm = mod.BuildModifierForm(field, operation, content, fd); if (testForm != null) { throw new HttpException(400, $"ProtoCMS: content modifier form for field '{fd.FieldName}' " + $"is required and must be an instance of " + $"'{testForm.GetType().FullName}'."); } continue; } foreach (var fvkv in fd.Config.Validators) { var hasValidator = false; foreach (var dv in ContentFieldValidator.DefinedValidators) { if (dv.Name != fvkv.Key || !dv.HandledFormTypes.Contains(form.GetType())) { continue; } hasValidator = true; var valCfg = fvkv.Value ?? Activator.CreateInstance(dv.ConfigType) .DirectCastTo <ContentFieldValidatorConfiguration>(); if (operation.IsAny(valCfg.ModifyOperationNamesThatIgnoreValidation)) { continue; } if (valCfg.GetType() != dv.ConfigType) { throw new InvalidOperationException( $"ProtoCMS: content field validator '{dv.Name}' (for validating form " + $"'{form.GetType().FullName}') can only accept config instance of type " + $"'{dv.ConfigType.FullName}'."); } var valRes = dv.ValidateForm(form, valCfg, contentType, fd); if (valRes != null) { foreach (var kv in valRes.Errors) { if (kv.Value == null || kv.Value.Length == 0) { continue; } foreach (var err in kv.Value) { result.AddError($"{fd.FieldName}.{kv.Key}", err); } } } } if (!hasValidator) { throw new InvalidOperationException($"ProtoCMS: there's no content field validator " + $"named '{fvkv.Key}' that can handle form " + $"'{form.GetType().FullName}' validation."); } } } return(result); }